Skip to content

Commit

Permalink
Chess now playable
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Mar 10, 2021
1 parent e0a8624 commit a4f951a
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 45 deletions.
95 changes: 95 additions & 0 deletions Board.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
extends TileMap

onready var PIECE = preload("res://Piece.tscn")

const LET = {"a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8}
const NUM = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 6:"f", 7:"g", 8:"h"}

signal error(title, message)
signal piece_moved(SAN)
var positions = {}
const TILE_OFFSET := Vector2(32,32)
onready var game = get_parent()

func add_piece(colour, type, pos):
if pos in self.positions:
emit_signal("error","Cannot add piece", "Tile "+str(pos)+" occupied")
var piece = PIECE.instance()
piece.init(colour, type)
add_child(piece)
update_piece(piece, pos)
piece.moved = false

func remove_piece(piece):
piece.queue_free()
self.positions.erase(piece)

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

func move_piece(pos:Vector2, newLoc:Vector2):
if not pos in self.positions:
emit_signal("error","Cannot move piece", "No piece in position")
return
if (newLoc.x * newLoc.y < 0) or newLoc.x > 8 or newLoc.y > 8:
emit_signal("error","Cannot move piece", "Not on board")
return

var moving = self.positions[pos]
var SAN = moving.TYPES_SAN[moving.type] + self.from_grid(pos)
var capture = false
var promote = false
var target = null

if moving.colour != game.currTurn:
emit_signal("error","Cannot move piece", "Not current player")
return

if newLoc in self.positions:
target = self.positions[newLoc]
if target.colour != moving.colour:
capture = true
else:
emit_signal("error","Cannot move piece", "Tile occupied")
return

if not moving.can_move(newLoc, capture):
emit_signal("error","Cannot move piece", "Piece cannot move there")
return

if target:
self.remove_piece(target)

self.update_piece(moving, newLoc)


if capture:
SAN += "x"
if promote:
SAN += "="+moving.TYPES_SAN[moving.type]
SAN += self.from_grid(newLoc)
match moving.castled:
-1:
SAN = "O-O"
var rook = self.positions[Vector2(1,moving.colour*7+1)]
update_piece(rook, Vector2(3,moving.colour*7+1))
moving.castled = 0
1:
SAN = "O-O-O"
var rook = self.positions[Vector2(8,moving.colour*7+1)]
update_piece(rook, Vector2(6,moving.colour*7+1))
moving.castled = 0

emit_signal("piece_moved", SAN)

func to_grid(pos: String) -> Vector2:
return Vector2(LET[pos[0]], int(pos[1]))

func from_grid(pos: Vector2) -> String:
return NUM[int(pos.x)] + str(pos.y)

62 changes: 62 additions & 0 deletions Draggable.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
extends Area2D

export (bool) var active = true
var dragging := false
var mouse_in := false
var stored_pos: Vector2
onready var parent = get_parent()

const COL_ACTIVE = 1 << 20

signal drag
signal stopdrag
signal deselect
signal select(parent)

func _ready():
add_to_group("draggable")
if active:
activate()
else:
deactivate()

func _undrag():
dragging = false
set_process_unhandled_input(false)

func _drag():
dragging = true
set_process_unhandled_input(true)

func _input(event):
if mouse_in:
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
stored_pos = parent.position
emit_signal("drag")
emit_signal("select", parent)
get_tree().set_input_as_handled()

func _unhandled_input(event):
if event is InputEventMouseButton:
if not event.pressed and dragging:
emit_signal("stopdrag")

func set_shape(shape: Shape2D):
$Area.set_shape(shape)

func activate():
active = true
collision_layer = COL_ACTIVE

func deactivate():
_undrag()
active = false
collision_layer = 0

func _mouse_enter():
mouse_in = true

func _mouse_exit():
mouse_in = false

18 changes: 18 additions & 0 deletions Draggable.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://Draggable.gd" type="Script" id=1]

[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 64, 64 )

[node name="Draggable" type="Area2D"]
collision_layer = 524288
collision_mask = 0
script = ExtResource( 1 )

[node name="Area" type="CollisionShape2D" parent="."]
shape = SubResource( 1 )
[connection signal="drag" from="." to="." method="_drag"]
[connection signal="mouse_entered" from="." to="." method="_mouse_enter"]
[connection signal="mouse_exited" from="." to="." method="_mouse_exit"]
[connection signal="stopdrag" from="." to="." method="_undrag"]
6 changes: 5 additions & 1 deletion FileParser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ extends Node

var path := ""
var data := {}
var turns := []
var turns := [Turn.new()]
const COLOURS = ["White", "Black"]
signal read
signal error(title, message)

func _ready():
turns = [Turn.new()]

