Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra custom movement #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scenes/falling_piece.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ radius = 15.0

[node name="FallingPiece" type="RigidBody2D"]
mass = 0.1
inertia = 1.0
physics_material_override = SubResource("PhysicsMaterial_j7sot")
gravity_scale = 0.2
inertia = 1.0
script = ExtResource("1_1yvsh")

[node name="CollisionShape" type="CollisionShape2D" parent="."]
Expand Down
2 changes: 1 addition & 1 deletion scripts/board.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func _draw():

match moves[move].type:
Moves.MOVE:
draw_circle(pos, tile_size/8, Color("000", 0.5))
draw_circle(pos, tile_size/8, Color(255 * moves[move].extra_move, 255 * moves[move].valid_move,0, 0.5))
Moves.CAPTURE:
draw_arc(pos, tile_size/2.5, 0, TAU, 16, Color("000", 0.7), 1)
Moves.CASTLE:
Expand Down
34 changes: 27 additions & 7 deletions scripts/moves.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,29 @@ const L_SHAPE = [[ 2, 1], [ 1, 2], [-1, 2], [-2, 1],

static func not_in_range(x, y): return x < 0 or x > 7 or y < 0 or y > 7

static func new_move() -> Dictionary:
return {"type": MOVE}
static func new_move(isValid: bool = true, isExtra: bool = false) -> Dictionary:
if isValid and isExtra:
return new_valid_extra_move()
if isExtra:
return new_invalid_extra_move()
return new_valid_move()

static func new_valid_move() -> Dictionary:
return {"type": MOVE, "valid_move": 1, "extra_move": 0}

static func new_valid_extra_move() -> Dictionary:
return {"type": MOVE, "valid_move": 1, "extra_move": 1}

static func new_invalid_extra_move() -> Dictionary:
return {"type": MOVE, "valid_move": 0, "extra_move": 1}

static func new_capture(take_piece) -> Dictionary:
return {"type": CAPTURE, "take_piece": take_piece}

static func new_castle(side) -> Dictionary:
return {"type": CASTLE, "side": side}

static func basic(pos: Vector2i, board: Array, directions: Array) -> Dictionary:
static func basic(pos: Vector2i, board: Array, directions: Array, isValid: bool = true, isExtra: bool = false) -> Dictionary:
var moves := {}

var color = board[pos.y][pos.x].team
Expand All @@ -35,7 +48,7 @@ static func basic(pos: Vector2i, board: Array, directions: Array) -> Dictionary:
var tile = board[y][x]

if !tile:
moves[Vector2i(x, y)] = new_move()
moves[Vector2i(x, y)] = new_move(isValid, isExtra)
elif color != tile.team:
moves[Vector2i(x, y)] = new_capture(tile)

Expand Down Expand Up @@ -67,15 +80,22 @@ static func line(pos: Vector2i, board: Array, directions: Array) -> Dictionary:

return moves

static func pawn(pos: Vector2i, board: Array, round_num: int) -> Dictionary:
static func pawn(pos: Vector2i, board: Array, round_num: int, directions: Array) -> Dictionary:
var extra_valid_moves := basic(pos, board, Moves.OCTO, true, true)
var extra_invalid_moves := basic(pos, board, [[2, 0], [-2, 0], [0, 2], [0, -2]], false, true)
var moves := {}

moves.merge(extra_valid_moves)
moves.merge(extra_invalid_moves)

var pawn_piece = board[pos.y][pos.x]
var pawn_color = pawn_piece.team

var move_dir := 1 if pawn_color == "black" else -1
var original_rank := 1 if pawn_color == "black" else 6

var color = board[pos.y][pos.x].team


# Move
var possible_moves := [Vector2i(0, move_dir)]

Expand All @@ -86,7 +106,7 @@ static func pawn(pos: Vector2i, board: Array, round_num: int) -> Dictionary:
if board[pos.y + move.y][pos.x]:
break

moves[pos + move] = new_move()
moves[pos + move] = new_valid_move()

# Attack
for attack_dir in [-1, 1]:
Expand Down
3 changes: 2 additions & 1 deletion scripts/piece.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var piece_id := PAWN:
var pos: Vector2i # Position in the board array, not screen

func _ready():

if team == "black":
texture = preload("res://assets/black.png")

Expand All @@ -47,7 +48,7 @@ func move_animation(new_pos: Vector2i):

func get_moves(board: Array) -> Dictionary:
match piece_id:
PAWN: return Moves.pawn(pos, board, game.round_num)
PAWN: return Moves.pawn(pos, board, game.round_num, Moves.OCTO)
KNIGHT: return Moves.basic(pos, board, Moves.L_SHAPE)
BISHOP: return Moves.line(pos, board, Moves.DIAGONAL)
ROOK: return Moves.line(pos, board, Moves.ORTHOGONAL)
Expand Down