Eliminate use of module(...) function

This commit is contained in:
Matthias Richter 2011-07-06 14:19:31 +02:00
parent 610de8fc98
commit 28b698fb00
7 changed files with 85 additions and 86 deletions

View File

@ -6,12 +6,12 @@ __HUMP__ is a small collection of tools for developing games with LÖVE.
Contents:
------------
* *gamestate.lua*: class to handle gamestates
* *timer.lua*: timed function calls and interpolation function wrappers
* *vector.lua*: powerful vector class (pure lua)
* *class.lua*: "class" system supporting function inheritance (pure lua)
* *camera.lua*: translate-, zoom- and rotatable camera
* *gamestate.lua*: class to handle gamestates
* *camera.lua*: move-, zoom- and rotatable camera
* *ringbuffer.lua*: a circular container
* *sequence.lua*: utility to handle ingame cutscenes and such
Documentation
=============
@ -21,9 +21,7 @@ You can find the documentation here: [http://vrld.github.com/hump/](http://vrld.
License
=======
Yay, *free software*:
> Copyright (c) 2010 Matthias Richter
> Copyright (c) 2010-2011 Matthias Richter
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal

View File

@ -24,14 +24,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
local setmetatable, require, love = setmetatable, require, love
module(...)
local vector = require(_PACKAGE..'vector')
local _PATH = (...):match('^(.*[%./])[^%.%/]+$') or ''
local vector = vector or Vector or require(_PATH..'vector')
local camera = {}
camera.__index = camera
function new(pos, zoom, rot)
local function new(pos, zoom, rot)
local pos = pos or vector(love.graphics.getWidth(), love.graphics.getHeight()) / 2
local zoom = zoom or 1
local rot = rot or 0
@ -82,9 +81,6 @@ function camera:mousepos()
return self:toWorldCoords(vector(love.mouse.getPosition()))
end
-- camera() as a shortcut to new()
do
local m = {}
m.__call = function(_, ...) return new(...) end
setmetatable(_M, m)
end
-- the module
return setmetatable({new = new},
{__call = function(_, ...) return new(...) end})

View File

@ -24,16 +24,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
local getfenv, setmetatable, getmetatable = getfenv, setmetatable, getmetatable
local type, assert, pairs, unpack = type, assert, pairs, unpack
local tostring, string_format = tostring, string.format
local print = print
module(...)
local function __NULL__() end
-- class "inheritance" by copying functions
function inherit(class, interface, ...)
local function inherit(class, interface, ...)
if not interface or type(interface) ~= "table" then return end
-- __index and construct are not overwritten as for them class[name] is defined
@ -50,7 +44,7 @@ function inherit(class, interface, ...)
end
-- class builder
function new(args)
local function new(args)
local super = {}
local name = '<unnamed class>'
local constructor = args or __NULL__
@ -60,13 +54,12 @@ function new(args)
name = args.name or name
constructor = args[1] or __NULL__
end
assert(type(constructor) == "function",
string_format('constructor has to be nil or a function'))
assert(type(constructor) == "function", 'constructor has to be nil or a function')
-- build class
local class = {}
class.__index = class
class.__tostring = function() return string_format("<instance of %s>", tostring(class)) end
class.__tostring = function() return ("<instance of %s>"):format(tostring(class)) end
class.construct, class.Construct = constructor or __NULL__, constructor or __NULL__
class.Construct = class.construct
class.inherit, class.Inherit = inherit, inherit
@ -74,7 +67,7 @@ function new(args)
class.is_a = function(self, other) return not not self.__is_a[other] end
-- intercept assignment in global environment to infer the class name
if not name then
if not (args and args.name) then
local env, env_meta, interceptor = getfenv(0), getmetatable(getfenv(0)), {}
function interceptor:__newindex(key, value)
if value == class then
@ -104,9 +97,6 @@ function new(args)
return setmetatable(class, meta)
end
-- class() as shortcut to class.new()
do
local m = {}
m.__call = function(_, ...) return new(...) end
setmetatable(_M, m)
end
-- the module
return setmetatable({new = new, inherit = inherit},
{__call = function(_,...) return new(...) end})

View File

@ -24,9 +24,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
local error, assert, love = error, assert, love
module(...)
local function __NULL__() end
-- default gamestate produces error on every callback
local function __ERROR__() error("Gamestate not initialized. Use Gamestate.switch()") end
@ -46,7 +43,7 @@ current = {
quit = __ERROR__,
}
function new()
local function new()
return {
init = __NULL__,
enter = __NULL__,
@ -64,7 +61,7 @@ function new()
}
end
function switch(to, ...)
local function switch(to, ...)
assert(to, "Missing argument: Gamestate to switch to")
current:leave()
local pre = current
@ -75,66 +72,66 @@ function switch(to, ...)
end
local _update
function update(...)
local function update(...)
if _update then _update(...) end
return current:update(...)
end
local _draw
function draw(...)
local function draw(...)
if _draw then _draw(...) end
return current:draw(...)
end
local _focus
function focus(...)
local function focus(...)
if _focus then _focus(...) end
return current:focus(...)
end
local _keypressed
function keypressed(...)
local function keypressed(...)
if _keypressed then _keypressed(...) end
return current:keypressed(...)
end
local _keyreleased
function keyreleased(...)
local function keyreleased(...)
if _keyreleased then _keyreleased(...) end
return current:keyreleased(...)
end
local _mousepressed
function mousepressed(...)
local function mousepressed(...)
if _mousereleased then _mousepressed(...) end
return current:mousepressed(...)
end
local _mousereleased
function mousereleased(...)
local function mousereleased(...)
if _mousereleased then _mousereleased(...) end
return current:mousereleased(...)
end
local _joystickpressed
function joystickpressed(...)
local function joystickpressed(...)
if _joystickpressed then _joystickpressed(...) end
return current:joystickpressed(...)
end
local _joystickreleased
function joystickreleased(...)
local function joystickreleased(...)
if _joystickreleased then _joystickreleased(...) end
return current:joystickreleased(...)
end
local _quit
function quit(...)
local function quit(...)
if _quit then _quit(...) end
return current:quit(...)
end
function registerEvents()
local function registerEvents()
_update = love.update
love.update = update
_draw = love.draw
@ -156,3 +153,20 @@ function registerEvents()
_quit = love.quit
love.quit = quit
end
-- the module
return {
new = new,
switch = switch,
update = update,
draw = draw,
focus = focus,
keypressed = keypressed,
keyreleased = keyreleased,
mousepressed = mousepressed,
mousereleased = mousereleased,
joystickpressed = joystickpressed,
joystickreleased = joystickreleased,
quit = quit,
registerEvents = registerEvents
}

View File

@ -24,12 +24,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
local setmetatable, getmetatable, table = setmetatable, getmetatable, table
module(...)
local ringbuffer = {}
ringbuffer.__index = ringbuffer
function new(...)
local function new(...)
local rb = {}
rb.items = {...}
rb.current = 1
@ -91,9 +89,6 @@ function ringbuffer:prev()
return self:get()
end
-- Ringbuffer() as a shortcut to Ringbuffer.new()
do
local m = {}
m.__call = function(_, ...) return new(...) end
setmetatable(_M, m)
end
-- the module
return setmetatable({new = new},
{__call = function(_, ...) return new(...) end})

View File

@ -24,13 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
local assert, type = assert, type
local pairs, ipairs = pairs, ipairs
local min, math_huge = math.min, math.huge
module(...)
functions = {}
function update(dt)
local functions = {}
local function update(dt)
local to_remove = {}
for func, delay in pairs(functions) do
delay = delay - dt
@ -45,14 +40,14 @@ function update(dt)
end
end
function add(delay, func)
local function add(delay, func)
assert(type(func) == "function", "second argument needs to be a function")
functions[func] = delay
end
function addPeriodic(delay, func, count)
local function addPeriodic(delay, func, count)
assert(type(func) == "function", "second argument needs to be a function")
local count = count or math_huge -- exploit below: math.huge - 1 = math.huge
local count = count or math.huge -- exploit below: math.huge - 1 = math.huge
add(delay, function(f)
if func(func) == false then return end
@ -63,11 +58,11 @@ function addPeriodic(delay, func, count)
end)
end
function clear()
local function clear()
functions = {}
end
function Interpolator(length, func)
local function Interpolator(length, func)
assert(type(func) == "function", "second argument needs to be a function")
local t = 0
return function(dt, ...)
@ -76,7 +71,7 @@ function Interpolator(length, func)
end
end
function Oscillator(length, func)
local function Oscillator(length, func)
assert(type(func) == "function", "second argument needs to be a function")
local t = 0
return function(dt, ...)
@ -84,3 +79,13 @@ function Oscillator(length, func)
return func(t/length, ...)
end
end
-- the module
return {
update = update,
add = add,
addPeriodic = addPeriodic,
clear = clear,
Interpolator = Interpolator,
Oscillator = Oscillator
}

View File

@ -24,21 +24,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
local setmetatable, getmetatable = setmetatable, getmetatable
local assert, type, tonumber = assert, type, tonumber
local assert = assert
local sqrt, cos, sin = math.sqrt, math.cos, math.sin
module(...)
local vector = {}
vector.__index = vector
function new(x,y)
local function new(x,y)
local v = {x = x or 0, y = y or 0}
setmetatable(v, vector)
return v
end
function isvector(v)
local function isvector(v)
return getmetatable(v) == vector
end
@ -102,11 +100,11 @@ function vector.permul(a,b)
end
function vector:len2()
return self * self
return self.x * self.x + self.y * self.y
end
function vector:len()
return sqrt(self*self)
return sqrt(self:len2())
end
function vector.dist(a, b)
@ -139,18 +137,21 @@ function vector:perpendicular()
end
function vector:projectOn(v)
assert(isvector(v), "invalid argument: cannot project onto anything other than a new.")
assert(isvector(v), "invalid argument: cannot project onto anything other than a vector")
return (self * v) * v / v:len2()
end
function vector:mirrorOn(other)
assert(isvector(other), "invalid argument: cannot mirror on anything other than a vector")
return 2 * self:projectOn(other) - self
end
function vector:cross(other)
assert(isvector(other), "cross: wrong argument types (<vector> expected)")
return self.x * other.y - self.y * other.x
end
-- vector() as shortcut to vector.new()
do
local m = {}
m.__call = function(_, ...) return new(...) end
setmetatable(_M, m)
end
-- the module
return setmetatable({new = new, isvector = isvector},
{__call = function(_, ...) return new(...) end})