Files
mortal-maze-the-game/world/terrain/terrain_piece.gd

151 lines
5.0 KiB
GDScript

@tool
class_name TerrainPiece
extends StaticBody2D
## Pieza de escenario: una sola medida controla dibujo y colision.
## Editar Dimensions en el Inspector, no la escala ni los nodos internos.
## Cada instancia crea su propia forma; duplicar una pieza no comparte su tamano.
@export_category("Geometry")
## Anchura y altura en pixeles. El origen de la pieza esta en su centro.
@export var dimensions: Vector2 = Vector2(256.0, 64.0):
set(value):
dimensions = Vector2(maxf(value.x, 1.0), maxf(value.y, 1.0))
_queue_refresh()
@export_category("Collision")
## Desactivar para usar esta superficie exclusivamente como decoracion.
@export var collision_enabled: bool = true:
set(value):
collision_enabled = value
_queue_refresh()
## Permite atravesar la plataforma desde abajo y aterrizar desde arriba.
@export var one_way: bool = false:
set(value):
one_way = value
_queue_refresh()
@export_range(1.0, 32.0, 0.5) var one_way_margin: float = 1.0:
set(value):
one_way_margin = value
_queue_refresh()
@export_category("Appearance")
## Se repite, no se estira. Sin textura se utiliza Fill Color.
@export var fill_texture: Texture2D:
set(value):
fill_texture = value
_queue_refresh()
## Borde superior opcional, por ejemplo la acera sobre el asfalto.
@export var top_texture: Texture2D:
set(value):
top_texture = value
_queue_refresh()
## Friso que cuelga bajo una plataforma. Es decorativo: la linea superior
## y la colision conservan exactamente las medidas de Geometry.
@export var fascia_texture: Texture2D:
set(value):
fascia_texture = value
_queue_refresh()
@export_range(0.0, 128.0, 1.0) var fascia_height: float = 0.0:
set(value):
fascia_height = maxf(value, 0.0)
_queue_refresh()
@export_range(1.0, 128.0, 1.0) var top_height: float = 24.0:
set(value):
top_height = value
_queue_refresh()
@export var fill_color: Color = Color(0.20, 0.18, 0.23):
set(value):
fill_color = value
_queue_refresh()
@export var outline_color: Color = Color(0.72, 0.63, 0.47, 0.0):
set(value):
outline_color = value
queue_redraw()
var _refresh_pending: bool = false
func _ready() -> void:
_refresh()
## Agrupa los cambios del Inspector y evita tocar fisica durante sus callbacks.
func _queue_refresh() -> void:
if not is_inside_tree() or _refresh_pending:
return
_refresh_pending = true
call_deferred("_refresh")
## Solo actualiza hijos existentes: no crea ni elimina nodos del editor.
func _refresh() -> void:
_refresh_pending = false
if not is_inside_tree() or not has_node("CollisionShape2D"):
return
var collision: CollisionShape2D = $CollisionShape2D
var rectangle := RectangleShape2D.new()
rectangle.size = dimensions
collision.shape = rectangle
collision.disabled = not collision_enabled
collision.one_way_collision = one_way
collision.one_way_collision_margin = one_way_margin
collision_layer = 1 if collision_enabled else 0
collision_mask = 0
var half: Vector2 = dimensions * 0.5
var fill: Polygon2D = $Fill
fill.polygon = PackedVector2Array([
Vector2(-half.x, -half.y), Vector2(half.x, -half.y),
Vector2(half.x, half.y), Vector2(-half.x, half.y),
])
fill.color = fill_color
fill.visible = fill_texture == null
var visual: Sprite2D = $Texture
visual.texture = fill_texture
visual.position = -half
visual.region_rect = Rect2(Vector2.ZERO, dimensions)
visual.visible = fill_texture != null
var top: Sprite2D = $Top
top.texture = top_texture
top.position = -half
top.region_rect = Rect2(Vector2.ZERO, Vector2(dimensions.x, minf(top_height, dimensions.y)))
top.visible = top_texture != null
var fascia: Sprite2D = get_node_or_null("Fascia") as Sprite2D
if fascia != null:
fascia.texture = fascia_texture
fascia.position = -half
fascia.region_rect = Rect2(Vector2.ZERO, Vector2(dimensions.x, fascia_height))
fascia.visible = fascia_texture != null and fascia_height > 0.0
# La luz del canto se pierde hacia el subsuelo. Se calcula una sola vez,
# en la misma geometria que la colision, sin shaders por baldosa.
var shade: Polygon2D = get_node_or_null("DepthShade") as Polygon2D
if shade != null:
var shaded_top: float = -half.y + minf(28.0, dimensions.y)
shade.polygon = PackedVector2Array([
Vector2(-half.x, shaded_top), Vector2(half.x, shaded_top),
Vector2(half.x, half.y), Vector2(-half.x, half.y),
])
var depth: float = minf(0.74, dimensions.y / 600.0)
shade.vertex_colors = PackedColorArray([
Color(0.06, 0.045, 0.085, 0.0), Color(0.06, 0.045, 0.085, 0.0),
Color(0.06, 0.045, 0.085, depth), Color(0.06, 0.045, 0.085, depth),
])
shade.visible = fill_texture != null and dimensions.y > 48.0
queue_redraw()
update_configuration_warnings()
func _draw() -> void:
if outline_color.a > 0.0:
draw_rect(Rect2(-dimensions * 0.5, dimensions), outline_color, false, 2.0)
func _get_configuration_warnings() -> PackedStringArray:
var warnings := PackedStringArray()
if not scale.is_equal_approx(Vector2.ONE):
warnings.append("Usa Dimensions para redimensionar. Deja Scale en (1, 1) para conservar una fisica predecible.")
return warnings