Helper Utilities for a Multitude of Problems is a set of lightweight helpers for the excellent LÖVE Engine.
It currently features
hump differs from other libraries in that every component is independent of the remaining ones (apart from camera.lua, which depends on the vector type). hump's footprint is very small and thus should fit nicely into your projects.
A vector class implementing everything you want to do with vectors, and some more.
Overview (click item for more details):
vector(x,y) | constructor |
isvector(v) | type check |
Vector:clone() | deep copy |
Vector:unpack() | element unfolding |
Operators | Arithmetics and ordering |
vector:permul(other) | per-element multiplication |
vector:len() | vector length |
vector:len2() | squared vector length |
vector:dist(other) | distance to vector |
vector:normalized() | normalize vector |
vector:normalize_inplace() | normalize vector in-place |
Vector:rotated(phi) | rotate vector |
Vector:rotate_inplace(phi) | rotate vector in-place |
v.x
and v.y
.
Parameters: | [number]x : | x coordinate |
---|---|---|
[number]y : | y coordinate | |
Returns: | the vector |
Parameters: | v | variable to test |
---|---|---|
Returns: | true if v is a vector |
Returns: | New vector with the same coordinates. |
---|
Returns: | the coordinate tuple x, y | |
---|---|---|
Example: | ||
v = vector(1,2) print(v:unpack()) -- prints "1 2" |
Arithmetic (+
, -
, *
, /
) and
comparative operators (==
, <=
, <
) are defined:
+
and -
only work on vectors. -
is also
the unary minus (e.g. print(-vector(1,2)) -- prints (-1,-2)
)a * b
works on vectors and numbers:a
is a number and b
is a vector (or vice versa),
the result the scalar multiplication.a
and b
both are vectors, then the result is the
dot product.a / b
is only defined for a
being a vector and b
being a number. Same as a * 1/b
.<=
and <
sort lexically, i.e. a < b
is true if it holds: a.x < b.x
or a.y < b.y
if
a.x == b.x
Example: |
---|
a,b = vector(0,1), vector(1,0) print(a:dist(b)) -- prints 1.4142135623731 |
Warning: This will change the state of all references to this vector.
Parameters: | [number]phi : | Rotation angle in radians. |
---|
Warning: This will change the state of all references to this vector.
class.lua provides a simple and easy to use class system. Albeit it's limitations, class.lua offers multiple inheritance and minimal reflection capabilities. Note that only methods will be inherited and runtime changes of a superclass won't affect subclasses.
The module only consists of three functions:
Class(constructor) | define class |
Class{name = name, constructor} | define named class |
Interface(name) | define interface |
Inherit(class, super, ...) | subclassing |
For an example, see below. Also be sure to read the warning regarding metamethods.
Parameters: | [optional function]constructor |
A function used to construct the class. The first parameter of this function is the object, the others are parameters given upon construction. |
---|---|---|
Returns: | the new class | |
Example | ||
Feline = Class(function(self, size, weight) self.size = size self.weight = weight end) function Feline:stats() return string.format("size: %.02f, weight %.02f", self.size, self.weight) end garfield = Feline(.7, 45) felix = Feline(.8, 12) print("Garfield: " .. garfield:stats(), "Felix: " .. felix:stats()) |
__tostring
metamethod. Parameters are the same as above.
Great for debugging. Both name
and constructor
can be omitted (but why would you want to?)
Example: |
---|
Feline = Class{name = "Feline", function(self, size, weight) self.size = size self.weight = weight end} print(Feline) -- prints 'Feline' |
Class{name = name}
, i.e. a possibly named class without constructor.
super
to class
. Multiple interfaces can be defined.
super
's constructor can be accessed via super.construct(self). See example below.
Feline = Class{name = "Feline", function(self, size, weight) self.size = size self.weight = weight end} function Feline:stats() return string.format("size: %.02f, weight %.02f", self.size, self.weight) end function Feline:speak() print("meow") end Cat = Class{name = "Cat", function(self, name, size, weight) Feline.construct(self, size, weight) self.name = name end} Inherit(Cat, Feline) function Cat:stats() return string.format("name: %s, %s", self.name, Feline.stats(self)) end Tiger = Class{name = "tiger", function(self, size, weight) Feline.construct(self, size, weight) end} Inherit(Tiger, Feline) function Tiger:speak() print("ROAR!") end felix = Cat("Felix", .8, 12) hobbes = Tiger(2.2, 68) print(felix:stats(), hobbes:stats()) felix:speak() hobbes:speak()
Be careful when using metamethods like __add
or __mul
:
When subclass inherits those methods from a superclass, but does not overwrite
them, the result of the operation may be of the type superclass. Consider the following:
A = Class(function(self, x) self.x = x end) function A:__add(other) return A(self.x + other.x) end function A:print() print("A:", self.x) end B = Class(function(self, x, y) A.construct(self, x) self.y = y end) Inherit(B, A) function B:print() print("B:", self.x, self.y) end function B:foo() print("foo") end one, two = B(1,2), B(3,4) result = one + two result:print() -- prints "A: 4" result:foo() -- error: method does not exist
Camera object to display only a partial region of the game world. The region can be zoomed and rotated. You can transform camera coordinates to world coordinated (e.g. get the location of the mouse in the game world). It is possible to have more than one camera per game.
The module defined the following funtions:
Camera(pos, zoom, rotation) | create new camera |
camera:rotate(phi) | rotate camera |
camera:translate(t) | move camera |
camera:draw(func) | apply camera transformations on function |
camera:apply() | apply camera transformations |
camera:deapply() | revert camera transformations |
camera:transform(p) | get world coordinates |
camera:mousepos() | get world coordinates of mouse |
Parameters: | [optional vector]pos : |
Initial position of the camera. Defaults to (0,0) . | |
---|---|---|---|
[optional number]zoom : |
Initial zoom. Defaults to 1 . | ||
[optional number]rotation : |
Initial rotation in radians. Defaults to 0 . | ||
Returns: | a new camera object |
phi
radians. Same as camera.rot = camera.rot + phi.
t
. Same as camera.pos = camera.pos + t
.
func
.
Shortcut to camera:apply()
and camera:deapply()
(see below).
Example |
---|
cam:draw(function() love.graphics.rectangle('fill', -100,-100, 200,200) end) |
camera:deapply()
.
Example: | (equivalent to the cam:draw() example above) |
---|---|
camera:apply() love.graphics.rectangle('fill', -100,-100, 200,200) camera:deapply() |
p
from camera coordinates to world coordinates.
You probably won't need this, but it is the basis to camera:mousepos()
.
Useful to separate different states of your game (hence "gamestate") like title screens,
level loading, main game, etc. Each gamestate can have it's own update()
, draw()
,
keyreleased()
, keypressed()
and mousereleased()
which correspond
to the ones defined in love. See the callback list for more details.
Additionally, each gamestate can define a enter(pre, ...)
and leave()
function,
which are called when using Gamestate.switch(to, ...)
. Do not attempt to use these functions
yourself to initialize the first gamestate, use the switch
function instead.
The module defines the following functions:
Gamestate.new() | New gamestate |
Gamestate.switch(to, ...) | Switch gamestate |
Gamestate.update(dt) | Call update callback |
Gamestate.draw() | Call draw callback |
Gamestate.keypressed(key, unicode) | Call keypressed callback |
Gamestate.keyreleased(key) | Call keyreleased callback |
Gamestate.mousereleased(x,y,btn) | Call mousereleased callback |
Gamestate.registerEvents() | Register all callbacks |
Returns: | The new (but empty) gamestate object. |
---|
Calls leave()
on the currently active gamestate.
Calls enter(current, ...)
on the target gamestate, where current
is the gamestate before the switch and ...
are the additionals arguments given
to Gamestate.switch
.
Parameters: | [gamestate]to : | target gamestate. |
---|---|---|
... : | additional arguments to pass | |
Returns: | the result of to:enter(current, ...) |
update(dt)
on current gamestate.
draw()
on current gamestate.
keypressed(key, unicode)
on current gamestate.
keyreleased(key)
on current gamestate.
mousereleased(x,y,btn)
on the current gamestate.
love.*
routines.
It is an error to call this anywhere else than love.load()
, since it overwrites the
callbacks. Dont worry though, your callbacks will still be executed.
enter(previous, ...) |
Gets called upon entering the state. Don't call this yourself, use
Gamestate.switch instead. |
leave() |
Gets called upon leaving the state. The same warning as with enter applies. |
update(dt) |
Manully called by Gamestate.update , or automatically like love.update
when using Gamestate.registerEvents() . |
draw() |
Manully called by Gamestate.draw , or automatically like love.draw |
keypressed(key, unicode) |
Manully called by Gamestate.keypressed , or automatically like love.keypressed |
keyreleased(key) |
Manully called by Gamestate.keyreleased , or automatically like love.keyreleased |
mousereleased(x,y,btn) |
Manully called by Gamestate.mousereleased , or automatically like love.mousereleased |
Yay, free software:
Copyright (c) 2010 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 in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can download this project in either zip or tar formats.
You can also clone the project with Git by running:
$ git clone git://github.com/vrld/hump