Skip to content

Commit

Permalink
✨ duplicate entity instance functionality (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain authored Sep 25, 2023
1 parent ae9a9e4 commit ac22138
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
13 changes: 13 additions & 0 deletions addons/pandora/model/entity.gd
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ func init_entity(id:String, name:String, icon_path:String, category_id:String) -
self._category_id = category_id


## Similar to instantiate(), this method will duplicate an existing
## instance with all its properties.
## In case this is called on an entity, it will act as instantiate()
func duplicate_instance() -> PandoraEntity:
var new_instance = instantiate()
# ensure to carry over any overrides!
if is_instance():
for key in _instance_properties:
var duplicated_property = _instance_properties[key].duplicate_instance()
new_instance._instance_properties[key] = duplicated_property
return new_instance


## Creates an instance of this entity.
func instantiate() -> PandoraEntity:
var entity = ScriptUtil.create_entity_from_script(get_script_path(), "", "", "", "")
Expand Down
4 changes: 4 additions & 0 deletions addons/pandora/model/property_instance.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func _init(_property:PandoraProperty, value:Variant) -> void:
self._property = _property


func duplicate_instance() -> PandoraPropertyInstance:
return PandoraPropertyInstance.new(_property, _value)


func get_property_name() -> String:
if _property != null:
return _property.get_property_name()
Expand Down
25 changes: 25 additions & 0 deletions test/backend/entity_backend_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,28 @@ func test_icon_color_overridden() -> void:
entity.set_icon_color(Color.BLUE)
category.set_icon_color(Color.RED)
assert_that(entity.get_icon_color()).is_equal(Color.BLUE)


func test_duplicate_entity_instance_inherits_original() -> void:
var backend = create_object_backend()
var category = backend.create_category("Category")
var entity = backend.create_entity("Entity", category)
backend.create_property(category, "test", "string", "")
var instance = entity.instantiate()
instance.set_string("test", "original-instance")
var duplicate = instance.duplicate_instance()
assert_that(duplicate.get_string('test')).is_equal("original-instance")


func test_duplicate_entity_instance_is_independent_of_former() -> void:
var backend = create_object_backend()
var category = backend.create_category("Category")
var entity = backend.create_entity("Entity", category)
backend.create_property(category, "test", "string", "")
var instance = entity.instantiate()
instance.set_string("test", "original-instance")
var duplicate = instance.duplicate_instance()
duplicate.set_string("test", "duplicate-instance")
assert_that(instance.get_string('test')).is_equal("original-instance")
assert_that(duplicate.get_string('test')).is_equal("duplicate-instance")

31 changes: 31 additions & 0 deletions test/model/entity_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# GdUnit generated TestSuite
class_name EntityTest
extends GdUnitTestSuite
@warning_ignore("unused_parameter")
@warning_ignore("return_value_discarded")


# TestSuite generated from
const __source = "res://addons/pandora/model/entity.gd"


func test_init_entity() -> void:
var entity = PandoraEntity.new()
entity.init_entity("123", "Test Entity", "res://hello.jpg", "123")
assert_that(entity.get_entity_id()).is_equal("123")


func test_instantiate_entity() -> void:
var entity = PandoraEntity.new()
entity.init_entity("123", "Test Entity", "res://hello.jpg", "123")
var instance = entity.instantiate()
assert_that(instance.get_entity_id()).is_equal("123")
assert_bool(instance.is_instance()).is_true()


func test_duplicate_entity() -> void:
var entity = PandoraEntity.new()
entity.init_entity("123", "Test Entity", "res://hello.jpg", "123")
var instance = entity.duplicate_instance()
assert_that(instance.get_entity_id()).is_equal("123")
assert_bool(instance.is_instance()).is_true()

0 comments on commit ac22138

Please sign in to comment.