ChessBuilder/Systems/Cards/Supernova.gd

158 lines
5.9 KiB
GDScript

class_name SupernovaCard extends Card
const CAPTURE_RADIUS = 4
var valid = false;
func _init():
super._init()
cardName = "Supernova"
rank = Rank.RANK_0
effectType = EffectType.PIECE_EFFECT
duration = 5
description = "All enemy units within 4 radius are captured"
unitWhitelist = ["King"]
remaining_turns = duration
func apply_effect(target_piece = null, board_flow = null, game_state = null):
attached_piece = target_piece
if !target_piece or !board_flow or !game_state:
print(cardName, " missing input param ")
return false
setup_persistent_effect(game_state)
# dont apply on card attachment
if !valid:
return true
# Get king's position
var king_pos = target_piece.get_parent().name.split("-")
var king_x = int(king_pos[0])
var king_y = int(king_pos[1])
# Collect all tiles within radius to check
var tiles_to_check = []
# Check in all directions (up, down, left, right and diagonals)
for dx in range(-CAPTURE_RADIUS, CAPTURE_RADIUS + 1):
for dy in range(-CAPTURE_RADIUS, CAPTURE_RADIUS + 1):
# Skip if outside Manhattan distance
if abs(dx) + abs(dy) > CAPTURE_RADIUS:
continue
# Calculate target coordinates
var target_x = king_x + dx
var target_y = king_y + dy
# Skip if outside board bounds
if target_x < 0 or target_x >= 8 or target_y < 0 or target_y >= 8:
continue
# Skip king's own position
if dx == 0 and dy == 0:
continue
# Add tile to check list
tiles_to_check.append(str(target_x) + "-" + str(target_y))
for tile_name in tiles_to_check:
var tile = board_flow.get_node(tile_name) as PieceContainer
if tile.get_piece() != null:
var piece = tile.get_piece()
if piece.Item_Color != target_piece.Item_Color: # Only capture enemy pieces
game_state.updatePoints(piece)
tile.remove_piece()
# Process tiles and add overlay
for tile_name in tiles_to_check:
var container = board_flow.get_node(tile_name) as PieceContainer
# Handle overlay through container's overlay management
container.remove_overlay("SupernovaOverlay")
var overlay = ColorRect.new()
overlay.name = "SupernovaOverlay"
overlay.color = Color(1, 0, 0, 0.3)
overlay.size = container.size
overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE
container.add_overlay(overlay)
overlay.show()
# Check for pieces to affect
var piece = container.get_piece()
if piece != null and piece.Item_Color != target_piece.Item_Color:
game_state.updatePoints(piece)
container.remove_piece()
# Setup timer to remove overlays
var timer = Timer.new()
board_flow.add_child(timer)
timer.wait_time = 1
timer.one_shot = true
timer.timeout.connect(func():
for tile_name in tiles_to_check:
var tile = board_flow.get_node(tile_name)
var overlay = tile.get_node_or_null("SupernovaOverlay")
if overlay:
overlay.queue_free()
timer.queue_free()
)
timer.start()
return true
func setup_persistent_effect(game_state):
# Connect to the turn change signal if not already connected
if !game_state.is_connected("turn_changed", Callable(self, "on_turn_changed")):
game_state.connect("turn_changed", Callable(self, "on_turn_changed"))
func on_turn_changed():
if attached_piece.get_parent().get_parent().owner.isPlayerTurn() and attached_piece and remaining_turns > 0:
# print(attached_piece.get_parent().get_parent(), attached_piece.get_parent().get_parent().owner)
var piece = attached_piece
var flow = attached_piece.get_parent().get_parent()
var state = attached_piece.get_parent().get_parent().owner
# print("Debug values:")
# print("- Piece: ", piece, " is null? ", piece == null)
# print("- Board Flow: ", flow, " is null? ", flow == null)
# print("- Game State: ", state, " is null? ", state == null)
# Now try apply_effect with these values
valid = true
apply_effect(piece, flow, state)
func remove_effect():
if attached_piece:
var game_state = attached_piece.get_parent().get_parent().owner
if game_state.is_connected("turn_changed", Callable(self, "on_turn_changed")):
game_state.disconnect("turn_changed", Callable(self, "on_turn_changed"))
super.remove_effect()
func update_visual_effect(king, board_flow):
# Could be extended with visual feedback like particles or highlighting
var radius_tiles = []
var king_pos = king.get_parent().name.split("-")
var king_x = int(king_pos[0])
var king_y = int(king_pos[1])
for x in range(max(0, king_x - CAPTURE_RADIUS), min(8, king_x + CAPTURE_RADIUS + 1)):
for y in range(max(0, king_y - CAPTURE_RADIUS), min(8, king_y + CAPTURE_RADIUS + 1)):
if abs(x - king_x) + abs(y - king_y) <= CAPTURE_RADIUS:
var tile_name = str(x) + "-" + str(y)
radius_tiles.append(board_flow.get_node(tile_name))
# Highlight affected tiles briefly
for tile in radius_tiles:
var style = StyleBoxFlat.new()
style.bg_color = Color(1, 0, 0, 0.3) # Red tint
tile.add_theme_stylebox_override("normal", style)
# Remove highlight after a short delay
var timer = Timer.new()
board_flow.add_child(timer)
timer.wait_time = 0.5
timer.one_shot = true
timer.timeout.connect(func():
tile.remove_theme_stylebox_override("normal")
timer.queue_free()
)
timer.start()