Skip to content

Instantly share code, notes, and snippets.

@a327ex
a327ex / Vector.lua
Created November 14, 2012 17:52
Vector class
-- https://github.com/vrld/hump/blob/master/vector.lua
-- http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/
local assert = assert
local sqrt, cos, sin, atan = math.sqrt, math.cos, math.sin, math.atan2
Vector = {}
Vector.__index = Vector
function Vector.new(x, y)
@a327ex
a327ex / struct.lua
Last active December 10, 2015 06:18
Struct library
-- The problem:
-- Lua has a problem whenever you want to define a C-like struct.
-- For instance, when I want to define a point structure without having
-- to define a new class I can create a table like so:
--
-- {x = number, y = number}
--
-- And a function that does something to points can use them like this:
--
-- function dist(p1, p2)
@a327ex
a327ex / chrono.lua
Created December 31, 2012 15:41
Chrono library
-- There are some timing related constructs that appear very often
-- while developing games. One such construct is as follows:
--
-- function update(dt)
-- counter = counter + dt
-- if counter >= action_delay then
-- counter = 0
-- action()
-- end
-- end
@a327ex
a327ex / getPSO.lua
Last active December 15, 2015 19:58
getPSO and basic particle system templates usage
function getPSO(filename)
local templates = loadstring("return " .. love.filesystem.read(filename))()
local pss = {}
local degToRad = function(d) return d*math.pi/180 end
local getPS = function(template)
local sprite = love.graphics.newImage('your_default_sprite_here.png')
local ps = love.graphics.newParticleSystem(sprite, template.buffer_size)
ps:setBufferSize(template.buffer_size)
local colors = {}
for i = 1, 8 do
@a327ex
a327ex / Graph.lua
Last active May 1, 2017 09:27
Graph class
Graph = {}
Graph.__index = Graph
function Graph.new()
return setmetatable({adjacency_list = {}}, Graph)
end
function Graph:__tostring()
local str = ""
for node, list in pairs(self.adjacency_list) do
@a327ex
a327ex / Graph.lua
Created June 23, 2013 00:26
Graph class with Floyd-Warshall
Graph = {}
Graph.__index = Graph
function Graph.new()
return setmetatable({adjacency_list = {}, nodes = {}, edges = {}, floyd_dists = {}}, Graph)
end
function Graph:__tostring()
local str = "----\nAdjacency List: \n"
for node, list in pairs(self.adjacency_list) do
@a327ex
a327ex / main.lua
Created August 21, 2015 20:25
Auto-updater
local async = require('async')
function love.load(args)
async.load()
async.ensure.atLeast(2)
local version = nil
local version_request = async.define("version_request", function()
local request = require('luajit-request')
local response = request.send(link to the version file)
export Object
class Object
new: (level, x, y, opts) =>
for k, v in pairs(opts) do self[k] = v
@id = uuid!
@dead = false
@creation_time = love.timer.getTime!
@depth = 50
@level = level
function love.load()
-- Setup necessary to make the screen have a pixelated look
window_width, window_height = 1280, 720
game_width, game_height = 480, 270
love.window.setMode(window_width, window_height)
love.graphics.setDefaultFilter('nearest')
love.graphics.setLineStyle('rough')
canvas = love.graphics.newCanvas(game_width, game_height)
physics_step()
timers = {}
function after(delay, action, repeatable, after, tag)
local tag = tag or uid() -- uid returns a unique id every time it's called, "or" will resolve the expression to the result of uid() if tag is nil (the same as if not tag then tag = uid() end)
timers[tag] = {type = 'after', current_time = 0, delay = delay, action = action, repeatable = repeatable, after = after, tag = tag}
end
function tween(delay, target, source, method, after, tag)
local tag = tag or uid()
local initial_values = {}