From 76a636d1a14d55dda924bf4afbfb0163a2f51f0a Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sun, 20 Sep 2020 12:22:29 -0500 Subject: [PATCH] Minor refactoring --- art/sprites/particle.png | Bin 0 -> 406 bytes main.lua | 142 ++++++++++++++++++++------------------- 2 files changed, 73 insertions(+), 69 deletions(-) create mode 100644 art/sprites/particle.png diff --git a/art/sprites/particle.png b/art/sprites/particle.png new file mode 100644 index 0000000000000000000000000000000000000000..d2e87fd9d942a419b0acf9ce69a7e1ee3a5bf72b GIT binary patch literal 406 zcmeAS@N?(olHy`uVBq!ia0vp^tRT$60wmSb-sJ%)g=CK)Uj~LMH3o);76yi2K%s^g z3=E|P3=FRl7#OT(FffQ0%-I!a1C(G&@^*J&_}|`tW 610 ) then + if ( self.y > 700 ) then self:killFinalize() return false else @@ -387,6 +397,17 @@ function Flier:physics_dead ( dt ) 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 () -- animations batSheet = love.graphics.newImage("art/sprites/bat.png") + flapFrames = {2,3,4,5} + idleFrames = {1} batFlapAnim = animx.newAnimation{ - img = batSheet, - tileWidth = 32, - frames = { 2, 3, 4, 5 } + img = batSheet, tileWidth = 32, frames = flapFrames }:onAnimOver( function() - player.actor:switch('idle') + self.actor:switch('idle') end ) batIdleAnim = animx.newAnimation { - img = batSheet, - tileWidth = 32, - frames = { 1 } + img = batSheet, tileWidth = 32, frames = idleFrames } batActor = animx.newActor { - ['idle'] = batIdleAnim, - ['flap'] = batFlapAnim + ['idle'] = batIdleAnim, ['flap'] = batFlapAnim }:switch('idle') Flier.initialize( self, 50, 100, batActor ) end +function Bat:update ( dt ) + self:physics( dt ) + self:checkBirdCollisions() +end + + -- return whether or not the Bat's colliding with given object function Bat:checkCollision ( other ) if ( colliding( self, other ) ) then @@ -440,17 +464,12 @@ function Bat:checkBirdCollisions () return nil end -function Bat:update ( dt ) - self:physics( dt ) - self:checkBirdCollisions() -end -- called after dead Bat falls through screen function Bat:killFinalize() lives = lives - 1 lifeText:set("Life " .. lives) - if ( lives <= 0 ) then gameover_load() else @@ -460,11 +479,7 @@ function Bat:killFinalize() end end -function Bat:kill () - self.living = false - psystem:moveTo(self.x, self.y) - psystem:emit(30) -end + ---------------------------------------- -- BIRD enemy characters @@ -472,45 +487,36 @@ end Bird = class('Bird', Flier) function Bird:initialize ( x, y ) + self.species = math.random(1,3) + -- 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") + flapFrames = { {2,3,4,5}, {7,8,9,10}, {7,8,9,10} } + idleFrames = { {1}, {6}, {6} } + birdFlapAnim = animx.newAnimation{ - img = birdSheet, - tileWidth = 32, - tileHeight = 32, - frames = flapFrames + img = birdSheet, tileWidth = 32, tileHeight = 32, frames = flapFrames[self.species] }:onAnimOver( function() self.actor:switch('idle') end ) birdIdleAnim = animx.newAnimation { - img = birdSheet, - tileWidth = 32, - tileHeight = 32, - frames = idleFrames + img = birdSheet, tileWidth = 32, tileHeight = 32, frames = idleFrames[self.species] } birdActor = animx.newActor { - ['idle'] = birdIdleAnim, - ['flap'] = birdFlapAnim + ['idle'] = birdIdleAnim, ['flap'] = birdFlapAnim }: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 ) end @@ -519,17 +525,6 @@ function Bird:update ( dt ) return self:physics( dt ) 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) function Bird:destiny () @@ -566,9 +561,18 @@ function Bird:destiny_y () end ----------------------------------------- --- GAME LOGIC ----------------------------------------- +-- after dead bird falls through screen +function Bird:killFinalize() + index = indexOf(birdRegistry, self) + table.remove( birdRegistry, index ) +end + + + +-------------------------------------------------------------------------------- +-- MISC GAME LOGIC +-------------------------------------------------------------------------------- +-- set up a new wave of birds function nextWave ( ) wave = wave + 1 waveText:set("Wave " .. wave) @@ -599,13 +603,12 @@ function judgeCollision ( a, b ) b.x_vel = b.x_vel * -1 end end - - ----------------------------------------- + +-------------------------------------------------------------------------------- -- UTIL blah blah blah ----------------------------------------- +-------------------------------------------------------------------------------- -- return whether or not two objects are colliding/overlapping 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 @@ -634,6 +637,7 @@ function greatestAbs ( a, b ) end end +-- return index of given item in list function indexOf ( list, item ) for i = 1,table.maxn(list) do if ( list[i] == item ) then