Skip to content

Instantly share code, notes, and snippets.

@mebens
mebens / gist:3991990
Created November 1, 2012 05:12
Calculate 8-axis movement angle with LÖVE and ammo-input
-- returns nil if there's no movement
function getDirection()
local xAxis = input.axisDown("left", "right")
local yAxis = input.axisDown("up", "down")
local xAngle = xAxis == 1 and 0 or (xAxis == -1 and math.tau / 2 or nil)
local yAngle = yAxis == 1 and math.tau / 4 or (yAxis == -1 and math.tau * 0.75 or nil)
if xAngle and yAngle then
-- x = 1, y = -1 is a special case the doesn't fit; not sure what I can do about it other than this:
if xAxis == 1 and yAxis == -1 then return yAngle + math.tau / 8 end
@mebens
mebens / gist:4035772
Created November 8, 2012 01:15
Spritemap that works with Ammo
Spritemap = class("Spritemap")
Spritemap._mt = {}
function Spritemap._mt:__index(key)
return rawget(self, "_" .. key) or self.class.__instanceDict[key]
end
Spritemap:enableAccessors()
function Spritemap:initialize(img, fw, fh, complete, ...)
@mebens
mebens / Sheet.lua
Created December 14, 2012 03:26
A Tilemap class. Unsure if the collision code works.
Sheet = class("Sheet")
function Sheet:initialize(img, tileWidth, tileHeight)
self.x = 0
self.y = 0
self.image = img
self.tw = tileWidth
self.th = tileHeight
self.quads = {}
@mebens
mebens / Tilemap.lua
Last active December 10, 2015 05:08
Working Tilemap class using a canvas.
Tilemap = class("Tilemap")
Tilemap._mt = {}
function Tilemap._mt:__index(key)
return rawget(self, "_" .. key) or self.class.__instanceDict[key]
end
Tilemap:enableAccessors()
function Tilemap:initialize(img, fw, fh, width, height)
@mebens
mebens / add-polars.lua
Created March 7, 2013 08:40
A Lua script that adds polar vectors, without compensation for significant figures.
#!/usr/bin/env lua
function exit(msg, code)
print(msg)
os.exit(code or 1)
end
function main()
if #arg < 4 then exit("Please provide a magnitude and angle for two polar vectors") end
local m1, a1, m2, a2 = unpack(arg)
@mebens
mebens / gist:4229527
Created December 6, 2012 23:54
Here's an interesting noise-based Love2D shader. Simply keep setting factor to a random number every frame (or some other interval).
extern float factor = 1;
extern float addPercent = 0.1;
extern float clamp = 0.85;
// from http://www.ozone3d.net/blogs/lab/20110427/glsl-random-generator/
float rand(vec2 n)
{
return 0.5 + 0.5 * fract(sin(dot(n.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
@mebens
mebens / gist:3929259
Created October 22, 2012 02:07
Generate a linear radial gradient in Love2D
local function scale(x, min1, max1, min2, max2)
return min2 + ((x - min1) / (max1 - min1)) * (max2 - min2)
end
local function distance(x1, y1, x2, y2)
return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end
function radialGradient(radius)
local data = love.image.newImageData(radius * 2, radius * 2)
@mebens
mebens / postfx.lua
Created December 6, 2012 02:30
A post-processing effects manager for Love2D
postfx = {}
postfx.all = {}
postfx.active = true
local PixelEffect = class("PixelEffect")
function PixelEffect:initialize(effect)
self.effect = effect
self.active = true
end
@mebens
mebens / gist:3904043
Created October 17, 2012 06:36
Generate sine wave tones in Love2D
local samples = 100000
local data = love.sound.newSoundData(samples)
local noteChange = 10000
local note = 200
local change = 50
local minimum = 100
for i = 0, samples * 2 - 1 do
if i % noteChange == 0 then
local factor = -2 + math.random(0, 4)
@mebens
mebens / guide.sh
Created January 13, 2019 07:46
Setting up an up-to-date LAMP stack server on Ubuntu 18.04
# Script correlating to tutorial at http://ebens.me/post/install-lamp-stack-ubuntu
# Even though you probably could, don't run it all at once
# new user
adduser new_user
usermod -a -G sudo new_user
su - new_user
# new repos