Skip to content

Commit

Permalink
boredom config corrupted somehow
Browse files Browse the repository at this point in the history
  • Loading branch information
xharris committed Sep 25, 2020
1 parent 21fdab2 commit 7c4cd3b
Show file tree
Hide file tree
Showing 27 changed files with 1,356 additions and 61 deletions.
3 changes: 3 additions & 0 deletions blankejs.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"editor.formatOnSave": true,
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"[javascript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
}
}
}
4 changes: 3 additions & 1 deletion love2d/docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Game{
}
```

`State(name, callbacks)` callbacks: enter, update(dt), draw, leave
`State(name, callbacks)`

callbacks: enter, update(dt), draw, leave

# Class Props

Expand Down
176 changes: 176 additions & 0 deletions love2d/lua/blanke/ecs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
local new_entities = {}
local dead_entities = {}

local entities = {}
local systems = {}

local entity_templates = {}
local spawn

Entity = callable {
__call = function(_, classname, props)
if props then
-- adding entity template
entity_templates[classname] = props

return function(args)
local t = copy(props)
table.update(t, args)
return World.add(t)
end

elseif type(classname) == "string" then
-- spawn from entity template
local t = copy(entity_templates[classname])
if t then
return World.add(t)
end

elseif type(classname) == "table" then
return World.add(props)
end
end,
spawn = function(classname, props)
local t = copy(props)
if props then
table.update(t, props)
end
return World.add(t)
end
}

System = callable {
__call = function(_, query, opt)
local cb = copy(opt)
cb.order = nil

table.insert(systems, {
query=query,
order=opt.order,
cb=cb,
entities={},
changed={},
removed={}
})

System.sort()
end,
order = {},
sort = function()
table.sort(systems, function(a, b)
if (type(a.order) ~= "number") then
a.order = a.order ~= nil and (System.order[a.order] or a.order) or (System.order._ or 0)
end
if (type(b.order) ~= "number") then
b.order = b.order ~= nil and (System.order[b.order] or b.order) or (System.order._ or 0)
end
return a.order > b.order
end)
end
}

All = function(...) return { type="all", args={...} } end
Some = function(...) return { type="some", args={...} } end
Not = function(...) return { type="not", args={...} } end

Test = function(query, obj, _not)
if type(query) == "string" then
return _not and obj[query] == nil or obj[query] ~= nil
end
if type(query) == "table" and query.args then
local qtype = query.type
if qtype == "all" then
return table.every(query.args, function(q) return Test(q, obj, _not) end)
elseif qtype == "some" then
return table.some(query.args, function(q) return Test(q, obj, _not) end)
elseif qtype == "not" then
return table.some(query.args, function(q) return Test(q, obj, not _not) end)
end
end
end

function Add(ent, k, v)
ent[k] = (v == nil and true or v)
for i = 1, table.len(systems) do
systems[i].changed[ent.uuid] = true
end
end

function Remove(ent, k)
for i = 1, table.len(systems) do
systems[i].changed[ent.uuid] = true
end
ent[k] = nil
end

function Destroy(ent)
local sys
ent.destroyed = true
for i = 1, table.len(systems) do
sys = systems[i]
sys.removed[ent.uuid] = true
if sys.cb.removed then sys.cb.removed(ent) end
end
end

World = {
add = function(obj) table.insert(new_entities, obj) end,
remove = function(obj) table.insert(dead_entities, obj) end,
update = function(dt)
local ent, sys
-- add new entities
for n = 1, table.len(new_entities) do
ent = new_entities[n]
if not ent.uuid then
ent.uuid = uuid()
entities[ent.uuid] = ent
end

for s = 1, table.len(systems) do
sys = systems[s]
if Test(sys.query, ent) then
-- entity fits in this system
table.insert(sys.entities, ent.uuid)
if sys.cb.added then sys.cb.added(ent) end
end
end
end
-- update systems
for s = 1, table.len(systems) do
sys = systems[s]
local update, removed = sys.cb.update, sys.cb.removed
if update then
table.filter(sys.entities, function(eid)
-- entity was removed from world
if sys.removed[eid] then
sys.removed[eid] = nil
return false

-- entity property was changed
elseif sys.changed[eid] then
sys.changed[eid] = nil
if Test(sys.query, entities[eid]) then
-- entity can stay
return true
else
-- entity removed from system
if removed then removed(entities[eid]) end
return false
end
else
update(entities[eid], dt)
end
return true
end)
-- check for changed entities that were not previously in this system
for eid,_ in pairs(sys.changed) do
if Test(sys.query, entities[eid]) and not table.includes(sys.entities, eid) then
table.insert(sys.entities, eid)
end
end
sys.changed = {}
end
end
new_entities = {}
end
}
38 changes: 23 additions & 15 deletions love2d/lua/blanke/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ callable = function(t)
return setmetatable(t, { __call = t.__call })
end

-- blanke_require("ecs")

memoize = nil
do
local mem_cache = {}
Expand Down Expand Up @@ -115,12 +117,12 @@ table.keys = function (t)
for k, v in pairs(t) do table.insert(ret,k) end
return ret
end
table.every = function (t)
for k,v in pairs(t) do if not v then return false end end
table.every = function (t, fn)
for k,v in pairs(t) do if fn ~= nil and not fn(v, k) or not v then return false end end
return true
end
table.some = function (t)
for k,v in pairs(t) do if v then return true end end
table.some = function (t, fn)
for k,v in pairs(t) do if fn ~= nil and fn(v, k) or v then return true end end
return false
end
table.len = function (t)
Expand Down Expand Up @@ -888,7 +890,6 @@ do
love.graphics.setBlendMode(last_blend)
end
end

Game = callable {
options = {
res = 'assets',
Expand Down Expand Up @@ -1275,6 +1276,7 @@ do
Physics.update(dt)
Timer.update(dt, dt_ms)
if Game.options.update(dt) == true then return end
-- World.update(dt) -- ecs
Blanke.iterUpdate(Game.updatables, dt)
State.update(dt)
State._check()
Expand Down Expand Up @@ -1605,7 +1607,10 @@ do
-- animation?
local anim_info = nil
if type(args) == 'string' then
args = animations[args] or { file=args }
if animations[args] then
anim_info = animations[args]
end
args = anim_info or { file=args }
elseif args.animation then
anim_info = animations[args.animation]
end
Expand Down Expand Up @@ -1653,7 +1658,7 @@ do
self.height = abs(self.image:getHeight() * self.scaley * self.scale)
end
end;
update = function(self,dt)
_update = function(self,dt)
-- update animation
if self.animated then
self.t = self.t + (dt * self.speed)
Expand All @@ -1666,6 +1671,7 @@ do
end
end
end;
update = function(self,dt) self:_update(dt) end,
_draw = function(self)
self:updateSize()
if self.animated then
Expand Down Expand Up @@ -3737,6 +3743,7 @@ do

-- change velocity by collision normal
if cols[i].bounce then
print(bounciness, nx, ny)
if hspeed and ((nx < 0 and hspeed > 0) or (nx > 0 and hspeed < 0)) then
obj.hspeed = -obj.hspeed * bounciness
end
Expand Down Expand Up @@ -5075,18 +5082,19 @@ do

Blanke.game_canvas:drawTo(_draw)

Draw{
{'push'},
{'color','black'},
{'rect','fill',0,0,Window.width,Window.height},
{'pop'}
}
Draw.push()
Draw.color('black')
Draw.rect('fill',0,0,Window.width,Window.height)
Draw.pop()

if Game.options.scale == true then
Blanke.game_canvas.x, Blanke.game_canvas.y = Blanke.padx, Blanke.pady
Blanke.game_canvas.scale = Blanke.scale
Draw.push()
Draw.translate(Blanke.padx, Blanke.pady)
Draw.scale(Blanke.scale)

Blanke.game_canvas:draw()

Draw.pop()
else
Blanke.game_canvas:draw()
end
Expand Down
20 changes: 20 additions & 0 deletions love2d/old.love.js

Large diffs are not rendered by default.

27 changes: 19 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@
"luamin": "^1.0.4",
"markdown-it": "^9.1.0",
"minimist": "^1.2.5",
"mustache": "^4.0.1",
"node-watch": "^0.6.0",
"request": "^2.88.2",
"true-case-path": "^2.2.1",
"update-electron-app": "^1.5.0",
"uuid": "^8.3.0",
"v8-compile-cache": "^2.1.1",
"walk": "^2.3.13"
},
Expand Down
Loading

0 comments on commit 7c4cd3b

Please sign in to comment.