mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 22:24:35 +00:00
[fixed] non-global compatible
This commit is contained in:
parent
429ef9b02e
commit
ad2fc71e7a
@ -9,12 +9,13 @@
|
|||||||
cancelling
|
cancelling
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local async = class()
|
local async = {}
|
||||||
|
async._mt = {__index = async}
|
||||||
|
|
||||||
function async:new()
|
function async:new()
|
||||||
return self:init({
|
return setmetatable({
|
||||||
tasks = {},
|
tasks = {},
|
||||||
})
|
}, self._mt)
|
||||||
end
|
end
|
||||||
|
|
||||||
--add a task to the kernel
|
--add a task to the kernel
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
todo: collect some stats on classes/optional global class registry
|
todo: collect some stats on classes/optional global class registry
|
||||||
]]
|
]]
|
||||||
|
|
||||||
return local function class(inherits)
|
local function class(inherits)
|
||||||
local c = {}
|
local c = {}
|
||||||
c.__mt = {__index = c}
|
c.__mt = {__index = c}
|
||||||
--handle single inheritence
|
--handle single inheritence
|
||||||
@ -41,3 +41,5 @@ return local function class(inherits)
|
|||||||
--done
|
--done
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return class
|
||||||
|
@ -2,15 +2,26 @@
|
|||||||
geometric intersection routines
|
geometric intersection routines
|
||||||
from simple point tests to shape vs shape tests
|
from simple point tests to shape vs shape tests
|
||||||
|
|
||||||
|
optimised pretty well in most places.
|
||||||
|
|
||||||
options for boolean or minimum separating vector results
|
options for boolean or minimum separating vector results
|
||||||
|
|
||||||
continuous sweeps (where provided) also return the
|
continuous sweeps (where provided) also return the
|
||||||
time-domain position of first intersection
|
time-domain position of first intersection
|
||||||
|
|
||||||
TODO: refactor storage to be pooled rather than fully local
|
TODO: refactor vector storage to be pooled rather than fully local
|
||||||
so these functions can be reentrant
|
so these functions can be reentrant
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
--import vec2 if not defined globally
|
||||||
|
local global_vec2 = vec2
|
||||||
|
local vec2 = global_vec2
|
||||||
|
if not vec2 then
|
||||||
|
local vec2_path = (...):gsub("intersect", "vec2")
|
||||||
|
vec2 = require(vec2_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
--module storage
|
||||||
local intersect = {}
|
local intersect = {}
|
||||||
|
|
||||||
--epsilon for collisions
|
--epsilon for collisions
|
||||||
|
@ -41,6 +41,10 @@ Some folks will have good reasons, which is why the functionality is present!
|
|||||||
|
|
||||||
Others may wish to reconsider, and save themselves typing `batteries` a few hundred times :)
|
Others may wish to reconsider, and save themselves typing `batteries` a few hundred times :)
|
||||||
|
|
||||||
|
# Why aren't various types using `class`?
|
||||||
|
|
||||||
|
To avoid a dependency on class.lua for those modules
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
MIT, see [here](license.txt)
|
MIT, see [here](license.txt)
|
||||||
|
12
sequence.lua
12
sequence.lua
@ -10,15 +10,15 @@
|
|||||||
|
|
||||||
local sequence = {}
|
local sequence = {}
|
||||||
|
|
||||||
sequence.mt = {__index = sequence}
|
sequence._mt = {__index = sequence}
|
||||||
|
|
||||||
--proxy missing table fns to table
|
--proxy missing table fns to global table api
|
||||||
sequence._mt = {__index = table}
|
sequence.__mt = {__index = table}
|
||||||
setmetatable(sequence, sequence._mt)
|
setmetatable(sequence, sequence.__mt)
|
||||||
|
|
||||||
--upgrade a table into a functional sequence
|
--upgrade a table into a sequence, or create a new sequence
|
||||||
function sequence:new(t)
|
function sequence:new(t)
|
||||||
return setmetatable(t or {}, sequence.mt)
|
return setmetatable(t or {}, sequence._mt)
|
||||||
end
|
end
|
||||||
|
|
||||||
--alias
|
--alias
|
||||||
|
@ -12,17 +12,16 @@
|
|||||||
on draw, the current state's draw callback is called
|
on draw, the current state's draw callback is called
|
||||||
|
|
||||||
TODO: consider coroutine friendliness
|
TODO: consider coroutine friendliness
|
||||||
|
|
||||||
depends on oo.lua supplying class()
|
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local state_machine = class()
|
local state_machine = {}
|
||||||
|
state_machine._mt = {__index = state_machine}
|
||||||
|
|
||||||
function state_machine:new(states, start)
|
function state_machine:new(states, start)
|
||||||
local ret = self:init({
|
local ret = setmetatable({
|
||||||
states = states or {},
|
states = states or {},
|
||||||
current_state = ""
|
current_state = ""
|
||||||
})
|
}, self._mt)
|
||||||
|
|
||||||
if start then
|
if start then
|
||||||
ret:set_state(start)
|
ret:set_state(start)
|
||||||
|
@ -9,8 +9,10 @@
|
|||||||
local _table = BATTERIES_TABLE_MODULE or table
|
local _table = BATTERIES_TABLE_MODULE or table
|
||||||
|
|
||||||
--apply prototype to module if it isn't the global table
|
--apply prototype to module if it isn't the global table
|
||||||
if BATTERIES_TABLE_MODULE ~= table then
|
--so it works "as if" it was the global table api
|
||||||
setmetatable(BATTERIES_TABLE_MODULE, {
|
--upgraded with these routines
|
||||||
|
if _table ~= table then
|
||||||
|
setmetatable(_table, {
|
||||||
__index = table,
|
__index = table,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
@ -209,7 +211,7 @@ end
|
|||||||
-- can be discarded, but it "feels dirty" :)
|
-- can be discarded, but it "feels dirty" :)
|
||||||
|
|
||||||
function _table.unpack2(t)
|
function _table.unpack2(t)
|
||||||
return t[1], t[2],
|
return t[1], t[2]
|
||||||
end
|
end
|
||||||
|
|
||||||
function _table.unpack3(t)
|
function _table.unpack3(t)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
--a natural ordering implied (eg textures for batching)
|
--a natural ordering implied (eg textures for batching)
|
||||||
|
|
||||||
local unique_mapping = {}
|
local unique_mapping = {}
|
||||||
unique_mapping.mt = {
|
unique_mapping._mt = {
|
||||||
__index = unique_mapping,
|
__index = unique_mapping,
|
||||||
__mode = "kv", --weak refs
|
__mode = "kv", --weak refs
|
||||||
}
|
}
|
||||||
@ -14,7 +14,7 @@ local _MAP_VARS = setmetatable({}, {
|
|||||||
})
|
})
|
||||||
|
|
||||||
function unique_mapping:new()
|
function unique_mapping:new()
|
||||||
local r = setmetatable({}, unique_mapping.mt)
|
local r = setmetatable({}, self._mt)
|
||||||
--set up the actual vars
|
--set up the actual vars
|
||||||
_MAP_VARS[r] = {
|
_MAP_VARS[r] = {
|
||||||
current_index = 0,
|
current_index = 0,
|
||||||
|
17
vec2.lua
17
vec2.lua
@ -5,18 +5,21 @@
|
|||||||
--[[
|
--[[
|
||||||
notes:
|
notes:
|
||||||
|
|
||||||
depends on a class() function as in batteries class.lua
|
|
||||||
|
|
||||||
some methods depend on math library extensions
|
some methods depend on math library extensions
|
||||||
|
|
||||||
math.clamp(v, min, max) - return v clamped between min and max
|
math.clamp(v, min, max) - return v clamped between min and max
|
||||||
math.round(v) - round v downwards if fractional part is < 0.5
|
math.round(v) - round v downwards if fractional part is < 0.5
|
||||||
]]
|
]]
|
||||||
|
|
||||||
local vec2 = class()
|
local vec2 = {}
|
||||||
|
|
||||||
vec2.type = "vec2"
|
vec2.type = "vec2"
|
||||||
|
|
||||||
|
--class
|
||||||
|
vec2._mt = {__index = vec2}
|
||||||
|
function vec2:init(t)
|
||||||
|
return setmetatable(t, self._mt)
|
||||||
|
end
|
||||||
|
|
||||||
--probably-too-flexible ctor
|
--probably-too-flexible ctor
|
||||||
function vec2:new(x, y)
|
function vec2:new(x, y)
|
||||||
if x and y then
|
if x and y then
|
||||||
@ -24,12 +27,14 @@ function vec2:new(x, y)
|
|||||||
elseif x then
|
elseif x then
|
||||||
if type(x) == "number" then
|
if type(x) == "number" then
|
||||||
return vec2:filled(x)
|
return vec2:filled(x)
|
||||||
elseif x.type == "vec2" then
|
elseif type(x) == "table" then
|
||||||
|
if x.type == "vec2" then
|
||||||
return x:copy()
|
return x:copy()
|
||||||
elseif type(x) == "table" and x[1] and x[2] then
|
elseif x[1] and x[2] then
|
||||||
return vec2:xy(x[1], x[2])
|
return vec2:xy(x[1], x[2])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
return vec2:zero()
|
return vec2:zero()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
16
vec3.lua
16
vec3.lua
@ -5,24 +5,30 @@
|
|||||||
--[[
|
--[[
|
||||||
notes:
|
notes:
|
||||||
|
|
||||||
depends on a class() function as in batteries class.lua
|
|
||||||
|
|
||||||
some methods depend on math library extensions
|
some methods depend on math library extensions
|
||||||
|
|
||||||
math.clamp(v, min, max) - return v clamped between min and max
|
math.clamp(v, min, max) - return v clamped between min and max
|
||||||
math.round(v) - round v downwards if fractional part is < 0.5
|
math.round(v) - round v downwards if fractional part is < 0.5
|
||||||
]]
|
]]
|
||||||
|
|
||||||
--defined globally? otherwise import vec2
|
--import vec2 if not defined globally
|
||||||
|
local global_vec2 = vec2
|
||||||
|
local vec2 = global_vec2
|
||||||
if not vec2 then
|
if not vec2 then
|
||||||
local path = ...
|
local path = ...
|
||||||
local vec2_path = path:sub(1, path:len() - 1) .. "2"
|
local vec2_path = path:sub(1, path:len() - 1) .. "2"
|
||||||
local vec2 = require(vec2_path)
|
vec2 = require(vec2_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
local vec3 = class()
|
local vec3 = {}
|
||||||
vec3.type = "vec3"
|
vec3.type = "vec3"
|
||||||
|
|
||||||
|
--class
|
||||||
|
vec3._mt = {__index = vec3}
|
||||||
|
function vec3:init(t)
|
||||||
|
return setmetatable(t, self._mt)
|
||||||
|
end
|
||||||
|
|
||||||
--probably-too-flexible ctor
|
--probably-too-flexible ctor
|
||||||
function vec3:new(x, y, z)
|
function vec3:new(x, y, z)
|
||||||
if x and y and z then
|
if x and y and z then
|
||||||
|
Loading…
Reference in New Issue
Block a user