mirror of
https://github.com/vrld/hump.git
synced 2024-11-23 12:24:19 +00:00
This commit is contained in:
commit
49a1e33c56
@ -51,7 +51,7 @@ end
|
|||||||
|
|
||||||
-- returns a deep copy of `other'
|
-- returns a deep copy of `other'
|
||||||
local function clone(other)
|
local function clone(other)
|
||||||
return include({}, other)
|
return setmetatable(include({}, other), getmetatable(other))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function new(class)
|
local function new(class)
|
||||||
|
@ -36,6 +36,7 @@ function GS.new(t) return t or {} end -- constructor - deprecated!
|
|||||||
|
|
||||||
function GS.switch(to, ...)
|
function GS.switch(to, ...)
|
||||||
assert(to, "Missing argument: Gamestate to switch to")
|
assert(to, "Missing argument: Gamestate to switch to")
|
||||||
|
assert(to ~= GS, "Can't call switch with colon operator")
|
||||||
local pre = stack[#stack]
|
local pre = stack[#stack]
|
||||||
;(pre.leave or __NULL__)(pre)
|
;(pre.leave or __NULL__)(pre)
|
||||||
;(to.init or __NULL__)(to)
|
;(to.init or __NULL__)(to)
|
||||||
@ -46,6 +47,7 @@ end
|
|||||||
|
|
||||||
function GS.push(to, ...)
|
function GS.push(to, ...)
|
||||||
assert(to, "Missing argument: Gamestate to switch to")
|
assert(to, "Missing argument: Gamestate to switch to")
|
||||||
|
assert(to ~= GS, "Can't call push with colon operator")
|
||||||
local pre = stack[#stack]
|
local pre = stack[#stack]
|
||||||
;(to.init or __NULL__)(to)
|
;(to.init or __NULL__)(to)
|
||||||
to.init = nil
|
to.init = nil
|
||||||
@ -53,11 +55,12 @@ function GS.push(to, ...)
|
|||||||
return (to.enter or __NULL__)(to, pre, ...)
|
return (to.enter or __NULL__)(to, pre, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function GS.pop()
|
function GS.pop(...)
|
||||||
assert(#stack > 1, "No more states to pop!")
|
assert(#stack > 1, "No more states to pop!")
|
||||||
local pre = stack[#stack]
|
local pre = stack[#stack]
|
||||||
stack[#stack] = nil
|
stack[#stack] = nil
|
||||||
return (pre.leave or __NULL__)(pre)
|
;(pre.leave or __NULL__)(pre)
|
||||||
|
return (stack[#stack].resume or __NULL__)(pre, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function GS.current()
|
function GS.current()
|
||||||
|
@ -83,7 +83,7 @@ local default = new()
|
|||||||
|
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
new = new,
|
new = new,
|
||||||
register = function(...) default:register(...) end,
|
register = function(...) return default:register(...) end,
|
||||||
emit = function(...) default:emit(...) end,
|
emit = function(...) default:emit(...) end,
|
||||||
remove = function(...) default:remove(...) end,
|
remove = function(...) default:remove(...) end,
|
||||||
clear = function(...) default:clear(...) end,
|
clear = function(...) default:clear(...) end,
|
||||||
|
@ -116,7 +116,7 @@ end
|
|||||||
-- ref.: http://blog.signalsondisplay.com/?p=336
|
-- ref.: http://blog.signalsondisplay.com/?p=336
|
||||||
local function trim(maxLen, x, y)
|
local function trim(maxLen, x, y)
|
||||||
local s = maxLen * maxLen / len2(x, y)
|
local s = maxLen * maxLen / len2(x, y)
|
||||||
s = s < 1 and 1 or math.sqrt(s)
|
s = s > 1 and 1 or math.sqrt(s)
|
||||||
return x * s, y * s
|
return x * s, y * s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ end
|
|||||||
local zero = new(0,0)
|
local zero = new(0,0)
|
||||||
|
|
||||||
local function isvector(v)
|
local function isvector(v)
|
||||||
return getmetatable(v) == vector
|
return type(v) == 'table' and type(v.x) == 'number' and type(v.y) == 'number'
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector:clone()
|
function vector:clone()
|
||||||
@ -169,16 +169,16 @@ end
|
|||||||
-- ref.: http://blog.signalsondisplay.com/?p=336
|
-- ref.: http://blog.signalsondisplay.com/?p=336
|
||||||
function vector:trim_inplace(maxLen)
|
function vector:trim_inplace(maxLen)
|
||||||
local s = maxLen * maxLen / self:len2()
|
local s = maxLen * maxLen / self:len2()
|
||||||
s = s < 1 and 1 or math.sqrt(s)
|
s = (s > 1 and 1) or math.sqrt(s)
|
||||||
self.x, self.y = self.x * s, self.y * s
|
self.x, self.y = self.x * s, self.y * s
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector:angleTo(other)
|
function vector:angleTo(other)
|
||||||
if other then
|
if other then
|
||||||
return atan2(self.y, self.y) - atan2(other.y, other.x)
|
return atan2(self.y, self.x) - atan2(other.y, other.x)
|
||||||
end
|
end
|
||||||
return atan2(self.y, self.y)
|
return atan2(self.y, self.x)
|
||||||
end
|
end
|
||||||
|
|
||||||
function vector:trimmed(maxLen)
|
function vector:trimmed(maxLen)
|
||||||
|
Loading…
Reference in New Issue
Block a user