Skip to content

Commit

Permalink
Eliminate use of module(...) function
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Richter committed Jul 6, 2011
1 parent 610de8f commit 28b698f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 86 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============
Expand All @@ -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
Expand Down
16 changes: 6 additions & 10 deletions camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
26 changes: 8 additions & 18 deletions class.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__
Expand All @@ -60,21 +54,20 @@ 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
class.__is_a = {[class] = true}
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
Expand Down Expand Up @@ -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})
46 changes: 30 additions & 16 deletions gamestate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,7 +43,7 @@ current = {
quit = __ERROR__,
}

function new()
local function new()
return {
init = __NULL__,
enter = __NULL__,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
13 changes: 4 additions & 9 deletions ringbuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
31 changes: 18 additions & 13 deletions timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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, ...)
Expand All @@ -76,11 +71,21 @@ 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, ...)
t = (t + dt) % length
return func(t/length, ...)
end
end

-- the module
return {
update = update,
add = add,
addPeriodic = addPeriodic,
clear = clear,
Interpolator = Interpolator,
Oscillator = Oscillator
}
Loading

0 comments on commit 28b698f

Please sign in to comment.