func read(filepath):
self.turns = []
self.path = filepath
var file := File.new()
var status = file.open(self.path, File.READ)
Expand Down
5 changes: 5 additions & 0 deletions MainGame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var nTurns = 0
signal turn_updated
var fileParsers := []
var currFileParser = 0
var currTurn = 0

func update_turns():
self.nTurns = len($FileParser.turns)
Expand All @@ -31,3 +32,7 @@ func load_turn(turn: int):
for pos in currTurn.positions:
$Board.add_piece("WB".find(pos[0]),"KQBNRP".find(pos[1]),currTurn.positions[pos])
emit_signal("turn_updated", self.turnID)


func _on_Board_piece_moved(SAN) -> void:
self.currTurn = 1 - self.currTurn
5 changes: 1 addition & 4 deletions MainGame.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,5 @@ tile_data = PoolIntArray( 65537, 1, 0, 65538, 0, 0, 65539, 1, 0, 65540, 0, 0, 65
script = ExtResource( 4 )

[node name="FileParser" parent="." instance=ExtResource( 1 )]

[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2( 320, 320 )
current = true
[connection signal="piece_moved" from="Board" to="." method="_on_Board_piece_moved"]
[connection signal="read" from="FileParser" to="." method="update_turns"]
37 changes: 32 additions & 5 deletions Piece.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends Sprite

enum TYPES {KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN}
enum COLOUR {WHITE=0, BLACK=1}
const PAWN_DIR = [-1, 1]
const PAWN_DIR = [1, -1]
const KNIGHT_MOVES = [Vector2(1,2),
Vector2(-1,2),
Vector2(-1,-2),
Expand All @@ -11,12 +11,14 @@ const KNIGHT_MOVES = [Vector2(1,2),
Vector2(-2,1),
Vector2(-2,-1),
Vector2(2,-1)]
const TYPES_SAN = "KQBNRP"

var gridPos : Vector2
var type : int
var moved : bool = false
var colour : int
onready var board = get_parent()
var castled : int = 0

func init(colour: int, type: int) -> void:
self.colour = colour
Expand All @@ -30,17 +32,42 @@ func can_move(newLoc, capture=false) -> bool:
TYPES.QUEEN:
return ref.x == 0 or ref.y == 0 or ref.x == ref.y
TYPES.KING:
return ref.length_squared < 2
print(self.moved, newLoc, self.colour*7+1)
if not self.moved and newLoc in [Vector2(3, self.colour*7+1), Vector2(7, self.colour*7+1)]:
print("Hello")
match int(newLoc.x):
3:
if (not newLoc in board.positions and
not newLoc + Vector2.RIGHT in board.positions and
Vector2(1,self.colour*7+1) in board.positions and
not board.positions[Vector2(1,self.colour*7+1)].moved):
self.castled = -1
return true
7:
if (not newLoc in board.positions and
not newLoc + Vector2.LEFT in board.positions and
Vector2(8,self.colour*7+1) in board.positions and
not board.positions[Vector2(8,self.colour*7+1)].moved):
self.castled = 1
return true
_:
print("Nope")
pass
return ref.length_squared() < 2
TYPES.KNIGHT:
return ref in KNIGHT_MOVES
TYPES.ROOK:
return ref.x == 0 or ref.y == 0
TYPES.BISHOP:
return ref.x == ref.y
TYPES.PAWN:
TYPES.PAWN:
if not self.moved:
return ref.x == int(capture) and raw in [PAWN_DIR[self.colour], PAWN_DIR[self.colour]*2]
return ref.x == int(capture) and raw.y in [PAWN_DIR[self.colour], PAWN_DIR[self.colour]*2]
else:
return ref.x == int(capture) and raw == PAWN_DIR[self.colour]
return ref.x == int(capture) and raw.y == PAWN_DIR[self.colour]
return false

func _on_Draggable_stopdrag() -> void:
var mousePos = get_viewport().get_mouse_position()
var newPos = board.world_to_map(mousePos)
board.move_piece(self.gridPos, newPos)
1 change: 1 addition & 0 deletions Piece.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ frame = 11
script = ExtResource( 2 )

[node name="Draggable" parent="." instance=ExtResource( 3 )]
[connection signal="stopdrag" from="Draggable" to="." method="_on_Draggable_stopdrag"]
10 changes: 8 additions & 2 deletions UI.gd
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
extends Control

onready var game = $ViewportContainer/Viewport/MainGame
onready var parser = $ViewportContainer/Viewport/MainGame/FileParser
onready var game = $MainGame
onready var parser = game.get_node("FileParser")
onready var turnsList = $PanelContainer/VBoxContainer/Turns/VBoxContainer
var lastTurn = 0

func _ready():
var file = File.new()
file.open("res://tmp.pgn", File.WRITE)
file.close()
load_file("res://tmp.pgn")

func load_file(path):
parser.read(path)

Expand Down
Loading

0 comments on commit a4f951a

Please sign in to comment.