2015-08-27 18:55:07 +00:00
|
|
|
local GLOBALS = {}
|
|
|
|
for k, v in pairs(_G) do
|
|
|
|
GLOBALS[k] = v
|
|
|
|
end
|
|
|
|
|
2015-03-29 07:50:42 +00:00
|
|
|
local tiny = require "tiny"
|
2015-05-04 03:46:18 +00:00
|
|
|
|
|
|
|
local function deep_copy(x)
|
|
|
|
if type(x) == 'table' then
|
|
|
|
local nx = {}
|
|
|
|
for k, v in next, x, nil do
|
|
|
|
nx[deep_copy(k)] = deep_copy(v)
|
|
|
|
end
|
|
|
|
return nx
|
|
|
|
else
|
|
|
|
return x
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local entityTemplate1 = {
|
|
|
|
xform = {x = 0, y = 0},
|
|
|
|
vel = {x = 1, y = 2},
|
|
|
|
name = "E1",
|
|
|
|
size = 11,
|
|
|
|
description = "It goes to 11.",
|
|
|
|
spinalTap = true
|
|
|
|
}
|
|
|
|
|
|
|
|
local entityTemplate2 = {
|
|
|
|
xform = {x = 2, y = 2},
|
|
|
|
vel = {x = -1, y = 0},
|
|
|
|
name = "E2",
|
|
|
|
size = 10,
|
2015-03-29 09:07:39 +00:00
|
|
|
description = "It does not go to 11.",
|
|
|
|
onlyTen = true
|
2015-03-29 03:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local entityTemplate3 = {
|
|
|
|
xform = {x = 4, y = 5},
|
|
|
|
vel = {x = 0, y = 3},
|
|
|
|
name = "E3",
|
|
|
|
size = 8,
|
2015-03-29 09:07:39 +00:00
|
|
|
description = "The smallest entity.",
|
|
|
|
littleMan = true
|
2015-03-29 03:19:22 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 07:50:42 +00:00
|
|
|
describe('tiny-ecs:', function()
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-29 09:07:39 +00:00
|
|
|
describe('Filters:', function()
|
2015-03-29 03:19:22 +00:00
|
|
|
|
|
|
|
local entity1, entity2, entity3
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
entity1 = deep_copy(entityTemplate1)
|
|
|
|
entity2 = deep_copy(entityTemplate2)
|
|
|
|
entity3 = deep_copy(entityTemplate3)
|
|
|
|
end)
|
|
|
|
|
2015-03-29 09:07:39 +00:00
|
|
|
it("Default Filters", function()
|
|
|
|
|
|
|
|
local ftap = tiny.requireAll("spinalTap")
|
|
|
|
local fvel = tiny.requireAll("vel")
|
|
|
|
local fxform = tiny.requireAll("xform")
|
2015-05-07 09:32:32 +00:00
|
|
|
local fall = tiny.requireAny("spinalTap", "onlyTen", "littleMan")
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-05-05 14:03:04 +00:00
|
|
|
-- Only select Entities without "spinalTap"
|
2015-05-07 09:32:32 +00:00
|
|
|
local frtap = tiny.rejectAny("spinalTap")
|
2015-05-05 14:03:04 +00:00
|
|
|
|
|
|
|
-- Select Entities without all three: "spinalTap", "onlyTen", and
|
|
|
|
-- "littleMan"
|
|
|
|
local frall = tiny.rejectAll("spinalTap", "onlyTen", "littleMan")
|
|
|
|
|
2015-04-21 13:47:36 +00:00
|
|
|
assert.truthy(fall(nil, entity1))
|
|
|
|
assert.truthy(ftap(nil, entity1))
|
|
|
|
assert.falsy(ftap(nil, entity2))
|
|
|
|
assert.truthy(fxform(nil, entity1))
|
|
|
|
assert.truthy(fxform(nil, entity2))
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-04-21 13:47:36 +00:00
|
|
|
assert.truthy(fall(nil, entity1))
|
|
|
|
assert.truthy(fall(nil, entity2))
|
|
|
|
assert.truthy(fall(nil, entity3))
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-05-05 14:03:04 +00:00
|
|
|
assert.falsy(frtap(nil, entity1))
|
|
|
|
assert.truthy(frtap(nil, entity2))
|
|
|
|
assert.truthy(frtap(nil, entity3))
|
|
|
|
|
|
|
|
assert.truthy(frall(nil, entity1))
|
|
|
|
assert.truthy(frall(nil, entity2))
|
|
|
|
assert.truthy(frall(nil, entity3))
|
|
|
|
|
2015-03-29 03:19:22 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('World:', function()
|
|
|
|
|
|
|
|
local world, entity1, entity2, entity3
|
|
|
|
|
2015-06-19 22:48:18 +00:00
|
|
|
local moveSystem = tiny.processingSystem()
|
2015-04-21 13:47:36 +00:00
|
|
|
moveSystem.filter = tiny.requireAll("xform", "vel")
|
2015-04-22 09:22:14 +00:00
|
|
|
function moveSystem:process(e, dt)
|
|
|
|
local xform = e.xform
|
|
|
|
local vel = e.vel
|
|
|
|
local x, y = xform.x, xform.y
|
|
|
|
local xvel, yvel = vel.x, vel.y
|
|
|
|
xform.x, xform.y = x + xvel * dt, y + yvel * dt
|
2015-04-21 13:47:36 +00:00
|
|
|
end
|
2015-03-29 03:19:22 +00:00
|
|
|
|
|
|
|
local timePassed = 0
|
2015-04-21 13:47:36 +00:00
|
|
|
local oneTimeSystem = tiny.system()
|
2015-05-04 01:09:34 +00:00
|
|
|
function oneTimeSystem:update(dt)
|
2015-04-21 13:47:36 +00:00
|
|
|
timePassed = timePassed + dt
|
2015-04-27 13:48:27 +00:00
|
|
|
end
|
2015-03-29 03:19:22 +00:00
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
entity1 = deep_copy(entityTemplate1)
|
|
|
|
entity2 = deep_copy(entityTemplate2)
|
|
|
|
entity3 = deep_copy(entityTemplate3)
|
2015-03-29 12:19:31 +00:00
|
|
|
world = tiny.world(moveSystem, oneTimeSystem, entity1, entity2, entity3)
|
2015-03-29 03:19:22 +00:00
|
|
|
timePassed = 0
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("Create World", function()
|
2015-04-25 09:59:41 +00:00
|
|
|
assert.equals(world:getEntityCount(), 3)
|
|
|
|
assert.equals(world:getSystemCount(), 2)
|
2015-03-29 03:19:22 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("Run simple simulation", function()
|
|
|
|
world:update(1)
|
|
|
|
assert.equals(timePassed, 1)
|
|
|
|
assert.equals(entity1.xform.x, 1)
|
|
|
|
assert.equals(entity1.xform.y, 2)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("Remove Entities", function()
|
|
|
|
world:remove(entity1, entity2)
|
|
|
|
world:update(1)
|
|
|
|
assert.equals(timePassed, 1)
|
|
|
|
assert.equals(entity1.xform.x, entityTemplate1.xform.x)
|
|
|
|
assert.equals(entity2.xform.x, entityTemplate2.xform.x)
|
|
|
|
assert.equals(entity3.xform.y, 8)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("Remove Systems", function()
|
2015-04-25 09:59:41 +00:00
|
|
|
world:remove(moveSystem, oneTimeSystem)
|
2015-03-29 03:19:22 +00:00
|
|
|
world:update(1)
|
|
|
|
assert.equals(timePassed, 0)
|
|
|
|
assert.equals(entity1.xform.x, entityTemplate1.xform.x)
|
2015-04-25 09:59:41 +00:00
|
|
|
assert.equals(0, world:getSystemCount())
|
2015-03-29 03:19:22 +00:00
|
|
|
end)
|
2015-03-30 07:22:23 +00:00
|
|
|
|
2015-05-04 03:46:18 +00:00
|
|
|
it("Deactivate and Activate Systems", function()
|
|
|
|
moveSystem.active = false
|
|
|
|
oneTimeSystem.active = false
|
2015-03-30 07:22:23 +00:00
|
|
|
world:update(1)
|
2015-05-03 15:28:18 +00:00
|
|
|
assert.equals(world:getSystemCount(), 2)
|
2015-03-30 07:22:23 +00:00
|
|
|
assert.equals(timePassed, 0)
|
|
|
|
assert.equals(entity1.xform.x, entityTemplate1.xform.x)
|
2015-05-04 03:46:18 +00:00
|
|
|
moveSystem.active = true
|
|
|
|
oneTimeSystem.active = true
|
2015-03-30 07:22:23 +00:00
|
|
|
world:update(1)
|
|
|
|
assert.equals(timePassed, 1)
|
|
|
|
assert.are_not.equal(entity1.xform.x, entityTemplate1.xform.x)
|
2015-05-03 15:28:18 +00:00
|
|
|
assert.equals(world:getSystemCount(), 2)
|
2015-03-30 07:22:23 +00:00
|
|
|
end)
|
2015-03-31 09:57:55 +00:00
|
|
|
|
|
|
|
it("Clear Entities", function()
|
|
|
|
world:clearEntities()
|
|
|
|
world:update(1)
|
2015-03-31 11:55:12 +00:00
|
|
|
assert.equals(0, world:getEntityCount())
|
2015-03-31 09:57:55 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("Clear Systems", function()
|
|
|
|
world:clearSystems()
|
|
|
|
world:update(1)
|
2015-03-31 11:55:12 +00:00
|
|
|
assert.equals(0, world:getSystemCount())
|
2015-03-31 09:57:55 +00:00
|
|
|
end)
|
|
|
|
|
2015-04-25 09:59:41 +00:00
|
|
|
it("Add Entities Multiple Times", function()
|
|
|
|
world:update(1)
|
|
|
|
world:add(entity1, entity2, entity3)
|
|
|
|
world:update(2)
|
|
|
|
assert.equals(2, world:getSystemCount())
|
|
|
|
assert.equals(3, world:getEntityCount())
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("Remove Entities Multiple Times", function()
|
|
|
|
world:update(1)
|
|
|
|
world:remove(entity1, entity2, entity3)
|
|
|
|
world:update(2)
|
|
|
|
world:remove(entity1, entity2, entity3)
|
|
|
|
world:update(2)
|
|
|
|
assert.equals(2, world:getSystemCount())
|
|
|
|
assert.equals(0, world:getEntityCount())
|
|
|
|
end)
|
2015-03-31 09:57:55 +00:00
|
|
|
|
2015-04-25 10:14:41 +00:00
|
|
|
it("Add Systems Multiple Times", function()
|
|
|
|
world:update(1)
|
|
|
|
world:add(moveSystem, oneTimeSystem)
|
|
|
|
world:update(2)
|
|
|
|
assert.equals(2, world:getSystemCount())
|
|
|
|
assert.equals(3, world:getEntityCount())
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("Remove Systems Multiple Times", function()
|
|
|
|
world:update(1)
|
|
|
|
world:remove(moveSystem)
|
|
|
|
world:update(2)
|
|
|
|
world:remove(moveSystem)
|
|
|
|
world:update(2)
|
|
|
|
assert.equals(1, world:getSystemCount())
|
|
|
|
assert.equals(3, world:getEntityCount())
|
|
|
|
end)
|
|
|
|
|
2015-04-25 13:37:04 +00:00
|
|
|
it("Reorder Systems", function()
|
|
|
|
world:update(1)
|
|
|
|
world:setSystemIndex(moveSystem, 2)
|
|
|
|
world:update(1)
|
2015-07-07 03:28:50 +00:00
|
|
|
assert.equals(2, moveSystem.index)
|
|
|
|
assert.equals(1, oneTimeSystem.index)
|
2015-04-25 13:37:04 +00:00
|
|
|
end)
|
|
|
|
|
2015-06-18 20:02:16 +00:00
|
|
|
it("Sorts Entities in Sorting Systems", function()
|
2015-06-19 22:48:18 +00:00
|
|
|
local sortsys = tiny.sortedProcessingSystem()
|
2015-06-18 20:02:16 +00:00
|
|
|
sortsys.filter = tiny.requireAll("vel")
|
|
|
|
function sortsys:compare(e1, e2)
|
|
|
|
return e1.vel.x < e2.vel.x
|
|
|
|
end
|
|
|
|
world:add(sortsys)
|
|
|
|
world:update(0)
|
|
|
|
assert.equals(sortsys.entities[1], entity2)
|
|
|
|
assert.equals(sortsys.entities[2], entity3)
|
|
|
|
assert.equals(sortsys.entities[3], entity1)
|
|
|
|
end)
|
2015-06-19 22:48:18 +00:00
|
|
|
|
2015-03-29 03:19:22 +00:00
|
|
|
end)
|
|
|
|
|
2015-08-27 18:55:07 +00:00
|
|
|
it("Doesn't pollute the global namespace", function()
|
|
|
|
assert.are.same(_G, GLOBALS)
|
|
|
|
end)
|
|
|
|
|
2015-03-29 03:19:22 +00:00
|
|
|
end)
|