Skip to content

Commit

Permalink
Migrate to piece object
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Mar 7, 2021
1 parent cb49ef8 commit e0a8624
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
14 changes: 3 additions & 11 deletions MainGame.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
extends Node

var piece = load("res://Piece.tscn")
const TILE_OFFSET := Vector2(32,32)

var turnID = 0
var nTurns = 0
signal turn_updated
var fileParsers := []
var currFileParser = 0

func update_turns():
self.nTurns = len($FileParser.turns)
Expand All @@ -30,12 +29,5 @@ func load_turn(turn: int):
for child in $Board.get_children(): # Clear old children
child.queue_free()
for pos in currTurn.positions:
var currPiece = piece.instance()
var loc = $Board.map_to_world(currTurn.positions[pos])
currPiece.frame = "KQBNRP".find(pos[1])
currPiece.name = pos
if pos[0] == "B":
currPiece.frame+=6
currPiece.position = loc + TILE_OFFSET
$Board.add_child(currPiece)
$Board.add_piece("WB".find(pos[0]),"KQBNRP".find(pos[1]),currTurn.positions[pos])
emit_signal("turn_updated", self.turnID)
4 changes: 3 additions & 1 deletion MainGame.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=6 format=2]

[ext_resource path="res://FileParser.tscn" type="PackedScene" id=1]
[ext_resource path="res://BoardTile.png" type="Texture" id=2]
[ext_resource path="res://MainGame.gd" type="Script" id=3]
[ext_resource path="res://Board.gd" type="Script" id=4]

[sub_resource type="TileSet" id=1]
0/name = "BoardTile.png 0"
Expand Down Expand Up @@ -43,6 +44,7 @@ cell_quadrant_size = 64
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 )

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

Expand Down
52 changes: 41 additions & 11 deletions Piece.gd
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
extends Label
extends Sprite

enum TYPES {KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN}
enum COLOUR {WHITE=0, BLACK=1}
const PAWN_DIR = [-1, 1]
const KNIGHT_MOVES = [Vector2(1,2),
Vector2(-1,2),
Vector2(-1,-2),
Vector2(1,-2),
Vector2(2,1),
Vector2(-2,1),
Vector2(-2,-1),
Vector2(2,-1)]

# Declare member variables here. Examples:
# var a: int = 2
# var b: String = "text"
var gridPos : Vector2
var type : int
var moved : bool = false
var colour : int
onready var board = get_parent()

func init(colour: int, type: int) -> void:
self.colour = colour
self.type = type
self.set_frame(colour*6 + type)

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
func can_move(newLoc, capture=false) -> bool:
var raw = newLoc - self.gridPos
var ref = raw.abs()
match type:
TYPES.QUEEN:
return ref.x == 0 or ref.y == 0 or ref.x == ref.y
TYPES.KING:
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:
if not self.moved:
return ref.x == int(capture) and raw in [PAWN_DIR[self.colour], PAWN_DIR[self.colour]*2]
else:
return ref.x == int(capture) and raw == PAWN_DIR[self.colour]
return false


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta: float) -> void:
# pass
9 changes: 7 additions & 2 deletions Piece.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://ChessSprite.png" type="Texture" id=1]
[ext_resource path="res://Piece.gd" type="Script" id=2]
[ext_resource path="res://Draggable.tscn" type="PackedScene" id=3]

[node name="Piece" type="Sprite"]
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 1 )
vframes = 2
hframes = 6
frame = 4
frame = 11
script = ExtResource( 2 )

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

0 comments on commit e0a8624

Please sign in to comment.