fixed multi movement p[ortal intereactions and added portal fen definitions

This commit is contained in:
2ManyProjects 2025-02-28 23:48:12 -06:00
parent a7b359f8be
commit e055b75a49
6 changed files with 119 additions and 12 deletions

View file

@ -43,7 +43,9 @@ var stockfishPath = "res://Assets/ChessEngines/stockfish/stockfish.exe"
@export var tileYSize: int = 50 @export var tileYSize: int = 50
@export var windowXSize: int = 1280 @export var windowXSize: int = 1280
@export var windowYSize: int = 720 @export var windowYSize: int = 720
@export var FEN: String = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" @export var FEN: String = "rnbqkbnr/pppppppp/4U3/2u5/1****u*1/4U3/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
# "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
# "2rnbqkbnr2/2pppppppp2/12/12/2********2/12/12/12/12/12/2PPPPPPPP2/2RNBQKBNR2 w KQkq - 0 1"
@onready var boardContainer: FlowContainer = $Flow @onready var boardContainer: FlowContainer = $Flow
@onready var stateMachine: StateMachine = $StateMachine @onready var stateMachine: StateMachine = $StateMachine
@ -82,7 +84,8 @@ func initializeTiles() -> void:
add_child(tileManager) add_child(tileManager)
await get_tree().process_frame await get_tree().process_frame
tileManager.initialize(boardContainer) tileManager.initialize(boardContainer)
tileManager.place_random_game_tiles() setupTilesFromFEN()
# tileManager.place_random_game_tiles()
func get_base_style(is_white: bool) -> StyleBoxFlat: func get_base_style(is_white: bool) -> StyleBoxFlat:
return lightStyle if is_white else darkStyle return lightStyle if is_white else darkStyle
@ -165,6 +168,17 @@ func getCurrentFen() -> String:
fen += "*" fen += "*"
else: else:
fen += "*" fen += "*"
elif tile is PortalTile:
var piece = container.get_piece()
if piece == null:
emptySquares += 1
else:
if emptySquares > 0:
fen += str(emptySquares)
emptySquares = 0
# Convert piece to FEN notation
var fenChar = getPieceFenChar(piece)
fen += fenChar
else: else:
var piece = container.get_piece() var piece = container.get_piece()
if piece == null: if piece == null:
@ -274,7 +288,7 @@ func createBoard() -> void:
func createTile(x: int, y: int, isWhite: bool) -> void: func createTile(x: int, y: int, isWhite: bool) -> void:
# print("CreateTile x ", x, " y ", y); # print("CreateTile x ", x, " y ", y);
var tile = PieceContainer.new() var tile = PieceContainer.new(str(x) + "-" + str(y))
tile.set_custom_minimum_size(Vector2(tileXSize, tileYSize)) tile.set_custom_minimum_size(Vector2(tileXSize, tileYSize))
tile.add_theme_stylebox_override("normal", lightStyle if isWhite else darkStyle) tile.add_theme_stylebox_override("normal", lightStyle if isWhite else darkStyle)
@ -355,9 +369,54 @@ func setupPiecesFromFEN() -> void:
x += int(c) x += int(c)
else: else:
var piece_info = getFENPieceInfo(c) var piece_info = getFENPieceInfo(c)
if piece_info.name != "":
placePiece(str(x) + "-" + str(board_y), piece_info.name, piece_info.color) placePiece(str(x) + "-" + str(board_y), piece_info.name, piece_info.color)
x += 1 x += 1
func setupTilesFromFEN() -> void:
var fen_parts = FEN.split(" ")
var rows = fen_parts[0].split("/")
var matchedPortals = {}
var portalCnt = 0;
# Iterate through rows in reverse to place black pieces at top
for y in range(rows.size()):
var x = 0
# Convert y coordinate to flip the board (7-y puts white at bottom)
var board_y = (boardYSize - 1) - y # For an 8x8 board
for c in rows[y]:
if c.is_valid_int():
# Skip empty squares
x += int(c)
else:
var loc = str(x) + "-" + str(board_y);
if tileManager.portalString.find(c) > -1:
var char = c
# shjould we lowercase?
if char in matchedPortals:
if !("p2" in matchedPortals[char]):
matchedPortals[char].p2 = loc
tileManager.place_portal_pair(matchedPortals[char].p1, matchedPortals[char].p2, portalCnt)
portalCnt += 1;
else:
matchedPortals[char] = { "p1": loc }
x += 1
else:
var tile = getFENTile(c, loc)
if tile != null:
tileManager.add_tile(loc, tile)
x += 1
func getFENTile(fen_char: String, location: String) -> Tile:
var tile = null
var container = boardContainer.get_node(location) as PieceContainer
var is_white = (int(location.split("-")[0]) + int(location.split("-")[1])) % 2 == 0
# Map FEN characters to piece names
match fen_char.to_upper():
"*": tile = WallTile.new(container, is_white, -1)
return tile
func getFENPieceInfo(fen_char: String) -> Dictionary: func getFENPieceInfo(fen_char: String) -> Dictionary:
var piece_info = { var piece_info = {
@ -433,7 +492,7 @@ func updateEffectDurations() -> void:
tileManager.update_tile_durations() tileManager.update_tile_durations()
func applyTileEffects() -> void: func applyTileEffects() -> void:
tileManager.apply_tile_effects() tileManager.apply_alltile_effects()
func applyCardEffects() -> void: func applyCardEffects() -> void:
@ -475,12 +534,14 @@ func resetBoard() -> void:
updateTurnIndicator() updateTurnIndicator()
func getMovableAreas() -> void: func getMovableAreas() -> void:
# print("HIGHLIGHTING getMovableAreas 1") print("HIGHLIGHTING getMovableAreas 1", selectedNode)
resetHighlights() resetHighlights()
areas.clear() areas.clear()
specialArea.clear() specialArea.clear()
var container = get_node("Flow/" + selectedNode) as PieceContainer var container = get_node("Flow/" + selectedNode) as PieceContainer
var piece = container.get_piece() var piece = container.get_piece()
if piece == null:
return
var piece_id = piece.get_instance_id() var piece_id = piece.get_instance_id()
# print("HIGHLIGHTING getMovableAreas 2") # print("HIGHLIGHTING getMovableAreas 2")
if deckManager.attached_cards.has(piece_id): if deckManager.attached_cards.has(piece_id):

View file

@ -3,8 +3,10 @@ class_name PieceContainer extends Button
var piece: Pawn = null var piece: Pawn = null
var effects: Node # Keep effects as a sub-node var effects: Node # Keep effects as a sub-node
var overlay_nodes: Array[Node] = [] var overlay_nodes: Array[Node] = []
var location: String
func _init() -> void: func _init(loc: String) -> void:
location = loc
# Setup effects node # Setup effects node
effects = Node.new() effects = Node.new()
effects.name = "Effects" effects.name = "Effects"

View file

@ -177,6 +177,7 @@ func handleMovement(location: String, generated: bool = false) -> void:
print("ANOTHER MOVE") print("ANOTHER MOVE")
game.resetHighlights() game.resetHighlights()
if game.selectedNode != "": if game.selectedNode != "":
game.tileManager.apply_tile_effect(game.selectedNode)
game.getMovableAreas() game.getMovableAreas()
if game.areas.size() == 0 and game.specialArea.size() == 0: if game.areas.size() == 0 and game.specialArea.size() == 0:
print("Consuming move because no valid moves left") print("Consuming move because no valid moves left")
@ -189,6 +190,7 @@ func handleMovement(location: String, generated: bool = false) -> void:
else: else:
game.hasMoved = false game.hasMoved = false
game.currentlyMovingPiece = null game.currentlyMovingPiece = null
# game.applyTileEffects()
# finished.emit(Constants.POST_MOVE) # finished.emit(Constants.POST_MOVE)

View file

@ -4,6 +4,7 @@ extends Node
var active_tiles: Dictionary = {} # location: Tile var active_tiles: Dictionary = {} # location: Tile
var board_flow: FlowContainer var board_flow: FlowContainer
var game: ChessGame var game: ChessGame
const portalString : String = "UuVvWwXxYyZz"
const PortalTile = preload("res://Systems/Tiles/Portal.gd") const PortalTile = preload("res://Systems/Tiles/Portal.gd")
func _init(flow: FlowContainer = null, chess_game: ChessGame = null) -> void: func _init(flow: FlowContainer = null, chess_game: ChessGame = null) -> void:
if flow: if flow:
@ -58,7 +59,7 @@ func update_tile_durations() -> void:
for location in expired_locations: for location in expired_locations:
remove_tile(location) remove_tile(location)
func apply_tile_effects() -> void: func apply_alltile_effects() -> void:
if active_tiles.is_empty(): if active_tiles.is_empty():
return return
@ -70,6 +71,17 @@ func apply_tile_effects() -> void:
if tile.can_affect_piece(piece): if tile.can_affect_piece(piece):
tile.apply_effect(piece) tile.apply_effect(piece)
func apply_tile_effect(location: String) -> void:
if active_tiles.is_empty() or !active_tiles.has(location):
return
var tile = active_tiles[location]
var container = board_flow.get_node(location) as PieceContainer
if container && container.has_piece():
var piece = container.get_piece()
if tile.can_affect_piece(piece):
tile.apply_effect(piece)
func clear_tiles() -> void: func clear_tiles() -> void:
var locations = active_tiles.keys() var locations = active_tiles.keys()
for location in locations: for location in locations:
@ -93,6 +105,8 @@ func place_random_game_tiles(num_tiles: int = 5) -> void:
var board_y = (game.boardYSize - 1) - y # Flip y coordinate to match board orientation var board_y = (game.boardYSize - 1) - y # Flip y coordinate to match board orientation
# Add all positions in this empty row # Add all positions in this empty row
for x in range(game.boardXSize): for x in range(game.boardXSize):
if active_tiles.has(str(x) + "-" + str(board_y)):
continue
available_positions.append(str(x) + "-" + str(board_y)) available_positions.append(str(x) + "-" + str(board_y))
available_positions.shuffle() available_positions.shuffle()
@ -175,14 +189,15 @@ func place_random_game_tiles(num_tiles: int = 5) -> void:
var next_pos = available_positions[i + 1] var next_pos = available_positions[i + 1]
var next_container = board_flow.get_node(next_pos) as PieceContainer var next_container = board_flow.get_node(next_pos) as PieceContainer
if next_container: if next_container:
place_portal_pair(pos, i, available_positions, container, is_white) place_portal(pos, i, available_positions, container, is_white)
skipNext = true skipNext = true
else: else:
# If we can't make a pair, fall back to a wall tile # If we can't make a pair, fall back to a wall tile
tile = WallTile.new(container, is_white, -1) tile = WallTile.new(container, is_white, -1)
add_tile(pos, tile) add_tile(pos, tile)
func place_portal_pair(pos: String = "", i: int = 0, available_positions: Array = [], container: PieceContainer = null, is_white: bool = false) -> void:
func place_portal(pos: String = "", i: int = 0, available_positions: Array = [], container: PieceContainer = null, is_white: bool = false) -> void:
var next_pos = available_positions[i + 1] var next_pos = available_positions[i + 1]
var next_container = board_flow.get_node(next_pos) as PieceContainer var next_container = board_flow.get_node(next_pos) as PieceContainer
if next_container: if next_container:
@ -202,3 +217,27 @@ func place_portal_pair(pos: String = "", i: int = 0, available_positions: Array
# Add both portals # Add both portals
add_tile(pos, portals[0]) add_tile(pos, portals[0])
add_tile(next_pos, portals[1]) add_tile(next_pos, portals[1])
func place_portal_pair(pos: String = "", next_pos: String = "", i: int = 0) -> void:
var container = board_flow.get_node(pos) as PieceContainer
var next_container = board_flow.get_node(next_pos) as PieceContainer
if next_container:
var is_white = (int(pos.split("-")[0]) + int(pos.split("-")[1])) % 2 == 0
var next_is_white = (int(next_pos.split("-")[0]) + int(next_pos.split("-")[1])) % 2 == 0
# Create portal pair with alternating blue/orange colors
# var portal_color = Color.ORANGE if (i % 2 == 0) else Color.BLUE
var portals = PortalTile.create_portal_pair(
container, is_white,
next_container, next_is_white,
-1, # permanent duration
i, # pair id
Color.ORANGE,
Color.BLUE
)
# Add both portals
add_tile(pos, portals[0])
add_tile(next_pos, portals[1])

View file

@ -21,7 +21,7 @@ func _init(button: Button, is_white: bool, d: int, id: int, color: Color) -> voi
func apply_effect(piece: Pawn = null) -> void: func apply_effect(piece: Pawn = null) -> void:
if !piece || piece == null || !is_effect_active() || !other_portal || !other_portal.is_effect_active(): if !piece || piece == null || !is_effect_active() || !other_portal || !other_portal.is_effect_active():
return return
if last_piece and last_piece.id == piece.id: if last_piece != null and last_piece.id == piece.id:
return return
# Check if partner portal tile has a piece on it # Check if partner portal tile has a piece on it
var partner_container = other_portal.base_button as PieceContainer var partner_container = other_portal.base_button as PieceContainer
@ -38,6 +38,9 @@ func apply_effect(piece: Pawn = null) -> void:
other_portal.last_piece = piece other_portal.last_piece = piece
# Move the piece # Move the piece
animate_portal_teleport(piece, current_container, target_container) animate_portal_teleport(piece, current_container, target_container)
print("*****************SETTING SELECTED*(*************)")
print(target_container.location)
game.selectedNode = target_container.location
# target_container.animate_movement(current_container, piece); # target_container.animate_movement(current_container, piece);
func animate_portal_teleport(piece: Pawn, origin_container: PieceContainer, destination_container: PieceContainer) -> void: func animate_portal_teleport(piece: Pawn, origin_container: PieceContainer, destination_container: PieceContainer) -> void: