Last active
October 3, 2019 15:53
-
-
Save ArnCarveris/52c077d32dd1e84ad8bc1a2e89dfdc8d to your computer and use it in GitHub Desktop.
[flecs](https://github.com/SanderMertens/flecs) [lua](http://lua-users.org/wiki/MetatableEvents) API draft [LRDB](https://github.com/satoren/vscode-lrdb)
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
local ecs = require("flecs"):init() | |
local ID = ecs.ID | |
local SYSTEM = ecs.SYSTEM | |
ecs:import("flecs_components_input") | |
ecs:import("flecs_components_graphics") | |
ecs:import("flecs_components_geometry") | |
ecs:import("flecs_components_transform") | |
ecs:new_component{"setup"} | |
ecs:new_component{"editable"} | |
ecs:new_component{"editor_entity"} | |
ecs:new_component{"editor_system"} | |
ecs:new_component{"gameplay_entity"} | |
ecs:new_component{"gameplay_system"} | |
local pipeline = ecs:new_pipeline{"pipeline", | |
EcsOnLoad = { bind_thread = { 0}, stage = false}, | |
EcsPostLoad = { bind_thread = {-1}, stage = true }, | |
EcsPreUpdate = { bind_thread = {-1}, stage = true }, | |
EcsOnUpdate = { bind_thread = {-1}, stage = true }, | |
EcsOnValidate = { bind_thread = {-1}, stage = true }, | |
EcsPostUpdate = { bind_thread = {-1}, stage = true }, | |
EcsPreStore = { bind_thread = {-1}, stage = true }, | |
EcsOnStore = { bind_thread = { 0}, stage = false}, | |
} | |
local Cursor = ecs:new_entity{"Cursor", | |
ID.EcsPosition2D | |
, ID.EcsRectangle | |
, ID.EcsColor | |
, ID.EcsLineColor | |
, ID.editor_entity | |
} | |
local Square = ecs:new_entity{"Square", | |
ID.EcsPosition2D | |
, ID.EcsSquare | |
, ID.EcsColor | |
, ID.EcsLineColor | |
, ID.setup | |
, ID.editable | |
} | |
local on_query_test = ecs:new_system{ | |
ID.EcsPosition2D | |
, ID.EcsSquare & ID.EcsRectangle -- EcsSquare, EcsSquare | |
, ID.EcsColor | ID.EcsLineColor -- EcsColor | EcsLineColor | |
, ID.setup | nil -- ?setup | |
,~ID.editable -- !editable | |
, SYSTEM.gameplay_system | |
, ID.EcsOnLoad, on_query_test = function(self) | |
end | |
} | |
ecs:new_system{"do_move_square", | |
ID.EcsInput | |
, Square.EcsPosition2D | |
, SYSTEM.gameplay_system | |
, ID.EcsOnUpdate, function(self, input, position) | |
local speed = 1 | |
if input.mouse.left.state == true then | |
position.x = position.x + input.mouse.rel.x | |
position.y = position.y + input.mouse.rel.y | |
else | |
if input.keys[ECS_KEY_D].state or input.keys[ECS_KEY_RIGHT].state then | |
x_v = 1.0 | |
elseif input.keys[ECS_KEY_A].state or input.keys[ECS_KEY_LEFT].state then | |
x_v = -1.0 | |
end | |
if input.keys[ECS_KEY_S].state or input.keys[ECS_KEY_DOWN].state then | |
x_v = 1.0 | |
elseif input.keys[ECS_KEY_W].state or input.keys[ECS_KEY_UP].state then | |
x_v = -1.0 | |
end | |
position.x = position.x + x_v * speed | |
position.y = position.y + y_v * speed | |
end | |
end | |
} | |
ecs:new_system{ | |
ID.EcsInput | |
, Cursor.EcsPosition2D | |
, SYSTEM.editor_system | |
, ID.EcsOnUpdate, do_move_cursor = function(self, input, position) | |
position.x = input.mouse.wnd.x | |
position.y = input.mouse.wnd.y | |
end | |
} | |
ecs:run_pipeline(pipeline, 0.0, nil) | |
ecs:delete_pipeline(pipeline) | |
ecs = nil |
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
local flecs = { ID = {}} | |
local __column_operators = { | |
__band = function(self, rhs) | |
return { | |
__op = "and", | |
__lhs = self.__id, | |
__rhs = rhs.__id | |
} | |
end, | |
__bor = function(self, rhs) | |
if self ~= nil and rhs ~= nil then | |
return { | |
__op = "or", | |
__lhs = self.__id, | |
__rhs = rhs.__id | |
} | |
elseif self ~= nil then | |
return { | |
__op = "or", | |
__lhs = self.__id | |
} | |
elseif rhs ~= nil then | |
return { | |
__op = "or", | |
__rhs = rhs.__id | |
} | |
else | |
return nil | |
end | |
end, | |
__bnot = function(self) | |
return { | |
__op = "not", | |
__lhs = self.__id | |
} | |
end | |
} | |
local __column_source_modificators = { | |
__newindex = function(self, k,v) | |
return nil | |
end, | |
__index = function(self, k) | |
return { | |
__from = self.__id, | |
__id = k | |
} | |
end | |
} | |
local function __setup_id(self, settings) | |
if settings["__id"] == nil then | |
for k, v in pairs(settings) do | |
local t = type(v) | |
if t == "string" then | |
settings["__id"] = v | |
settings[k] = nil | |
break | |
elseif t == "function" then | |
settings["__id"] = k | |
settings[k] = nil | |
break | |
end | |
end | |
end | |
return settings | |
end | |
local function __setup_components(self, settings) | |
for k, v in pairs(settings) do | |
local t = type(v) | |
if k ~= "__id" and t == "table" then | |
settings[v.__id] = v | |
settings[k] = nil | |
end | |
end | |
return settings | |
end | |
local function __setup_column_source_modificator(self, id) | |
self[id] = { | |
__id = id | |
} | |
setmetatable(self[id], __column_source_modificators) | |
end | |
local function __print(self, method, settings) | |
print("flecs:",method,"{") | |
for k,v in pairs(settings) do print("\t", k, "=",v) end | |
print("}") | |
end | |
function flecs:init() | |
__setup_column_source_modificator(self, "SELF" ) | |
__setup_column_source_modificator(self, "OWNED" ) | |
__setup_column_source_modificator(self, "SHARED" ) | |
__setup_column_source_modificator(self, "EMPTY" ) | |
__setup_column_source_modificator(self, "CONTAINER") | |
__setup_column_source_modificator(self, "CASCADE" ) | |
__setup_column_source_modificator(self, "SYSTEM" ) | |
__setup_column_source_modificator(self, "SINGLETON") | |
__setup_column_source_modificator(self, "ENTITY" ) | |
return self | |
end | |
function flecs:import(module) | |
return require(module):import(self) | |
end | |
function flecs:new_component(settings) | |
__setup_id(self, settings) | |
__print(self, "new_component", settings) | |
setmetatable(settings, __column_operators) | |
self.ID[settings.__id] = settings | |
return settings | |
end | |
function flecs:new_system(settings) | |
__setup_id(self, settings) | |
__print(self, "new_system", settings) | |
return settings | |
end | |
function flecs:new_entity(settings) | |
__setup_id(self, settings) | |
__setup_components(self, settings) | |
__print(self, "new_entity", settings) | |
return settings | |
end | |
function flecs:new_pipeline(settings) | |
for k, v in pairs(settings) do | |
local t = type(v) | |
if t == "string" then | |
settings["__id"] = v | |
settings[k] = nil | |
elseif t == "table" then | |
v["__id"] = k | |
self.ID[k] = v | |
end | |
end | |
__print(self, "new_pipeline", settings) | |
return settings | |
end | |
function flecs:run_pipeline(pipeline, dt, ud) | |
end | |
function flecs:delete_pipeline(pipeline) | |
end | |
return flecs |
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
local module = { | |
EcsRectangle = { }, | |
EcsSquare = { } | |
} | |
function module:import(ecs) | |
self.EcsRectangle = ecs:new_component{"EcsRectangle", width = 0.0, height = 0.0 } | |
self.EcsSquare = ecs:new_component{"EcsSquare", size = 0.0 } | |
return self | |
end | |
return module |
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
local module = { | |
EcsColor = {}, | |
EcsLineColor = {} | |
} | |
function module:import(ecs) | |
self.EcsColor = ecs:new_component{"EcsColor", r = 0.0, g = 0.0, b=0.0, a=0.0 } | |
self.EcsLineColor = ecs:new_component{"EcsLineColor", self.EcsColor} | |
return self | |
end | |
return module |
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
local module = { | |
EcsInput = {} | |
} | |
function module:import(ecs) | |
self.EcsInput = ecs:new_component{"EcsInput"} | |
return self | |
end | |
return module |
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
local module = { | |
EcsPosition2D = { } | |
} | |
function module:import(ecs) | |
self.EcsPosition2D = ecs:new_component{"EcsPosition2D", x = 0.0, y = 0.0 } | |
return self | |
end | |
return module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment