ecs/test/test.lua

80 lines
1.6 KiB
Lua

local ecs = require "ecs"
-- local test = require "ecs_test"
local Position = require "ecs.Position"
local Velocity = require "ecs.Velocity"
local Meta = require "ecs.Meta"
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"))
random = {
speed = 10,
something = "Hello, World!",
alive = true
}
ent:add_component(ecs.Wrapper.new("Random", random))
-- @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("Wrapper"):for_each(function(ent)
wrapped = ent:get_component("Wrapper").table
print(wrapped.speed)
wrapped.speed = 11
print(wrapped.speed)
print(random.speed)
random.speed = 20
print(wrapped.speed)
print(random.speed)
print(wrapped.alive)
wrapped.alive = false
print(wrapped.alive)
print(wrapped.something)
end)