Skip to content

Instantly share code, notes, and snippets.

@WesleyAC
Created December 13, 2013 16:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WesleyAC/7946902 to your computer and use it in GitHub Desktop.
Save WesleyAC/7946902 to your computer and use it in GitHub Desktop.
WorldPhysics, my new, hacked together, feature creeped library for love!
world = {
physics = {
new = function(objtype, d, i, w, h, ubody, ushape)
if world.physics[objtype] == nil then world.physics[objtype] = {all = {}} end
world.physics[objtype].all[#world.physics[objtype].all+1] = {}
world.physics[objtype].all[#world.physics[objtype].all].body = ubody
world.physics[objtype].all[#world.physics[objtype].all].shape = ushape
world.physics[objtype].all[#world.physics[objtype].all].fixture = love.physics.newFixture(world.physics[objtype].all[#world.physics[objtype].all].body, world.physics[objtype].all[#world.physics[objtype].all].shape, d)
world.physics[objtype].all[#world.physics[objtype].all].image = i
world.physics[objtype].all[#world.physics[objtype].all].width = w
world.physics[objtype].all[#world.physics[objtype].all].height = h
if world.physics[objtype].init ~= nil then world.physics[objtype].init(world.physics[objtype].all[#world.physics[objtype].all]) end
end,
render = function()
for obj,objkey in pairs(world.physics) do
if type(objkey) == "table" then
for objnum=1,#objkey.all,1 do
if objkey.all[objnum] ~= "null" then
love.graphics.draw(objkey.all[objnum].image, objkey.all[objnum].body:getX(), objkey.all[objnum].body:getY(), objkey.all[objnum].body:getAngle(), 1, 1, objkey.all[objnum].width/2, objkey.all[objnum].height/2)
end
end
end
end
end,
update = function(dt)
for obj,objkey in pairs(world.physics) do
if type(objkey) == "table" then
for objnum=1,#objkey.all,1 do
if objkey.all[objnum] ~= "null" then
if objkey.update ~= nil then objkey.update(objkey.all[objnum], dt) end
end
end
end
end
end,
delete = function(x, y, objtype)
deld = false
for obj,objkey in pairs(world.physics) do
if type(objkey) == "table" then
for objnum=1,#objkey.all,1 do
if objkey.all[objnum] ~= "null" then
if AABB(objkey.all[objnum].body:getX()-(objkey.all[objnum].width/2), objkey.all[objnum].body:getY()-(objkey.all[objnum].height/2), objkey.all[objnum].width, objkey.all[objnum].height, x, y, 0, 0) then
if obj == objtype or objtype == nil then
objkey.all[objnum].body:destroy()
objkey.all[objnum] = "null"
deld = true
end
end
end
end
end
end
return deld
end,
explode = function(x, y, objtype, num)
if world.physics.delete(x, y, objtype, img) then
for i=1,num,1 do
world.physics.new("particle", 2, img, 3, 3, love.physics.newBody(physicsworld, x, y, "dynamic"), love.physics.newRectangleShape(0, 0, 3, 3))
end
end
end,
initphysics = function(pxm)
love.physics.setMeter(pxm)
physicsworld = love.physics.newWorld(0, 9.81*pxm)
end,
addobjecttype = function(name, objtype)
if objtype ~= nil then
world.physics[name] = objtype
else
world.physics[name] = {all = {}}
end
end
},
debuginfo = {
render = function()
love.graphics.print("WorldPhysics v0.1.3 ALPHA\nFPS: " .. tostring(love.timer.getFPS()), 0, 0)
end
},
effects = {
screenshake = {
shaking = false,
timer = 1,
length = 0,
strength = 0,
shake = function(str)
rstr = world.effects.screenshake.strength+(0-world.effects.screenshake.strength)*(world.effects.screenshake.timer/world.effects.screenshake.length)
love.graphics.translate(math.random(-rstr, rstr), math.random(-rstr, rstr))
end,
startshake = function(str, length)
world.effects.screenshake.shaking = true
world.effects.screenshake.timer = 1
world.effects.screenshake.strength = str
world.effects.screenshake.length = length
end,
update = function(dt)
world.effects.screenshake.timer = world.effects.screenshake.timer + dt
if world.effects.screenshake.timer > world.effects.screenshake.length then world.effects.screenshake.shaking = false end
end,
render = function()
if world.effects.screenshake.shaking then world.effects.screenshake.shake(world.effects.screenshake.strength) end
end
},
shaders = {
toggle = function(shader)
world.effects.shaders[shader].active = not world.effects.shaders[shader].active
end,
setparams = function(shader, params)
for key,val in pairs(params) do
world.effects.shaders[shader].params[key] = val
end
end,
render = function()
for key,val in pairs(world.effects.shaders) do
if type(val) == "table" then
if val.active then
for pkey,pval in pairs(val.params) do
val[1]:send(pkey, pval)
end
love.graphics.setPixelEffect(val[1])
end
end
end
end
}
},
update = function(dt)
physicsworld:update(dt)
world.physics.update(dt)
world.effects.screenshake.update(dt)
end,
render = function()
world.effects.shaders.render()
world.effects.screenshake.render()
world.physics.render()
world.debuginfo.render()
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment