/
PlayState.as
executable file
·404 lines (355 loc) · 11.8 KB
/
PlayState.as
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package
{
import org.flixel.*;
public class PlayState extends FlxState
{
[Embed(source="data/tech_tiles.png")] protected var ImgTech:Class;
[Embed(source="data/dirt_top.png")] protected var ImgDirtTop:Class;
[Embed(source="data/dirt.png")] protected var ImgDirt:Class;
[Embed(source="data/mode.mp3")] protected var SndMode:Class;
[Embed(source="data/countdown.mp3")] protected var SndCount:Class;
[Embed(source="data/gibs.png")] private var ImgGibs:Class;
[Embed(source="data/spawner_gibs.png")] private var ImgSpawnerGibs:Class;
[Embed(source="data/miniframe.png")] private var ImgMiniFrame:Class;
//major game object storage
protected var _blocks:FlxGroup;
protected var _decorations:FlxGroup;
protected var _bullets:FlxGroup;
protected var _player:Player;
protected var _enemies:FlxGroup;
protected var _spawners:FlxGroup;
protected var _enemyBullets:FlxGroup;
protected var _littleGibs:FlxEmitter;
protected var _bigGibs:FlxEmitter;
protected var _hud:FlxGroup;
protected var _gunjam:FlxGroup;
//meta groups, to help speed up collisions
protected var _objects:FlxGroup;
protected var _hazards:FlxGroup;
//HUD/User Interface stuff
protected var _score:FlxText;
protected var _score2:FlxText;
protected var _scoreTimer:Number;
protected var _jamTimer:Number;
//just to prevent weirdness during level transition
protected var _fading:Boolean;
override public function create():void
{
FlxG.mouse.hide();
//Here we are creating a pool of 100 little metal bits that can be exploded.
//We will recycle the crap out of these!
_littleGibs = new FlxEmitter();
_littleGibs.setXSpeed(-150,150);
_littleGibs.setYSpeed(-200,0);
_littleGibs.setRotation(-720,-720);
_littleGibs.gravity = 350;
_littleGibs.bounce = 0.5;
_littleGibs.makeParticles(ImgGibs,100,10,true,0.5);
//Next we create a smaller pool of larger metal bits for exploding.
_bigGibs = new FlxEmitter();
_bigGibs.setXSpeed(-200,200);
_bigGibs.setYSpeed(-300,0);
_bigGibs.setRotation(-720,-720);
_bigGibs.gravity = 350;
_bigGibs.bounce = 0.35;
_bigGibs.makeParticles(ImgSpawnerGibs,50,20,true,0.5);
//Then we'll set up the rest of our object groups or pools
_blocks = new FlxGroup();
_decorations = new FlxGroup();
_enemies = new FlxGroup();
_spawners = new FlxGroup();
_hud = new FlxGroup();
_enemyBullets = new FlxGroup();
_bullets = new FlxGroup();
//Now that we have references to the bullets and metal bits,
//we can create the player object.
_player = new Player(316,300,_bullets,_littleGibs);
//This refers to a custom function down at the bottom of the file
//that creates all our level geometry with a total size of 640x480.
//This in turn calls buildRoom() a bunch of times, which in turn
//is responsible for adding the spawners and spawn-cameras.
generateLevel();
//Add bots and spawners after we add blocks to the state,
//so that they're drawn on top of the level, and so that
//the bots are drawn on top of both the blocks + the spawners.
add(_spawners);
add(_littleGibs);
add(_bigGibs);
add(_blocks);
add(_decorations);
add(_enemies);
//Then we add the player and set up the scrolling camera,
//which will automatically set the boundaries of the world.
add(_player);
FlxG.camera.setBounds(0,0,640,640,true);
FlxG.camera.follow(_player,FlxCamera.STYLE_PLATFORMER);
//We add the bullets to the scene here,
//so they're drawn on top of pretty much everything
add(_enemyBullets);
add(_bullets);
add(_hud);
//Finally we are going to sort things into a couple of helper groups.
//We don't add these groups to the state, we just use them for collisions later!
_hazards = new FlxGroup();
_hazards.add(_enemyBullets);
_hazards.add(_spawners);
_hazards.add(_enemies);
_objects = new FlxGroup();
_objects.add(_enemyBullets);
_objects.add(_bullets);
_objects.add(_enemies);
_objects.add(_player);
_objects.add(_littleGibs);
_objects.add(_bigGibs);
//From here on out we are making objects for the HUD,
//that is, the player score, number of spawners left, etc.
//First, we'll create a text field for the current score
_score = new FlxText(FlxG.width/4,0,FlxG.width/2);
_score.setFormat(null,16,0xd8eba2,"center",0x131c1b);
_hud.add(_score);
if(FlxG.scores.length < 2)
{
FlxG.scores.push(0);
FlxG.scores.push(0);
}
//Then for the player's highest and last scores
if(FlxG.score > FlxG.scores[0])
FlxG.scores[0] = FlxG.score;
if(FlxG.scores[0] != 0)
{
_score2 = new FlxText(FlxG.width/2,0,FlxG.width/2);
_score2.setFormat(null,8,0xd8eba2,"right",_score.shadow);
_hud.add(_score2);
_score2.text = "HIGHEST: "+FlxG.scores[0]+"\nLAST: "+FlxG.score;
}
FlxG.score = 0;
_scoreTimer = 0;
//Then we create the "gun jammed" notification
_gunjam = new FlxGroup();
_gunjam.add(new FlxSprite(0,FlxG.height-22).makeGraphic(FlxG.width,24,0xff131c1b));
_gunjam.add(new FlxText(0,FlxG.height-22,FlxG.width,"GUN IS JAMMED").setFormat(null,16,0xd8eba2,"center"));
_gunjam.visible = false;
_hud.add(_gunjam);
//After we add all the objects to the HUD, we can go through
//and set any property we want on all the objects we added
//with this sweet function. In this case, we want to set
//the scroll factors to zero, to make sure the HUD doesn't
//wiggle around while we play.
_hud.setAll("scrollFactor",new FlxPoint(0,0));
_hud.setAll("cameras",[FlxG.camera]);
FlxG.playMusic(SndMode);
FlxG.flash(0xff131c1b);
_fading = false;
//Debugger Watch examples
FlxG.watch(_player,"x");
FlxG.watch(_player,"y");
FlxG.watch(FlxG,"score");
}
override public function destroy():void
{
super.destroy();
_blocks = null;
_decorations = null;
_bullets = null;
_player = null;
_enemies = null;
_spawners = null;
_enemyBullets = null;
_littleGibs = null;
_bigGibs = null;
_hud = null;
_gunjam = null;
//meta groups, to help speed up collisions
_objects = null;
_hazards = null;
//HUD/User Interface stuff
_score = null;
_score2 = null;
}
override public function update():void
{
//save off the current score and update the game state
var oldScore:uint = FlxG.score;
super.update();
//collisions with environment
FlxG.collide(_blocks,_objects);
FlxG.overlap(_hazards,_player,overlapped);
FlxG.overlap(_bullets,_hazards,overlapped);
//check to see if the player scored any points this frame
var scoreChanged:Boolean = oldScore != FlxG.score
//Jammed message
if(FlxG.keys.justPressed("C") && _player.flickering)
{
_jamTimer = 1;
_gunjam.visible = true;
}
if(_jamTimer > 0)
{
if(!_player.flickering)
_jamTimer = 0;
_jamTimer -= FlxG.elapsed;
if(_jamTimer < 0)
_gunjam.visible = false;
}
if(!_fading)
{
//Score + countdown stuffs
if(scoreChanged)
_scoreTimer = 2;
_scoreTimer -= FlxG.elapsed;
if(_scoreTimer < 0)
{
if(FlxG.score > 0)
{
if(FlxG.score > 100)
FlxG.score -= 100;
else
{
FlxG.score = 0;
_player.kill();
}
_scoreTimer = 1;
scoreChanged = true;
//Play loud beeps if your score is low
var volume:Number = 0.35;
if(FlxG.score < 600)
volume = 1.0;
FlxG.play(SndCount,volume);
}
}
//Fade out to victory screen stuffs
if(_spawners.countLiving() <= 0)
{
_fading = true;
FlxG.fade(0xffd8eba2,3,onVictory);
}
}
//actually update score text if it changed
if(scoreChanged)
{
if(!_player.alive) FlxG.score = 0;
_score.text = FlxG.score.toString();
}
}
//This is an overlap callback function, triggered by the calls to FlxU.overlap().
protected function overlapped(Sprite1:FlxSprite,Sprite2:FlxSprite):void
{
if((Sprite1 is EnemyBullet) || (Sprite1 is Bullet))
Sprite1.kill();
Sprite2.hurt(1);
}
//A FlxG.fade callback, like in MenuState.
protected function onVictory():void
{
FlxG.music.stop();
FlxG.switchState(new VictoryState());
}
//These next two functions look crazy, but all they're doing is generating
//the level structure and placing the enemy spawners.
protected function generateLevel():void
{
var r:uint = 160;
var b:FlxTileblock;
//First, we create the walls, ceiling and floors:
b = new FlxTileblock(0,0,640,16);
b.loadTiles(ImgTech);
_blocks.add(b);
b = new FlxTileblock(0,16,16,640-16);
b.loadTiles(ImgTech);
_blocks.add(b);
b = new FlxTileblock(640-16,16,16,640-16);
b.loadTiles(ImgTech);
_blocks.add(b);
b = new FlxTileblock(16,640-24,640-32,8);
b.loadTiles(ImgDirtTop);
_blocks.add(b);
b = new FlxTileblock(16,640-16,640-32,16);
b.loadTiles(ImgDirt);
_blocks.add(b);
//Then we split the game world up into a 4x4 grid,
//and generate some blocks in each area. Some grid spaces
//also get a spawner!
buildRoom(r*0,r*0,true);
buildRoom(r*1,r*0);
buildRoom(r*2,r*0);
buildRoom(r*3,r*0,true);
buildRoom(r*0,r*1,true);
buildRoom(r*1,r*1);
buildRoom(r*2,r*1);
buildRoom(r*3,r*1,true);
buildRoom(r*0,r*2);
buildRoom(r*1,r*2);
buildRoom(r*2,r*2);
buildRoom(r*3,r*2);
buildRoom(r*0,r*3,true);
buildRoom(r*1,r*3);
buildRoom(r*2,r*3);
buildRoom(r*3,r*3,true);
}
//Just plops down a spawner and some blocks - haphazard and crappy atm but functional!
protected function buildRoom(RX:uint,RY:uint,Spawners:Boolean=false):void
{
//first place the spawn point (if necessary)
var rw:uint = 20;
var sx:uint;
var sy:uint;
if(Spawners)
{
sx = 2+FlxG.random()*(rw-7);
sy = 2+FlxG.random()*(rw-7);
}
//then place a bunch of blocks
var numBlocks:uint = 3+FlxG.random()*4;
if(!Spawners) numBlocks++;
var maxW:uint = 10;
var minW:uint = 2;
var maxH:uint = 8;
var minH:uint = 1;
var bx:uint;
var by:uint;
var bw:uint;
var bh:uint;
var check:Boolean;
for(var i:uint = 0; i < numBlocks; i++)
{
do
{
//keep generating different specs if they overlap the spawner
bw = minW + FlxG.random()*(maxW-minW);
bh = minH + FlxG.random()*(maxH-minH);
bx = -1 + FlxG.random()*(rw+1-bw);
by = -1 + FlxG.random()*(rw+1-bh);
if(Spawners)
check = ((sx>bx+bw) || (sx+3<bx) || (sy>by+bh) || (sy+3<by));
else
check = true;
} while(!check);
var b:FlxTileblock;
b = new FlxTileblock(RX+bx*8,RY+by*8,bw*8,bh*8);
b.loadTiles(ImgTech);
_blocks.add(b);
//If the block has room, add some non-colliding "dirt" graphics for variety
if((bw >= 4) && (bh >= 5))
{
b = new FlxTileblock(RX+bx*8+8,RY+by*8,bw*8-16,8);
b.loadTiles(ImgDirtTop);
_decorations.add(b);
b = new FlxTileblock(RX+bx*8+8,RY+by*8+8,bw*8-16,bh*8-24);
b.loadTiles(ImgDirt);
_decorations.add(b);
}
}
if(Spawners)
{
//Finally actually add the spawner
var sp:Spawner = new Spawner(RX+sx*8,RY+sy*8,_bigGibs,_enemies,_enemyBullets,_littleGibs,_player);
_spawners.add(sp);
//Then create a dedicated camera to watch the spawner
_hud.add(new FlxSprite(3 + (_spawners.length-1)*16, 3, ImgMiniFrame));
var camera:FlxCamera = new FlxCamera(10 + (_spawners.length-1)*32,10,24,24,1);
camera.follow(sp);
FlxG.addCamera(camera);
}
}
}
}