|
| 1 | +extends Control |
| 2 | +class_name CommonGemCell |
| 3 | +# VARS |
| 4 | +@onready var sprite:Sprite2D = $Sprite2D |
| 5 | +@onready var anim_player_fx:AnimationPlayer = $AnimPlayerFx |
| 6 | +@onready var anim_sprite_explode:AnimatedSprite2D = $AnimSpriteExplode |
| 7 | +@onready var anim_burst_1:AnimatedSprite2D = $AnimatedBurst1 |
| 8 | +@onready var anim_burst_3:AnimatedSprite2D = $AnimatedBurst3 |
| 9 | +@onready var debug_label_sel_num:Label = $DebugLabelSelNum |
| 10 | +@onready var debug_ui_panel:Panel = $DebugUIPanel |
| 11 | +@onready var audio_gem_explode:AudioStreamPlayer = $AudioGemExplode |
| 12 | +@onready var audio_gem_move:AudioStreamPlayer = $AudioGemMove |
| 13 | +@onready var label_points:Label = $LabelPoints |
| 14 | +# PROPS |
| 15 | +const SPRITE_SCALE:Vector2 = Vector2(0.5, 0.5) |
| 16 | +const DROP_OFFSET:int = 128 # (the sprite is centered in the 128x128 container, and uses a 64,64 position) |
| 17 | +var gem_color:Enums.GemColor |
| 18 | +# Declare and preload textures |
| 19 | +var gem_textures: Dictionary = { |
| 20 | + Enums.GemColor.WHITE: preload("res://assets/gems/space/gem1.png"), |
| 21 | + Enums.GemColor.RED: preload("res://assets/gems/space/gem2.png"), |
| 22 | + Enums.GemColor.YELLOW: preload("res://assets/gems/space/gem3.png"), |
| 23 | + Enums.GemColor.GREEN: preload("res://assets/gems/space/gem4.png"), |
| 24 | + Enums.GemColor.BROWN: preload("res://assets/gems/space/gem5.png"), |
| 25 | + Enums.GemColor.PURPLE: preload("res://assets/gems/space/gem6.png") |
| 26 | +} |
| 27 | +var gem_textures_food: Dictionary = { |
| 28 | + Enums.GemColor.WHITE: preload("res://assets/gems/characters_0001.png"), |
| 29 | + Enums.GemColor.RED: preload("res://assets/gems/characters_0002.png"), |
| 30 | + Enums.GemColor.YELLOW: preload("res://assets/gems/characters_0003.png"), |
| 31 | + Enums.GemColor.GREEN: preload("res://assets/gems/characters_0005.png"), |
| 32 | + Enums.GemColor.BROWN: preload("res://assets/gems/characters_0006.png"), |
| 33 | + Enums.GemColor.PURPLE: preload("res://assets/gems/characters_0007.png") |
| 34 | +} |
| 35 | + |
| 36 | +func initialize(colorIn: Enums.GemColor): |
| 37 | + # A: |
| 38 | + gem_color = colorIn |
| 39 | + # B: |
| 40 | + update_texture() |
| 41 | + # C: |
| 42 | + #panel_hover.visible = false |
| 43 | + |
| 44 | +func explode_gem(colorIn: Enums.GemColor, pointsIn:int): |
| 45 | + # A: set color immediately so code in `GameBoard.gd` canstart checking this cell's color |
| 46 | + gem_color = colorIn |
| 47 | + # B: |
| 48 | + play_selected_anim(false) |
| 49 | + play_anim_explode(pointsIn) |
| 50 | + |
| 51 | +func replace_gem(colorIn: Enums.GemColor, rows_to_drop: int = 1): |
| 52 | + #print("[replace_gem]: colorIn=", colorIn, " rows_to_drop=", rows_to_drop) |
| 53 | + |
| 54 | + # Calculate the beginning position based on how many rows the gem needs to drop |
| 55 | + var drop_height = DROP_OFFSET * rows_to_drop |
| 56 | + # DEBUG!!! vfvvvvvvvv |
| 57 | + debug_ui_panel.get_child(0).get_child(0).text = "drop-ROWS" |
| 58 | + debug_ui_panel.get_child(0).get_child(1).text = str(rows_to_drop) |
| 59 | + debug_ui_panel.get_child(0).get_child(2).text = "drop-H" |
| 60 | + debug_ui_panel.get_child(0).get_child(3).text = str(round(drop_height)) |
| 61 | + # DEBUG!!! ^^ |
| 62 | + |
| 63 | + var beg_pos = Vector2(sprite.position.x, sprite.position.y - drop_height) |
| 64 | + sprite.position = beg_pos |
| 65 | + sprite.visible = true |
| 66 | + |
| 67 | + # Initialize the gem with the new color and ensure it's visible |
| 68 | + initialize(colorIn) |
| 69 | + sprite.visible = true # Make sure the sprite is visible if it was hidden after explosion |
| 70 | + |
| 71 | + # Call the drop animation deferred to ensure it starts after other logic |
| 72 | + call_deferred("drop_in_gem") |
| 73 | + |
| 74 | +func drop_in_gem(): |
| 75 | + var tween = get_tree().create_tween() |
| 76 | + # DESIGN: final position should *always* be its default [relative] position |
| 77 | + tween.tween_property(sprite, "position", Enums.SRPITE_POS, Enums.TWEEN_TIME) |
| 78 | + |
| 79 | +func update_texture(): |
| 80 | + if gem_color in gem_textures: |
| 81 | + sprite.texture = gem_textures[gem_color] |
| 82 | + #print("[gem_cell] loaded sprite.texture: ", gem_color) |
| 83 | + else: |
| 84 | + print("ERROR: Texture for gem color not found") |
| 85 | + |
| 86 | +# ========================================================= |
| 87 | + |
| 88 | +func play_audio_gem_move(): |
| 89 | + audio_gem_move.play() |
| 90 | + |
| 91 | +func play_selected_anim(selected:bool): |
| 92 | + if selected: |
| 93 | + anim_player_fx.play("selected") |
| 94 | + else: |
| 95 | + anim_player_fx.stop() |
| 96 | + sprite.scale = SPRITE_SCALE |
| 97 | + label_points.visible = false |
| 98 | + |
| 99 | +# @desc: both AnimPlayer & AnimExplode are 1-sec |
| 100 | +func play_anim_explode(points:int): |
| 101 | + # A: sound effect |
| 102 | + audio_gem_explode.play() |
| 103 | + |
| 104 | + # B: explode effect (scale down to zero) |
| 105 | + # IMPORTANT: use play/stop or scale wont reset! |
| 106 | + anim_player_fx.play("explode") |
| 107 | + sprite.visible = false |
| 108 | + |
| 109 | + # C: show points |
| 110 | + label_points.text = "+"+str(points) |
| 111 | + if points > 0: |
| 112 | + anim_player_fx.play("new_points") |
| 113 | + |
| 114 | + # D: explode animation (exploding sprite) |
| 115 | + anim_sprite_explode.visible = true |
| 116 | + anim_sprite_explode.play("default") |
| 117 | + anim_burst_3.visible = true |
| 118 | + anim_burst_3.play("default") |
| 119 | + anim_burst_1.visible = true |
| 120 | + anim_burst_1.play("default") |
| 121 | + await get_tree().create_timer(Enums.EXPLODE_DELAY).timeout |
| 122 | + |
| 123 | + # LAST: reset anim-player effects (after await above) |
| 124 | + anim_sprite_explode.visible = false |
| 125 | + anim_burst_3.visible = false |
| 126 | + anim_burst_1.visible = false |
| 127 | + anim_player_fx.stop() |
| 128 | + label_points.visible = false # anim_player_fx stop/reset above unhides it |
| 129 | + |
| 130 | +# ========================================================= |
| 131 | + |
| 132 | +func debug_show_selnum(num:int): |
| 133 | + if not num or num == 0: |
| 134 | + debug_label_sel_num.visible = false |
| 135 | + debug_label_sel_num.text = "-" |
| 136 | + else: |
| 137 | + debug_label_sel_num.visible = true |
| 138 | + debug_label_sel_num.text = str(num) |
| 139 | + |
| 140 | +func debug_show_debug_panel(isShow:bool): |
| 141 | + debug_ui_panel.visible = isShow |
| 142 | + if not isShow: |
| 143 | + debug_ui_panel.get_child(0).get_child(0).text = "." |
| 144 | + debug_ui_panel.get_child(0).get_child(1).text = "." |
| 145 | + debug_ui_panel.get_child(0).get_child(2).text = "." |
| 146 | + debug_ui_panel.get_child(0).get_child(3).text = "." |
0 commit comments