Skip to content

Commit

Permalink
Explosions are now separate. FPS got better?
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Robinson committed Jan 14, 2012
1 parent 7287a57 commit 344d3c0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 79 deletions.
37 changes: 9 additions & 28 deletions bad.lua
Expand Up @@ -4,43 +4,24 @@ function Bad:new(x, y)
return setmetatable({
x = x,
y = y,
state = "enter"
dead = false
}, {__index = self})
end

function Bad:advance(dt)
if self.state == "dying" then
self.death_duration = self.death_duration - dt

if self.death_duration < 0 then
self.state = "dead"
end
end
end

function Bad:hit(bullet)
if self.state == "enter" then
self.state = "dying"
self.death_duration = 1

return true
end
function Bad:collide(bullet)
self.dead = true
end

function Bad:draw()
local size = 20

if self.state == "enter" then
love.graphics.triangle(
'line',
self.x, self.y + (size / 2),
self.x + (size / 2), self.y - (size / 2),
self.x - (size / 2), self.y - (size / 2)
)
elseif self.state == "dying" then
local r, g, b, a = love.graphics.getColor()
love.graphics.setColor(math.random() * 255, math.random() * 255, math.random() * 255, self.death_duration * 255)
love.graphics.circle('fill', self.x, self.y, 50 * self.death_duration)
love.graphics.setColor(r, g, b, a)
end
love.graphics.triangle(
'line',
self.x, self.y + (size / 2),
self.x + (size / 2), self.y - (size / 2),
self.x - (size / 2), self.y - (size / 2)
)
end
7 changes: 0 additions & 7 deletions bullet.lua
Expand Up @@ -15,13 +15,6 @@ function Bullet:advance(dt)
self.y = self.y - self.v * math.cos(self.r) * dt
end

function Bullet:hits(thing)
abs_x = math.abs(self.x - thing.x)
abs_y = math.abs(self.y - thing.y)
distance = math.sqrt(math.pow(abs_x, 2) + math.pow(abs_y, 2))
return distance < self.radius
end

function Bullet:draw()
love.graphics.circle('fill', self.x, self.y, self.radius)
end
Expand Down
88 changes: 44 additions & 44 deletions main.lua
@@ -1,6 +1,7 @@
require 'bad'
require 'ship'
require 'boom'
require 'bullet'
require 'ship'

joystick = {
n = 0,
Expand All @@ -13,13 +14,22 @@ joystick = {
}

debug = {}

ships = {}
bullets = {}
baddies = {}
booms = {}

live = {ships, bullets, baddies}
all = {ships, bullets, baddies, booms}

function love.load(arg)
love.joystick.open(joystick.n)

ship = Ship:new(400, 300, joystick)
for i=1,1000,1 do
table.insert(ships, ship)

for i = 1, 1000, 1 do
table.insert(baddies, Bad:new(math.random() * 500, math.random() * 50))
end
end
Expand All @@ -35,69 +45,59 @@ function love.joystickreleased(j, b)
fire_everything = false
end

function delete_offscreen_bullets()
local trash = {}

for i, v in ipairs(bullets) do
if v:is_offscreen() then
table.insert(trash, i, 1)
end
end

for i, v in ipairs(trash) do
table.remove(bullets, v)
end
end

function update_fire_state(dt)
function update_fire_state()
if fire_everything then
table.insert(bullets, Bullet:new(ship.x, ship.y, "player"))
end
end

function destroy_baddies()
local trash = {}
function is_colliding(a, b)
local abs_x = a.x - b.x
local abs_y = a.y - b.y
-- LOSING
return math.abs(abs_x) < a.radius and math.abs(abs_y) < a.radius
-- WINNING
-- distance = math.sqrt(abs_x * abs_x + abs_y * abs_y)
-- return distance < a.radius
end

for i,bad in ipairs(baddies) do
for ib,bullet in ipairs(bullets) do
if bullet:hits(bad) then
local responded = bad:hit(bullet)
function collision_detection()
for i_bad, bad in ipairs(baddies) do
for i_bullet, bullet in ipairs(bullets) do
if is_colliding(bullet, bad) then
bad:collide(bullet)

if responded then
table.insert(trash, ib, 1)
break
if bad.dead then
table.remove(baddies, i_bad)
table.insert(booms, Boom:new(bad.x, bad.y))
end

table.remove(bullets, i_bullet)
end
end
end
end

for i, v in ipairs(trash) do
table.remove(bullets, v)
end
function update()
update_fire_state()
collision_detection()
end

function advance(dt)
for i, things in ipairs({{ship}, bullets, baddies}) do
for i, t in ipairs(things) do
for i_things, things in ipairs(all) do
for i_t, t in ipairs(things) do
t:advance(dt)

if t.dead then
table.remove(things, i_t)
end
end
end
end

function update(dt)
update_fire_state(dt)

destroy_baddies()
end

function clean()
delete_offscreen_bullets()
end

function love.update(dt)
update(dt)
advance(dt)
clean()
update(dt)
end

function love.keypressed(k)
Expand All @@ -107,7 +107,7 @@ function love.keypressed(k)
end

function love.draw()
for i, things in ipairs({{ship}, bullets, baddies}) do
for i, things in ipairs(all) do
for i, t in ipairs(things) do
t:draw()
end
Expand Down

0 comments on commit 344d3c0

Please sign in to comment.