tile_map.gd 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. extends TileMap
  2. var secret_layer: int # You can have multiple layers if you make this an array.
  3. var player_in_secret: bool
  4. var layer_alpha := 1.0
  5. func _init() -> void:
  6. for i in get_layers_count(): # Find the secret layer by name.
  7. if get_layer_name(i) == "Secret":
  8. secret_layer = i
  9. func _ready() -> void:
  10. set_process(false)
  11. func _process(delta: float) -> void:
  12. if player_in_secret:
  13. if layer_alpha > 0.3:
  14. layer_alpha = move_toward(layer_alpha, 0.3, delta) # Animate the layer transparency.
  15. set_layer_modulate(secret_layer, Color(1, 1, 1, layer_alpha))
  16. else:
  17. set_process(false)
  18. else:
  19. if layer_alpha < 1.0:
  20. layer_alpha = move_toward(layer_alpha, 1.0, delta)
  21. set_layer_modulate(secret_layer, Color(1, 1, 1, layer_alpha))
  22. else:
  23. set_process(false)
  24. func _use_tile_data_runtime_update(layer: int, _coords: Vector2i) -> bool:
  25. if layer == secret_layer:
  26. return true
  27. return false
  28. func _tile_data_runtime_update(_layer: int, _coords: Vector2i, tile_data: TileData) -> void:
  29. tile_data.set_collision_polygons_count(0, 0) # Remove collision for secret layer.
  30. func _on_secret_detector_body_entered(body: Node2D) -> void:
  31. if not body is CharacterBody2D: # Detect player only.
  32. return
  33. player_in_secret = true
  34. set_process(true)
  35. func _on_secret_detector_body_exited(body: Node2D) -> void:
  36. if not body is CharacterBody2D:
  37. return
  38. player_in_secret = false
  39. set_process(true)