110 lines
3.5 KiB
GDScript
110 lines
3.5 KiB
GDScript
@tool
|
|
class_name SceneryProp
|
|
extends Node2D
|
|
|
|
## Decoracion reutilizable con colision opcional e independiente de su profundidad.
|
|
## El origen esta abajo, en el centro: colocar el nodo sobre la superficie del suelo.
|
|
## Los presets de barril, farola, caja y fachadas reutilizan este mismo comportamiento.
|
|
|
|
@export_category("Appearance")
|
|
@export var texture: Texture2D:
|
|
set(value):
|
|
texture = value
|
|
_queue_refresh()
|
|
## Tamano visible en pixeles; no modifica la colision deliberadamente.
|
|
@export var display_size: Vector2 = Vector2(64.0, 64.0):
|
|
set(value):
|
|
display_size = Vector2(maxf(value.x, 1.0), maxf(value.y, 1.0))
|
|
_queue_refresh()
|
|
@export var tint: Color = Color.WHITE:
|
|
set(value):
|
|
tint = value
|
|
_queue_refresh()
|
|
## Solo cambia el orden de dibujo. Un objeto de fondo puede seguir siendo solido.
|
|
@export_enum("Background:-8", "Behind characters:-2", "At world depth:0", "In front of characters:10") var draw_layer: int = -2:
|
|
set(value):
|
|
draw_layer = value
|
|
_queue_refresh()
|
|
@export var flip_h: bool = false:
|
|
set(value):
|
|
flip_h = value
|
|
_queue_refresh()
|
|
|
|
@export_category("Collision")
|
|
## Activado: obstaculo solido en la capa World. Desactivado: se atraviesa.
|
|
@export var collision_enabled: bool = false:
|
|
set(value):
|
|
collision_enabled = value
|
|
_queue_refresh()
|
|
@export var collision_dimensions: Vector2 = Vector2(48.0, 56.0):
|
|
set(value):
|
|
collision_dimensions = Vector2(maxf(value.x, 1.0), maxf(value.y, 1.0))
|
|
_queue_refresh()
|
|
## Desplazamiento desde los pies; una Y negativa coloca la forma encima del suelo.
|
|
@export var collision_offset: Vector2 = Vector2(0.0, -28.0):
|
|
set(value):
|
|
collision_offset = value
|
|
_queue_refresh()
|
|
## Guia del editor; nunca se muestra durante la partida.
|
|
@export var preview_collision: bool = false:
|
|
set(value):
|
|
preview_collision = value
|
|
queue_redraw()
|
|
|
|
var _refresh_pending: bool = false
|
|
|
|
|
|
func _ready() -> void:
|
|
_refresh()
|
|
|
|
|
|
func _queue_refresh() -> void:
|
|
if not is_inside_tree() or _refresh_pending:
|
|
return
|
|
_refresh_pending = true
|
|
call_deferred("_refresh")
|
|
|
|
|
|
## Las formas se crean por instancia, nunca se modifica un recurso compartido.
|
|
func _refresh() -> void:
|
|
_refresh_pending = false
|
|
if not is_inside_tree() or not has_node("Visual"):
|
|
return
|
|
z_as_relative = false
|
|
z_index = draw_layer
|
|
var visual: Sprite2D = $Visual
|
|
visual.texture = texture
|
|
visual.position = Vector2(0.0, -display_size.y * 0.5)
|
|
visual.self_modulate = tint
|
|
visual.flip_h = flip_h
|
|
if texture != null:
|
|
visual.scale = display_size / texture.get_size()
|
|
|
|
var body: StaticBody2D = $Body
|
|
body.collision_layer = 1 if collision_enabled else 0
|
|
body.collision_mask = 0
|
|
body.position = collision_offset
|
|
var collision: CollisionShape2D = $Body/CollisionShape2D
|
|
var rectangle := RectangleShape2D.new()
|
|
rectangle.size = collision_dimensions
|
|
collision.shape = rectangle
|
|
collision.disabled = not collision_enabled
|
|
queue_redraw()
|
|
update_configuration_warnings()
|
|
|
|
|
|
func _draw() -> void:
|
|
if Engine.is_editor_hint() and preview_collision:
|
|
var bounds := Rect2(collision_offset - collision_dimensions * 0.5, collision_dimensions)
|
|
var color := Color(0.25, 0.85, 1.0, 0.85) if collision_enabled else Color(1.0, 0.6, 0.25, 0.65)
|
|
draw_rect(bounds, color, false, 2.0)
|
|
|
|
|
|
func _get_configuration_warnings() -> PackedStringArray:
|
|
var warnings := PackedStringArray()
|
|
if texture == null:
|
|
warnings.append("Asigna Texture o utiliza uno de los presets de world/props.")
|
|
if not scale.is_equal_approx(Vector2.ONE):
|
|
warnings.append("Usa Display Size y Collision Dimensions; deja Scale en (1, 1).")
|
|
return warnings
|