2021-08-21 17:13:21 +02:00

82 lines
1.9 KiB
GDScript

extends Reference
class_name GDB_Entity
var _properties := [].duplicate()
var _property_ids := [].duplicate()
################
# public stuff #
################
func get_properties() -> Array:
return _properties
func get_property_ids() -> Array:
return _property_ids
func get_property(index : int) -> Object:
if index < 0 || index >= _properties.size():
return null
return _properties[index]
func get_property_id(index : int) -> String:
if index < 0 || index >= _property_ids.size():
return ""
return _property_ids[index]
func get_property_by_id(id : String) -> Object:
return get_property(find_property_by_id(id))
func add_property(id : String, property : Object, index := -1) -> void:
if !GDB_Property.is_valid_property(property):
printerr("GDB_Entity: attempted to add invalid property.")
return
if find_property(property) >= 0:
return
if find_property_by_id(id) >= 0:
printerr("GDB_Entity: duplicate property id")
return
if index < 0:
index = _properties.size()
index = min(index, _properties.size())
_properties.insert(index, property)
_property_ids.insert(index, id)
emit_signal("property_added", index)
func remove_property(property : Object) -> bool:
var index := find_property(property)
if index < 0:
return false
_properties.remove(index)
_property_ids.remove(index)
emit_signal("property_removed", index, property)
return true
func remove_property_at(index : int) -> bool:
if index < 0 || index >= _properties.size():
return false
return remove_property(_properties[index])
func find_property(property : Object) -> int:
return _properties.find(property)
func find_property_by_id(id : String) -> int:
return _property_ids.find(id)
################
# static stuff #
################
static func is_valid_entity(object : Object) -> bool:
return object != null \
&& object.has_method("get_properties")
###########
# signals #
###########
signal property_added(index)
signal property_removed(index, property)