Skip to content

Commit

Permalink
Entity State Container
Browse files Browse the repository at this point in the history
  • Loading branch information
tredfern committed Dec 13, 2021
1 parent f131faa commit 14dd93f
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 337 deletions.
58 changes: 29 additions & 29 deletions moonpie/entities/actions.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local Actions = {}
Actions.types = {
ADD = "ENTITIES_ADD",
REMOVE = "ENTITIES_REMOVE"
}

function Actions.add(entity)
return {
type = Actions.types.ADD,
payload = {
entity = entity
}
}
end

function Actions.remove(entity)
return {
type = Actions.types.REMOVE,
payload = {
entity = entity
}
}
end

-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local Actions = {}
Actions.types = {
ADD = "ENTITIES_ADD",
REMOVE = "ENTITIES_REMOVE"
}

function Actions.add(entity)
return {
type = Actions.types.ADD,
payload = {
entity = entity
}
}
end

function Actions.remove(entity)
return {
type = Actions.types.REMOVE,
payload = {
entity = entity
}
}
end

return Actions
46 changes: 23 additions & 23 deletions moonpie/entities/actions_spec.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.actions", function()
local Actions = require "moonpie.entities.actions"

it("can create an add entity action", function()
local entity = {}
local action = Actions.add(entity)

assert.equals("ENTITIES_ADD", action.type)
assert.equals(entity, action.payload.entity)
end)

it("can create a remove entity action", function()
local entity = {}
local action = Actions.remove(entity)

assert.equals("ENTITIES_REMOVE", action.type)
assert.equals(entity, action.payload.entity)
end)
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.actions", function()
local Actions = require "moonpie.entities.actions"

it("can create an add entity action", function()
local entity = {}
local action = Actions.add(entity)

assert.equals("ENTITIES_ADD", action.type)
assert.equals(entity, action.payload.entity)
end)

it("can create a remove entity action", function()
local entity = {}
local action = Actions.remove(entity)

assert.equals("ENTITIES_REMOVE", action.type)
assert.equals(entity, action.payload.entity)
end)
end)
34 changes: 17 additions & 17 deletions moonpie/entities/entity.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local Entity = {}

function Entity.new(...)
local e = {}

for _, v in ipairs{...} do
e[v.name] = v.value
end

return e
end

-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local Entity = {}

function Entity.new(...)
local e = {}

for _, v in ipairs{...} do
e[v.name] = v.value
end

return e
end

return Entity
48 changes: 24 additions & 24 deletions moonpie/entities/entity_spec.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.entity", function()
local Entity = require "moonpie.entities.entity"
local Property = require "moonpie.entities.property"

it("can return a new entity", function()
local e = Entity.new()
assert.is_table(e)
end)

it("can take some properties to define the entity", function()
local e = Entity.new(
Property("name", "Oskar"),
Property("position", { x = 10, y = 39 })
)

assert.equals("Oskar", e.name)
assert.equals(10, e.position.x)
assert.equals(39, e.position.y)
end)
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.entity", function()
local Entity = require "moonpie.entities.entity"
local Property = require "moonpie.entities.property"

it("can return a new entity", function()
local e = Entity.new()
assert.is_table(e)
end)

it("can take some properties to define the entity", function()
local e = Entity.new(
Property("name", "Oskar"),
Property("position", { x = 10, y = 39 })
)

assert.equals("Oskar", e.name)
assert.equals(10, e.position.x)
assert.equals(39, e.position.y)
end)
end)
20 changes: 10 additions & 10 deletions moonpie/entities/property.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

return function(name, value)
return {
name = name,
value = value
}
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

return function(name, value)
return {
name = name,
value = value
}
end
24 changes: 12 additions & 12 deletions moonpie/entities/property_spec.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.property", function()
local Property = require "moonpie.entities.property"

it("tracks the name for the property", function()
local p = Property("foo")
assert.equals("foo", p.name)
end)
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.property", function()
local Property = require "moonpie.entities.property"

it("tracks the name for the property", function()
local p = Property("foo")
assert.equals("foo", p.name)
end)
end)
34 changes: 17 additions & 17 deletions moonpie/entities/reducer.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local createSlice = require "moonpie.state.create_slice"
local Actions = require "moonpie.entities.actions"
local tables = require "moonpie.tables"

return createSlice({
[Actions.types.ADD] = function(state, action)
state[#state + 1] = action.payload.entity
return state
end,
[Actions.types.REMOVE] = function(state, action)
return tables.removeItem(state, action.payload.entity)
end,
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local createSlice = require "moonpie.state.create_slice"
local Actions = require "moonpie.entities.actions"
local tables = require "moonpie.tables"

return createSlice({
[Actions.types.ADD] = function(state, action)
state[#state + 1] = action.payload.entity
return state
end,
[Actions.types.REMOVE] = function(state, action)
return tables.removeItem(state, action.payload.entity)
end,
})
56 changes: 28 additions & 28 deletions moonpie/entities/reducer_spec.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.reducer", function()
local reducer = require "moonpie.entities.reducer"

it("can process adding entities", function()
local e = {}
local state = reducer({}, {
type = "ENTITIES_ADD",
payload = { entity = e }
})

assert.array_includes(e, state)
end)

it("can process removing entities", function()
local e = {}
local state = { e }
state = reducer(state, {
type = "ENTITIES_REMOVE",
payload = { entity = e }
})

assert.not_array_includes(e, state)
end)
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

describe("moonpie.entities.reducer", function()
local reducer = require "moonpie.entities.reducer"

it("can process adding entities", function()
local e = {}
local state = reducer({}, {
type = "ENTITIES_ADD",
payload = { entity = e }
})

assert.array_includes(e, state)
end)

it("can process removing entities", function()
local e = {}
local state = { e }
state = reducer(state, {
type = "ENTITIES_REMOVE",
payload = { entity = e }
})

assert.not_array_includes(e, state)
end)
end)
42 changes: 21 additions & 21 deletions moonpie/entities/selectors.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local tables = require "moonpie.tables"
local Selectors = {}

function Selectors.getAllWithComponents(state, ...)
local entities = state.entities

local components = tables.pack(...)

local comparison = function(entity)
return tables.all(components, function(c) return entity[c] end)
end

return tables.select(entities, comparison)
end


-- Copyright (c) 2021 Trevor Redfern
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT

local tables = require "moonpie.tables"
local Selectors = {}

function Selectors.getAllWithComponents(state, ...)
local entities = state.entities

local components = tables.pack(...)

local comparison = function(entity)
return tables.all(components, function(c) return entity[c] end)
end

return tables.select(entities, comparison)
end


return Selectors
Loading

0 comments on commit 14dd93f

Please sign in to comment.