Commit d7d5e30f authored by Carlo's avatar Carlo

Added assets, hump, and main.lua

parent 9a3cfe89
HUMP - Helper Utilities for Massive Progression
===============================================
__HUMP__ is a small collection of tools for developing games with LÖVE.
Contents:
------------
* *gamestate.lua*: Easy gamestate management.
* *timer.lua*: Delayed and time-limited function calls and tweening.
* *vector.lua*: 2D vector math.
* *vector-light.lua*: Lightweight 2D vector math (for optimisation purposes - leads to potentially ugly code).
* *class.lua*: Lightweight object orientation (class or prototype based).
* *signal.lua*: Simple Signal/Slot (aka. Observer) implementation.
* *camera.lua*: Move-, zoom- and rotatable camera with camera locking and movement smoothing.
Documentation
=============
You can find the documentation here: [hump.readthedocs.org](http://hump.readthedocs.org)
License
=======
> Copyright (c) 2010-2013 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.
--[[
Copyright (c) 2010-2015 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.
]]--
local _PATH = (...):match('^(.*[%./])[^%.%/]+$') or ''
local cos, sin = math.cos, math.sin
local camera = {}
camera.__index = camera
-- Movement interpolators (for camera locking/windowing)
camera.smooth = {}
function camera.smooth.none()
return function(dx,dy) return dx,dy end
end
function camera.smooth.linear(speed)
assert(type(speed) == "number", "Invalid parameter: speed = "..tostring(speed))
return function(dx,dy, s)
-- normalize direction
local d = math.sqrt(dx*dx+dy*dy)
local dts = math.min((s or speed) * love.timer.getDelta(), d) -- prevent overshooting the goal
if d > 0 then
dx,dy = dx/d, dy/d
end
return dx*dts, dy*dts
end
end
function camera.smooth.damped(stiffness)
assert(type(stiffness) == "number", "Invalid parameter: stiffness = "..tostring(stiffness))
return function(dx,dy, s)
local dts = love.timer.getDelta() * (s or stiffness)
return dx*dts, dy*dts
end
end
local function new(x,y, zoom, rot, smoother)
x,y = x or love.graphics.getWidth()/2, y or love.graphics.getHeight()/2
zoom = zoom or 1
rot = rot or 0
smoother = smoother or camera.smooth.none() -- for locking, see below
return setmetatable({x = x, y = y, scale = zoom, rot = rot, smoother = smoother}, camera)
end
function camera:lookAt(x,y)
self.x, self.y = x, y
return self
end
function camera:move(dx,dy)
self.x, self.y = self.x + dx, self.y + dy
return self
end
function camera:position()
return self.x, self.y
end
function camera:rotate(phi)
self.rot = self.rot + phi
return self
end
function camera:rotateTo(phi)
self.rot = phi
return self
end
function camera:zoom(mul)
self.scale = self.scale * mul
return self
end
function camera:zoomTo(zoom)
self.scale = zoom
return self
end
function camera:attach(x,y,w,h, noclip)
x,y = x or 0, y or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
self._sx,self._sy,self._sw,self._sh = love.graphics.getScissor()
if not noclip then
love.graphics.setScissor(x,y,w,h)
end
local cx,cy = x+w/2, y+h/2
love.graphics.push()
love.graphics.translate(cx, cy)
love.graphics.scale(self.scale)
love.graphics.rotate(self.rot)
love.graphics.translate(-self.x, -self.y)
end
function camera:detach()
love.graphics.pop()
love.graphics.setScissor(self._sx,self._sy,self._sw,self._sh)
end
function camera:draw(...)
local x,y,w,h,noclip,func
local nargs = select("#", ...)
if nargs == 1 then
func = ...
elseif nargs == 5 then
x,y,w,h,func = ...
elseif nargs == 6 then
x,y,w,h,noclip,func = ...
else
error("Invalid arguments to camera:draw()")
end
self:attach(x,y,w,h,noclip)
func()
self:detach()
end
-- world coordinates to camera coordinates
function camera:cameraCoords(x,y, ox,oy,w,h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
-- x,y = ((x,y) - (self.x, self.y)):rotated(self.rot) * self.scale + center
local c,s = cos(self.rot), sin(self.rot)
x,y = x - self.x, y - self.y
x,y = c*x - s*y, s*x + c*y
return x*self.scale + w/2 + ox, y*self.scale + h/2 + oy
end
-- camera coordinates to world coordinates
function camera:worldCoords(x,y, ox,oy,w,h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
-- x,y = (((x,y) - center) / self.scale):rotated(-self.rot) + (self.x,self.y)
local c,s = cos(-self.rot), sin(-self.rot)
x,y = (x - w/2 - ox) / self.scale, (y - h/2 - oy) / self.scale
x,y = c*x - s*y, s*x + c*y
return x+self.x, y+self.y
end
function camera:mousePosition(ox,oy,w,h)
local mx,my = love.mouse.getPosition()
return self:worldCoords(mx,my, ox,oy,w,h)
end
-- camera scrolling utilities
function camera:lockX(x, smoother, ...)
local dx, dy = (smoother or self.smoother)(x - self.x, self.y, ...)
self.x = self.x + dx
return self
end
function camera:lockY(y, smoother, ...)
local dx, dy = (smoother or self.smoother)(self.x, y - self.y, ...)
self.y = self.y + dy
return self
end
function camera:lockPosition(x,y, smoother, ...)
return self:move((smoother or self.smoother)(x - self.x, y - self.y, ...))
end
function camera:lockWindow(x, y, x_min, x_max, y_min, y_max, smoother, ...)
-- figure out displacement in camera coordinates
x,y = self:cameraCoords(x,y)
local dx, dy = 0,0
if x < x_min then
dx = x - x_min
elseif x > x_max then
dx = x - x_max
end
if y < y_min then
dy = y - y_min
elseif y > y_max then
dy = y - y_max
end
-- transform displacement to movement in world coordinates
local c,s = cos(-self.rot), sin(-self.rot)
dx,dy = (c*dx - s*dy) / self.scale, (s*dx + c*dy) / self.scale
-- move
self:move((smoother or self.smoother)(dx,dy,...))
end
-- the module
return setmetatable({new = new, smooth = camera.smooth},
{__call = function(_, ...) return new(...) end})
--[[
Copyright (c) 2010-2013 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.
]]--
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
# Makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build
# User-friendly check for sphinx-build
ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/)
endif
# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " applehelp to make an Apple Help Book"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " xml to make Docutils-native XML files"
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
@echo " coverage to run coverage check of the documentation (if enabled)"
clean:
rm -rf $(BUILDDIR)/*
html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."
json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."
htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."
qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/hump.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/hump.qhc"
applehelp:
$(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
@echo
@echo "Build finished. The help book is in $(BUILDDIR)/applehelp."
@echo "N.B. You won't be able to view it unless you put it in" \
"~/Library/Documentation/Help or install it in your application" \
"bundle."
devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/hump"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/hump"
@echo "# devhelp"
epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."
latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
latexpdfja:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through platex and dvipdfmx..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."
man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."
info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."
linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."
doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."
coverage:
$(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
@echo "Testing of coverage in the sources finished, look at the " \
"results in $(BUILDDIR)/coverage/python.txt."
xml:
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
@echo
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."
pseudoxml:
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
@echo
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
(function() {
"use strict";
// DISCLAIMER: I just started learning d3, so this is certainly not good
// idiomatic d3 code. But hey, it works (kinda).
var tweens = {
'out': function(f) { return function(s) { return 1-f(1-s) } },
'chain': function(f1, f2) { return function(s) { return ((s<.5) ? f1(2*s) : 1+f2(2*s-1)) * .5 } },
'linear': function(s) { return s },
'quad': function(s) { return s*s },
'cubic': function(s) { return s*s*s },
'quart': function(s) { return s*s*s*s },
'quint': function(s) { return s*s*s*s*s },
'sine': function(s) { return 1 - Math.cos(s*Math.PI/2) },
'expo': function(s) { return Math.pow(2, 10*(s-1)) },
'circ': function(s) { return 1 - Math.sqrt(Math.max(0,1-s*s)) },
'back': function(s) { var b = 1.70158; return s*s*((b+1)*s - b) },
'bounce': function(s) {
return Math.min(
7.5625 * Math.pow(s, 2),
7.5625 * Math.pow((s - .545455), 2) + .75,
7.5625 * Math.pow((s - .818182), 2) + .90375,
7.5625 * Math.pow((s - .954546), 2) + .984375)
},
'elastic': function(s) {
return -Math.sin(2/0.3 * Math.PI * (s-1) - Math.asin(1)) * Math.pow(2, 10*(s-1))
},
};
var tweenfunc = tweens.linear;
var width_graph = 320,
width_anim_move = 110,
width_anim_rotate = 110,
width_anim_size = 110,
height = 250;
// "UI"
var graph_ui = d3.select("#tween-graph").append("div")
.attr("id", "tween-graph-ui");
// rest see below
// the graph
var graph = d3.select("#tween-graph").append("svg")
.attr("width", width_graph).attr("height", height);
// background
graph.append("rect")
.attr("width", "100%").attr("height", "100%")
.attr("style", "fill:rgb(240,240,240);stroke-width:1;stroke:rgb(100,100,100);");
var y_zero = height * .78, y_one = height * .22;
graph.append("rect")
.attr("y", y_one)
.attr("width", "100%").attr("height", y_zero - y_one)
.attr("style", "fill:steelblue;fill-opacity:.3;stroke-width:1;stroke:rgba(100,100,100,.7)");
// time arrow
graph.append("defs")
.append("marker")
.attr("id", "triangle")
.attr("viewBox", "0 0 10 10")
.attr("refX", 1).attr("refY", 5)
.attr("markerWidth", 4)
.attr("markerHeight", 4)
.attr("orient", "auto")
.attr("style", "fill:rgba(0,0,0,.5)")
.append("path").attr("d", "M 0 0 L 10 5 L 0 10 z");
graph.append("line")
.attr("x1", width_graph/2-80)
.attr("x2", width_graph/2+80)
.attr("y1", y_zero + 40).attr("y2", y_zero + 40)
.attr("style", "stroke-width:2;stroke:rgba(0,0,0,.5)")
.attr("marker-end", "url(#triangle)");
graph.append("text")
.text("Time")
.attr("x", width_graph/2).attr("y", y_zero + 55)
.attr("style", "text-anchor:middle;fill:rgba(0,0,0,.5);font-size:15px");
// the actual graph
var curve = d3.svg.line()
.x(function(x) { return x*width_graph; })
.y(function(x) { return tweenfunc(x) * (y_one - y_zero) + y_zero; })
var graph_curve = graph.append("path").attr("d", curve(d3.range(0,1.05,.005)))
.attr("style", "fill:none;stroke-width:2;stroke:seagreen;");
var graph_marker = graph.append("circle")
.attr("r", 5)
.attr("style", "stroke:goldenrod;fill:none;stroke-width:3");
// finally, a label
var graph_label = graph.append("text")
.text("linear")
.attr("x", width_graph/2).attr("y", 20)
.attr("style", "text-anchor:middle;font-weight:bold;font-size:15px;");
// animation examples - moving ball
var anim_move = d3.select("#tween-graph").append("svg")
.attr("width", width_anim_move).attr("height", height);
anim_move.append("rect")
.attr("width", "100%").attr("height", "100%")
.attr("style", "fill:rgb(240,240,240);stroke-width:1;stroke:rgb(100,100,100);");
anim_move.append("rect")
.attr("width", 10).attr("height", (y_zero - y_one))
.attr("x", width_anim_move/2-5).attr("y", y_one)
.attr("style", "fill:black;opacity:.1");
var anim_move_ball = anim_move.append("circle")
.attr("cx", width_anim_move/2).attr("cy", y_one)
.attr("r", 17)
.attr("style", "fill:steelblue;stroke:rgb(90,90,90);stroke-width:5;");
// animation examples - rotating square
var anim_rotate = d3.select("#tween-graph").append("svg")
.attr("width", width_anim_size).attr("height", height);
anim_rotate.append("rect")
.attr("width", "100%").attr("height", "100%")
.attr("style", "fill:rgb(240,240,240);stroke-width:1;stroke:rgb(100,100,100);");
var w = width_anim_size/2;
var anim_rotate_square = anim_rotate.append("rect")
.attr("x", -w/2).attr("y", -w/4)
.attr("width", w).attr("height", w/2)
.attr("style", "fill:steelblue;stroke:rgb(90,90,90);stroke-width:5;");
// animation examples - resizing ellipse
var anim_size = d3.select("#tween-graph").append("svg")
.attr("width", width_anim_size).attr("height", height);
anim_size.append("rect")
.attr("width", "100%").attr("height", "100%")
.attr("style", "fill:rgb(240,240,240);stroke-width:1;stroke:rgb(100,100,100);");
anim_size.append("ellipse")
.attr("cx", width_anim_size/2).attr("cy", height/2)
.attr("rx", 40).attr("ry", 120)
.attr("style", "fill:rgb(150,150,150);stroke:black;stroke-width:2;opacity:.1");
var anim_size_ellipse = anim_size.append("ellipse")
.attr("cx", width_anim_size/2).attr("cy", height/2)
.attr("rx", 40).attr("ry", 40)
.attr("style", "fill:steelblue;stroke:rgb(90,90,90);stroke-width:5;");
// make it move!
var t = 0;
window.setInterval(function() {
t = (t + .025 / 3);
if (t > 1.3) { t = -.3; }
var tt = Math.max(Math.min(t, 1), 0);
var s = tweenfunc(tt)
var yy = s * (y_one - y_zero) + y_zero;
var translate = "translate("+(width_anim_size/2)+" "+(height/2)+")";
var rotate = "rotate(" + (s * 360) + ")";
graph_marker.attr("cx", tt*width_graph).attr("cy", yy);
anim_move_ball.attr("cy", y_one + y_zero - yy);
anim_rotate_square.attr("transform", translate + " " + rotate);
anim_size_ellipse.attr("ry", s * 80 + 40);
}, 25);
// ui continued
graph_ui.append("strong").text("Function: ");
var select_modifier = graph_ui.append("select");
select_modifier.append("option").text("in");
select_modifier.append("option").text("out");
select_modifier.append("option").text("in-out");
select_modifier.append("option").text("out-in");
graph_ui.append("strong").text("-")
var select_func = graph_ui.append("select")
var funcs = [];
for (var k in tweens)
{
if (k != "out" && k != "chain")
{
select_func.append("option").text(k);
funcs.push(k);
}
}
var change_tweenfunc = function()
{
var fname = funcs[select_func.node().selectedIndex];
var mod = select_modifier.node().selectedIndex;
tweenfunc = tweens[fname];
if (mod == 1) // out
tweenfunc = tweens.out(tweenfunc);
else if (mod == 2) // in-out
tweenfunc = tweens.chain(tweenfunc, tweens.out(tweenfunc));
else if (mod == 3) // out-in
tweenfunc = tweens.chain(tweens.out(tweenfunc), tweenfunc);
// update curve
graph_curve.attr("d", curve(d3.range(0,1.05,.005)))
// update label
if (mod != 0)
graph_label.text((['in','out','in-out','out-in'])[mod] + "-" + fname);
else
graph_label.text(fname);
}
select_func.on("change", change_tweenfunc);
select_modifier.on("change", change_tweenfunc);
})();
hump.camera
===========
::
Camera = require "hump.camera"
A camera utility for LÖVE. A camera can "look" at a position. It can zoom in
and out and it can rotate it's view. In the background, this is done by
actually moving, scaling and rotating everything in the game world. But don't
worry about that.
**Example**::
function love.load()
camera = Camera(player.pos.x, player.pos.y)
end
function love.update(dt)
local dx,dy = player.x - cam.x, player.y - cam.y
camera:move(dx/2, dy/2)
end
Function Reference
------------------
.. function:: Camera.new(x,y, zoom, rot)
:param numbers x,y: Point for the camera to look at. (optional)
:param number zoom: Camera zoom. (optional)
:param number rot: Camera rotation in radians. (optional)
:returns: A new camera.
Creates a new camera. You can access the camera position using ``camera.x,
camera.y``, the zoom using ``camera.scale`` and the rotation using ``camera.rot``.
The module variable name can be used at a shortcut to ``new()``.
**Example**::
camera = require 'hump.camera'
-- camera looking at (100,100) with zoom 2 and rotated by 45 degrees
cam = camera(100,100, 2, math.pi/2)
.. function:: camera:move(dx,dy)
:param numbers dx,dy: Direction to move the camera.
:returns: The camera.
Move the camera *by* some vector. To set the position, use
:func:`camera:lookAt`.
This function is shortcut to ``camera.x,camera.y = camera.x+dx, camera.y+dy``.
**Examples**::
function love.update(dt)
camera:move(dt * 5, dt * 6)
end
::
function love.update(dt)
camera:move(dt * 5, dt * 6):rotate(dt)
end
.. function:: camera:lookAt(x,y)
:param numbers x,y: Position to look at.
:returns: The camera.
Let the camera look at a point. In other words, it sets the camera position. To
move the camera *by* some amount, use :func:`camera:move`.
This function is shortcut to ``camera.x,camera.y = x, y``.
**Examples**::
function love.update(dt)
camera:lookAt(player.pos:unpack())
end
::
function love.update(dt)
camera:lookAt(player.pos:unpack()):rotate(player.rot)
end
.. function:: camera:position()
:returns: ``x,y`` -- Camera position.
Returns ``camera.x, camera.y``.
**Example**::
-- let the camera fly!
local cam_dx, cam_dy = 0, 0
function love.mousereleased(x,y)
local cx,cy = camera:position()
dx, dy = x-cx, y-cy
end
function love.update(dt)
camera:move(dx * dt, dy * dt)
end
.. function:: camera:rotate(angle)
:param number angle: Rotation angle in radians
:returns: The camera.
Rotate the camera by some angle. To set the angle use :func:`camera:rotateTo`.
This function is shortcut to ``camera.rot = camera.rot + angle``.
**Examples**::
function love.update(dt)
camera:rotate(dt)
end
::
function love.update(dt)
camera:rotate(dt):move(dt,dt)
end
.. function:: camera:rotateTo(angle)
:param number angle: Rotation angle in radians
:returns: The camera.
Set rotation: ``camera.rot = angle``.
**Example**::
camera:rotateTo(math.pi/2)
.. function:: camera:zoom(mul)
:param number mul: Zoom change. Should be > 0.
:returns: The camera.
*Multiply* zoom: ``camera.scale = camera.scale * mul``.
**Examples**::
camera:zoom(2) -- make everything twice as big
::
camera:zoom(0.5) -- ... and back to normal
::
camera:zoom(-1) -- mirror and flip everything upside down
.. function:: camera:zoomTo(zoom)
:param number zoom: New zoom.
:returns: The camera.
Set zoom: ``camera.scale = zoom``.
**Example**::
camera:zoomTo(1) -- reset zoom
.. function:: camera:attach()
Start looking through the camera.
Apply camera transformations, i.e. move, scale and rotate everything until
``camera:detach()`` as if looking through the camera.
**Example**::
function love.draw()
camera:attach()
draw_world()
camera:detach()
draw_hud()
end
.. function:: camera:detach()
Stop looking through the camera.
**Example**::
function love.draw()
camera:attach()
draw_world()
camera:detach()
draw_hud()
end
.. function:: camera:draw(func)
:param function func: Drawing function to be wrapped.
Wrap a function between a ``camera:attach()``/``camera:detach()`` pair.
Equivalent to::
camera:attach()
func()
camera:detach()
**Example**::
function love.draw()
camera:draw(draw_world)
draw_hud()
end
.. function:: camera:worldCoords(x, y)
:param numbers x, y: Point to transform.
:returns: ``x,y`` -- Transformed point.
Because a camera has a point it looks at, a rotation and a zoom factor, it
defines a coordinate system. A point now has two sets of coordinates: One
defines where the point is to be found in the game world, and the other
describes the position on the computer screen. The first set of coordinates is
called world coordinates, the second one camera coordinates. Sometimes it is
needed to convert between the two coordinate systems, for example to get the
position of a mouse click in the game world in a strategy game, or to see if an
object is visible on the screen.
:func:`camera:worldCoords` and :func:`camera:cameraCoords` transform points
between these two coordinate systems.
**Example**::
x,y = camera:worldCoords(love.mouse.getPosition())
selectedUnit:plotPath(x,y)
.. function:: camera:cameraCoords(x, y)
:param numbers x, y: Point to transform.
:returns: ``x,y`` -- Transformed point.
Because a camera has a point it looks at, a rotation and a zoom factor, it
defines a coordinate system. A point now has two sets of coordinates: One
defines where the point is to be found in the game world, and the other
describes the position on the computer screen. The first set of coordinates is
called world coordinates, the second one camera coordinates. Sometimes it is
needed to convert between the two coordinate systems, for example to get the
position of a mouse click in the game world in a strategy game, or to see if an
object is visible on the screen.
:func:`camera:worldCoords` and :func:`camera:cameraCoords` transform points
between these two coordinate systems.
**Example**::
x,y = camera:cameraCoords(player.pos.x, player.pos.y)
love.graphics.line(x, y, love.mouse.getPosition())
.. function:: camera:mousePosition()
:returns: Mouse position in world coordinates.
Shortcut to ``camera:worldCoords(love.mouse.getPosition())``.
**Example**::
x,y = camera:mousePosition()
selectedUnit:plotPath(x,y)
Camera Movement Control
-----------------------
Camera movement is one of these things that go almost unnoticed when done well,
but add a lot to the overall experience.
The article `Scroll Back: The Theory and Practice of Cameras in SideScrollers
<http://gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_Practice_of_Cameras_in_SideScrollers.php>`_
by Itay Keren gives a lot of insight into how to design good camera systems.
**hump.camera** offers functions that help to implement most of the techniques
discussed in the article. The functions :func:`camera:lockX`,
:func:`camera:lockY`, :func:`camera:lockPosition`, and :func:`camera:lockWindow`
move the camera so that the interesting content stays in frame.
Note that the functions must be called every frame::
function love.update()
-- vertical locking
camera:lockX(player.pos.x)
end
All movements are subject to smoothing (see :ref:`Movement Smoothers
<movement-smoothers>`).
You can specify a default movement smoother by assigning the variable
:attr:`camera.smoother`::
cam.smoother = Camera.smooth.linear(100)
.. function:: camera:lockX(x, smoother, ...)
:param number x: X coordinate (in world coordinates) to lock to.
:param function smoother: Movement smoothing override. (optional)
:param mixed ...: Additional parameters to the smoothing function. (optional)
Horizontal camera locking: Keep the camera locked on the defined ``x``-position
(in *world coordinates*). The ``y``-position is not affected.
You can define an off-center locking position by "aiming" the camera left or
right of your actual target. For example, to center the player 20 pixels to the
*left* of the screen, aim 20 pixels to it's *right* (see examples).
**Examples**::
-- lock on player vertically
camera:lockX(player.x)
::
-- ... with linear smoothing at 25 px/s
camera:lockX(player.x, Camera.smooth.linear(25))
::
-- lock player 20px left of center
camera:lockX(player.x + 20)
.. function:: camera:lockY(y, smoother, ...)
:param number y: Y coordinate (in world coordinates) to lock to.
:param function smoother: Movement smoothing override. (optional)
:param mixed ...: Additional parameters to the smoothing function. (optional)
Vertical camera locking: Keep the camera locked on the defined ``y``-position
(in *world coordinates*). The ``x``-position is not affected.
You can define an off-center locking position by "aiming" the camera above or
below your actual target. For example, to center the player 20 pixels *below* the
screen center, aim 20 pixels *above* it (see examples).
**Examples**::
-- lock on player horizontally
camera:lockY(player.y)
::
-- ... with damped smoothing with a stiffness of 10
camera:lockY(player.y, Camera.smooth.damped(10))
::
-- lock player 20px below the screen center
camera:lockY(player.y - 20)
.. function:: camera:lockPosition(x,y, smoother, ...)
:param numbers x,y: Position (in world coordinates) to lock to.
:param function smoother: Movement smoothing override. (optional)
:param mixed ...: Additional parameters to the smoothing function. (optional)
Horizontal and vertical camera locking: Keep the camera locked on the defined
position (in *world coordinates*).
You can define an off-center locking position by "aiming" the camera to the
opposite direction away from your real target.
For example, to center the player 10 pixels to the *left* and 20 pixels *above*
the screen center, aim 10 pixels to the *right* and 20 pixels *below*.
**Examples**::
-- lock on player
camera:lockPosition(player.x, player.y)
::
-- lock 50 pixels into player's aiming direction
camera:lockPosition(player.x - player.aiming.x * 50, player.y - player.aiming.y * 50)
.. function:: camera:lockWindow(x,y, x_min, x_max, y_min, y_max, smoother, ...)
:param numbers x,y: Position (in world coordinates) to lock to.
:param numbers x_min: Upper left X coordinate of the camera window *(in camera coordinates!)*.
:param numbers x_max: Lower right X coordinate of the camera window *(in camera coordinates!)*.
:param numbers y_min: Upper left Y coordinate of the camera window *(in camera coordinates!)*.
:param numbers y_max: Lower right Y coordinate of the camera window *(in camera coordinates!)*.
:param function smoother: Movement smoothing override. (optional)
:param mixed ...: Additional parameters to the smoothing function. (optional)
The most powerful locking method: Lock camera to ``x,y``, but only move the
camera if the position would be out of the screen-rectangle defined by ``x_min``,
``x_max``, ``y_min``, ``y_max``.
.. note::
The locking window is defined in camera coordinates, whereas the position to
lock to is defined in world coordinates!
All of the other locking methods can be implemented by window locking. For
position locking, set ``x_min = x_max`` and ``y_min = y_max``.
Off-center locking can be done by defining the locking window accordingly.
**Examples**::
-- lock on player
camera:lock(player.x, player.y)
.. attribute:: camera.smoother
The default smoothing operator. Must be a ``function`` with the following
prototype::
function customSmoother(dx,dy, ...)
do_stuff()
return new_dx,new_dy
end
where ``dx,dy`` is the offset the camera would move before smoothing and
``new_dx, new_dy`` is the offset the camera should move after smoothing.
.. _movement-smoothers:
Movement Smoothers
^^^^^^^^^^^^^^^^^^
It is not always desirable that the camera instantly locks on a target.
`Platform snapping
<http://gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_Practice_of_Cameras_in_SideScrollers.php#h.rncuomopycy0>`_,
for example, would look terrible if the camera would instantly jump to the
focussed platform.
Smoothly moving the camera to the locked position can also give the illusion of
a camera operator an add to the overall feel of your game.
**hump.camera** allows to smooth the movement by either passing movement
smoother functions to the locking functions or by setting a default smoother
(see :attr:`camera.smoother`).
Smoothing functions must have the following prototype::
function customSmoother(dx,dy, ...)
do_stuff()
return new_dx,new_dy
end
where ``dx,dy`` is the offset the camera would move before smoothing and
``new_dx, new_dy`` is the offset the camera should move after smoothing.
This is a simple "rubber-band" smoother::
function rubber_band(dx,dy)
local dt = love.timer.getDelta()
return dx*dt, dy*dt
end
**hump.camera** defines generators for the most common smoothers:
.. function:: Camera.smooth.none()
:returns: Smoothing function.
Dummy smoother: does not smooth the motion.
**Example**::
cam.smoother = Camera.smooth.none()
.. function:: Camera.smooth.linear(speed)
:param number speed: Smoothing speed.
:returns: Smoothing function.
Smoothly moves the camera towards to snapping goal with constant speed.
**Examples**::
cam.smoother = Camera.smooth.linear(100)
::
-- warning: creates a function every frame!
camera:lockX(player.x, Camera.smooth.linear(25))
.. function:: Camera.smooth.damped(stiffness)
:param number stiffness: Speed of the camera movement.
:returns: Smoothing function.
Smoothly moves the camera towards the goal with a speed proportional to the
distance to the target.
Stiffness defines the speed of the motion: Higher values mean that the camera
moves more quickly.
**Examples**::
cam.smoother = Camera.smooth.damped(10)
::
-- warning: creates a function every frame!
camera:lockPosition(player.x, player.y, Camera.smooth.damped(2))
hump.class
==========
::
Class = require "hump.class"
A small, fast class/prototype implementation with multiple inheritance.
Implements `class commons <https://github.com/bartbes/Class-Commons>`_.
**Example**::
Critter = Class{
init = function(self, pos, img)
self.pos = pos
self.img = img
end,
speed = 5
}
function Critter:update(dt, player)
-- see hump.vector
local dir = (player.pos - self.pos):normalize_inplace()
self.pos = self.pos + dir * Critter.speed * dt
end
function Critter:draw()
love.graphics.draw(self.img, self.pos.x, self.pos.y)
end
Function Reference
------------------
.. function:: Class.new()
.. function:: Class.new({init = constructor, __includes = parents, ...})
:param function constructor: Class constructor. Can be accessed with ``theclass.init(object, ...)``. (optional)
:param class or table of classes parents: Classes to inherit from. Can either be a single class or a table of classes. (optional)
:param mixed ...: Any other fields or methods common to all instances of this class. (optional)
:returns: The class.
Declare a new class.
``init()`` will receive the new object instance as first argument. Any other
arguments will also be forwarded (see examples), i.e. ``init()`` has the
following signature::
function init(self, ...)
If you do not specify a constructor, an empty constructor will be used instead.
The name of the variable that holds the module can be used as a shortcut to
``new()`` (see example).
**Examples**::
Class = require 'hump.class' -- `Class' is now a shortcut to new()
-- define a class class
Feline = Class{
init = function(self, size, weight)
self.size = size
self.weight = weight
end;
-- define a method
stats = function(self)
return string.format("size: %.02f, weight: %.02f", self.size, self.weight)
end;
}
-- create two objects
garfield = Feline(.7, 45)
felix = Feline(.8, 12)
print("Garfield: " .. garfield:stats(), "Felix: " .. felix:stats())
::
Class = require 'hump.class'
-- same as above, but with 'external' function definitions
Feline = Class{}
function Feline:init(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)
print(Feline, garfield)
::
Class = require 'hump.class'
A = Class{
foo = function() print('foo') end
}
B = Class{
bar = function() print('bar') end
}
-- single inheritance
C = Class{__includes = A}
instance = C()
instance:foo() -- prints 'foo'
instance:bar() -- error: function not defined
-- multiple inheritance
D = Class{__includes = {A,B}}
instance = D()
instance:foo() -- prints 'foo'
instance:bar() -- prints 'bar'
::
-- class attributes are shared across instances
A = Class{ foo = 'foo' } -- foo is a class attribute/static member
one, two, three = A(), A(), A()
print(one.foo, two.foo, three.foo) --> prints 'foo foo foo'
one.foo = 'bar' -- overwrite/specify for instance `one' only
print(one.foo, two.foo, three.foo) --> prints 'bar foo foo'
A.foo = 'baz' -- overwrite for all instances without specification
print(one.foo, two.foo, three.foo) --> prints 'bar baz baz'
.. function:: class.init(object, ...)
:param Object object: The object. Usually ``self``.
:param mixed ...: Arguments to pass to the constructor.
:returns: Whatever the parent class constructor returns.
Calls class constructor of a class on an object.
Derived classes should use this function their constructors to initialize the
parent class(es) portions of the object.
**Example**::
Class = require 'hump.class'
Shape = Class{
init = function(self, area)
self.area = area
end;
__tostring = function(self)
return "area = " .. self.area
end
}
Rectangle = Class{__includes = Shape,
init = function(self, width, height)
Shape.init(self, width * height)
self.width = width
self.height = height
end;
__tostring = function(self)
local strs = {
"width = " .. self.width,
"height = " .. self.height,
Shape.__tostring(self)
}
return table.concat(strs, ", ")
end
}
print( Rectangle(2,4) ) -- prints 'width = 2, height = 4, area = 8'
.. function:: Class:include(other)
:param tables other: Parent classes/mixins.
:returns: The class.
Inherit functions and variables of another class, but only if they are not
already defined. This is done by (deeply) copying the functions and variables
over to the subclass.
.. note::
``class:include()`` doesn't actually care if the arguments supplied are
hump classes. Just any table will work.
.. note::
You can use ``Class.include(a, b)`` to copy any fields from table ``a``
to table ``b`` (see second example).
**Examples**::
Class = require 'hump.class'
Entity = Class{
init = function(self)
GameObjects.register(self)
end
}
Collidable = {
dispatch_collision = function(self, other, dx, dy)
if self.collision_handler[other.type])
return collision_handler[other.type](self, other, dx, dy)
end
return collision_handler["*"](self, other, dx, dy)
end,
collision_handler = {["*"] = function() end},
}
Spaceship = Class{
init = function(self)
self.type = "Spaceship"
-- ...
end
}
-- make Spaceship collidable
Spaceship:include(Collidable)
Spaceship.collision_handler["Spaceship"] = function(self, other, dx, dy)
-- ...
end
::
-- using Class.include()
Class = require 'hump.class'
a = {
foo = 'bar',
bar = {one = 1, two = 2, three = 3},
baz = function() print('baz') end,
}
b = {
foo = 'nothing to see here...'
}
Class.include(b, a) -- copy values from a to b
-- note that neither a nor b are hump classes!
print(a.foo, b.foo) -- prints 'bar nothing to see here...'
b.baz() -- prints 'baz'
b.bar.one = 10 -- changes only values in b
print(a.bar.one, b.bar.one) -- prints '1 10'
.. function:: class:clone()
:returns: A deep copy of the class/table.
Create a clone/deep copy of the class.
.. note::
You can use ``Class.clone(a)`` to create a deep copy of any table (see
second example).
**Examples**::
Class = require 'hump.class'
point = Class{ x = 0, y = 0 }
a = point:clone()
a.x, a.y = 10, 10
print(a.x, a.y) --> prints '10 10'
b = point:clone()
print(b.x, b.y) --> prints '0 0'
c = a:clone()
print(c.x, c.y) --> prints '10 10'
::
-- using Class.clone() to copy tables
Class = require 'hump.class'
a = {
foo = 'bar',
bar = {one = 1, two = 2, three = 3},
baz = function() print('baz') end,
}
b = Class.clone(a)
b.baz() -- prints 'baz'
b.bar.one = 10
print(a.bar.one, b.bar.one) -- prints '1 10'
Caveats
-------
Be careful when using metamethods like ``__add`` or ``__mul``: If a 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::
Class = require 'hump.class'
A = Class{init = function(self, x) self.x = x end}
function A:__add(other) return A(self.x + other.x) end
function A:show() print("A:", self.x) end
B = Class{init = function(self, x, y) A.init(self, x) self.y = y end}
function B:show() print("B:", self.x, self.y) end
function B:foo() print("foo") end
B:include(A)
one, two = B(1,2), B(3,4)
result = one + two -- result will be of type A, *not* B!
result:show() -- prints "A: 4"
result:foo() -- error: method does not exist
Note that while you can define the ``__index`` metamethod of the class, this is
not a good idea: It will break the class mechanism. To add a custom ``__index``
metamethod without breaking the class system, you have to use ``rawget()``. But
beware that this won't affect subclasses::
Class = require 'hump.class'
A = Class{}
function A:foo() print('bar') end
function A:__index(key)
print(key)
return rawget(A, key)
end
instance = A()
instance:foo() -- prints foo bar
B = Class{__includes = A}
instance = B()
instance:foo() -- prints only foo
# -*- coding: utf-8 -*-
#
# hump documentation build configuration file, created by
# sphinx-quickstart on Sat Oct 10 13:10:12 2015.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
import shlex
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.mathjax',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
# The encoding of source files.
#source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'hump'
copyright = u'2015, Matthias Richter'
author = u'Matthias Richter'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.0'
# The full version, including alpha/beta/rc tags.
release = '1.0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']
# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []
# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False
# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False
# -- Options for HTML output ----------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'alabaster'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# If false, no module index is generated.
#html_domain_indices = True
# If false, no index is generated.
#html_use_index = True
# If true, the index is split into individual pages for each letter.
#html_split_index = False
# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None
# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr'
#html_search_language = 'en'
# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
#html_search_options = {'type': 'default'}
# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#html_search_scorer = 'scorer.js'
# Output file base name for HTML help builder.
htmlhelp_basename = 'humpdoc'
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
# Latex figure (float) alignment
#'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'hump.tex', u'hump Documentation',
u'Matthias Richter', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# If true, show page references after internal links.
#latex_show_pagerefs = False
# If true, show URL addresses after external links.
#latex_show_urls = False
# Documents to append as an appendix to all manuals.
#latex_appendices = []
# If false, no module index is generated.
#latex_domain_indices = True
# -- Options for manual page output ---------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'hump', u'hump Documentation',
[author], 1)
]
# If true, show URL addresses after external links.
#man_show_urls = False
# -- Options for Texinfo output -------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'hump', u'hump Documentation',
author, 'hump', 'One line description of project.',
'Miscellaneous'),
]
# Documents to append as an appendix to all manuals.
#texinfo_appendices = []
# If false, no module index is generated.
#texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'
# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False
primary_domain = "js"
highlight_language = "lua"
import sphinx_rtd_theme
html_theme = 'sphinx_rtd_theme'
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
hump.gamestate
==============
::
Gamestate = require "hump.gamestate"
A gamestate encapsulates independent data an behaviour in a single table.
A typical game could consist of a menu-state, a level-state and a game-over-state.
**Example**::
local menu = {} -- previously: Gamestate.new()
local game = {}
function menu:draw()
love.graphics.print("Press Enter to continue", 10, 10)
end
function menu:keyreleased(key, code)
if key == 'return' then
Gamestate.switch(game)
end
end
function game:enter()
Entities.clear()
-- setup entities here
end
function game:update(dt)
Entities.update(dt)
end
function game:draw()
Entities.draw()
end
function love.load()
Gamestate.registerEvents()
Gamestate.switch(menu)
end
.. _callbacks:
Gamestate Callbacks
-------------------
A gamestate can define all callbacks that LÖVE defines. In addition, there are
callbacks for initalizing, entering and leaving a state:
``init()``
Called once, and only once, before entering the state the first time. See
:func:`Gamestate.switch`.
``enter(previous, ...)``
Called every time when entering the state. See :func:`Gamestate.switch`.
``leave()``
Called when leaving a state. See :func:`Gamestate.switch` and :func:`Gamestate.pop`.
``resume()``
Called when re-entering a state by :func:`Gamestate.pop`-ing another state.
``update()``
Update the game state. Called every frame.
``draw()``
Draw on the screen. Called every frame.
``focus()``
Called if the window gets or looses focus.
``keypressed()``
Triggered when a key is pressed.
``keyreleased()``
Triggered when a key is released.
``mousepressed()``
Triggered when a mouse button is pressed.
``mousereleased()``
Triggered when a mouse button is released.
``joystickpressed()``
Triggered when a joystick button is pressed.
``joystickreleased()``
Triggered when a joystick button is released.
``quit()``
Called on quitting the game. Only called on the active gamestate.
When using :func:`Gamestate.registerEvents`, all these callbacks will be called by the
corresponding LÖVE callbacks and receive receive the same arguments (e.g.
``state:update(dt)`` will be called by ``love.update(dt)``).
**Example**::
menu = {} -- previously: Gamestate.new()
function menu:init()
self.background = love.graphics.newImage('bg.jpg')
Buttons.initialize()
end
function menu:enter(previous) -- runs every time the state is entered
Buttons.setActive(Buttons.start)
end
function menu:update(dt) -- runs every frame
Buttons.update(dt)
end
function menu:draw()
love.graphics.draw(self.background, 0, 0)
Buttons.draw()
end
function menu:keyreleased(key)
if key == 'up' then
Buttons.selectPrevious()
elseif key == 'down' then
Buttons.selectNext()
elseif
Buttons.active:onClick()
end
end
function menu:mousereleased(x,y, mouse_btn)
local button = Buttons.hovered(x,y)
if button then
Button.select(button)
if mouse_btn == 'l' then
button:onClick()
end
end
end
Function Reference
------------------
.. function:: Gamestate.new()
:returns: An empty table.
**Deprecated: Use the table constructor instead (see example)**
Declare a new gamestate (just an empty table). A gamestate can define several
callbacks.
**Example**::
menu = {}
-- deprecated method:
menu = Gamestate.new()
.. function:: Gamestate.switch(to, ...)
:param Gamestate to: Target gamestate.
:param mixed ...: Additional arguments to pass to ``to:enter(current, ...)``.
:returns: The results of ``to:enter(current, ...)``.
Switch to a gamestate, with any additional arguments passed to the new state.
Switching a gamestate will call the ``leave()`` callback on the current
gamestate, replace the current gamestate with ``to``, call the ``init()`` function
if, and only if, the state was not yet inialized and finally call
``enter(old_state, ...)`` on the new gamestate.
.. note::
Processing of callbacks is suspended until ``update()`` is called on the new
gamestate, but the function calling :func:`Gamestate.switch` can still continue - it is
your job to make sure this is handled correctly. See also the examples below.
**Examples**::
Gamestate.switch(game, level_two)
::
-- stop execution of the current state by using return
if player.has_died then
return Gamestate.switch(game, level_two)
end
-- this will not be called when the state is switched
player:update()
.. function:: Gamestate.Gamestate.current()
:returns: The active gamestate.
Returns the currently activated gamestate.
**Example**::
function love.keypressed(key)
if Gamestate.current() ~= menu and key == 'p' then
Gamestate.push(pause)
end
end
.. function:: Gamestate.push(to, ...)
:param Gamestate to: Target gamestate.
:param mixed ...: Additional arguments to pass to ``to:enter(current, ...)``.
:returns: The results of ``to:enter(current, ...)``.
Pushes the ``to`` on top of the state stack, i.e. makes it the active state.
Semantics are the same as ``switch(to, ...)``, except that ``leave()`` is *not*
called on the previously active state.
Useful for pause screens, menus, etc.
.. note::
Processing of callbacks is suspended until ``update()`` is called on the
new gamestate, but the function calling ``GS.push()`` can still continue -
it is your job to make sure this is handled correctly. See also the
example below.
**Example**::
-- pause gamestate
Pause = Gamestate.new()
function Pause:enter(from)
self.from = from -- record previous state
end
function Pause:draw()
local W, H = love.graphics.getWidth(), love.graphics.getHeight()
-- draw previous screen
self.from:draw()
-- overlay with pause message
love.graphics.setColor(0,0,0, 100)
love.graphics.rectangle('fill', 0,0, W,H)
love.graphics.setColor(255,255,255)
love.graphics.printf('PAUSE', 0, H/2, W, 'center')
end
-- [...]
function love.keypressed(key)
if Gamestate.current() ~= menu and key == 'p' then
return Gamestate.push(pause)
end
end
.. function:: Gamestate.pop(...)
:returns: The results of ``new_state:resume(...)``.
Calls ``leave()`` on the current state and then removes it from the stack, making
the state below the current state and calls ``resume(...)`` on the activated state.
Does *not* call ``enter()`` on the activated state.
.. note::
Processing of callbacks is suspended until ``update()`` is called on the
new gamestate, but the function calling ``GS.pop()`` can still continue -
it is your job to make sure this is handled correctly. See also the
example below.
**Example**::
-- extending the example of Gamestate.push() above
function Pause:keypressed(key)
if key == 'p' then
return Gamestate.pop() -- return to previous state
end
end
.. function:: Gamestate.<callback>(...)
:param mixed ...: Arguments to pass to the corresponding function.
:returns: The result of the callback function.
Calls a function on the current gamestate. Can be any function, but is intended
to be one of the :ref:`callbacks`. Mostly useful when not using
:func:`Gamestate.registerEvents`.
**Example**::
function love.draw()
Gamestate.draw() -- <callback> is `draw'
end
function love.update(dt)
Gamestate.update(dt) -- pass dt to currentState:update(dt)
end
function love.keypressed(key, code)
Gamestate.keypressed(key, code) -- pass multiple arguments
end
.. function:: Gamestate.registerEvents([callbacks])
:param table callbacks: Names of the callbacks to register. If omitted,
register all love callbacks (optional).
Overwrite love callbacks to call ``Gamestate.update()``, ``Gamestate.draw()``,
etc. automatically. ``love`` callbacks (e.g. ``love.update()``) are still
invoked as usual.
This is by done by overwriting the love callbacks, e.g.::
local old_update = love.update
function love.update(dt)
old_update(dt)
return Gamestate.current:update(dt)
end
.. note::
Only works when called in love.load() or any other function that is
executed *after* the whole file is loaded.
**Examples**::
function love.load()
Gamestate.registerEvents()
Gamestate.switch(menu)
end
-- love callback will still be invoked
function love.update(dt)
Timer.update(dt)
-- no need for Gamestate.update(dt)
end
::
function love.load()
-- only register draw, update and quit
Gamestate.registerEvents{'draw', 'update', 'quit'}
Gamestate.switch(menu)
end
**hump** - Helper Utilities for a Multitude of Problems
=======================================================
**hump** is a set of lightweight helpers for the awesome `LÖVE
<http://love2d.org>`_ game framework. It will help to get you over the initial
hump when starting to build a new game.
**hump** differs from many other libraries in that every component is
independent of the remaining ones.
The footprint is very small, so the library should fit nicely into your
projects.
Read on
-------
.. toctree::
:maxdepth: 2
hump.gamestate <gamestate>
hump.timer <timer>
hump.vector <vector>
hump.vector-light <vector-light>
hump.class <class>
hump.signal <signal>
hump.camera <camera>
license
Get hump
--------
You can view and download the individual modules on github: `vrld/hump
<http://github.com/vrld/hump>`_.
You may also download the whole packed sourcecode either in the `zip
<http://github.com/vrld/hump/zipball/master>`_ or `tar
<http://github.com/vrld/hump/tarball/master>`_ format.
Using `Git <http://git-scm.com>`_, you can clone the project by running:
git clone git://github.com/vrld/hump
Once done, tou can check for updates by running
git pull
Indices and tables
------------------
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
License
=======
Copyright (c) 2011-2015 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.
hump.signal
===========
::
Signal = require 'hump.signal'
A simple yet effective implementation of `Signals and Slots
<http://en.wikipedia.org/wiki/Signals_and_slots>`_, aka the `Observer pattern
<http://en.wikipedia.org/wiki/Observer_pattern>`_: Functions can be dynamically
bound to signals. When a *signal* is *emitted*, all registered functions will
be invoked. Simple as that.
``hump.signal`` makes things a little more interesing by allowing to emit all
signals that match a `Lua string pattern
<http://www.lua.org/manual/5.1/manual.html#5.4.1>`_.
**Example**::
-- in AI.lua
Signal.register('shoot', function(x,y, dx,dy)
-- for every critter in the path of the bullet:
-- try to avoid being hit
for critter in pairs(critters) do
if critter:intersectsRay(x,y, dx,dy) then
critter:setMoveDirection(-dy, dx)
end
end
end)
-- in sounds.lua
Signal.register('shoot', function()
Sounds.fire_bullet:play()
end)
-- in main.lua
function love.keypressed(key)
if key == ' ' then
local x,y = player.pos:unpack()
local dx,dy = player.direction:unpack()
Signal.emit('shoot', x,y, dx,dy)
end
end
Function Reference
------------------
.. function:: Signal.new()
:returns: A new signal registry.
Creates a new signal registry that is independent of the default registry: It
will manage it's own list of signals and does not in any way affect the the
global registry. Likewise, the global registry does not affect the instance.
.. note::
If you don't need multiple independent registries, you can use the
global/default registry (see examples).
.. note::
Unlike the default one, signal registry instances use the colon-syntax,
i.e., you need to call ``instance:emit('foo', 23)`` instead of
``Signal.mit('foo', 23)``.
**Example**::
player.signals = Signal.new()
.. function:: Signal.register(s, f)
:param string s: The signal identifier.
:param function f: The function to register.
:returns: A function handle to use in :func:`Signal.remove()`.
Registers a function ``f`` to be called when signal ``s`` is emitted.
**Examples**::
Signal.register('level-complete', function() self.fanfare:play() end)
::
handle = Signal.register('level-load', function(level) level.show_help() end)
::
menu:register('key-left', select_previous_item)
.. function:: Signal.emit(s, ...)
:param string s: The signal identifier.
:param mixed ...: Arguments to pass to the bound functions. (optional)
Calls all functions bound to signal ``s`` with the supplied arguments.
**Examples**::
function love.keypressed(key)
-- using a signal instance
if key == 'left' then menu:emit('key-left') end
end
::
if level.is_finished() then
-- adding arguments
Signal.emit('level-load', level.next_level)
end
.. function:: Signal.remove(s, ...)
:param string s: The signal identifier.
:param functions ...: Functions to unbind from the signal.
Unbinds (removes) functions from signal ``s``.
**Example**::
Signal.remove('level-load', handle)
.. function:: Signal.clear(s)
:param string s: The signal identifier.
Removes all functions from signal ``s``.
**Example**::
Signal.clear('key-left')
.. function:: Signal.emitPattern(p, ...)
:param string p: The signal identifier pattern.
:param mixed ...: Arguments to pass to the bound functions. (optional)
Emits all signals that match a `Lua string pattern
<http://www.lua.org/manual/5.1/manual.html#5.4.1>`_.
**Example**::
-- emit all update signals
Signal.emitPattern('^update%-.*', dt)
.. function:: Signal.removePattern(p, ...)
:param string p: The signal identifier pattern.
:param functions ...: Functions to unbind from the signals.
Removes functions from all signals that match a `Lua string pattern
<http://www.lua.org/manual/5.1/manual.html#5.4.1>`_.
**Example**::
Signal.removePattern('key%-.*', play_click_sound)
.. function:: Signal.clearPattern(p)
:param string p: The signal identifier pattern.
Removes **all** functions from all signals that match a `Lua string pattern
<http://www.lua.org/manual/5.1/manual.html#5.4.1>`_.
**Examples**::
Signal.clearPattern('sound%-.*')
::
player.signals:clearPattern('.*') -- clear all signals
hump.timer
==========
::
Timer = require "hump.timer"
hump.timer offers a simple interface to schedule the execution of functions. It
is possible to run functions *after* and *for* some amount of time. For
example, a timer could be set to move critters every 5 seconds or to make the
player invincible for a short amount of time.
In addition to that, ``hump.timer`` offers various `tweening
<http://en.wikipedia.org/wiki/Inbetweening>`_ functions that make it
easier to produce `juicy games <http://www.youtube.com/watch?v=Fy0aCDmgnxg>`_.
**Example**::
function love.keypressed(key)
if key == ' ' then
Timer.after(1, function() print("Hello, world!") end)
end
end
function love.update(dt)
Timer.update(dt)
end
Function Reference
------------------
.. function:: Timer.new()
:returns: A timer instance.
Creates a new timer instance that is independent of the global timer: It will
manage it's own list of scheduled functions and does not in any way affect the
the global timer. Likewise, the global timer does not affect timer instances.
.. note::
If you don't need multiple independent schedulers, you can use the
global/default timer (see examples).
.. note::
Unlike the default timer, timer instances use the colon-syntax, i.e.,
you need to call ``instance:after(1, foo)`` instead of ``Timer.after(1,
foo)``.
**Example**::
menuTimer = Timer.new()
.. function:: Timer.after(delay, func)
:param number delay: Number of seconds the function will be delayed.
:param function func: The function to be delayed.
:returns: The timer handle. See also :func:`Timer.cancel`.
Schedule a function. The function will be executed after ``delay`` seconds have
elapsed, given that ``update(dt)`` is called every frame.
.. note::
There is no guarantee that the delay will not be exceeded, it is only
guaranteed that the function will *not* be executed *before* the delay has
passed.
``func`` will receive itself as only parameter. This is useful to implement
periodic behavior (see the example).
**Examples**::
-- grant the player 5 seconds of immortality
player.isInvincible = true
Timer.after(5, function() player.isInvincible = false end)
::
-- print "foo" every second. See also every()
Timer.after(1, function(func) print("foo") Timer.after(1, func) end)
::
--Using a timer instance:
menuTimer:after(1, finishAnimation)
.. function:: Timer.script(func)
:param function func: Script to execute.
Execute a function that can be paused without causing the rest of the program to
be suspended. ``func`` will receive a function - ``wait`` - to do interrupt the
script (but not the whole program) as only argument. The function prototype of
wait is: ``wait(delay)``.
**Examples**::
Timer.script(function(wait)
print("Now")
wait(1)
print("After one second")
wait(1)
print("Bye!")
end)
::
-- useful for splash screens
Timer.script(function(wait)
Timer.tween(0.5, splash.pos, {x = 300}, 'in-out-quad')
wait(5) -- show the splash for 5 seconds
Timer.tween(0.5, slpash.pos, {x = 800}, 'in-out-quad')
end)
::
-- repeat something with a varying delay
Timer.script(function(wait)
while true do
spawn_ship()
wait(1 / (1-production_speed))
end
end)
::
-- jumping with timer.script
self.timers:script(function(wait)
local w = 1/12
self.jumping = true
Timer.tween(w*2, self, {z = -8}, "out-cubic", function()
Timer.tween(w*2, self, {z = 0},"in-cubic")
end)
self.quad = self.quads.jump[1]
wait(w)
self.quad = self.quads.jump[2]
wait(w)
self.quad = self.quads.jump[3]
wait(w)
self.quad = self.quads.jump[4]
wait(w)
self.jumping = false
self.z = 0
end)
.. function:: Timer.every(delay, func[, count])
:param number delay: Number of seconds between two consecutive function calls.
:param function func: The function to be called periodically.
:param number count: Number of times the function is to be called (optional).
:returns: The timer handle. See also :func:`Timer.cancel`.
Add a function that will be called ``count`` times every ``delay`` seconds.
If ``count`` is omitted, the function will be called until it returns ``false``
or :func:`Timer.cancel` or :func:`Timer.clear` is called on the timer instance.
**Example**::
-- toggle light on and off every second
Timer.every(1, function() lamp:toggleLight() end)
::
-- launch 5 fighters in quick succession (using a timer instance)
mothership_timer:every(0.3, function() self:launchFighter() end, 5)
::
-- flicker player's image as long as he is invincible
Timer.every(0.1, function()
player:flipImage()
return player.isInvincible
end)
.. function:: Timer.during(delay, func[, after])
:param number delay: Number of seconds the func will be called.
:param function func: The function to be called on ``update(dt)``.
:param function after: A function to be called after delay seconds (optional).
:returns: The timer handle. See also :func:`Timer.cancel`.
Run ``func(dt)`` for the next ``delay`` seconds. The function is called every
time ``update(dt)`` is called. Optionally run ``after()`` once ``delay``
seconds have passed.
``after()`` will receive itself as only parameter.
.. note::
You should not add new timers in ``func(dt)``, as this can lead to random
crashes.
**Examples**::
-- play an animation for 5 seconds
Timer.during(5, function(dt) animation:update(dt) end)
::
-- shake the camera for one second
local orig_x, orig_y = camera:pos()
Timer.during(1, function()
camera:lookAt(orig_x + math.random(-2,2), orig_y + math.random(-2,2))
end, function()
-- reset camera position
camera:lookAt(orig_x, orig_y)
end)
::
player.isInvincible = true
-- flash player for 3 seconds
local t = 0
player.timer:during(3, function(dt)
t = t + dt
player.visible = (t % .2) < .1
end, function()
-- make sure the player is visible after three seconds
player.visible = true
player.isInvincible = false
end)
.. function:: Timer.cancel(handle)
:param table handle: The function to be canceled.
Prevent a timer from being executed in the future.
**Examples**::
function tick()
print('tick... tock...')
end
handle = Timer.every(1, tick)
-- later
Timer.cancel(handle) -- NOT: Timer.cancel(tick)
::
-- using a timer instance
function tick()
print('tick... tock...')
end
handle = menuTimer:every(1, tick)
-- later
menuTimer:cancel(handle)
.. function:: Timer.clear()
Remove all timed and periodic functions. Functions that have not yet been
executed will discarded.
**Examples**::
Timer.clear()
::
menuTimer:clear()
.. function:: Timer.update(dt)
:param number dt: Time that has passed since the last ``update()``.
Update timers and execute functions if the deadline is reached. Call in
``love.update(dt)``.
**Examples**::
function love.update(dt)
do_stuff()
Timer.update(dt)
end
::
-- using hump.gamestate and a timer instance
function menuState:update(dt)
self.timers:update(dt)
end
.. function:: Timer.tween(duration, subject, target, method, after, ...)
:param number duration: Duration of the tween.
:param table subject: Object to be tweened.
:param table target: Target values.
:param string method: Tweening method, defaults to 'linear' (:ref:`see here
<tweening-methods>`, optional).
:param function after: Function to execute after the tween has finished
(optiona).
:param mixed ...: Additional arguments to the *tweening* function.
:returns: A timer handle.
`Tweening <http://en.wikipedia.org/wiki/Inbetweening>`_ (short for
in-betweening) is the process that happens between two defined states. For
example, a tween can be used to gradually fade out a graphic or move a text
message to the center of the screen. For more information why tweening should
be important to you, check out this great talk on `juicy games
<http://www.youtube.com/watch?v=Fy0aCDmgnxg>`_.
``hump.timer`` offers two interfaces for tweening: the low-level
:func:`Timer.during` and the higher level interface :func:`Timer.tween`.
To see which tweening methods hump offers, :ref:`see below <tweening-methods>`.
**Examples**::
function love.load()
color = {0, 0, 0}
Timer.tween(10, color, {255, 255, 255}, 'in-out-quad')
end
function love.update(dt)
Timer.update(dt)
end
function love.draw()
love.graphics.setBackgroundColor(color)
end
::
function love.load()
circle = {rad = 10, pos = {x = 400, y = 300}}
-- multiple tweens can work on the same subject
-- and nested values can be tweened, too
Timer.tween(5, circle, {rad = 50}, 'in-out-quad')
Timer.tween(2, circle, {pos = {y = 550}}, 'out-bounce')
end
function love.update(dt)
Timer.update(dt)
end
function love.draw()
love.graphics.circle('fill', circle.pos.x, circle.pos.y, circle.rad)
end
::
function love.load()
-- repeated tweening
circle = {rad = 10, x = 100, y = 100}
local grow, shrink, move_down, move_up
grow = function()
Timer.tween(1, circle, {rad = 50}, 'in-out-quad', shrink)
end
shrink = function()
Timer.tween(2, circle, {rad = 10}, 'in-out-quad', grow)
end
move_down = function()
Timer.tween(3, circle, {x = 700, y = 500}, 'bounce', move_up)
end
move_up = function()
Timer.tween(5, circle, {x = 200, y = 200}, 'out-elastic', move_down)
end
grow()
move_down()
end
function love.update(dt)
Timer.update(dt)
end
function love.draw()
love.graphics.circle('fill', circle.x, circle.y, circle.rad)
end
.. _tweening-methods:
Tweening methods
----------------
At the core of tweening lie interpolation methods. These methods define how the
output should look depending on how much time has passed. For example, consider
the following tween::
-- now: player.x = 0, player.y = 0
Timer.tween(2, player, {x = 2})
Timer.tween(4, player, {y = 8})
At the beginning of the tweens (no time passed), the interpolation method would
place the player at ``x = 0, y = 0``. After one second, the player should be at
``x = 1, y = 2``, and after two seconds the output is ``x = 2, y = 4``.
The actual duration of and time since starting the tween is not important, only
the fraction of the two. Similarly, the starting value and output are not
important to the interpolation method, since it can be calculated from the
start and end point. Thus an interpolation method can be fully characterized by
a function that takes a number between 0 and 1 and returns a number that
defines the output (usually also between 0 and 1). The interpolation function
must hold that the output is 0 for input 0 and 1 for input 1.
**hump** predefines several commonly used interpolation methods, which are
generalized versions of `Robert Penner's easing
functions <http://www.robertpenner.com/easing/>`_. Those are:
``'linear'``,
``'quad'``,
``'cubic'``,
``'quart'``,
``'quint'``,
``'sine'``,
``'expo'``,
``'circ'``,
``'back'``,
``'bounce'``, and
``'elastic'``.
It's hard to understand how these functions behave by staring at a graph, so
below are some animation examples. You can change the type of the tween by
changing the selections.
.. raw:: html
<div id="tween-graph"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="_static/graph-tweens.js"></script>
Note that while the animations above show tweening of shapes, other attributes
(color, opacity, volume of a sound, ...) can be changed as well.
Custom interpolators
^^^^^^^^^^^^^^^^^^^^
.. warning:
This is a stub
You can add custom interpolation methods by adding them to the `tween` table::
Timer.tween.sqrt = function(t) return math.sqrt(t) end
-- or just Timer.tween.sqrt = math.sqrt
Access the your method like you would the predefined ones. You can even use the
modyfing prefixes::
Timer.tween(5, 'in-out-sqrt', circle, {radius = 50})
You can also invert and chain functions::
outsqrt = Timer.tween.out(math.sqrt)
inoutsqrt = Timer.tween.chain(math.sqrt, outsqrt)
hump.vector-light
=================
::
vector = require "hump.vector-light"
An table-free version of :doc:`hump.vector <vector>`. Instead of a vector type,
``hump.vector-light`` provides functions that operate on numbers.
.. note::
Using this module instead of :doc:`hump.vector <vector>` may result in
faster code, but does so at the expense of speed of development and code
readability. Unless you are absolutely sure that your code is
significantly slowed down by :doc:`hump.vector <vector>`, I recommend using
it instead.
**Example**::
function player:update(dt)
local dx,dy = 0,0
if love.keyboard.isDown('left') then
dx = -1
elseif love.keyboard.isDown('right') then
dx = 1
end
if love.keyboard.isDown('up') then
dy = -1
elseif love.keyboard.isDown('down') then
dy = 1
end
dx,dy = vector.normalize(dx, dy)
player.velx, player.vely = vector.add(player.velx, player.vely,
vector.mul(dy, dx, dy))
if vector.len(player.velx, player.vely) > player.max_velocity then
player.velx, player.vely = vector.mul(player.max_velocity,
vector.normalize(player.velx, player.vely)
end
player.x = player.x + dt * player.velx
player.y = player.y + dt * player.vely
end
Function Reference
------------------
.. function:: vector.str(x,y)
:param numbers x,y: The vector.
:returns: The string representation.
Produce a human-readable string of the form ``(x,y)``.
Useful for debugging.
**Example**::
print(vector.str(love.mouse.getPosition()))
.. function:: vector.fromPolar(angle, radius)
:param number angle: Angle of the vector in radians.
:param number radius: Length of the vector.
:returns: ``x``, ``y``: The vector in cartesian coordinates.
Convert polar coordinates to cartesian coordinates.
The ``angle`` is measured against the vector (1,0), i.e., the x axis.
**Examples**::
x,y = vector.polar(math.pi,10)
.. function:: vector.toPolar(x, y)
:param numbers x,y: A vector.
:returns: ``angle``, ``radius``: The vector in polar coordinates.
Convert the vector to polar coordinates, i.e., the angle and the radius/lenth.
**Example**::
-- complex multiplication
phase1, abs1 = vector.toPolar(re1, im1)
phase2, abs2 = vector.toPolar(re2, im2)
vector.fromPolar(phase1+phase2, abs1*abs2)
.. function:: vector.mul(s, x,y)
:param number s: A scalar.
:param numbers x,y: A vector.
:returns: ``x*s, y*s``.
Computes ``x*s,y*s``. The order of arguments is chosen so that it's possible to
chain operations (see example).
**Example**::
velx,vely = vec.mul(dt, vec.add(velx,vely, accx,accy))
.. function:: vector.div(s, x,y)
:param number s: A scalar.
:param numbers x,y: A vector.
:returns: ``x/s, y/s``.
Computes ``x/s,y/s``. The order of arguments is chosen so that it's possible to
chain operations (see example).
**Example**::
x,y = vec.div(self.zoom, x-w/2, y-h/2)
.. function:: vector.add(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1+x2, x1+x2``.
Computes the sum \\((x1+x2, y1+y2)\\)`` of two vectors. Meant to be used in
conjunction with other functions like :func:`vector.mul`.
**Example**::
player.x,player.y = vector.add(player.x,player.y, vector.mul(dt, dx,dy))
.. function:: vector.sub(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1-x2, x1-x2``.
Computes the difference \\((x1-x2, y1-y2)\\) of two vectors. Meant to be used in
conjunction with other functions like :func:`vector.mul`.
**Example**::
dx,dy = vector.sub(400,300, love.mouse.getPosition())
.. function:: vector.permul(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1*x2, y1*y2``.
Component-wise multiplication, i.e.: ``x1*x2, y1*y2``.
**Example**::
x,y = vector.permul(x,y, 1,1.5)
.. function:: vector.dot(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1*x2 + y1*y2``.
Computes the `dot product <http://en.wikipedia.org/wiki/Dot_product>`_ of two
vectors: ``x1*x2 + y1*y2``.
**Example**::
cosphi = vector.dot(rx,ry, vx,vy)
.. function:: vector.cross(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1*y2 - y1*x2``.
Computes the `cross product <http://en.wikipedia.org/wiki/Cross_product>`_ of
two vectors: ``x1*y2 - y1*x2``.
**Example**::
parallelogram_area = vector.cross(ax,ay, bx,by)
.. function:: vector.vector.det(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1*y2 - y1*x2``.
Alias to :func:`vector.cross`.
**Example**::
parallelogram_area = vector.det(ax,ay, bx,by)
.. function:: vector.eq(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1 == x2 and y1 == y2``
Test for equality.
**Example**::
if vector.eq(x1,y1, x2,y2) then be.happy() end
.. function:: vector.le(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1 <= x2 and y1 <= y2``.
Test for partial lexicographical order, ``<=``.
**Example**::
if vector.le(x1,y1, x2,y2) then be.happy() end
.. function:: vector.lt(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: ``x1 < x2 or (x1 == x2) and y1 <= y2``.
Test for strict lexicographical order, ``<``.
**Example**::
if vector.lt(x1,y1, x2,y2) then be.happy() end
.. function:: vector.len(x,y)
:param numbers x,y: The vector.
:returns: Length of the vector.
Get length of a vector, i.e. ``math.sqrt(x*x + y*y)``.
**Example**::
distance = vector.len(love.mouse.getPosition())
.. function:: vector.len2(x,y)
:param numbers x,y: The vector.
:returns: Squared length of the vector.
Get squared length of a vector, i.e. ``x*x + y*y``.
**Example**::
-- get closest vertex to a given vector
closest, dsq = vertices[1], vector.len2(px-vertices[1].x, py-vertices[1].y)
for i = 2,#vertices do
local temp = vector.len2(px-vertices[i].x, py-vertices[i].y)
if temp < dsq then
closest, dsq = vertices[i], temp
end
end
.. function:: vector.dist(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: The distance of the points.
Get distance of two points. The same as ``vector.len(x1-x2, y1-y2)``.
**Example**::
-- get closest vertex to a given vector
-- slightly slower than the example using len2()
closest, dist = vertices[1], vector.dist(px,py, vertices[1].x,vertices[1].y)
for i = 2,#vertices do
local temp = vector.dist(px,py, vertices[i].x,vertices[i].y)
if temp < dist then
closest, dist = vertices[i], temp
end
end
.. function:: vector.dist2(x1,y1, x2,y2)
:param numbers x1,y1: First vector.
:param numbers x2,y2: Second vector.
:returns: The squared distance of two points.
Get squared distance of two points. The same as ``vector.len2(x1-x2, y1-y2)``.
**Example**::
-- get closest vertex to a given vector
closest, dsq = vertices[1], vector.dist2(px,py, vertices[1].x,vertices[1].y)
for i = 2,#vertices do
local temp = vector.dist2(px,py, vertices[i].x,vertices[i].y)
if temp < dsq then
closest, dsq = vertices[i], temp
end
end
.. function:: vector.normalize(x,y)
:param numbers x,y: The vector.
:returns: Vector with same direction as the input vector, but length 1.
Get normalized vector, i.e. a vector with the same direction as the input
vector, but with length 1.
**Example**::
dx,dy = vector.normalize(vx,vy)
.. function:: vector.rotate(phi, x,y)
:param number phi: Rotation angle in radians.
:param numbers x,y: The vector.
:returns: The rotated vector
Get a rotated vector.
**Example**::
-- approximate a circle
circle = {}
for i = 1,30 do
local phi = 2 * math.pi * i / 30
circle[i*2-1], circle[i*2] = vector.rotate(phi, 0,1)
end
.. function:: vector.perpendicular(x,y)
:param numbers x,y: The vector.
:returns: A vector perpendicular to the input vector
Quick rotation by 90°. The same (but faster) as ``vector.rotate(math.pi/2, x,y)``.
**Example**::
nx,ny = vector.normalize(vector.perpendicular(bx-ax, by-ay))
.. function:: vector.project(x,y, u,v)
:param numbers x,y: The vector to project.
:param numbers u,v: The vector to project onto.
:returns: The projected vector.
Project vector onto another vector.
**Example**::
vx_p,vy_p = vector.project(vx,vy, ax,ay)
.. function:: vector.mirror(x,y, u,v)
:param numbers x,y: The vector to mirror.
:param numbers u,v: The vector defining the axis.
:returns: The mirrored vector.
Mirrors vector on the axis defined by the other vector.
**Example**::
vx,vy = vector.mirror(vx,vy, surface.x,surface.y)
.. function:: vector.angleTo(ox,y, u,v)
:param numbers x,y: Vector to measure the angle.
:param numbers u,v (optional): Reference vector.
:returns: Angle in radians.
Measures the angle between two vectors. ``u`` and ``v`` default to ``0`` if omitted,
i.e. the function returns the angle to the coordinate system.
**Example**::
lean = vector.angleTo(self.upx, self.upy, 0,1)
if lean > .1 then self:fallOver() end
.. function:: vector.trim(max_length, x,y)
:param number max_length: Maximum allowed length of the vector.
:param numbers x,y: Vector to trum.
:returns: The trimmed vector.
Trim the vector to ``max_length``, i.e. return a vector that points in the same
direction as the source vector, but has a magnitude smaller or equal to
``max_length``.
**Example**::
vel_x, vel_y = vector.trim(299792458,
vector.add(vel_x, vel_y,
vector.mul(mass * dt, force_x, force_y)))
hump.vector
===========
::
vector = require "hump.vector"
A handy 2D vector class providing most of the things you do with vectors.
You can access the individual coordinates by ``vec.x`` and ``vec.y``.
.. note::
The vectors are stored as tables. Most operations create new vectors and
thus new tables, which *may* put the garbage collector under stress.
If you experience slowdowns that are caused by hump.vector, try the
table-less version :doc:`hump.vector-light <vector-light>`.
**Example**::
function player:update(dt)
local delta = vector(0,0)
if love.keyboard.isDown('left') then
delta.x = -1
elseif love.keyboard.isDown('right') then
delta.x = 1
end
if love.keyboard.isDown('up') then
delta.y = -1
elseif love.keyboard.isDown('down') then
delta.y = 1
end
delta:normalizeInplace()
player.velocity = player.velocity + delta * player.acceleration * dt
if player.velocity:len() > player.max_velocity then
player.velocity = player.velocity:normalized() * player.max_velocity
end
player.position = player.position + player.velocity * dt
end
Vector arithmetic
-----------------
**hump** provides vector arithmetic by implement the corresponding metamethods
(``__add``, ``__mul``, etc.). Here are the semantics:
``vector + vector = vector``
Component wise sum: \\((a,b) + (x,y) = (a+x, b+y)\\)
``vector - vector = vector``
Component wise difference: \\((a,b) - (x,y) = (a-x, b-y)\\)
``vector * vector = number``
Dot product: \\((a,b) \\cdot (x,y) = a\\cdot x + b\\cdot y\\)
``number * vector = vector``
Scalar multiplication/scaling: \\((a,b) \\cdot s = (s\\cdot a, s\\cdot b)\\)
``vector * number = vector``
Scalar multiplication/scaling: \\(s \\cdot (x,y) = (s\\cdot x, s\\cdot y)\\)
``vector / number = vector``
Scalar multiplication/scaling: \\((a,b) / s = (a/s, b/s)\\).
Common relations are also defined:
``a == b``
Same as ``a.x == b.x and a.y == b.y``.
``a <= b``
Same as ``a.x <= b.x and a.y <= b.y``.
``a < b``
Lexicographical order: ``a.x < b.x or (a.x == b.x and a.y < b.y)``.
**Example**::
-- acceleration, player.velocity and player.position are vectors
acceleration = vector(0,-9)
player.velocity = player.velocity + acceleration * dt
player.position = player.position + player.velocity * dt
Function Reference
------------------
.. function:: vector.new(x,y)
:param numbers x,y: Coordinates.
:returns: The vector.
Create a new vector.
**Examples**::
a = vector.new(10,10)
::
-- as a shortcut, you can call the module like a function:
vector = require "hump.vector"
a = vector(10,10)
.. function:: vector.fromPolar(angle, radius)
:param number angle: Angle of the vector in radians.
:param number radius: Length of the vector.
:returns: The vector in cartesian coordinates.
Create a new vector from polar coordinates.
The ``angle`` is measured against the vector (1,0), i.e., the x axis.
**Examples**::
a = vector.polar(math.pi,10)
.. function:: vector.isvector(v)
:param mixed v: The variable to test.
:returns: ``true`` if ``v`` is a vector, ``false`` otherwise.
Test whether a variable is a vector.
**Example**::
if not vector.isvector(v) then
v = vector(v,0)
end
.. function:: vector:clone()
:returns: Copy of the vector.
Copy a vector. Assigning a vector to a variable will create a *reference*, so
when modifying the vector referenced by the new variable would also change the
old one::
a = vector(1,1) -- create vector
b = a -- b references a
c = a:clone() -- c is a copy of a
b.x = 0 -- changes a,b and c
print(a,b,c) -- prints '(1,0), (1,0), (1,1)'
**Example**::
copy = original:clone()
.. function:: vector:unpack()
:returns: The coordinates ``x,y``.
Extract coordinates.
**Examples**::
x,y = pos:unpack()
::
love.graphics.draw(self.image, self.pos:unpack())
.. function:: vector:permul(other)
:param vector other: The second source vector.
:returns: Vector whose components are products of the source vectors.
Multiplies vectors coordinate wise, i.e. ``result = vector(a.x * b.x, a.y *
b.y)``.
Does not change either argument vectors, but creates a new one.
**Example**::
-- scale with different magnitudes
scaled = original:permul(vector(1,1.5))
.. function:: vector:len()
:returns: Length of the vector.
Get length of the vector, i.e. ``math.sqrt(vec.x * vec.x + vec.y * vec.y)``.
**Example**::
distance = (a - b):len()
.. function:: vector:toPolar()
:returns: The vector in polar coordinates (angle, radius).
Convert the vector to polar coordinates, i.e., the angle and the radius/lenth.
**Example**::
-- complex multiplication
p, q = a:toPolar(), b:toPolar()
c = vector(p.x+q.x, p.y*q.y)
.. function:: vector:len2()
:returns: Squared length of the vector.
Get squared length of the vector, i.e. ``vec.x * vec.x + vec.y * vec.y``.
**Example**::
-- get closest vertex to a given vector
closest, dsq = vertices[1], (pos - vertices[1]):len2()
for i = 2,#vertices do
local temp = (pos - vertices[i]):len2()
if temp < dsq then
closest, dsq = vertices[i], temp
end
end
.. function:: vector:dist(other)
:param vector other: Other vector to measure the distance to.
:returns: The distance of the vectors.
Get distance of two vectors. The same as ``(a - b):len()``.
**Example**::
-- get closest vertex to a given vector
-- slightly slower than the example using len2()
closest, dist = vertices[1], pos:dist(vertices[1])
for i = 2,#vertices do
local temp = pos:dist(vertices[i])
if temp < dist then
closest, dist = vertices[i], temp
end
end
.. function:: vector:dist2(other)
:param vector other: Other vector to measure the distance to.
:returns: The squared distance of the vectors.
Get squared distance of two vectors. The same as ``(a - b):len2()``.
**Example**::
-- get closest vertex to a given vector
-- slightly faster than the example using len2()
closest, dsq = vertices[1], pos:dist2(vertices[1])
for i = 2,#vertices do
local temp = pos:dist2(vertices[i])
if temp < dsq then
closest, dsq = vertices[i], temp
end
end
.. function:: vector:normalized()
:returns: Vector with same direction as the input vector, but length 1.
Get normalized vector: a vector with the same direction as the input vector,
but with length 1.
Does not change the input vector, but creates a new vector.
**Example**::
direction = velocity:normalized()
.. function:: vector:normalizeInplace()
:returns: Itself -- the normalized vector
Normalize a vector, i.e. make the vector to have length 1. Great to use on
intermediate results.
.. warning::
This modifies the vector. If in doubt, use :func:`vector:normalized()`.
**Example**::
normal = (b - a):perpendicular():normalizeInplace()
.. function:: vector:rotated(angle)
:param number angle: Rotation angle in radians.
:returns: The rotated vector
Get a vector with same length, but rotated by ``angle``:
.. image:: _static/vector-rotated.png
:alt: Sketch of rotated vector.
Does not change the input vector, but creates a new vector.
**Example**::
-- approximate a circle
circle = {}
for i = 1,30 do
local phi = 2 * math.pi * i / 30
circle[#circle+1] = vector(0,1):rotated(phi)
end
.. function:: vector:rotateInplace(angle)
:param number angle: Rotation angle in radians.
:returns: Itself -- the rotated vector
Rotate a vector in-place. Great to use on intermediate results.
.. warning::
Yhis modifies the vector. If in doubt, use :func:`vector:rotated()`.
**Example**::
-- ongoing rotation
spawner.direction:rotateInplace(dt)
.. function:: vector:perpendicular()
:returns: A vector perpendicular to the input vector
Quick rotation by 90°. Creates a new vector. The same (but faster) as
``vec:rotate(math.pi/2)``:
.. image:: _static/vector-perpendicular.png
:alt: Sketch of two perpendicular vectors
**Example**::
normal = (b - a):perpendicular():normalizeInplace()
.. function:: vector:projectOn(v)
:param vector v: The vector to project on.
:returns: ``vector`` The projected vector.
Project vector onto another vector:
.. image:: _static/vector-projectOn.png
:alt: Sketch of vector projection.
**Example**::
velocity_component = velocity:projectOn(axis)
.. function:: vector:mirrorOn(v)
:param vector v: The vector to mirror on.
:returns: The mirrored vector.
Mirrors vector on the axis defined by the other vector:
.. image: _static/vector-mirrorOn.png
:alt: Sketch of a vector mirrored on another vector
**Example**::
deflected_velocity = ball.velocity:mirrorOn(surface_normal)
.. function:: vector:cross(other)
:param vector other: Vector to compute the cross product with.
:returns: ``number`` Cross product of both vectors.
Get cross product of two vectors. Equals the area of the parallelogram spanned
by both vectors.
**Example**::
parallelogram_area = a:cross(b)
.. function:: vector:angleTo(other)
:param vector other: Vector to measure the angle to (optional).
:returns: Angle in radians.
Measures the angle between two vectors. If ``other`` is omitted it defaults
to the vector ``(0,0)``, i.e. the function returns the angle to the coordinate
system.
**Example**::
lean = self.upvector:angleTo(vector(0,1))
if lean > .1 then self:fallOver() end
.. function:: vector:trimmed(max_length)
:param number max_length: Maximum allowed length of the vector.
:returns: A trimmed vector.
Trim the vector to ``max_length``, i.e. return a vector that points in the same
direction as the source vector, but has a magnitude smaller or equal to
``max_length``.
Does not change the input vector, but creates a new vector.
**Example**::
ship.velocity = ship.force * ship.mass * dt
ship.velocity = ship.velocity:trimmed(299792458)
.. function:: vector:trimInplace(max_length)
:param number max_length: Maximum allowed length of the vector.
:returns: Itself -- the trimmed vector.
Trim the vector to ``max_length``, i.e. return a vector that points in the same
direction as the source vector, but has a magnitude smaller or equal to
``max_length``.
.. warning::
Yhis modifies the vector. If in doubt, use :func:`vector:trimmed()`.
**Example**::
ship.velocity = (ship.velocity + ship.force * ship.mass * dt):trimInplace(299792458)
--[[
Copyright (c) 2010-2013 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.
]]--
local function __NULL__() end
-- default gamestate produces error on every callback
local state_init = setmetatable({leave = __NULL__},
{__index = function() error("Gamestate not initialized. Use Gamestate.switch()") end})
local stack = {state_init}
local initialized_states = setmetatable({}, {__mode = "k"})
local state_is_dirty = true
local GS = {}
function GS.new(t) return t or {} end -- constructor - deprecated!
local function change_state(stack_offset, to, ...)
local pre = stack[#stack]
-- initialize only on first call
;(initialized_states[to] or to.init or __NULL__)(to)
initialized_states[to] = __NULL__
stack[#stack+stack_offset] = to
state_is_dirty = true
return (to.enter or __NULL__)(to, pre, ...)
end
function GS.switch(to, ...)
assert(to, "Missing argument: Gamestate to switch to")
assert(to ~= GS, "Can't call switch with colon operator")
;(stack[#stack].leave or __NULL__)(stack[#stack])
return change_state(0, to, ...)
end
function GS.push(to, ...)
assert(to, "Missing argument: Gamestate to switch to")
assert(to ~= GS, "Can't call push with colon operator")
return change_state(1, to, ...)
end
function GS.pop(...)
assert(#stack > 1, "No more states to pop!")
local pre, to = stack[#stack], stack[#stack-1]
stack[#stack] = nil
;(pre.leave or __NULL__)(pre)
state_is_dirty = true
return (to.resume or __NULL__)(to, pre, ...)
end
function GS.current()
return stack[#stack]
end
-- fetch event callbacks from love.handlers
local all_callbacks = { 'draw', 'errhand', 'update' }
for k in pairs(love.handlers) do
all_callbacks[#all_callbacks+1] = k
end
function GS.registerEvents(callbacks)
local registry = {}
callbacks = callbacks or all_callbacks
for _, f in ipairs(callbacks) do
registry[f] = love[f] or __NULL__
love[f] = function(...)
registry[f](...)
return GS[f](...)
end
end
end
-- forward any undefined functions
setmetatable(GS, {__index = function(_, func)
-- call function only if at least one 'update' was called beforehand
-- (see issue #46)
if not state_is_dirty or func == 'update' then
state_is_dirty = false
return function(...)
return (stack[#stack][func] or __NULL__)(stack[#stack], ...)
end
end
return __NULL__
end})
return GS
--[[
Copyright (c) 2012-2013 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.
]]--
local Registry = {}
Registry.__index = function(self, key)
return Registry[key] or (function()
local t = {}
rawset(self, key, t)
return t
end)()
end
function Registry:register(s, f)
self[s][f] = f
return f
end
function Registry:emit(s, ...)
for f in pairs(self[s]) do
f(...)
end
end
function Registry:remove(s, ...)
local f = {...}
for i = 1,select('#', ...) do
self[s][f[i]] = nil
end
end
function Registry:clear(...)
local s = {...}
for i = 1,select('#', ...) do
self[s[i]] = {}
end
end
function Registry:emitPattern(p, ...)
for s in pairs(self) do
if s:match(p) then self:emit(s, ...) end
end
end
function Registry:registerPattern(p, f)
for s in pairs(self) do
if s:match(p) then self:register(s, f) end
end
return f
end
function Registry:removePattern(p, ...)
for s in pairs(self) do
if s:match(p) then self:remove(s, ...) end
end
end
function Registry:clearPattern(p)
for s in pairs(self) do
if s:match(p) then self[s] = {} end
end
end
-- instancing
function Registry.new()
return setmetatable({}, Registry)
end
-- default instance
local default = Registry.new()
-- module forwards calls to default instance
local module = {}
for k in pairs(Registry) do
if k ~= "__index" then
module[k] = function(...) return default[k](default, ...) end
end
end
return setmetatable(module, {__call = Registry.new})
local timer = require 'timer'()
describe('hump.timer', function()
it('runs a function during a specified time', function()
local delta, remaining
timer:during(10, function(...) delta, remaining = ... end)
timer:update(2)
assert.are.equal(delta, 2)
assert.are.equal(8, remaining)
timer:update(5)
assert.are.equal(delta, 5)
assert.are.equal(3, remaining)
timer:update(10)
assert.are.equal(delta, 10)
assert.are.equal(0, remaining)
end)
it('runs a function after a specified time', function()
local finished1 = false
local finished2 = false
timer:after(3, function(...) finished1 = true end)
timer:after(5, function(...) finished2 = true end)
timer:update(4)
assert.are.equal(true, finished1)
assert.are.equal(false, finished2)
timer:update(4)
assert.are.equal(true, finished1)
assert.are.equal(true, finished2)
end)
it('runs a function every so often', function()
local count = 0
timer:every(1, function(...) count = count + 1 end)
timer:update(3)
assert.are.equal(3, count)
timer:update(7)
assert.are.equal(10, count)
end)
it('can script timed events', function()
local state
timer:script(function(wait)
state = 'foo'
wait(1)
state = 'bar'
end)
assert.are.equal('foo', state)
timer:update(0.5)
assert.are.equal('foo', state)
timer:update(1)
assert.are.equal('bar', state)
end)
it('cancels and clears timer functions', function()
pending('to be tested...')
end)
it('tweens', function()
pending('to be tested...')
end)
end)
--[[
Copyright (c) 2010-2013 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.
]]--
local Timer = {}
Timer.__index = Timer
local function _nothing_() end
function Timer:update(dt)
local to_remove = {}
for handle in pairs(self.functions) do
-- handle: {
-- time = <number>,
-- after = <function>,
-- during = <function>,
-- limit = <number>,
-- count = <number>,
-- }
handle.time = handle.time + dt
handle.during(dt, math.max(handle.limit - handle.time, 0))
while handle.time >= handle.limit and handle.count > 0 do
if handle.after(handle.after) == false then
handle.count = 0
break
end
handle.time = handle.time - handle.limit
handle.count = handle.count - 1
end
if handle.count == 0 then
table.insert(to_remove, handle)
end
end
for i = 1, #to_remove do
self.functions[to_remove[i]] = nil
end
end
function Timer:during(delay, during, after)
local handle = { time = 0, during = during, after = after or _nothing_, limit = delay, count = 1 }
self.functions[handle] = true
return handle
end
function Timer:after(delay, func)
return self:during(delay, _nothing_, func)
end
function Timer:every(delay, after, count)
local count = count or math.huge -- exploit below: math.huge - 1 = math.huge
local handle = { time = 0, during = _nothing_, after = after, limit = delay, count = count }
self.functions[handle] = true
return handle
end
function Timer:cancel(handle)
self.functions[handle] = nil
end
function Timer:clear()
self.functions = {}
end
function Timer:script(f)
local co = coroutine.wrap(f)
co(function(t)
self:after(t, co)
coroutine.yield()
end)
end
Timer.tween = setmetatable({
-- helper functions
out = function(f) -- 'rotates' a function
return function(s, ...) return 1 - f(1-s, ...) end
end,
chain = function(f1, f2) -- concatenates two functions
return function(s, ...) return (s < .5 and f1(2*s, ...) or 1 + f2(2*s-1, ...)) * .5 end
end,
-- useful tweening functions
linear = function(s) return s end,
quad = function(s) return s*s end,
cubic = function(s) return s*s*s end,
quart = function(s) return s*s*s*s end,
quint = function(s) return s*s*s*s*s end,
sine = function(s) return 1-math.cos(s*math.pi/2) end,
expo = function(s) return 2^(10*(s-1)) end,
circ = function(s) return 1 - math.sqrt(1-s*s) end,
back = function(s,bounciness)
bounciness = bounciness or 1.70158
return s*s*((bounciness+1)*s - bounciness)
end,
bounce = function(s) -- magic numbers ahead
local a,b = 7.5625, 1/2.75
return math.min(a*s^2, a*(s-1.5*b)^2 + .75, a*(s-2.25*b)^2 + .9375, a*(s-2.625*b)^2 + .984375)
end,
elastic = function(s, amp, period)
amp, period = amp and math.max(1, amp) or 1, period or .3
return (-amp * math.sin(2*math.pi/period * (s-1) - math.asin(1/amp))) * 2^(10*(s-1))
end,
}, {
-- register new tween
__call = function(tween, self, len, subject, target, method, after, ...)
-- recursively collects fields that are defined in both subject and target into a flat list
local function tween_collect_payload(subject, target, out)
for k,v in pairs(target) do
local ref = subject[k]
assert(type(v) == type(ref), 'Type mismatch in field "'..k..'".')
if type(v) == 'table' then
tween_collect_payload(ref, v, out)
else
local ok, delta = pcall(function() return (v-ref)*1 end)
assert(ok, 'Field "'..k..'" does not support arithmetic operations')
out[#out+1] = {subject, k, delta}
end
end
return out
end
method = tween[method or 'linear'] -- see __index
local payload, t, args = tween_collect_payload(subject, target, {}), 0, {...}
local last_s = 0
return self:during(len, function(dt)
t = t + dt
local s = method(math.min(1, t/len), unpack(args))
local ds = s - last_s
last_s = s
for _, info in ipairs(payload) do
local ref, key, delta = unpack(info)
ref[key] = ref[key] + delta * ds
end
end, after)
end,
-- fetches function and generated compositions for method `key`
__index = function(tweens, key)
if type(key) == 'function' then return key end
assert(type(key) == 'string', 'Method must be function or string.')
if rawget(tweens, key) then return rawget(tweens, key) end
local function construct(pattern, f)
local method = rawget(tweens, key:match(pattern))
if method then return f(method) end
return nil
end
local out, chain = rawget(tweens,'out'), rawget(tweens,'chain')
return construct('^in%-([^-]+)$', function(...) return ... end)
or construct('^out%-([^-]+)$', out)
or construct('^in%-out%-([^-]+)$', function(f) return chain(f, out(f)) end)
or construct('^out%-in%-([^-]+)$', function(f) return chain(out(f), f) end)
or error('Unknown interpolation method: ' .. key)
end})
-- Timer instancing
function Timer.new()
return setmetatable({functions = {}, tween = Timer.tween}, Timer)
end
-- default instance
local default = Timer.new()
-- module forwards calls to default instance
local module = {}
for k in pairs(Timer) do
if k ~= "__index" then
module[k] = function(...) return default[k](default, ...) end
end
end
module.tween = setmetatable({}, {
__index = Timer.tween,
__newindex = function(k,v) Timer.tween[k] = v end,
__call = function(t, ...) return default:tween(...) end,
})
return setmetatable(module, {__call = Timer.new})
--[[
Copyright (c) 2012-2013 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.
]]--
local sqrt, cos, sin, atan2 = math.sqrt, math.cos, math.sin, math.atan2
local function str(x,y)
return "("..tonumber(x)..","..tonumber(y)..")"
end
local function mul(s, x,y)
return s*x, s*y
end
local function div(s, x,y)
return x/s, y/s
end
local function add(x1,y1, x2,y2)
return x1+x2, y1+y2
end
local function sub(x1,y1, x2,y2)
return x1-x2, y1-y2
end
local function permul(x1,y1, x2,y2)
return x1*x2, y1*y2
end
local function dot(x1,y1, x2,y2)
return x1*x2 + y1*y2
end
local function det(x1,y1, x2,y2)
return x1*y2 - y1*x2
end
local function eq(x1,y1, x2,y2)
return x1 == x2 and y1 == y2
end
local function lt(x1,y1, x2,y2)
return x1 < x2 or (x1 == x2 and y1 < y2)
end
local function le(x1,y1, x2,y2)
return x1 <= x2 and y1 <= y2
end
local function len2(x,y)
return x*x + y*y
end
local function len(x,y)
return sqrt(x*x + y*y)
end
local function fromPolar(angle, radius)
return cos(angle)*radius, sin(angle)*radius
end
local function toPolar(x, y)
return atan2(y,x), len(x,y)
end
local function dist2(x1,y1, x2,y2)
return len2(x1-x2, y1-y2)
end
local function dist(x1,y1, x2,y2)
return len(x1-x2, y1-y2)
end
local function normalize(x,y)
local l = len(x,y)
if l > 0 then
return x/l, y/l
end
return x,y
end
local function rotate(phi, x,y)
local c, s = cos(phi), sin(phi)
return c*x - s*y, s*x + c*y
end
local function perpendicular(x,y)
return -y, x
end
local function project(x,y, u,v)
local s = (x*u + y*v) / (u*u + v*v)
return s*u, s*v
end
local function mirror(x,y, u,v)
local s = 2 * (x*u + y*v) / (u*u + v*v)
return s*u - x, s*v - y
end
-- ref.: http://blog.signalsondisplay.com/?p=336
local function trim(maxLen, x, y)
local s = maxLen * maxLen / len2(x, y)
s = s > 1 and 1 or math.sqrt(s)
return x * s, y * s
end
local function angleTo(x,y, u,v)
if u and v then
return atan2(y, x) - atan2(v, u)
end
return atan2(y, x)
end
-- the module
return {
str = str,
fromPolar = fromPolar,
toPolar = toPolar,
-- arithmetic
mul = mul,
div = div,
add = add,
sub = sub,
permul = permul,
dot = dot,
det = det,
cross = det,
-- relation
eq = eq,
lt = lt,
le = le,
-- misc operations
len2 = len2,
len = len,
dist2 = dist2,
dist = dist,
normalize = normalize,
rotate = rotate,
perpendicular = perpendicular,
project = project,
mirror = mirror,
trim = trim,
angleTo = angleTo,
}
--[[
Copyright (c) 2010-2013 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.
]]--
local assert = assert
local sqrt, cos, sin, atan2 = math.sqrt, math.cos, math.sin, math.atan2
local vector = {}
vector.__index = vector
local function new(x,y)
return setmetatable({x = x or 0, y = y or 0}, vector)
end
local zero = new(0,0)
local function fromPolar(angle, radius)
return new(cos(angle) * radius, sin(angle) * radius)
end
local function isvector(v)
return type(v) == 'table' and type(v.x) == 'number' and type(v.y) == 'number'
end
function vector:clone()
return new(self.x, self.y)
end
function vector:unpack()
return self.x, self.y
end
function vector:__tostring()
return "("..tonumber(self.x)..","..tonumber(self.y)..")"
end
function vector.__unm(a)
return new(-a.x, -a.y)
end
function vector.__add(a,b)
assert(isvector(a) and isvector(b), "Add: wrong argument types (<vector> expected)")
return new(a.x+b.x, a.y+b.y)
end
function vector.__sub(a,b)
assert(isvector(a) and isvector(b), "Sub: wrong argument types (<vector> expected)")
return new(a.x-b.x, a.y-b.y)
end
function vector.__mul(a,b)
if type(a) == "number" then
return new(a*b.x, a*b.y)
elseif type(b) == "number" then
return new(b*a.x, b*a.y)
else
assert(isvector(a) and isvector(b), "Mul: wrong argument types (<vector> or <number> expected)")
return a.x*b.x + a.y*b.y
end
end
function vector.__div(a,b)
assert(isvector(a) and type(b) == "number", "wrong argument types (expected <vector> / <number>)")
return new(a.x / b, a.y / b)
end
function vector.__eq(a,b)
return a.x == b.x and a.y == b.y
end
function vector.__lt(a,b)
return a.x < b.x or (a.x == b.x and a.y < b.y)
end
function vector.__le(a,b)
return a.x <= b.x and a.y <= b.y
end
function vector.permul(a,b)
assert(isvector(a) and isvector(b), "permul: wrong argument types (<vector> expected)")
return new(a.x*b.x, a.y*b.y)
end
function vector:toPolar()
return new(atan2(self.x, self.y), self:len())
end
function vector:len2()
return self.x * self.x + self.y * self.y
end
function vector:len()
return sqrt(self.x * self.x + self.y * self.y)
end
function vector.dist(a, b)
assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
local dx = a.x - b.x
local dy = a.y - b.y
return sqrt(dx * dx + dy * dy)
end
function vector.dist2(a, b)
assert(isvector(a) and isvector(b), "dist: wrong argument types (<vector> expected)")
local dx = a.x - b.x
local dy = a.y - b.y
return (dx * dx + dy * dy)
end
function vector:normalizeInplace()
local l = self:len()
if l > 0 then
self.x, self.y = self.x / l, self.y / l
end
return self
end
function vector:normalized()
return self:clone():normalizeInplace()
end
function vector:rotateInplace(phi)
local c, s = cos(phi), sin(phi)
self.x, self.y = c * self.x - s * self.y, s * self.x + c * self.y
return self
end
function vector:rotated(phi)
local c, s = cos(phi), sin(phi)
return new(c * self.x - s * self.y, s * self.x + c * self.y)
end
function vector:perpendicular()
return new(-self.y, self.x)
end
function vector:projectOn(v)
assert(isvector(v), "invalid argument: cannot project vector on " .. type(v))
-- (self * v) * v / v:len2()
local s = (self.x * v.x + self.y * v.y) / (v.x * v.x + v.y * v.y)
return new(s * v.x, s * v.y)
end
function vector:mirrorOn(v)
assert(isvector(v), "invalid argument: cannot mirror vector on " .. type(v))
-- 2 * self:projectOn(v) - self
local s = 2 * (self.x * v.x + self.y * v.y) / (v.x * v.x + v.y * v.y)
return new(s * v.x - self.x, s * v.y - self.y)
end
function vector:cross(v)
assert(isvector(v), "cross: wrong argument types (<vector> expected)")
return self.x * v.y - self.y * v.x
end
-- ref.: http://blog.signalsondisplay.com/?p=336
function vector:trimInplace(maxLen)
local s = maxLen * maxLen / self:len2()
s = (s > 1 and 1) or math.sqrt(s)
self.x, self.y = self.x * s, self.y * s
return self
end
function vector:angleTo(other)
if other then
return atan2(self.y, self.x) - atan2(other.y, other.x)
end
return atan2(self.y, self.x)
end
function vector:trimmed(maxLen)
return self:clone():trimInplace(maxLen)
end
-- the module
return setmetatable({new = new, fromPolar = fromPolar, isvector = isvector, zero = zero},
{__call = function(_, ...) return new(...) end})
Gamestate = require "hump.gamestate"
--temporary scaling factor
TF = 2
HEIGHT = 360*TF
WIDTH = 640*TF
math.randomseed(os.time())
firstplayer = math.random(1, 4)
local menu = {}
local game = {}
local options = {}
local credits = {}
local helps = {}
local stats = {}
local summary = {}
local inputname = {}
function initialize_all() --sets the values to defaults
win = false
lose = false
paused = false
active_meal = false
meal_picture=nil
meal_name = "no meal"
food_name = nil
food_desc = "food"
picktype = "random"
go_left = 0
grow_left = 0
glow_left = 0
go_max = 0
grow_max = 0
glow_max = 0
last_player=firstplayer
turn_number=firstplayer
has_gamestarted = false
has_distributed = false
is_confirming = false
gamestate = "distribution"
tempgo = nil
tempgrow = nil
tempglow = nil
csf = 2*TF --card scaling factor
message_top = "Game begin!!!"
message_bottom = "Player "..firstplayer.."'s turn to set a meal"
seccounter=0
passlist = {true,true,true,true}
playername=""
end
function deal()
deck = {c01, c02, c03, c04, c05, c06, c07, c08, c09, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c01, c02, c03, c04, c05, c06, c07, c08, c09, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30}
shuffle(deck)
-- add 5 cards to hand (all players)
hand = {deck[1],deck[2],deck[3],deck[4],deck[5]}
hand2 = {deck[6],deck[7],deck[8],deck[9],deck[10]}
hand3 = {deck[11],deck[12],deck[13],deck[14],deck[15]}
hand4 = {deck[16],deck[17],deck[18],deck[19],deck[20]}
handz = {hand,hand2,hand3,hand4}
for i=1, 20 do --remove opening cards from deck
table.remove(deck,1)
end
has_distributed = true --player now has hand; can pick a card
end
function drawcard (this_hand)
if #deck>0 then
table.insert(this_hand,deck[1])
table.remove(deck,1)
end
end
function love.load()
love.window.setMode(640*TF, 360*TF, {fullscreen = false, resizable = false})
love.window.setTitle("Tong-EATS")
Gamestate.registerEvents()
Gamestate.switch(menu)
--initialize values hihi
initialize_all()
--menu assets
bg = love.graphics.newImage("assets/images/img_Background.png")
settings = love.graphics.newImage("assets/images/img_Gear.png")
fork = love.graphics.newImage("assets/images/img_Fork.png")
spoon = love.graphics.newImage("assets/images/img_Spoon.png")
help = love.graphics.newImage("assets/images/img_Help.png")
title = love.graphics.newImage("assets/images/img_Title-08.png")
menuPad = love.graphics.newImage("assets/images/img_MenuPad.png")
menuFont = love.graphics.newFont("assets/fonts/SantJoanDespi-Regular.otf", 30*TF)
menuFontDesc = love.graphics.newFont("assets/fonts/SantJoanDespi-Regular.otf", 12*TF)
medFont = love.graphics.newFont("assets/fonts/Avenir-Next-LT-Pro-Bold_5182.ttf", 18*TF)
medFontDesc = love.graphics.newFont("assets/fonts/Avenir-Next-LT-Pro-Bold_5182.ttf", 12*TF)
coolFont = love.graphics.newFont("assets/fonts/BoB.ttf", 35*TF)
bgm = love.audio.newSource("assets/music/happy_loop.wav")
bgm:setLooping(true)
bgm:play()
--assets
nil_img = love.graphics.newImage("assets/fake/wala.png")
fakewin_img = love.graphics.newImage("assets/fake/fakewin.png")
fakelose_img = love.graphics.newImage("assets/fake/fakelose.png")
fakepausescreen_img = love.graphics.newImage("assets/fake/fakepause.png")
bg_img = love.graphics.newImage("assets/images/img_Background.png")
pause_img = love.graphics.newImage("assets/images/img_Pause.png")
plate_img = love.graphics.newImage("assets/images/img_Plate.png")
icon_glow = love.graphics.newImage("assets/images/img_Glow.png")
icon_go = love.graphics.newImage("assets/images/img_Go.png")
icon_grow = love.graphics.newImage("assets/images/img_Grow.png")
half_glow1 = love.graphics.newImage("assets/images/glow_half1.png")
half_glow2 = love.graphics.newImage("assets/images/glow_half2.png")
half_grow1 = love.graphics.newImage("assets/images/grow_half1.png")
half_grow2 = love.graphics.newImage("assets/images/grow_half2.png")
half_go = love.graphics.newImage("assets/images/go_half.png")
--actual card image loading
apple_img = love.graphics.newImage("assets/Cards/cards-02.png")
bittergourd_img = love.graphics.newImage("assets/Cards/cards-01.png")
rice_img = love.graphics.newImage("assets/Cards/cards-13.png")
peanuts_img = love.graphics.newImage("assets/Cards/cards-28.png")
pork_img = love.graphics.newImage("assets/Cards/cards-23.png")
lamb_img = love.graphics.newImage("assets/Cards/cards-25.png")
potato_img = love.graphics.newImage("assets/Cards/cards-14.png")
cabbage_img = love.graphics.newImage("assets/Cards/cards-09.png")
spaghetti_img = love.graphics.newImage("assets/Cards/cards-12.png")
--pechay_img = love.graphics.newImage("assets/Cards/cards-30.png")
chicken_img = love.graphics.newImage("assets/Cards/cards-22.png")
cheese_img = love.graphics.newImage("assets/Cards/cards-17.png")
carrot_img = love.graphics.newImage("assets/Cards/cards-07.png")
tomato_img = love.graphics.newImage("assets/Cards/cards-05.png")
cucumber_img = love.graphics.newImage("assets/Cards/cards-04.png")
beef_img = love.graphics.newImage("assets/Cards/cards-20.png")
banana_img = love.graphics.newImage("assets/Cards/cards-03.png")
cereal_img = love.graphics.newImage("assets/Cards/cards-29.png")
oatmeal_img = love.graphics.newImage("assets/Cards/cards-16.png")
corn_img = love.graphics.newImage("assets/Cards/cards-10.png")
kidneybeans_img = love.graphics.newImage("assets/Cards/cards-26.png")
yoghurt_img = love.graphics.newImage("assets/Cards/cards-27.png")
butter_img = love.graphics.newImage("assets/Cards/cards-11.png")
celery_img = love.graphics.newImage("assets/Cards/cards-06.png")
grapes_img = love.graphics.newImage("assets/Cards/cards-08.png")
eggs_img = love.graphics.newImage("assets/Cards/cards-24.png")
fish_img = love.graphics.newImage("assets/Cards/cards-21.png")
macaroni_img = love.graphics.newImage("assets/Cards/cards-15.png")
lasagna_img = love.graphics.newImage("assets/Cards/cards-19.png")
oil_img = love.graphics.newImage("assets/Cards/cards-18.png")
--card table
c01 = {nutval=3, goval=12, growval=7, glowval=9, pic=apple_img, kind="glow", name="apple", desc="Great for teachers, not so for doctors"}
c02 = {nutval=5, goval=8, growval=6, glowval=7, pic=bittergourd_img, kind="glow", name="bittergourd", desc="It's great for the skin!"}
c03 = {nutval=4, goval=9, growval=6, glowval=7, pic=rice_img, kind="go", name="rice", desc="Boiled, Fried, Sticky, anything goes well with rice!"}
c04 = {nutval=5, goval=12, growval=7, glowval=9, pic=peanuts_img, kind="grow", name="peanuts", desc="Well loved, Makes you go NUTS! "}
c05 = {nutval=6, goval=8, growval=6, glowval=7, pic=pork_img, kind="grow", name="pork", desc="Oink oink!"}
c06 = {nutval=4, goval=10, growval=6, glowval=8, pic=lamb_img, kind="grow", name="lamb", desc="High-class, delectable meat! Mmmm!"}
c07 = {nutval=6, goval=8, growval=6, glowval=7, pic=potato_img, kind="go", name="potato", desc="Great as fries, but better as chips."}
c08 = {nutval=3, goval=12, growval=7, glowval=9, pic=cabbage_img, kind="glow", name="cabbage", desc="Nothing goes better with cabbage than cabbage!"}
c09 = {nutval=2, goval=9, growval=6, glowval=7, pic=spaghetti_img, kind="go", name="spaghetti", desc="These noodles, given a sauce, is what kids love the most."}
--c10 = {nutval=5, goval=8, growval=6, glowval=7, pic=pechay_img, kind="glow", name="pechay", desc="Green and crunchy!"}
c11 = {nutval=5, goval=8, growval=6, glowval=7, pic=chicken_img, kind="grow", name="chicken", desc="Do the chicken dance!"}
c12 = {nutval=5, goval=8, growval=6, glowval=7, pic=cheese_img, kind="grow", name="cheese", desc="Early mouse cacthes the Cheese!"}
c13 = {nutval=3, goval=9, growval=6, glowval=7, pic=carrot_img, kind="glow", name="carrot", desc="They're good for your eyesight, and we aren't lying."}
c14 = {nutval=5, goval=11, growval=7, glowval=9, pic=tomato_img, kind="glow", name="tomato", desc="To-MAY-toe or To-MAH-toe?"}
c15 = {nutval=6, goval=9, growval=6, glowval=7, pic=cucumber_img, kind="glow", name="cucumber", desc="Be cool, like a cucumber."}
c16 = {nutval=3, goval=11, growval=7, glowval=9, pic=beef_img, kind="grow", name="beef", desc="Moo!"}
c17 = {nutval=2, goval=10, growval=6, glowval=8, pic=banana_img, kind="glow", name="banana", desc="It peels well today."}
c18 = {nutval=4, goval=12, growval=7, glowval=9, pic=cereal_img, kind="go", name="cereal", desc="Great in the morning with milk."}
c19 = {nutval=3, goval=8, growval=6, glowval=7, pic=oatmeal_img, kind="go", name="oatmeal", desc="For heart health!"}
c20 = {nutval=2, goval=11, growval=7, glowval=9, pic=corn_img, kind="go", name="corn", desc="Pop, pop, pop!"}
c21 = {nutval=5, goval=8, growval=6, glowval=7, pic=kidneybeans_img, kind="grow", name="kidneybeans", desc="They give you, not your car, gas."}
c22 = {nutval=5, goval=8, growval=6, glowval=7, pic=yoghurt_img, kind="grow", name="yoghurt", desc="Sour but delectable. "}
c23 = {nutval=6, goval=9, growval=6, glowval=7, pic=butter_img, kind="go", name="butter", desc="I can't believe it IS butter!"}
c24 = {nutval=4, goval=11, growval=7, glowval=9, pic=celery_img, kind="glow", name="celery", desc="Does have a minty taste."}
c25 = {nutval=6, goval=9, growval=6, glowval=7, pic=grapes_img, kind="glow", name="grapes", desc="Pop 'em in your mouth, or make it wine a little."}
c26 = {nutval=2, goval=8, growval=6, glowval=7, pic=eggs_img, kind="grow", name="eggs", desc="Who wants scrambled eggs?"}
c27 = {nutval=4, goval=9, growval=6, glowval=7, pic=fish_img, kind="grow", name="fish", desc="Glub Glub Glub..."}
c28 = {nutval=3, goval=12, growval=7, glowval=9, pic=macaroni_img, kind="go", name="macaroni", desc="Macaroni and Cheese, my favorite!"}
c29 = {nutval=4, goval=9, growval=6, glowval=7, pic=lasagna_img, kind="go", name="lasagna", desc="Don't tell Garfield."}
c30 = {nutval=3, goval=8, growval=6, glowval=7, pic=oil_img, kind="go", name="oil", desc="Not really food, but it makes your food good!"}
--foe card backs image loading
back1 = love.graphics.newImage("assets/foebacks/1_left.png")
back2 = love.graphics.newImage("assets/foebacks/2_left.png")
back3 = love.graphics.newImage("assets/foebacks/3_left.png")
back4 = love.graphics.newImage("assets/foebacks/4_left.png")
back5 = love.graphics.newImage("assets/foebacks/5_left.png")
foebacks = {nil_img, back1, back2, back3, back4, back5}
--eligibility of cards
hand1e = {false,false,false,false,false}
hand2e = {false,false,false,false,false}
hand3e = {false,false,false,false,false}
hand4e = {false,false,false,false,false}
handels = {hand1e,hand2e,hand3e,hand4e}
--recent_card = nil_img
deck = {c01, c02, c03, c04, c05, c06, c07, c08, c09, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c01, c02, c03, c04, c05, c06, c07, c08, c09, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30}
hand_pos_x = {} --left corners of the cards in hand
hand_pos_y = {} --top corners of the cards in hand
for i=1,5 do
card_coords = {(nil_img:getWidth()+5)*csf*i, HEIGHT-nil_img:getHeight()*csf}
table.insert(hand_pos_x, card_coords[1]) --creates the table's hand positions
table.insert(hand_pos_y, card_coords[2]) --creates the table's hand positions
table.insert(hand_pos_y, card_coords[2]) --creates the table's hand positions
end
deal()
--message queue
msgq = {}
--options assets
musicOff = love.graphics.newImage("assets/images/img_music-off.png")
musicOn = love.graphics.newImage("assets/images/img_music_on.png")
sfxOff = love.graphics.newImage("assets/images/img_sfx-off.png")
sfxOn = love.graphics.newImage("assets/images/img_sfx_on.png")
optionsPad = love.graphics.newImage("assets/images/img_options-menu.png")
sfxIsOn = true
sfxImg = sfxOn
sfxY = 105*TF
musicIsOn = true
musicImg = musicOn
--credits assets
--help assets
--stats assets
--input name assets
keyboard = love.graphics.newImage("assets/images/KeyBoard.png")
name = {}
for i = 1, 20 do
name[i] = ""
end
namecount = 0
playername = ""
end
function newMsg(message) --8 messages on screen that show history
if #msgq<8 then
table.insert(msgq, message)
table.insert(msgq, "GO: "..go_left.." GROW: "..grow_left.." GLOW: "..glow_left)
else
msgq[1]=msgq[3]
msgq[2]=msgq[4]
msgq[3]=msgq[5]
msgq[4]=msgq[6]
msgq[5]=msgq[7]
msgq[6]=msgq[8]
msgq[7]=message
msgq[8]="GO: "..go_left.." GROW: "..grow_left.." GLOW: "..glow_left
end
end
local function printCoordinates()
x, y = love.mouse.getPosition()
love.graphics.setColor(0, 0, 0)
love.graphics.printf("x:", 0, 0, 500*TF, "left")
love.graphics.printf(x, 30*TF, 0, 500*TF, "left")
love.graphics.printf("y:", 0, 30, 500*TF, "left")
love.graphics.printf(y, 30*TF, 30, 500*TF, "left")
end
local function inCircle(cx, cy, radius, x, y)
local dx = cx - x
local dy = cy - y
return dx * dx + dy * dy <= radius * radius
end
function inputname:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0, 0, 0.24*TF, 0.24*TF*HEIGHT/(320*TF))
love.graphics.draw(keyboard, 195, 243, 0, 0.7*TF, 0.7*TF)
love.graphics.setColor(54, 56, 10)
love.graphics.printf(name, 245, 262, 500*TF, "left")
love.graphics.printf("What's your name?", 370, 135, 500*TF, "left")
end
function inputname:mousepressed(x, y, button, istouch)
if button == 1 and x >= 237 and x <= 285 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "Q"
elseif button == 1 and x >= 305 and x <= 353 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "W"
elseif button == 1 and x >= 373 and x <= 421 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "E"
elseif button == 1 and x >= 441 and x <= 489 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "R"
elseif button == 1 and x >= 509 and x <= 557 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "T"
elseif button == 1 and x >= 577 and x <= 625 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "Y"
elseif button == 1 and x >= 645 and x <= 693 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "U"
elseif button == 1 and x >= 713 and x <= 761 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "I"
elseif button == 1 and x >= 781 and x <= 829 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "O"
elseif button == 1 and x >= 849 and x <= 897 and y >= 354 and y <= 408 then
namecount = namecount + 1
name[namecount] = "P"
elseif button == 1 and x >= 266 and x <= 314 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "A"
elseif button == 1 and x >= 334 and x <= 382 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "S"
elseif button == 1 and x >= 402 and x <= 450 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "D"
elseif button == 1 and x >= 470 and x <= 518 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "F"
elseif button == 1 and x >= 538 and x <= 586 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "G"
elseif button == 1 and x >= 606 and x <= 654 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "H"
elseif button == 1 and x >= 674 and x <= 722 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "J"
elseif button == 1 and x >= 742 and x <= 790 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "K"
elseif button == 1 and x >= 810 and x <= 858 and y >= 420 and y <= 473 then
namecount = namecount + 1
name[namecount] = "L"
elseif button == 1 and x >= 289 and x <= 337 and y >= 484 and y <= 538 then
namecount = namecount + 1
name[namecount] = "Z"
elseif button == 1 and x >= 357 and x <= 405 and y >= 484 and y <= 538 then
namecount = namecount + 1
name[namecount] = "X"
elseif button == 1 and x >= 425 and x <= 473 and y >= 484 and y <= 538 then
namecount = namecount + 1
name[namecount] = "C"
elseif button == 1 and x >= 493 and x <= 541 and y >= 484 and y <= 538 then
namecount = namecount + 1
name[namecount] = "V"
elseif button == 1 and x >= 561 and x <= 609 and y >= 484 and y <= 538 then
namecount = namecount + 1
name[namecount] = "B"
elseif button == 1 and x >= 629 and x <= 677 and y >= 484 and y <= 538 then
namecount = namecount + 1
name[namecount] = "N"
elseif button == 1 and x >= 697 and x <= 745 and y >= 484 and y <= 538 then
namecount = namecount + 1
name[namecount] = "M"
elseif button == 1 and x >= 915 and x <= 1048 and y >= 355 and y <= 473 then
name[namecount] = ""
namecount = namecount-1
elseif button == 1 and x >= 876 and x <= 1048 and y >= 420 and y <= 473 then
namecount = namecount-1
name[namecount] = ""
elseif button == 1 and x >= 237 and x <= 933 and y >= 549 and y <= 602 then
namecount = namecount + 1
name[namecount] = " "
elseif button == 1 and x >= 762 and x <= 1048 and y >= 484 and y <= 538 then
for i = 1, 20 do
playername = playername .. name[i]
end
Gamestate.switch(game)
elseif button == 1 and inCircle(1032, 301, 34, x, y) then
for i = 1, 20 do
playername = playername .. name[i]
end
Gamestate.switch(game)
elseif button == 1 and x >= 953 and x <= 1048 and y >= 549 and y <= 602 then
Gamestate.switch(menu)
name = {}
for i = 1, 20 do
name[i] = ""
end
namecount = 0
end
end
function menu:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0, 0, 0.24*TF, 0.24*TF*HEIGHT/(320*TF))
love.graphics.draw(title, 110*TF, 20*TF-(320*TF-HEIGHT)/2, 0, 0.24*TF, 0.24*TF)
love.graphics.draw(menuPad, 236.5*TF, 145*TF-(320*TF-HEIGHT)/2, 0, 0.24*TF, 0.24*TF)
selector = -100*TF
x, y = love.mouse.getPosition()
if x >= 237*TF and x <= 403*TF and y >= 189*TF-(320*TF-HEIGHT)/2 and y <= 219*TF-(320*TF-HEIGHT)/2 then
selector = 200*TF-(320*TF-HEIGHT)/2
elseif x >= 237*TF and x <= 403*TF and y >= 220*TF-(320*TF-HEIGHT)/2 and y <= 249*TF-(320*TF-HEIGHT)/2 then
selector = 230*TF-(320*TF-HEIGHT)/2
else selector = -100*TF-(320*TF-HEIGHT)/2
end
love.graphics.draw(fork, 243*TF, selector, 0, 0.12*TF, 0.12*TF)
love.graphics.draw(spoon, 367*TF, selector, 0, 0.12*TF, 0.12*TF)
love.graphics.draw(settings, 10*TF, 265*TF-(320*TF-HEIGHT)/2, 0, 0.24*TF, 0.24*TF)
love.graphics.draw(help, 75*TF, 265*TF-(320*TF-HEIGHT)/2, 0, 0.24*TF, 0.24*TF)
love.graphics.setFont(menuFont)
love.graphics.setColor(54, 56, 10)
love.graphics.printf("START", 282*TF, 186*TF-(320*TF-HEIGHT)/2, 500*TF, "left")
love.graphics.printf("STATS", 282*TF, 217*TF-(320*TF-HEIGHT)/2, 500*TF, "left")
end
function menu:mousepressed(x, y, button, istouch)
if button == 1 and inCircle(33*TF, 288*TF, 25*TF-(320*TF-HEIGHT)/2, x, y) then
Gamestate.switch(options)
elseif button == 1 and inCircle(91*TF, 288*TF, 25*TF-(320*TF-HEIGHT)/2, x, y) then
Gamestate.switch(summary)
elseif button == 1 and x >= 237*TF and x <= 403*TF and y >= 189*TF-(320*TF-HEIGHT)/2 and y <= 219*TF-(320*TF-HEIGHT)/2 then
Gamestate.switch(inputname)
elseif button == 1 and x >= 237*TF and x <= 403*TF and y >= 220*TF-(320*TF-HEIGHT)/2 and y <= 249*TF-(320*TF-HEIGHT)/2 then
Gamestate.switch(stats)
end
end
function shuffle( a )
local c = #a
math.randomseed( os.time() )
for i = 1, (c * 200) do
local ndx0 = math.random( 1, c )
local ndx1 = math.random( 1, c )
local temp = a[ ndx0 ]
a[ ndx0 ] = a[ ndx1 ]
a[ ndx1 ] = temp
end
return a
end
--ai function (card picker)
function ai_pick(ai_num,selection_type)
--random selection (picks a random ELIGIBLE card, and passes otherwise)
if selection_type=="random" and not (lose or win) then
if not active_meal then
r=1
--get meal values from card
active_meal=true
meal_name = handz[ai_num][r].name
meal_player = ai_num
go_left = handz[ai_num][r].goval
grow_left = handz[ai_num][r].growval
glow_left = handz[ai_num][r].glowval
go_max = go_left
glow_max = glow_left
grow_max = grow_left
table.remove(handz[ai_num],r)
newMsg("player "..ai_num.." set new "..meal_name.." meal")
message_top = "Player "..ai_num.." set down a new "..meal_name.." meal"
passlist = {false,false,false,false}
else
--pick a card (s=0 means pass)
s = 0
--print(ai_num, handels[ai_num][1], handels[ai_num][2], handels[ai_num][3], handels[ai_num][4], handels[ai_num][5])
if handels[ai_num][1] then
s = 1
elseif handels[ai_num][2] then
s = 2
elseif handels[ai_num][3] then
s = 3
elseif handels[ai_num][4] then
s = 4
elseif handels[ai_num][5] then
s = 5
end
if s>0 then
if handz[ai_num][s].kind=="go" then
go_left = go_left-handz[ai_num][s].nutval
elseif handz[ai_num][s].kind=="grow" then
grow_left = grow_left-handz[ai_num][s].nutval
else
glow_left = glow_left-handz[ai_num][s].nutval
end
newMsg("player "..ai_num.." played "..handz[ai_num][s].name)
message_top = "Player "..ai_num.." set down a "..handz[ai_num][s].name.." card"
table.remove(handz[ai_num],s)
passlist = {false,false,false,false}
else
if #handz[ai_num]<5 then
drawcard(handz[ai_num])
end
newMsg("player "..ai_num.." passed")
turn_number = turn_number+1
if turn_number>4 then turn_number=1 end
passlist[ai_num]=true
message_top = "Player "..ai_num.." passed"
end
end
end
end
function game:update(dt)
seccounter = seccounter + dt
for i=2, 4 do
if #handz[i]==0 and not win then
lose=true
end
end
--deals with eligibility of card picks (handels)
if (active_meal) then
for i=1, 5 do
for j=1, 4 do
handels[j][i]=false
end
end
for j=1, 4 do
for i=1, #handz[j] do
if handz[j][i].nutval<=go_left and handz[j][i].kind=="go" then
handels[j][i]=true
elseif handz[j][i].nutval<=grow_left and handz[j][i].kind=="grow" then
handels[j][i]=true
elseif handz[j][i].nutval<=glow_left and handz[j][i].kind=="glow" then
handels[j][i]=true
else
handels[j][i]=false
end
end
end
end
--fixes messages
message_bottom = "Player "..turn_number.."'s turn to"
if gamestate == "p1play" then message_bottom = "Your turn to" end
if active_meal then
message_bottom = message_bottom.." pick a card"
else
message_bottom = message_bottom.." set a meal"
end
--message specials
if not hand1e[1] and not hand1e[2] and not hand1e[3] and not hand1e[4] and not hand1e[5] and gamestate=="p1play" and not win and not lose then
if active_meal then message_bottom = "Cannot place. TAP to pass." end
end
--what if noone has any eligible cards?
cansomeoneputsomething=false
for i=1, 4 do
if passlist[i]==false then cansomeoneputsomething = true end
end
print(cansomeoneputsomething)
if not cansomeoneputsomething and not win and not lose then
active_meal = false
if last_player==1 then
gamestate="p1play"
elseif last_player==2 then
gamestate="p2play"
elseif last_player==3 then
gamestate="p3play"
else
gamestate="p4play"
end
turn_number = last_player
newMsg("meal cannot continue. player "..last_player.." will start.")
end
--handles ai picking that happens after your pick
if (seccounter>2) and has_gamestarted then --ai picks every 2 second
if gamestate=="p4play" then
ai_pick(4, picktype)
gamestate="p1play"
last_player=4
turn_number=1
end
if gamestate=="p3play" then
ai_pick(3, picktype)
gamestate="p4play"
last_player=3
turn_number=4
end
if gamestate=="p2play" then
ai_pick(2, picktype)
gamestate="p3play"
last_player=2
turn_number=3
end
seccounter=0
end
end
function game:mousepressed(x, y, button, istouch)
has_gamestarted = true
--handles post game
if (win or lose) then
initialize_all()
deal()
Gamestate.switch(menu)
end
--confirms your card
if is_confirming and not lose then
if x<WIDTH/2 then
is_confirming=false
if not active_meal then
newMsg("player 1 set new "..meal_name.." meal")
message_top = "You set a new "..meal_name.." meal"
active_meal=true
go_max = go_left
grow_max = grow_left
glow_max = glow_left
else
newMsg("player 1 played "..hand[selected_ndx].name)
message_top = "You set a "..hand[selected_ndx].name.." card"
go_left = tempgo
grow_left = tempgrow
glow_left = tempglow
end
table.remove(hand,selected_ndx)
passlist = {false,false,false,false}
is_confirming = false
last_player=1
turn_number = 2
gamestate="p2play"
else
is_confirming=false
end
end
--handles your picking of cards
if not paused and not lose then
if gamestate=="p1play" then
--what if you can't play any card?
if not hand1e[1] and not hand1e[2] and not hand1e[3] and not hand1e[4] and not hand1e[5] then --no pwede
if #hand<5 and active_meal then
drawcard(hand)
end
passlist[1] = true
turn_number = 2
gamestate="p2play"
end
--checks for each card in hand
for i=1,#hand do
cardleft = hand_pos_x[i]
cardtop = hand_pos_y[i]
if (x>=cardleft and x<=cardleft+nil_img:getWidth()*csf and y>=cardtop and y<=cardtop+nil_img:getHeight()*csf) then
if not active_meal then
--get meal values from card
meal_name = hand[i].name
food_name = meal_name
meal_picture = hand[i].pic
food_desc = hand[i].desc
meal_player = 1
selected_ndx = i
go_left = hand[i].goval
grow_left = hand[i].growval
glow_left = hand[i].glowval
--print(meal_name,go_left,grow_left,glow_left)
is_confirming=true
else --there's a meal on the plate!
skipthis=false
if not hand1e[1] and not hand1e[2] and not hand1e[3] and not hand1e[4] and not hand1e[5] then --no pwede
passlist[1] = true
turn_number = 2
gamestate="p2play"
else
if hand1e[i] and #hand>=i then --card can be picked
if hand[i].kind=="go" then
tempgo = go_left-hand[i].nutval
tempglow = glow_left
tempgrow = grow_left
elseif hand[i].kind=="grow" then
tempgrow = grow_left-hand[i].nutval
tempgo = go_left
tempglow = glow_left
elseif hand[i].kind=="glow" then
tempglow = glow_left-hand[i].nutval
tempgo = go_left
tempgrow = grow_left
end
else
skipthis=true
end
if not skipthis then
--print(meal_name,go_left,grow_left,glow_left)
--print(hand1e[1],hand1e[2],hand1e[3],hand1e[4],hand1e[5],i)
is_confirming=true
meal_picture = hand[i].pic
food_desc = hand[i].desc
food_name = hand[i].name
selected_ndx = i
end
end
end
end
end
end
end
--check if you won
if #hand==0 then
win = true
end
--handles pause and resume
if (not paused and x<pause_img:getWidth()*TF/4.16875+10 and y<pause_img:getHeight()*TF/4.16875+10) then
paused=true
elseif paused then
paused=false
end
end
function game:draw()
love.graphics.setColor(255,255,255)
love.graphics.rectangle("fill",0,0,WIDTH*TF,HEIGHT*TF) --draw white bg
love.graphics.draw(bg_img,0,0,0,1*TF/4.16875,1*TF/4.16875*HEIGHT/(320*TF),0,0,0,0)
love.graphics.draw(pause_img,5*TF,5*TF,0,1*TF/4.16875,1*TF/4.16875,0,0,0,0)
love.graphics.draw(plate_img,WIDTH/5,HEIGHT/2-plate_img:getHeight()*TF/(2*4.16875),0,1*TF/4.16875,1*TF/4.16875,0,0)
--draw the progression plate
if (go_max-go_left)/go_max > 0.35 then
love.graphics.draw(half_go,WIDTH*0.35,HEIGHT*0.3,0,TF,TF)
end
if (go_max-go_left)/go_max > 0.7 then
love.graphics.draw(half_go,WIDTH*0.37,HEIGHT*0.4,0,TF,TF)
end
if (grow_max-grow_left)/grow_max > 0.35 then
love.graphics.draw(half_grow1,WIDTH*0.27,HEIGHT*0.21,0,TF,TF)
end
if (grow_max-grow_left)/grow_max > 0.7 then
love.graphics.draw(half_grow2,WIDTH*0.215,HEIGHT*0.32,0,TF,TF)
end
if (glow_max-glow_left)/glow_max > 0.35 then
love.graphics.draw(half_glow1,WIDTH*0.18,HEIGHT*0.46,0,TF,TF)
end
if (glow_max-glow_left)/glow_max > 0.7 then
love.graphics.draw(half_glow2,WIDTH*0.26,HEIGHT*0.56,0,TF,TF)
end
--draw the progression values
love.graphics.setColor(255,255,150,150)
love.graphics.rectangle("fill",WIDTH*2.75/5,HEIGHT/4,HEIGHT/2,HEIGHT/2)
love.graphics.setColor(255,255,255)
love.graphics.draw(icon_go,WIDTH*2.85/4,HEIGHT*0.3,0,TF/4.16875/2,TF/4.16875/2)
love.graphics.draw(icon_grow,WIDTH*2.85/4,HEIGHT*0.45,0,TF/4.16875/2,TF/4.16875/2)
love.graphics.draw(icon_glow,WIDTH*2.85/4,HEIGHT*0.6,0,TF/4.16875/2,TF/4.16875/2)
love.graphics.setColor(0,0,0)
love.graphics.setFont(menuFont)
love.graphics.printf(go_left.."/"..go_max,WIDTH*2.4/4,HEIGHT*0.3,WIDTH)
love.graphics.printf(grow_left.."/"..grow_max,WIDTH*2.4/4,HEIGHT*0.45,WIDTH)
love.graphics.printf(glow_left.."/"..glow_max,WIDTH*2.4/4,HEIGHT*0.6,WIDTH)
love.graphics.setColor(255,255,255)
-- if card has been picked (removed)
--love.graphics.draw(recent_card,WIDTH/2-Aimg:getWidth()/2*csf, HEIGHT/2-Aimg:getHeight()/2*csf, 0, csf, csf, 0, 0)
--draw stats overlay
--[[
love.graphics.setFont(love.graphics.newFont(10*TF))
love.graphics.printf("Go: "..tostring(go_left),0,0,WIDTH)
love.graphics.printf("Grow: "..tostring(grow_left),0,10*TF,WIDTH)
love.graphics.printf("Glow: "..tostring(glow_left),0,20*TF,WIDTH)
love.graphics.printf(meal_name.." meal set by player "..tostring(meal_player),0,30*TF,WIDTH)
if not hand1e[1] and not hand1e[2] and not hand1e[3] and not hand1e[4] and not hand1e[5] and gamestate=="p1play" and not win and not lose and not is_confirming then
love.graphics.setFont(love.graphics.newFont(15*TF))
love.graphics.setColor(0,0,0)
if active_meal then
--love.graphics.printf("Cannot play a card. TAP to pass.",0,HEIGHT/2-15*TF,WIDTH,"center")
else
--love.graphics.printf("Set a new meal. TAP to pick any card.",0,HEIGHT/2-15*TF,WIDTH,"center")
end
end
--]]
--draw message history queue
--[[
love.graphics.setColor(0,0,0,50)
love.graphics.setFont(love.graphics.newFont(12*TF))
love.graphics.rectangle("fill",WIDTH*2/3,15*TF,WIDTH/3,4*10*TF*100)
love.graphics.setColor(255,255,255)
love.graphics.printf("History",WIDTH*2/3+10*TF,3*TF, WIDTH/3)
love.graphics.setFont(love.graphics.newFont(10*TF))
for i=1, #msgq do
love.graphics.printf(msgq[i],WIDTH*2/3+5*TF,(5+10*i)*TF, WIDTH/3)
end
--]]
--draw top and bottom messages
if not is_confirming and not (lose or win) then
love.graphics.setColor(0,0,0,100)
love.graphics.rectangle("fill",0,HEIGHT*0.8,WIDTH,HEIGHT*0.2)
love.graphics.setColor(255,255,50)
love.graphics.setFont(menuFontDesc)
love.graphics.printf(message_top,WIDTH*0.7,10*TF,WIDTH*0.3)
love.graphics.printf("Deck Remaining: "..#deck,WIDTH*0.1,10*TF,WIDTH)
love.graphics.setFont(menuFont)
love.graphics.printf(message_bottom,WIDTH*0.55,HEIGHT*0.8,WIDTH*0.45)
love.graphics.setColor(255,255,255)
end
for i=1, #hand do --draws the hand cards
love.graphics.draw(hand[i].pic,(nil_img:getWidth()+5)*csf*i,HEIGHT-nil_img:getHeight()*csf,0,csf/4.16875/2,csf/4.16875/2,0,0)
end
--ai players hands draw
love.graphics.draw(foebacks[#hand4+1],0,HEIGHT/2-foebacks[#hand4+1]:getHeight()*TF/2/4.16875,0,1*TF/4.16875,1*TF/4.16875)
love.graphics.draw(foebacks[#hand2+1],WIDTH,HEIGHT/2+foebacks[#hand2+1]:getHeight()*TF/2/4.16875,3.14,1*TF/4.16875,1*TF/4.16875)
love.graphics.draw(foebacks[#hand3+1],WIDTH/2+foebacks[#hand3+1]:getHeight()*TF/2/4.16875,0,3.14/2,1*TF/4.16875,1*TF/4.16875)
--if game hasn't started yet
if (not has_gamestarted) then
love.graphics.setColor(200,255,200)
love.graphics.rectangle("fill",0,0,WIDTH,HEIGHT)
love.graphics.setColor(0,0,0)
love.graphics.setFont(love.graphics.newFont(30*TF))
love.graphics.printf("Hi, " .. playername .. "! Click to Play!",0,HEIGHT/2-30*TF,WIDTH,"center")
end
--if selecting
if is_confirming and not lose and not win and gamestate == "p1play" then
love.graphics.setColor(0,0,0,100)
love.graphics.rectangle("fill",0,0,WIDTH,HEIGHT)
love.graphics.setColor(255,255,255)
love.graphics.draw(meal_picture,WIDTH/4-meal_picture:getWidth(),HEIGHT/2-meal_picture:getHeight(),0,2,2)
love.graphics.setFont(coolFont)
love.graphics.setColor(255,255,100)
love.graphics.printf(food_name,WIDTH/2,HEIGHT/2-meal_picture:getHeight(),WIDTH)
love.graphics.setFont(menuFontDesc)
love.graphics.printf(food_desc,WIDTH*0.45,HEIGHT/3,WIDTH/2)
love.graphics.printf("Tap the left side of the screen to CONFIRM.",WIDTH*0.45,HEIGHT/2,WIDTH/2)
love.graphics.printf("Tap the right side of the screen to CANCEL.",WIDTH*0.45,HEIGHT/2+15*TF,WIDTH/2)
love.graphics.setColor(255,255,255)
end
--if win
if win then
love.graphics.draw(fakewin_img,0,0,0,TF,TF*HEIGHT/(320*TF))
end
--if lose
if lose then
love.graphics.draw(fakelose_img,0,0,0,TF,TF*HEIGHT/(320*TF))
end
--if paused
if paused and not win and not lose then
love.graphics.draw(fakepausescreen_img,0,0,0,TF,TF*HEIGHT/(320*TF))
end
end
function options:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0, 0, 0.24*TF, 0.24*TF*HEIGHT/(320*TF))
love.graphics.draw(optionsPad, 212*TF, 82*TF, 0, 0.24*TF, 0.24*TF)
if sfxIsOn == true then
sfxImg = sfxOn
sfxY = 105*TF
else
sfxImg = sfxOff
sfxY = 117*TF
end
love.graphics.draw(sfxImg, 245*TF, sfxY, 0, 0.24*TF, 0.24*TF)
if musicIsOn == true then
bgm:play()
musicImg = musicOn
else
bgm:stop()
musicImg = musicOff
end
love.graphics.draw(musicImg, 355*TF, 105*TF, 0, 0.24*TF, 0.24*TF)
love.graphics.setColor(54, 56, 10)
love.graphics.setFont(menuFont)
love.graphics.printf("CREDITS", 266*TF, 153*TF, 500*TF, "left")
love.graphics.printf("BACK", 284*TF, 183*TF, 500*TF, "left")
end
function options:mousepressed(x, y, button, istouch)
if button == 1 and inCircle(265*TF, 125*TF, 20*TF, x, y) then
sfxIsOn = not(sfxIsOn)
elseif button == 1 and inCircle(375*TF, 125*TF, 20*TF, x, y) then
musicIsOn = not(musicIsOn)
elseif button == 1 and x >= 213*TF and x <= 427*TF and y >= 156*TF and y <= 185*TF then
Gamestate.switch(credits)
elseif button == 1 and x >= 213*TF and x <= 427*TF and y >= 186*TF and y <= 215*TF then
Gamestate.switch(menu)
end
end
function credits:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0, 0, 0.24*TF, 0.24*TF*HEIGHT/(320*TF))
love.graphics.setColor(0, 0, 0)
love.graphics.printf("Tomy Callanta", 72*TF, 55*TF, 500*TF, "left")
love.graphics.printf("Mabry Fonseca", 72*TF, 100*TF, 500*TF, "left")
love.graphics.printf("Carlo Mendoza", 72*TF, 145*TF, 500*TF, "left")
love.graphics.printf("Chino Nava", 72*TF, 190*TF, 500*TF, "left")
love.graphics.printf("Christian Chan", 72*TF, 235*TF, 500*TF, "left")
end
function credits:mousepressed(x, y, button, istouch)
if button == 1 then
Gamestate.switch(options)
end
end
function summary:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0, 0, 0.24*TF, 0.24*TF*HEIGHT/(320*TF))
love.graphics.setColor(0, 0, 0)
love.graphics.printf("Summary", 30*TF, 10*TF, 1150, "center")
love.graphics.setFont(medFont)
love.graphics.printf("Tong-EATS is a simple 3–4 player card game software intended for children (ages 6+), but can also be played by adults. The game would be a mobile application that uses the internet to connect the players to each other. However, this prototype version only pits the player against three AI players.", 30*TF, 70*TF, 1150)
love.graphics.printf("The purpose of the game is to teach children proper nutrition in a fun and entertaining way. More specifically, the things that the players of the game should learn are the following:\n(1) different basic categories of healthy food (go, grow glow)\n(2) balancing these three kinds of food properly.", 30*TF, 190*TF, 1150)
love.graphics.setFont(menuFont)
end
function summary:mousepressed(x, y, button, istouch)
if button == 1 then
Gamestate.switch(helps)
end
end
function helps:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0, 0, 0.24*TF, 0.24*TF*HEIGHT/(320*TF))
love.graphics.setColor(0, 0, 0)
love.graphics.printf("Help", 30*TF, 2*TF, 1150, "center")
love.graphics.setFont(medFontDesc)
love.graphics.printf("Below are the formalized mechanics of the game:\n\n\nA. This game is made for 3 - 4 players. Each of them gets five (5) cards at the start.\n\nB. First person to drop a card decides the “meal” to be made by the players. Each card defines the meal components it requires, and meal options are written on the base card.\n\nC. The players then drop a valid card on their turn. Valid cards have weight less than or equal to the remaining total weight in each category.\n\nD. If the player has no valid card, he / she passes and draws a card from the deck.\n\nE. If a player puts down a card and all others pass, the player drops another card to start a next meal.\n\nF. Also, if a player completes a previous meal, he / she starts a new one by playing another card.\n\nG.The first person to play his / her entire hand wins!", 30*TF, 50*TF, 1150)
love.graphics.setFont(menuFont)
end
function helps:mousepressed(x, y, button, istouch)
if button == 1 then
Gamestate.switch(menu)
end
end
function stats:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(bg, 0, 0, 0, 0.24*TF, 0.24*TF*HEIGHT/(320*TF))
love.graphics.setColor(0, 0, 0)
love.graphics.printf("STATS BRUH", 72*TF, 55*TF, 500, "left")
end
function stats:mousepressed(x, y, button, istouch)
if button == 1 then
Gamestate.switch(menu)
end
end
function love.keypressed (key)
if key=="escape" then
if Gamestate.current() == menu then love.event.quit()
elseif Gamestate.current() == game and (lose or win) then love.event.quit()
elseif Gamestate.current() == credits then Gamestate.switch(options)
elseif Gamestate.current() == helps then Gamestate.switch(summary)
elseif Gamestate.current() == stats or Gamestate.current() == summary or Gamestate.current() == options then Gamestate.switch(menu)
elseif is_confirming then is_confirming = false end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment