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