-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchicagoEnemyObjects.lua
executable file
·326 lines (273 loc) · 9.75 KB
/
chicagoEnemyObjects.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
----------------------------------
-- THE BIG 3 VIDEO GAME ---
-- [email protected] ---
----------------------------------
-- Script sets the properties of all enemies (things that are alive)
-- for the Chicago endless runner level
local mainScene = nil
local prevTime = system.getTimer()
local enemyObjects = {
activeEnemies = {}
}
local function enemyDispose(enemy,bDestroyAll)
if ( enemy.dispose ) then
enemy:dispose(bDestroyAll)
enemy.dispose = nil
end
if( not enemy.bDoNotDestroy ) then
enemy:removeSelf()
else
enemy.isVisible = false
end
enemy = nil
end
-- Damage behavior when the enemy attacks the player
local function doDamageSplash(self,enemy,bCentered)
local xMin,yMin = mainScene.city.displayGroup:contentToLocal( enemy.contentBounds.xMin, enemy.contentBounds.yMin )
local xMax,yMax = mainScene.city.displayGroup:contentToLocal( enemy.contentBounds.xMax, enemy.contentBounds.yMax )
local x,y = xMin+((xMax-xMin)/2), yMin+((yMax-yMin)/2)
if( not bCentered ) then
mainScene:showDamageHit( x - ((enemy.width/4)), y, true )
else
mainScene:showDamageHit( x, y, true )
end
end
-- See if enemy needs to be discarded (dead of off screen)
local function checkForDiscardedObjects(self)
for i, obj in ipairs( self.activeEnemies ) do
local bDiscard = false
if( not obj ) then
bDiscard = true
else
if( obj.contentBounds.xMax < mainScene.leftEdge and not obj.bDisableCulling ) then
bDiscard = true
end
if( not obj.health ) then
obj.health = 0
obj.health = 0
end
if( not bDiscard and obj.health <= 0 ) then
bDiscard = true
if( not obj.explosionScale ) then
obj.explosionScale = 1
end
mainScene:doExplosion( obj, obj.explosionScale)
end
end
if( bDiscard ) then
table.remove( self.activeEnemies, i )
if ( obj ) then
enemyDispose(obj)
break
end
end
end
end
-- Behavior for when an enemy is hit by a player weapon
function enemyObjects:doWeaponHit( weapon, enemy )
enemy.health = enemy.health - 1
if( enemy.health > 0 ) then
doDamageSplash(self, enemy, false )
else
if( enemy.onDeath ) then
enemy.onDeath()
end
doDamageSplash(self, enemy, true )
mainScene:doExplosion(enemy, enemy.explosionScale )
mainScene:addPoints(100)
end
end
function enemyObjects:getActive()
if( not mainScene.enemies.activeEnemies ) then
mainScene.enemies.activeEnemies = {}
end
return mainScene.enemies.activeEnemies
end
-- Get number of active enemies for debugging purposes
function GetActiveEnemies()
if( not mainScene ) then
return nil
end
return table.getn( mainScene.enemies.activeEnemies )
end
function enemyObjects:add( enemy )
table.insert( self.activeEnemies, enemy )
if( enemy.bGroundSpawn ) then
mainScene.city:setGroundSpawnCoordinate( enemy.x + (enemy.width/2) + 50, enemy.name )
end
end
function enemyObjects:spawn( enemy, randomDelay, minDelay )
if( mainScene.specialPowers:checkSpawn() ) then
return
end
if( self.bDisable and not enemy ) then
return
end
if( not randomDelay ) then
randomDelay = 1
end
if( not minDelay ) then
minDelay = 0
end
local obj = nil
if ( not enemy ) then
local candidates = {}
for i, candidate in ipairs( mainScene.enemies.allEnemies ) do
if(candidate.bEnableAutoSpawn ) then
table.insert( candidates, candidate )
end
end
if ( table.getn( candidates ) < 1 ) then
return
end
enemy = candidates[ math.random( table.getn( candidates ) ) ]
end
if( not enemy ) then
return
end
local function goEnemy()
if( not mainScene or mainScene.bGameOver ) then
return
end
obj = enemy:spawn(mainScene)
if( obj ) then
obj.isVisible = true
if( not obj.health ) then
obj.health = 1
end
if( table.getn( obj ) > 1 ) then
for i, item in ipairs( obj ) do
if( item.x ) then
enemyObjects:add( item )
end
end
else
if( not obj.x ) then
enemyObjects:add( obj[1] )
else
enemyObjects:add( obj )
end
end
end
end
local delay = math.random(randomDelay)+minDelay
-- If an item is spawning on the ground, build in a potential delay so two items aren't on the ground at the same time.
if( enemy.bGroundSpawn ) then
if( not mainScene.groundSpawnTime ) then
mainScene.groundSpawnTime = 0
end
if( ( system.getTimer() + delay ) <= ( mainScene.groundSpawnTime + 500 ) ) then
delay = delay + 500
end
end
mainScene.groundSpawnTime = system.getTimer() + delay
timer.performWithDelay( delay, goEnemy )
end
local function collisionCheck(event)
local self = event.source.params.myself
if( not mainScene or mainScene.bGameOver ) then
return
end
if( not mainScene.enemies.counter ) then
mainScene.enemies.counter = 0
end
-- Only check collisions of player and obstacle if the object is "alive"
for i, enemy in ipairs( mainScene.enemies.activeEnemies ) do
if( not enemy.health ) then
enemy.health = 1
end
if( mainScene.enemies.counter > 3 ) then
mainScene.enemies.counter = 0
if( enemy.contentBounds and not enemy.bObstacleImmunity and enemy.health > 0 and mainScene:checkCollision(mainScene.player.avatar, enemy ) ) then
if( not enemy.bNoDamage ) then
mainScene.player:doEnemyCollision( enemy )
end
if( enemy.onPlayerCollision and not enemy.bDidCollisionCallback and ( enemy.bNoDamage or mainScene.player:isInvincible() ) ) then
enemy.bDidCollisionCallback = true
enemy:onPlayerCollision()
end
if( enemy.health <= 0 ) then
break
end
end
else
mainScene.enemies.counter = mainScene.enemies.counter + 1
end
-- See if the enemies are colliding with any obstacle objects that have been knocked over by the player.)
for z, obstacle in ipairs( mainScene.obstacles.collidedObjects ) do
if( enemy.health > 0 and not enemy.bImmortal and not enemy.bNoDamage and not enemy.bObstacleImmunity and not obstacle.disableEnemyCollision and mainScene:checkCollision( enemy, obstacle ) and enemy.contentBounds.xMin < mainScene.rightEdge - 40 ) then
doDamageSplash(self, enemy, true )
mainScene:doExplosion(enemy, enemy.explosionScale )
mainScene:addPoints(350)
enemy.health = 0
end
end
end
end
function enemyObjects:update()
mainScene.enemies.jerkoffHands:update(mainScene)
if( not mainScene.enemies.sanityCheckTime ) then
mainScene.enemies.sanityCheckTime = system.getTimer()
end
if( system.getTimer() - mainScene.enemies.sanityCheckTime > 7000 and not mainScene.bVinceAlive ) then
mainScene.enemies.sanityCheckTime = system.getTimer()
end
for i, enemy in ipairs( mainScene.enemies.activeEnemies ) do
if( enemy and enemy.x and mainScene.player.avatar.x ) then
if( enemy.update ) then
enemy:update()
end
if( not enemy.health ) then
enemy.health = 0
end
if( enemy.bDisableCulling and enemy.health <1 ) then
return
end
if( enemy.health < 1 or not enemy.removeSelf ) then
if( enemy.removeSelf ) then
enemy:removeSelf()
end
table.remove( mainScene.enemies.activeEnemies, i )
enemy = nil
end
end
end
checkForDiscardedObjects(self)
end
function enemyObjects:init(scene)
mainScene = scene
self.activeEnemies = {}
self.allEnemies = {}
mainScene.enemies.mudSharks = require( "chicagoMudSharks" )
table.insert( self.allEnemies, mainScene.enemies.mudSharks )
mainScene.enemies.blackHawks = require( "chicagoBlackHawks" )
table.insert( self.allEnemies, mainScene.enemies.blackHawks )
mainScene.enemies.jerkoffHands = require( "chicagoJerkoffHand" )
table.insert( self.allEnemies, mainScene.enemies.jerkoffHands )
mainScene.enemies.redBats = require( "chicagoRedBats" )
table.insert( self.allEnemies, mainScene.enemies.redBats )
mainScene.enemies.toaster = require( "chicagoToaster" )
table.insert( self.allEnemies, mainScene.enemies.toaster )
mainScene.enemies.basketball = require( "chicagoBasketballs" )
table.insert( self.allEnemies, mainScene.enemies.basketball )
mainScene.enemies.randyRanch = require( "chicagoRandyRanch" )
table.insert( self.allEnemies, mainScene.enemies.randyRanch )
mainScene.enemies.vince = require( "chicagoVince" )
table.insert( self.allEnemies, mainScene.enemies.vince )
self.updateTimer = timer.performWithDelay( 33, collisionCheck, -1 )
self.updateTimer.params = { myself = self }
end
function enemyObjects:destroy()
for i, enemy in ipairs( enemyObjects:getActive() ) do
enemyDispose(enemy,true)
end
timer.cancel( self.updateTimer )
mainScene.bSmoggy = false
mainScene.bRemoveSmog = false
mainScene.bVinceAlive = nil
mainScene.enemies.activeEnemies = {}
mainScene.enemies.allEnemies = nil
mainScene.enemies = nil
mainScene = nil
end
return enemyObjects;