ecs/test/test.lua

103 lines
2.1 KiB
Lua

require "ecs"
local Position = require "ecs.Position"
local Velocity = require "ecs.Velocity"
local Meta = require "ecs.Meta"
local LuaComponent = require 'ecs.LuaComponent'
function init()
print("Creating LuaComponents")
LuaData = LuaComponent.create("LuaData", {
speed = "number",
something = "string",
alive = "boolean"
})
TestThing = LuaComponent.create("TestThing", {
test = "string"
})
end
function run()
print("Running!")
manager = get_manager()
ent = manager:create_entity()
ent:add_component(Position.new(1.9, 9.7))
ent:add_component(Velocity.new(0.2, 0.3))
ent:add_component(Meta.new("Soldier"))
ent:add_component(LuaData.new({
speed = 10,
something = "Hello, World!",
alive = true
}))
ent:add_component(TestThing.new({
test = "This is a test!"
}))
-- @todo Make this happen...
-- stats = {
-- hp = 100,
-- magic = 30
-- }
--
-- ent:add_component(ecs.Wrapper.new("Stats", stats))
print(ent:has_components(Position, Velocity))
pos = ent:get_component(Position)
print("x: " .. pos.x)
print("y: " .. pos.y)
vel = ent:get_component(Velocity)
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
print("View test")
manager:view(Position, Velocity):for_each(function(ent)
pos = ent:get_component(Position)
vel = ent:get_component(Velocity)
pos.x = pos.x + vel.x
pos.y = pos.y + vel.y
print(ent)
print("x: " .. pos.x)
print("y: " .. pos.y)
print("v_x: " .. vel.x)
print("v_y: " .. vel.y)
if ent:has_components(Meta) then
meta = ent:get_component(Meta)
print("name: " .. meta.name)
end
end)
-- @todo Implement this
-- for uuid,entity in pairs(view) do
-- print "TEST"
-- end
manager:view(LuaData):for_each(function(ent)
d = ent:get_component(LuaData)
print("speed: " .. d.speed)
d.speed = 11
print("speed: " .. d.speed)
print("alive: ") print(d.alive)
d.alive = false
print("alive: ") print(d.alive)
print("Something: " .. d.something)
end)
manager:view(TestThing):for_each(function(ent)
t = ent:get_component(TestThing)
print("test: " .. t.test)
end)
print("Done running!")
end