Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Mar 17, 2021
1 parent 4948777 commit f663af4
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 175 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Plugin Specific ignores
default_env.tres
icon.png
icon.png.import
project.godot
scn/
override.cfg

# Godot-specific ignores
.import/
export.cfg
export_presets.cfg

# Mono-specific ignores
.mono/
data_*/
4 changes: 2 additions & 2 deletions Board.gd
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func update_piece(piece, location: Vector2):
if piece.gridPos in self.positions:
self.positions.erase(piece.gridPos)
piece.gridPos = location
piece.position = self.map_to_world(piece.gridPos) + TILE_OFFSET
piece.moved = true
self.positions[piece.gridPos] = piece

piece.move_to_pos()

func move_piece(pos:Vector2, newLoc:Vector2):
if not pos in self.positions:
emit_signal("error","Cannot move piece", "No piece in position")
Expand Down
5 changes: 4 additions & 1 deletion MainGame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends Node
var currTurn = 0
signal turn_updated(turn, game)
signal new_turn(game, turn)
signal branch(game, branchName)
signal branch(game)
var games := []
var currGameIndex = 0
var currGame : Game
Expand Down Expand Up @@ -75,3 +75,6 @@ func branch(toCopy: Game = null, turn: int = -1) -> Game:

copy.path = basename + ":" + str(num+1)
return copy

func _error(title, message):
get_tree().get_root().get_node("Root").error(title, message)
11 changes: 10 additions & 1 deletion MainGame.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
1/shapes = [ ]
1/z_index = 0

[node name="MainGame" type="Node"]
[node name="MainGame" type="Viewport"]
physics_object_picking = true
script = ExtResource( 3 )

[node name="Board" type="TileMap" parent="."]
Expand All @@ -45,6 +46,9 @@ cell_y_sort = true
format = 1
tile_data = PoolIntArray( 65537, 1, 0, 65538, 0, 0, 65539, 1, 0, 65540, 0, 0, 65541, 1, 0, 65542, 0, 0, 65543, 1, 0, 65544, 0, 0, 131073, 0, 0, 131074, 1, 0, 131075, 0, 0, 131076, 1, 0, 131077, 0, 0, 131078, 1, 0, 131079, 0, 0, 131080, 1, 0, 196609, 1, 0, 196610, 0, 0, 196611, 1, 0, 196612, 0, 0, 196613, 1, 0, 196614, 0, 0, 196615, 1, 0, 196616, 0, 0, 262145, 0, 0, 262146, 1, 0, 262147, 0, 0, 262148, 1, 0, 262149, 0, 0, 262150, 1, 0, 262151, 0, 0, 262152, 1, 0, 327681, 1, 0, 327682, 0, 0, 327683, 1, 0, 327684, 0, 0, 327685, 1, 0, 327686, 0, 0, 327687, 1, 0, 327688, 0, 0, 393217, 0, 0, 393218, 1, 0, 393219, 0, 0, 393220, 1, 0, 393221, 0, 0, 393222, 1, 0, 393223, 0, 0, 393224, 1, 0, 458753, 1, 0, 458754, 0, 0, 458755, 1, 0, 458756, 0, 0, 458757, 1, 0, 458758, 0, 0, 458759, 1, 0, 458760, 0, 0, 524289, 0, 0, 524290, 1, 0, 524291, 0, 0, 524292, 1, 0, 524293, 0, 0, 524294, 1, 0, 524295, 0, 0, 524296, 1, 0 )
script = ExtResource( 4 )
__meta__ = {
"_edit_lock_": true
}

[node name="PromoteDialogue" type="WindowDialog" parent="Board"]
margin_left = 718.519
Expand Down Expand Up @@ -93,6 +97,11 @@ text = "Rook"

[node name="FileParser" type="Node" parent="."]
script = ExtResource( 1 )

