64 lines
1.6 KiB
GDScript
64 lines
1.6 KiB
GDScript
class_name WorldEntityRef
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 1
|
|
|
|
var _entity_type: StringName
|
|
var _entity_id: StringName
|
|
|
|
|
|
func _init(entity_type: StringName = &"", entity_id: StringName = &"") -> void:
|
|
_entity_type = entity_type
|
|
_entity_id = entity_id
|
|
|
|
|
|
static func create(entity_type: StringName, entity_id: StringName) -> WorldEntityRef:
|
|
var entity_ref := WorldEntityRef.new(entity_type, entity_id)
|
|
return entity_ref if entity_ref.is_valid() else null
|
|
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> WorldEntityRef:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not record_data.has_all(["entity_type", "entity_id"]):
|
|
return null
|
|
return create(StringName(record_data["entity_type"]), StringName(record_data["entity_id"]))
|
|
|
|
|
|
func is_valid() -> bool:
|
|
return not _entity_type.is_empty() and not _entity_id.is_empty()
|
|
|
|
|
|
func get_entity_type() -> StringName:
|
|
return _entity_type
|
|
|
|
|
|
func get_entity_id() -> StringName:
|
|
return _entity_id
|
|
|
|
|
|
func index_key() -> String:
|
|
var type_text := String(_entity_type)
|
|
var id_text := String(_entity_id)
|
|
return "%d:%s%d:%s" % [type_text.length(), type_text, id_text.length(), id_text]
|
|
|
|
|
|
func equals(other: WorldEntityRef) -> bool:
|
|
return (
|
|
other != null
|
|
and _entity_type == other.get_entity_type()
|
|
and _entity_id == other.get_entity_id()
|
|
)
|
|
|
|
|
|
func duplicate_ref() -> WorldEntityRef:
|
|
return WorldEntityRef.new(_entity_type, _entity_id)
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return {
|
|
"schema_version": SCHEMA_VERSION,
|
|
"entity_type": String(_entity_type),
|
|
"entity_id": String(_entity_id),
|
|
}
|