Files
DungeonCrawlerWorld/scripts/presentation/presentation_event.gd
T

89 lines
2.2 KiB
GDScript

class_name PresentationEvent
extends RefCounted
## A display-only, immutable translation of one authoritative GameEvent.
## Getter calls return a fresh source snapshot so presentation code cannot mutate
## the source domain event or this adapter's captured event data.
##
## GDScript's leading underscore is only a convention: declared fields remain
## writable. The backing properties below are virtual so _set can absorb every
## write after construction.
const BACKING_PROPERTIES := [&"_source_event", &"_kind", &"_notification", &"_accent"]
var _backing_values: Dictionary[StringName, Variant] = {}
var _is_initialized := false
var _source_event: GameEvent:
set(value):
_set(&"_source_event", value)
get:
return _captured_source_snapshot()
var _kind: StringName:
set(value):
_set(&"_kind", value)
get:
return _backing_values.get(&"_kind", &"") as StringName
var _notification: String:
set(value):
_set(&"_notification", value)
get:
return _backing_values.get(&"_notification", "") as String
var _accent: Color:
set(value):
_set(&"_accent", value)
get:
return _backing_values.get(&"_accent", Color.WHITE) as Color
var source_event: GameEvent:
get:
return _copy_event(_source_event)
var kind: StringName:
get:
return _kind
var notification: String:
get:
return _notification
var accent: Color:
get:
return _accent
func _init(source: GameEvent, presentation_kind: StringName, text: String = "", color: Color = Color.WHITE) -> void:
_source_event = _copy_event(source)
_kind = presentation_kind
_notification = text if not text.is_empty() else source.message
_accent = color
_is_initialized = true
func _set(property: StringName, value: Variant) -> bool:
if property not in BACKING_PROPERTIES:
return false
if not _is_initialized:
_backing_values[property] = value
return true
func _get(property: StringName) -> Variant:
if property in BACKING_PROPERTIES:
return _backing_values.get(property)
return null
func _captured_source_snapshot() -> GameEvent:
return _copy_event(_backing_values.get(&"_source_event") as GameEvent)
static func _copy_event(event: GameEvent) -> GameEvent:
return GameEvent.new(
event.turn,
event.floor,
event.kind,
event.payload,
event.message,
event.actor_id,
event.target_id,
event.cell,
)