[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2( 320, 320 )
current = true
[connection signal="error" from="Board" to="." method="_error"]
[connection signal="piece_moved" from="Board" to="." method="_on_Board_piece_moved"]
[connection signal="button_down" from="Board/PromoteDialogue/GridContainer/Queen" to="Board" method="promote" binds= [ 1 ]]
[connection signal="button_down" from="Board/PromoteDialogue/GridContainer/Bishop" to="Board" method="promote" binds= [ 2 ]]
Expand Down
19 changes: 10 additions & 9 deletions Piece.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ onready var board = get_parent()
var castled : int = 0
var ID : String

func _process(delta: float) -> void:
if $Draggable.dragging:
self.position = get_global_mouse_position()

func init(colour: int, type: int) -> void:
self.colour = colour
self.type = type
Expand All @@ -30,14 +34,7 @@ func init(colour: int, type: int) -> void:
func can_move(newLoc, capture=false) -> bool:
var raw = newLoc - self.gridPos
var ref = raw.abs()

# if type != TYPES.KNIGHT:
# $RayCast2D.cast_to = raw*64
# $RayCast2D.force_raycast_update()
# print($RayCast2D.cast_to, $RayCast2D.is_colliding())
# if $RayCast2D.is_colliding():
# return false


match type:
TYPES.QUEEN:
return ref.x == 0 or ref.y == 0 or ref.x == ref.y
Expand Down Expand Up @@ -75,9 +72,13 @@ func can_move(newLoc, capture=false) -> bool:
return false

func _on_Draggable_stopdrag() -> void:
var mousePos = get_viewport().get_mouse_position()
var mousePos = get_global_mouse_position()
var newPos = board.world_to_map(mousePos)
board.move_piece(self.gridPos, newPos)
move_to_pos()

func move_to_pos():
self.position = board.map_to_world(self.gridPos) + board.TILE_OFFSET

func get_ID() -> String:
return "WB"[self.colour] + TYPES_SAN[self.type]
4 changes: 0 additions & 4 deletions Piece.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ script = ExtResource( 2 )

[node name="Draggable" parent="." instance=ExtResource( 3 )]
collision_mask = 524288

[node name="RayCast2D" type="RayCast2D" parent="."]
collision_mask = 524288
collide_with_areas = true
[connection signal="stopdrag" from="Draggable" to="." method="_on_Draggable_stopdrag"]
7 changes: 7 additions & 0 deletions Root.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends Node


func error(title: String, message: String):
$ErrorPopup.window_title = title
$ErrorPopup/ErrorMessage.text = message
$ErrorPopup.popup_centered()
17 changes: 10 additions & 7 deletions Turn.gd
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func _init(colour: int = COLOUR.BLACK, boardState: Dictionary = STARTING_BOARD_S
self.comment += "; "+ commentOverride

func which_piece(colour, label, disamb, pos) -> String:
# Determine which piece moved for parsing a FEN or PGN game
var search : String
match colour:
COLOUR.WHITE:
Expand All @@ -85,7 +86,7 @@ func which_piece(colour, label, disamb, pos) -> String:

if len(possibles) == 0:
printerr("No possible piece found")
return "Not found"
null.get_node("Crash")
elif len(possibles) == 1:
return possibles[0]

Expand Down Expand Up @@ -117,34 +118,36 @@ func which_piece(colour, label, disamb, pos) -> String:
if ref.x == ref.y:
return poss
"P":
var moveDir = self.PAWN_DIR[colour]
if ((colour == COLOUR.WHITE and positions[poss].y == 2) or # Initial move of two
(colour == COLOUR.BLACK and positions[poss].y == 7)):
if ref.x == 0 and rawLoc.y in [colour, 2*colour]:
if ref.x == 0 and rawLoc.y in [moveDir, 2*moveDir]:
return poss
# En passant?
if self.capture:
if ref.x == 1 and rawLoc.y in [colour, 2*colour]:
if ref.x == 1 and rawLoc.y in [moveDir, 2*moveDir]:
self.captureLocation = positions[poss] + Vector2(1,1)
return poss
else:
if self.capture:
if rawLoc.y == colour and ref.x == 1:
if rawLoc.y == moveDir and ref.x == 1:
return poss
if ref.x == 0 and rawLoc.y == colour:
if ref.x == 0 and rawLoc.y == moveDir:
return poss

printerr(piece, " ", get_colour(), " ", label, " ", pos, " ", to_grid(pos))
printerr(positions)
null.get_node("Crash")
return "Not found"

func to_grid(pos: String) -> Vector2:
# Turn algebraic notation into gridPos
return Vector2(LET[pos[0]], int(pos[1]))

func from_grid(pos: Vector2) -> String:
# Turn gridPos into algebraic notation
return NUM[int(pos.x)] + str(pos.y)

func move(piece, newLoc):
# Simulate moving a piece between plies for parsing algebraic notation
if self.promote:
self.positions.erase(piece)
piece = self.get_colour()[0] + self.promote + char(65+self.promotions)
Expand Down
36 changes: 17 additions & 19 deletions UI.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
extends Control

onready var game := $MainGame
onready var game := get_parent().get_node("ViewportContainer/MainGame")
onready var parser := game.get_node("FileParser")
onready var tabs := $PanelContainer/VBoxContainer/TurnsTabber
onready var filePath := $PanelContainer/VBoxContainer/HBoxContainer3/Path
onready var details := $PanelContainer/VBoxContainer/GameDetails
onready var UI := $UIPanel/VBoxContainer
onready var tabs := UI.get_node("TurnsTabber")
onready var filePath := UI.get_node("FileControl/Path")
onready var details := UI.get_node("GameDetails")
var lastTurn = 0
var lastGame = 0
var TurnsTab = preload("res://TurnHolder.tscn")
Expand All @@ -16,7 +17,7 @@ func _ready():
load_file("res://tmp.pgn", true)

func set_headers():
var currGame = $MainGame.currGame
var currGame = game.currGame
details.text = currGame.path+"\n"
for data in currGame.data:
details.text += data+" : "+currGame.data[data]+"\n"
Expand Down Expand Up @@ -47,9 +48,6 @@ func new_turn(game: int, turn: Turn):
button.connect('pressed', self, 'set_turn', [i, game])
turnsList.add_child(button)

func quit():
get_tree().quit()

func update_turn(newTurn, newGame):
var turnsList = tabs.get_tab_control(self.lastGame).get_node("TurnsList")
turnsList.get_child(lastTurn).disabled = false
Expand All @@ -59,17 +57,11 @@ func update_turn(newTurn, newGame):
self.lastTurn = newTurn
self.lastGame = newGame
set_headers()
$PanelContainer/VBoxContainer/HBoxContainer/TurnNo.text = str(game.currGame.turns[newTurn].turnNo)
UI.get_node("TurnDetails/TurnNo").text = str(game.currGame.turns[newTurn].turnNo)

func set_turn(turn: int, idx: int = -1):
self.game.load_turn(turn, idx)

func _on_prevTurn_button_up() -> void:
self.game.prev_turn()

func _on_nextTurn_button_up() -> void:
self.game.next_turn()

func _on_Search_pressed() -> void:
$FileDialog.popup_centered()

Expand All @@ -87,7 +79,6 @@ func _on_SaveGame_pressed() -> void:
_error("Not implemented", "Saving of games not currently implemented")

func _on_CloseGame_pressed() -> void:
print(tabs.get_children())
var idx = tabs.get_current_tab()

# Delete from stored games
Expand All @@ -108,6 +99,13 @@ func _on_CloseGame_pressed() -> void:
game.load_turn(0,0)

func _error(title: String, message: String):
$ErrorPopup.window_title = title
$ErrorPopup/ErrorMessage.text = message
$ErrorPopup.popup_centered()
get_tree().get_root().get_node("Root").error(title, message)

func _on_nextTurn_pressed() -> void:
self.game.next_turn()

func _on_prevTurn_pressed() -> void:
self.game.prev_turn()

func _on_QuitButton_pressed() -> void:
get_tree().quit()
Loading

0 comments on commit f663af4

Please sign in to comment.