CONCORD IS DEAD

Long live Concord!!
This commit is contained in:
Pablo Ariel Mayobre 2020-03-14 07:45:25 -03:00
parent 5dffe04b72
commit c640641b09
No known key found for this signature in database
GPG key ID: 5ACD9E6858BEB0A9
16 changed files with 263 additions and 424 deletions

View file

@ -5,61 +5,59 @@ local Component = Concord.component
local System = Concord.system
local Assemblage = Concord.assemblage
local Game = Concord.context()
local Legs = Component(function(e, legCount)
local Legs = Component("Legs", function(e, legCount)
e.legCount = legCount or 0
end)
local Instinct = Component(function(e) -- luacheck: ignore
local Instinct = Component("Instinct", function(e) -- luacheck: ignore
end)
local Cool = Component(function(e, coolness)
local Cool = Component("Cool", function(e, coolness)
e.coolness = coolness
end)
local Wings = Component(function(e)
local Wings = Component("Wings", function(e)
e.wingCount = 2
end)
local Animal = Assemblage(function(e, legCount)
local Animal = function(e, legCount)
e
:give(Legs, legCount)
:give(Instinct)
:give("Legs", legCount)
:give("Instinct")
print("Animal")
end)
end
local Lion = Assemblage(function(e, coolness)
local Lion = function(e, coolness)
e
:assemble(Animal, 4)
:give(Cool, coolness)
print("Lion")
end)
end
local Eagle = Assemblage(function(e)
local Eagle = function(e)
e
:assemble(Animal, 2)
:give(Wings)
print("Eagle")
end)
end
local Griffin = Assemblage(function(e, coolness)
local Griffin = function(e, coolness)
e
:assemble(Animal, 4)
:assemble(Lion, coolness * 2)
:assemble(Eagle)
end)
end
local myAnimal = Entity()
:assemble(Griffin, 5)
--:apply()
print(myAnimal:has(Legs))
print(myAnimal:has(Instinct))
print(myAnimal:has(Cool))
print(myAnimal:has(Wings))
print(myAnimal:has("Legs"))
print(myAnimal:has("Instinct"))
print(myAnimal:has("Cool"))
print(myAnimal:has("Wings"))

View file

@ -11,11 +11,10 @@ local function display(t)
end
end
local test_component_1 = Concord.component(function(e, x, y)
local test_component_1 = Concord.component("test_component_1", function(e, x, y)
e.x = x or 0
e.y = y or 0
end)
Concord.components.register("test_component_1", test_component_1)
function test_component_1:serialize()
return {
@ -29,10 +28,9 @@ function test_component_1:deserialize(data)
self.y = data.y or 0
end
local test_component_2 = Concord.component(function(e, foo)
local test_component_2 = Concord.component("test_component_2", function(e, foo)
e.foo = foo
end)
Concord.components.register("test_component_2", test_component_2)
function test_component_2:serialize()
return {
@ -50,8 +48,8 @@ local world_2 = Concord.world()
-- Test Entity
Concord.entity(world_1)
:give(test_component_1, 100, 50)
:give(test_component_2, "Hello World!")
:give("test_component_1", 100, 50)
:give("test_component_2", "Hello World!")
-- Serialize world
local data = world_1:serialize()
@ -62,8 +60,8 @@ world_2:deserialize(data)
-- Check result
local test_entity_copy = world_2:getEntities()[1]
local test_comp_1 = test_entity_copy[test_component_1]
local test_comp_2 = test_entity_copy[test_component_2]
local test_comp_1 = test_entity_copy["test_component_1"]
local test_comp_2 = test_entity_copy["test_component_2"]
print(test_comp_1.x, test_comp_1.y)
print(test_comp_2.foo)

View file

@ -6,33 +6,33 @@ local System = Concord.system
local Game = Concord.world()
local Position = Component(function(e, x, y)
local Position = Component('Position', function(e, x, y)
e.x = x
e.y = y
end)
local Rectangle = Component(function(e, w, h)
local Rectangle = Component('Rectangle', function(e, w, h)
e.w = w
e.h = h
end)
local Circle = Component(function(e, r)
local Circle = Component('Circle', function(e, r)
e.r = r
end)
local Color = Component(function(e, r, g, b, a)
local Color = Component('Color', function(e, r, g, b, a)
e.r = r
e.g = g
e.b = b
e.a = a
end)
local RectangleRenderer = System({Position, Rectangle})
local RectangleRenderer = System{pool = {'Position', 'Rectangle'}}
function RectangleRenderer:draw()
for _, e in ipairs(self.pool) do
local position = e:get(Position)
local rectangle = e:get(Rectangle)
local color = e:get(Color)
local position = e:get('Position')
local rectangle = e:get('Rectangle')
local color = e:get('Color')
love.graphics.setColor(255, 255, 255)
if color then
@ -43,7 +43,7 @@ function RectangleRenderer:draw()
end
end
local CircleRenderer = System({Position, Circle})
local CircleRenderer = System{pool = {'Position', 'Circle'}}
function CircleRenderer:flush()
for _, e in ipairs(self.pool.removed) do
print(tostring(e).. " was removed from my pool D:")
@ -52,9 +52,9 @@ end
function CircleRenderer:draw()
for _, e in ipairs(self.pool) do
local position = e:get(Position)
local circle = e:get(Circle)
local color = e:get(Color)
local position = e:get('Position')
local circle = e:get('Circle')
local color = e:get('Color')
love.graphics.setColor(255, 255, 255)
if color then
@ -65,7 +65,7 @@ function CircleRenderer:draw()
end
end
local RandomRemover = System({})
local RandomRemover = System{pool = {}}
function RandomRemover:init()
self.time = 0
@ -93,11 +93,11 @@ Game:addSystem(CircleRenderer(), "draw")
for _ = 1, 100 do
local e = Entity()
e:give(Position, love.math.random(0, 700), love.math.random(0, 700))
e:give(Rectangle, love.math.random(5, 20), love.math.random(5, 20))
e:give('Position', love.math.random(0, 700), love.math.random(0, 700))
e:give('Rectangle', love.math.random(5, 20), love.math.random(5, 20))
if love.math.random(0, 1) == 0 then
e:give(Color, love.math.random(), love.math.random(), love.math.random(), 1)
e:give('Color', love.math.random(), love.math.random(), love.math.random(), 1)
end
Game:addEntity(e)
@ -105,11 +105,11 @@ end
for _ = 1, 100 do
local e = Entity()
e:give(Position, love.math.random(0, 700), love.math.random(0, 700))
e:give(Circle, love.math.random(5, 20))
e:give('Position', love.math.random(0, 700), love.math.random(0, 700))
e:give('Circle', love.math.random(5, 20))
if love.math.random(0, 1) == 0 then
e:give(Color, love.math.random(), love.math.random(), love.math.random(), 1)
e:give('Color', love.math.random(), love.math.random(), love.math.random(), 1)
end
Game:addEntity(e)