diff --git a/examples/derived.nelua b/examples/derived.nelua index 35c398d..32c9a83 100755 --- a/examples/derived.nelua +++ b/examples/derived.nelua @@ -27,7 +27,7 @@ local io = require 'io' local entity = require 'rotor.entity' local component = require 'rotor.component' local derived_entity = require 'rotor.derived_entity' -local utils = require 'rotor.utils' +local rotor_concepts = require 'rotor.concepts' local Class = @enum{ Person = 0, @@ -60,14 +60,12 @@ local Person = @entity(record{ person_controller: PersonController, }) -function Person.init(name: string, class: Class): Person - local person: Person = { - person_controller = { - =name, - =class - } +-- the an_entity_subset_ptr accepts any entity pointer that have at least the components of Person. +function Person.derived_init(a_person: #[rotor_concepts.value.an_entity_subset_ptr(Person.value)]#, name: string, class: Class) + a_person.person_controller = { + =name, + =class } - return person end -- warrior @@ -80,10 +78,12 @@ local Warrior = @derived_entity(Person, record{ }) function Warrior.init(name: string): Warrior - local person = Person.init(name, Class.Warrior) + local warrior: Warrior + Person.derived_init(&warrior, name, Class.Warrior) - local warrior = utils.copy_entity(&person, @Warrior) - warrior.warrior_controller.atk = 100 + warrior.warrior_controller = { + atk = 100 + } return warrior end @@ -98,10 +98,12 @@ local Wizard = @derived_entity(Person, record{ }) function Wizard.init(name: string): Wizard - local person = Person.init(name, Class.Wizard) - - local wizard = utils.copy_entity(&person, @Wizard) - wizard.wizard_controller.mp = 120 + local wizard: Wizard + Person.derived_init(&wizard, name, Class.Warrior) + + wizard.wizard_controller = { + mp = 120 + } return wizard end diff --git a/examples/derived_entity_tree.nelua b/examples/derived_entity_tree.nelua index a8e7dae..a753933 100755 --- a/examples/derived_entity_tree.nelua +++ b/examples/derived_entity_tree.nelua @@ -17,8 +17,7 @@ local storage = require 'rotor.storage' local component = require 'rotor.component' local entity = require 'rotor.entity' local system = require 'rotor.system' -local concepts = require 'rotor.concepts' -local utils = require 'rotor.utils' +local rotor_concepts = require 'rotor.concepts' local derived_entity = require 'rotor.derived_entity' -- nene @@ -82,7 +81,7 @@ local Sun.storage: storage(Sun, 1) local SolarSystem = @record{} -- this is a polymorphic function that accepts any pointer of an entity that have a position component -local function position_hierarchy(entity_ptr: #[concepts.value.an_entity_ptr_with({Position.value})]#) +local function position_hierarchy(entity_ptr: #[rotor_concepts.value.an_entity_ptr_with({Position.value})]#) ## local entity_type = entity_ptr.type.subtype -- for each child entity that have a position component @@ -102,7 +101,7 @@ local function position_hierarchy(entity_ptr: #[concepts.value.an_entity_ptr_wit ## end end -local function orbital_bodies_hierarchy(entity_ptr: concepts.an_entity_ptr) +local function orbital_bodies_hierarchy(entity_ptr: rotor_concepts.an_entity_ptr) ## local entity_type = entity_ptr.type.subtype ## local pos_field = entity_type:find_field_of_type(Position.value) diff --git a/examples/entity_tree.nelua b/examples/entity_tree.nelua index 0d2d4ff..6e80443 100755 --- a/examples/entity_tree.nelua +++ b/examples/entity_tree.nelua @@ -17,7 +17,7 @@ local storage = require 'rotor.storage' local component = require 'rotor.component' local entity = require 'rotor.entity' local system = require 'rotor.system' -local concepts = require 'rotor.concepts' +local rotor_concepts = require 'rotor.concepts' -- nene local Nene = require 'nene' @@ -84,7 +84,7 @@ local Sun.storage: storage(Sun, 1) local SolarSystem = @record{} -- this is a polymorphic function that accepts any pointer of an entity that have a position component -local function position_hierarchy(entity_ptr: #[concepts.value.an_entity_ptr_with({Position.value})]#) +local function position_hierarchy(entity_ptr: #[rotor_concepts.value.an_entity_ptr_with({Position.value})]#) ## local entity_type = entity_ptr.type.subtype -- for each child entity that have a position component @@ -104,7 +104,7 @@ local function position_hierarchy(entity_ptr: #[concepts.value.an_entity_ptr_wit ## end end -local function orbital_bodies_hierarchy(entity_ptr: concepts.an_entity_ptr) +local function orbital_bodies_hierarchy(entity_ptr: rotor_concepts.an_entity_ptr) ## local entity_type = entity_ptr.type.subtype ## local pos_field = entity_type:find_field_of_type(Position.value) diff --git a/rotor/utils.nelua b/rotor/utils.nelua index 7d1b9f1..46d5301 100755 --- a/rotor/utils.nelua +++ b/rotor/utils.nelua @@ -9,8 +9,6 @@ This module have some useful utility functions. -- The util module -local concepts = require 'rotor.concepts' - local utils = @record{} -- returns if the type it's accepted to be on an entity field. @@ -37,35 +35,4 @@ function utils.value.assert_record_for_entity(type) end ]] ---[[ -Returns a value of type `new_entity_type`, whose components are copied from -the `another_entity` argument (which must be an entity). - -Usage: -```lua --- code taken from the `derived` example -function Warrior.init(name: string): Warrior - local person = Person.init(name, Class.Warrior) - - local warrior = utils.copy_entity(&person, @Warrior) - warrior.warrior_controller.atk = 100 - - return warrior -end -``` -]] -function utils.copy_entity(another_entity: concepts.an_entity_ptr, new_entity_type: type): #[new_entity_type.value]# - local new_entity: new_entity_type - - ## for _, ne_field in ipairs(new_entity_type.value.fields) do - ## for _, ae_field in ipairs(another_entity.type.subtype.fields) do - ## if ne_field.type == ae_field.type then - new_entity.#|ne_field.name|# = another_entity.#|ae_field.name|# - ## end - ## end - ## end - - return new_entity -end - return utils