Skip to content

Commit

Permalink
remove utils.copy_entity and update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre-LA committed Jan 16, 2023
1 parent cc4f48c commit e1190f7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 55 deletions.
32 changes: 17 additions & 15 deletions examples/derived.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -60,14 +60,12 @@ local Person <nickname '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
Expand All @@ -80,10 +78,12 @@ local Warrior <nickname'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
Expand All @@ -98,10 +98,12 @@ local Wizard <nickname '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
Expand Down
7 changes: 3 additions & 4 deletions examples/derived_entity_tree.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions examples/entity_tree.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
33 changes: 0 additions & 33 deletions rotor/utils.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

0 comments on commit e1190f7

Please sign in to comment.