ChessBuilder/Systems/Tiles/Portal.gd

121 lines
No EOL
4.6 KiB
GDScript

class_name PortalTile
extends Tile
var pair_id: int # To identify which portals are paired
var other_portal: PortalTile = null # Reference to paired portal
var portal_color: Color # Each pair should have a distinct color
var teleported_pieces: Array[int] = [] # Track pieces that have already been teleported
var last_piece : Pawn = null
func _init(button: Button, is_white: bool, d: int, id: int, color: Color) -> void:
super._init(button, is_white, d)
tile_name = "Portal"
description = "Teleports pieces to its paired portal on first contact"
type = TileType.GENERAL
tile_owner = TileOwner.GAME
duration = d
pair_id = id
portal_color = color
passable = true
jumpable = true
func apply_effect(piece: Pawn = null) -> void:
if !piece || piece == null || !is_effect_active() || !other_portal || !other_portal.is_effect_active():
return
if last_piece and last_piece.id == piece.id:
return
# Check if partner portal tile has a piece on it
var partner_container = other_portal.base_button as PieceContainer
if partner_container && partner_container.has_piece():
return
var current_container = piece.get_parent() as PieceContainer
if current_container:
var target_container = other_portal.base_button as PieceContainer
if target_container:
last_piece = piece
other_portal.last_piece = piece
# Move the piece
animate_portal_teleport(piece, current_container, target_container)
# target_container.animate_movement(current_container, piece);
func animate_portal_teleport(piece: Pawn, origin_container: PieceContainer, destination_container: PieceContainer) -> void:
# Save original properties
var original_scale = piece.scale
var original_modulate = piece.modulate
var original_z_index = piece.z_index
# Raise z-index during animation
piece.z_index = 2
# Step 1: Move to portal center
var portal_center = base_button.global_position + (base_button.size / 2)
var step1_tween = piece.create_tween()
step1_tween.set_trans(Tween.TRANS_LINEAR)
step1_tween.tween_property(piece, "global_position", portal_center, 0.2)
await step1_tween.finished
# Step 2: Shrink and fade out
var step2_tween = piece.create_tween()
step2_tween.set_trans(Tween.TRANS_QUAD)
step2_tween.set_ease(Tween.EASE_OUT)
# Ensure modulate maintains the piece's color (white or black)
var fade_color = original_modulate.darkened(0.5)
fade_color.a = 0.0 # Fully transparent
# Animate scale and transparency
step2_tween.tween_property(piece, "scale", Vector2.ZERO, 0.3)
step2_tween.parallel().tween_property(piece, "modulate", fade_color, 0.3)
await step2_tween.finished
# Step 3: Reparent to destination container
# origin_container.remove_child(piece)
# destination_container.add_child(piece)
# Position at target portal center
var target_portal_center = other_portal.base_button.global_position + (other_portal.base_button.size / 2)
piece.global_position = target_portal_center
# Step 4: Grow and fade in at destination
var step4_tween = piece.create_tween()
step4_tween.set_trans(Tween.TRANS_BACK) # A bit of bounce
step4_tween.set_ease(Tween.EASE_OUT)
# Reset scale and opacity
step4_tween.tween_property(piece, "scale", original_scale, 0.3)
step4_tween.parallel().tween_property(piece, "modulate", original_modulate, 0.3)
await step4_tween.finished
# Reset z-index
piece.z_index = original_z_index
func update_appearance() -> void:
if is_effect_active() && base_button:
var style = StyleBoxFlat.new()
var base_color = Utils.LIGHT_CELL if base_is_white else Utils.DARK_CELL
# Blend portal color with base tile color
style.bg_color = Color(
(base_color.r + portal_color.r) / 2,
(base_color.g + portal_color.g) / 2,
(base_color.b + portal_color.b) / 2
)
base_button.add_theme_stylebox_override("normal", style)
else:
restore_base_appearance()
# Static method to create a pair of portals
static func create_portal_pair(
button1: Button, is_white1: bool,
button2: Button, is_white2: bool,
duration: int, pair_id: int,
color1: Color, color2: Color,
) -> Array[PortalTile]:
var portal1 = PortalTile.new(button1, is_white1, duration, pair_id, color1)
var portal2 = PortalTile.new(button2, is_white2, duration, pair_id, color2)
# Link the portals together
portal1.other_portal = portal2
portal2.other_portal = portal1
return [portal1, portal2]