-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
337 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.