Files
sijcdusmode/scripts/door.gd
T
sijcdu 7c78a088a0 Add diamond collection and a level-clear door
The three diamonds and both doors were painted as tiles in TerrainBase, which cannot act as pickups or triggers. Replaced them with real prefabs that reuse the same atlas tiles (diamond = (7,3), doors = (10,6) and (11,5)) and erase their placeholder tile on ready. Collecting all three diamonds unlocks the door; touching it fades to black, pauses the tree and shows the level-clear screen with a next-level button (currently reloads the level, as there is only one). HUD now shows the diamond count.
2026-10-07 21:04:58 +08:00

55 lines
1.5 KiB
GDScript

extends Area2D
## 门:集齐钻石后碰到就过关;没集齐则闪一下红色提示。
@export var required_diamonds: int = 3
## 门贴图在图集里的区域(不同门可以不一样)
@export var atlas_region: Rect2 = Rect2(180, 108, 18, 18)
## 开场时擦掉脚下的占位地砖
@export var erase_placeholder_tile: bool = true
const ATLAS := preload("res://assets/game/tilesets/kenney_tiles_atlas.png")
var _sprite: Sprite2D = null
func _ready() -> void:
body_entered.connect(_on_body_entered)
_sprite = get_node_or_null("Sprite")
if _sprite != null:
var tex := AtlasTexture.new()
tex.atlas = ATLAS
tex.region = atlas_region
_sprite.texture = tex
if erase_placeholder_tile:
_erase_placeholder_tile()
func _on_body_entered(body: Node2D) -> void:
if not body.has_method("add_diamond"):
return
if int(body.diamonds) < required_diamonds:
_flash_locked()
return
var clear := get_tree().get_first_node_in_group("level_clear")
if clear != null:
clear.show_clear()
func _flash_locked() -> void:
if _sprite == null:
return
_sprite.modulate = Color(1.0, 0.45, 0.45)
await get_tree().create_timer(0.4).timeout
if is_instance_valid(_sprite):
_sprite.modulate = Color.WHITE
func _erase_placeholder_tile() -> void:
var root: Node = owner if owner != null else get_parent()
if root == null:
return
for layer in root.find_children("*", "TileMapLayer", true, false):
var cell: Vector2i = layer.local_to_map(layer.to_local(global_position))
if layer.get_cell_source_id(cell) != -1:
layer.erase_cell(cell)