Skip to content
Snippets Groups Projects
Pawn.gd 1.93 KiB
extends Piece

class_name Pawn

enum Upgrade { DOUBLE_MOVE, DIAGONAL_MOVE }

func _init(is_white: bool, position: Vector2):
	super._init(is_white, position)
	# Testing upgrades
	# upgrades.append(Upgrade.DOUBLE_MOVE)
	# upgrades.append(Upgrade.DIAGONAL_MOVE)


func get_valid_moves(board_state: Array, pos: Vector2) -> Array[Vector2]:
	var valid_moves: Array[Vector2] = []
	var x = pos.x
	var y = pos.y

	if is_white:
		# Check if the pawn is at the starting position
		if (y == 7 or Upgrade.DOUBLE_MOVE in upgrades) and board_state[x][y - 1] == null:
			valid_moves.append(Vector2(x, y - 1))
			if board_state[x][y - 2] == null:
				valid_moves.append(Vector2(x, y - 2))
		elif board_state[x][y - 1] == null: 
			valid_moves.append(Vector2(x, y - 1))

		# Check if the pawn can capture an enemy piece
		if x > 0 and y > 0 and board_state[x - 1][y - 1] != null and not board_state[x - 1][y - 1].is_white:
			valid_moves.append(Vector2(x - 1, y - 1))
		if x < 7 and y > 0 and board_state[x + 1][y - 1] != null and not board_state[x + 1][y - 1].is_white:
			valid_moves.append(Vector2(x + 1, y - 1))
		
		# Check if the pawn can move diagonally without capturing
		if Upgrade.DIAGONAL_MOVE in upgrades:
			if x > 0 and y > 0 and board_state[x - 1][y - 1] == null:
				valid_moves.append(Vector2(x - 1, y - 1))
			if x < 7 and y > 0 and board_state[x + 1][y - 1] == null:
				valid_moves.append(Vector2(x + 1, y - 1))

	else:
		if y < 1 and board_state[x][y + 1] == null:
			valid_moves.append(Vector2(x, y + 1))
			if board_state[x][y + 2] == null:
				valid_moves.append(Vector2(x, y + 2))
		elif board_state[x][y + 1] == null: 
			valid_moves.append(Vector2(x, y + 1))

		if x > 0 and y < 7 and board_state[x - 1][y + 1] != null and board_state[x - 1][y + 1].is_white:
			valid_moves.append(Vector2(x - 1, y + 1))
		if x < 7 and y < 7 and board_state[x + 1][y + 1] != null and board_state[x + 1][y + 1].is_white:
			valid_moves.append(Vector2(x + 1, y + 1))

	return valid_moves