-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
250 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.