Minor refactoring

This commit is contained in:
Jaidyn Ann 2020-09-20 12:22:29 -05:00
parent 8a8b67f656
commit 76a636d1a1
2 changed files with 73 additions and 69 deletions

BIN
art/sprites/particle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 B

138
main.lua
View File

@ -8,8 +8,9 @@ left = 0; right = 1; up = 2; down = 3
upleft = 4; downleft = 5; upright = 6; downright = 7 upleft = 4; downleft = 5; upright = 6; downright = 7
menu = 0; game = 1; gameover = 2 menu = 0; game = 1; gameover = 2
--------------------------------------------------------------------------------
---------------------------------------- -- GAME STATES
--------------------------------------------------------------------------------
-- LOVE -- LOVE
---------------------------------------- ----------------------------------------
-- LOAD -- LOAD
@ -269,7 +270,10 @@ function game_keyreleased (key)
end end
----------------------------------------
--------------------------------------------------------------------------------
-- ENTITY CLASSES
--------------------------------------------------------------------------------
-- FLIER entity superclass -- FLIER entity superclass
---------------------------------------- ----------------------------------------
-- birds and bats both fly. fliers. -- birds and bats both fly. fliers.
@ -321,9 +325,14 @@ end
-- physics on the x-axis -- physics on the x-axis
function Flier:physics_x ( dt ) function Flier:physics_x ( dt )
turn = 150
if ( self.species ) then -- if bird
max_vel = 280
min_vel = -280
else
max_vel = 300 max_vel = 300
min_vel = -300 min_vel = -300
turn = 150 end
-- holding arrow-key -- holding arrow-key
if ( self.moving ) then if ( self.moving ) then
@ -374,12 +383,13 @@ function Flier:physics_y ( dt )
self.y = self.y + self.y_vel * dt self.y = self.y + self.y_vel * dt
end end
-- if not living; in death-spiral
function Flier:physics_dead ( dt ) function Flier:physics_dead ( dt )
-- ignore all input, fall through bottom -- ignore all input, fall through bottom
gravity = 2 gravity = 2
self.y_vel = self.y_vel + gravity self.y_vel = self.y_vel + gravity
self.y = self.y + self.y_vel * dt self.y = self.y + self.y_vel * dt
if ( self.y > 610 ) then if ( self.y > 700 ) then
self:killFinalize() self:killFinalize()
return false return false
else else
@ -387,6 +397,17 @@ function Flier:physics_dead ( dt )
end end
end end
-- kill the Flier, show cool particles
function Flier:kill ()
self.living = false
psystem:moveTo(self.x, self.y)
psystem:emit(30)
end
-- run after Flier falls through screen
function Flier:killFinalize ()
end
---------------------------------------- ----------------------------------------
@ -397,29 +418,32 @@ Bat = class('Bat', Flier)
function Bat:initialize () function Bat:initialize ()
-- animations -- animations
batSheet = love.graphics.newImage("art/sprites/bat.png") batSheet = love.graphics.newImage("art/sprites/bat.png")
flapFrames = {2,3,4,5}
idleFrames = {1}
batFlapAnim = animx.newAnimation{ batFlapAnim = animx.newAnimation{
img = batSheet, img = batSheet, tileWidth = 32, frames = flapFrames
tileWidth = 32,
frames = { 2, 3, 4, 5 }
}:onAnimOver( function() }:onAnimOver( function()
player.actor:switch('idle') self.actor:switch('idle')
end ) end )
batIdleAnim = animx.newAnimation { batIdleAnim = animx.newAnimation {
img = batSheet, img = batSheet, tileWidth = 32, frames = idleFrames
tileWidth = 32,
frames = { 1 }
} }
batActor = animx.newActor { batActor = animx.newActor {
['idle'] = batIdleAnim, ['idle'] = batIdleAnim, ['flap'] = batFlapAnim
['flap'] = batFlapAnim
}:switch('idle') }:switch('idle')
Flier.initialize( self, 50, 100, batActor ) Flier.initialize( self, 50, 100, batActor )
end end
function Bat:update ( dt )
self:physics( dt )
self:checkBirdCollisions()
end
-- return whether or not the Bat's colliding with given object -- return whether or not the Bat's colliding with given object
function Bat:checkCollision ( other ) function Bat:checkCollision ( other )
if ( colliding( self, other ) ) then if ( colliding( self, other ) ) then
@ -440,17 +464,12 @@ function Bat:checkBirdCollisions ()
return nil return nil
end end
function Bat:update ( dt )
self:physics( dt )
self:checkBirdCollisions()
end
-- called after dead Bat falls through screen -- called after dead Bat falls through screen
function Bat:killFinalize() function Bat:killFinalize()
lives = lives - 1 lives = lives - 1
lifeText:set("Life " .. lives) lifeText:set("Life " .. lives)
if ( lives <= 0 ) then if ( lives <= 0 ) then
gameover_load() gameover_load()
else else
@ -460,11 +479,7 @@ function Bat:killFinalize()
end end
end end
function Bat:kill ()
self.living = false
psystem:moveTo(self.x, self.y)
psystem:emit(30)
end
---------------------------------------- ----------------------------------------
-- BIRD enemy characters -- BIRD enemy characters
@ -472,45 +487,36 @@ end
Bird = class('Bird', Flier) Bird = class('Bird', Flier)
function Bird:initialize ( x, y ) function Bird:initialize ( x, y )
self.species = math.random(1,3)
-- animations -- animations
self.species = math.random(2,3)
if ( self.species == 3 ) then
flapFrames = { 2, 3, 4, 5 }
idleFrames = { 1 }
elseif ( self.species == 2 or self.species == 3 ) then
flapFrames = { 7, 8, 9, 10 }
idleFrames = { 6 }
end
if ( self.species == 3 ) then
self.direction = math.random(left, right)
else
self.direction = right
end
birdSheet = love.graphics.newImage("art/sprites/bird.png") birdSheet = love.graphics.newImage("art/sprites/bird.png")
flapFrames = { {2,3,4,5}, {7,8,9,10}, {7,8,9,10} }
idleFrames = { {1}, {6}, {6} }
birdFlapAnim = animx.newAnimation{ birdFlapAnim = animx.newAnimation{
img = birdSheet, img = birdSheet, tileWidth = 32, tileHeight = 32, frames = flapFrames[self.species]
tileWidth = 32,
tileHeight = 32,
frames = flapFrames
}:onAnimOver( function() }:onAnimOver( function()
self.actor:switch('idle') self.actor:switch('idle')
end ) end )
birdIdleAnim = animx.newAnimation { birdIdleAnim = animx.newAnimation {
img = birdSheet, img = birdSheet, tileWidth = 32, tileHeight = 32, frames = idleFrames[self.species]
tileWidth = 32,
tileHeight = 32,
frames = idleFrames
} }
birdActor = animx.newActor { birdActor = animx.newActor {
['idle'] = birdIdleAnim, ['idle'] = birdIdleAnim, ['flap'] = birdFlapAnim
['flap'] = birdFlapAnim
}:switch('idle') }:switch('idle')
self.actor = birdActor
if ( self.species == 3 ) then
self.direction = math.random(left, right)
else
self.direction = right
end
Flier.initialize( self, x, y, birdActor ) Flier.initialize( self, x, y, birdActor )
end end
@ -519,17 +525,6 @@ function Bird:update ( dt )
return self:physics( dt ) return self:physics( dt )
end end
function Bird:killFinalize()
index = indexOf(birdRegistry, self)
table.remove( birdRegistry, index )
end
function Bird:kill ()
self.living = false
psystem:moveTo(self.x, self.y)
psystem:emit(30)
end
-- basic "ai" (determines where the bird should go) -- basic "ai" (determines where the bird should go)
function Bird:destiny () function Bird:destiny ()
@ -566,9 +561,18 @@ function Bird:destiny_y ()
end end
---------------------------------------- -- after dead bird falls through screen
-- GAME LOGIC function Bird:killFinalize()
---------------------------------------- index = indexOf(birdRegistry, self)
table.remove( birdRegistry, index )
end
--------------------------------------------------------------------------------
-- MISC GAME LOGIC
--------------------------------------------------------------------------------
-- set up a new wave of birds
function nextWave ( ) function nextWave ( )
wave = wave + 1 wave = wave + 1
waveText:set("Wave " .. wave) waveText:set("Wave " .. wave)
@ -602,10 +606,9 @@ end
--------------------------------------------------------------------------------
----------------------------------------
-- UTIL blah blah blah -- UTIL blah blah blah
---------------------------------------- --------------------------------------------------------------------------------
-- return whether or not two objects are colliding/overlapping -- return whether or not two objects are colliding/overlapping
function colliding ( a, b ) function colliding ( a, b )
if ( inRange(a.x, b.x - 16, b.x + 16) and inRange(a.y, b.y - 16, b.y + 16) ) then if ( inRange(a.x, b.x - 16, b.x + 16) and inRange(a.y, b.y - 16, b.y + 16) ) then
@ -634,6 +637,7 @@ function greatestAbs ( a, b )
end end
end end
-- return index of given item in list
function indexOf ( list, item ) function indexOf ( list, item )
for i = 1,table.maxn(list) do for i = 1,table.maxn(list) do
if ( list[i] == item ) then if ( list[i] == item ) then