File: gamemanager.d

package info (click to toggle)
tumiki-fighters 0.2.dfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,992 kB
  • ctags: 5
  • sloc: xml: 1,767; makefile: 28
file content (656 lines) | stat: -rw-r--r-- 17,666 bytes parent folder | download | duplicates (4)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
 * $Id: gamemanager.d,v 1.5 2004/05/14 14:35:37 kenta Exp $
 *
 * Copyright 2004 Kenta Cho. All rights reserved.
 */
module abagames.tf.gamemanager;

private import std.math;
private import std.conv;
private import opengl;
private import SDL;
private import bulletml;
private import abagames.util.rand;
private import abagames.util.actorpool;
private import abagames.util.vector;
private import abagames.util.sdl.gamemanager;
private import abagames.util.sdl.texture;
private import abagames.util.sdl.pad;
private import abagames.util.sdl.sound;
private import abagames.tf.prefmanager;
private import abagames.tf.screen;
private import abagames.tf.ship;
private import abagames.tf.field;
private import abagames.tf.particle;
private import abagames.tf.fragment;
private import abagames.tf.bulletactor;
private import abagames.tf.bulletactorpool;
private import abagames.tf.barragemanager;
private import abagames.tf.soundmanager;
private import abagames.tf.letterrender;
private import abagames.tf.tumiki;
private import abagames.tf.enemy;
private import abagames.tf.stagemanager;
private import abagames.tf.splinter;
private import abagames.tf.scoresign;
private import abagames.tf.damagegauge;
private import abagames.tf.mobileletter;
private import abagames.tf.attractmanager;

/**
 * Manage the game status and actor pools.
 */
public class GameManager: abagames.util.sdl.gamemanager.GameManager {
 public:
  bool nowait = false;
  static enum State {
    START_GAME, IN_GAME, GAMEOVER, PAUSE, TITLE, END_GAME,
  }
  int state;
  int stage;
  static enum Mode {
    NORMAL, EXTRA
  }
  int mode = Mode.EXTRA;
 private:
  Pad pad;
  PrefManager prefManager;
  StageManager stageManager;
  Screen screen;
  Rand rand;
  Field field;
  Ship ship;
  ParticlePool particles;
  ActorPool fragments;
  BulletActorPool bullets;
  EnemyPool enemies;
  SplinterPool splinters;
  ActorPool signs;
  DamageGauge gauge;
  MobileLetterPool letters;
  AttractManager attractManager;
  int cnt;
  int pauseCnt;
  const float SLOWDOWN_START_BULLETS_SPEED = 30;
  float interval;
  int score;
  const int LEFT_BONUS_NORMAL = 10000;
  const int LEFT_BONUS_EXTRA = 30000;
  int leftBonus;
  const int LEFT_NUM = 2;
  int left;
  const int FIRST_EXTEND_NORMAL = 100000;
  const int EVERY_EXTEND_NORMAL = 300000;
  const int FIRST_EXTEND_EXTRA = 200000;
  const int EVERY_EXTEND_EXTRA = 500000;
  int firstExtend, everyExtend;
  int extendScore;
  const int BOSSTIMER_FREEZED = 999999999;
  int bossTimer;
  const int STAGE_END_CNT = 360;
  int bossDstCnt;
  const int CREDIT_NUM = 2;
  int credit;

  // Initialize actor pools, load BGMs/SEfs and textures.
  public override void init() {
    BarrageManager.loadBulletMLs();
    pad = cast(Pad) input;
    prefManager = cast(PrefManager) abstPrefManager;
    screen = cast(Screen) abstScreen;
    interval = mainLoop.INTERVAL_BASE;
    rand = new Rand;
    field = new Field;
    field.init();
    scope ParticleInitializer pi = new ParticleInitializer;
    particles = new ParticlePool(128, pi);
    scope Fragment fragmentClass = new Fragment;
    scope FragmentInitializer fi = new FragmentInitializer;
    fragments = new ActorPool(128, fragmentClass, fi);
    ship = new Ship;
    ship.init(pad, field, particles, fragments, this);
    scope SplinterInitializer si = new SplinterInitializer(ship, field, particles, this);
    splinters = new SplinterPool(144, si);
    scope BulletActorInitializer bi = new BulletActorInitializer(field, ship, particles, splinters);
    bullets = new BulletActorPool(512, bi);
    ship.setBulletActorPool(bullets);
    ship.initStuckEnemies(splinters);
    gauge = new DamageGauge;
    scope EnemyInitializer ei = new EnemyInitializer
      (this, field, bullets, ship, splinters, particles, fragments, gauge);
    enemies = new EnemyPool(64, ei);
    bullets.setEnemies(enemies);
    scope ScoreSignInitializer ssi = new ScoreSignInitializer;
    scope ScoreSign scoreSignClass = new ScoreSign;
    signs = new ActorPool(32, scoreSignClass, ssi);
    scope MobileLetterInitializer mli = new MobileLetterInitializer;
    letters = new MobileLetterPool(32, mli, field);
    stageManager = new StageManager(this, enemies, field);
    bullets.setStageManager(stageManager);
    attractManager = new AttractManager(pad, prefManager, this);
    SoundManager.init(this);
    Tumiki.createDisplayLists();
    LetterRender.createDisplayLists();
  }

  public override void start() {
    stage = 0;
    startTitle();
  }

  public override void close() {
    BarrageManager.unloadBulletMLs();
    SoundManager.close();
    LetterRender.deleteDisplayLists();
    Tumiki.deleteDisplayLists();
  }

  private void startTitle() {
    state = State.TITLE;
    Music.haltMusic();
    if (stage >= StageManager.STAGE_NUM)
      stage = StageManager.STAGE_NUM - 1;
    field.start(stage);
    field.setGroundY(0);
    letters.clear();
    signs.clear();
    attractManager.startTitle();
  }

  private void startInGame() {
    state = State.IN_GAME;
    score = 0;
    left = LEFT_NUM;
    if (mode == Mode.NORMAL) {
      firstExtend = FIRST_EXTEND_NORMAL;
      everyExtend = EVERY_EXTEND_NORMAL;
      leftBonus = LEFT_BONUS_NORMAL;
    } else {
      firstExtend = FIRST_EXTEND_EXTRA;
      everyExtend = EVERY_EXTEND_EXTRA;
      leftBonus = LEFT_BONUS_EXTRA;
    }
    extendScore = firstExtend;
    startStage();
    field.setGroundY(0);
    Splinter.setSignNum(0);
    signs.clear();
  }

  public void startInGameFirst() {
    stage = 0;
    startInGame();
    state = State.START_GAME;
    ship.startStage();
    Splinter.setSignNum(2);
    credit = CREDIT_NUM;
  }

  public void startEnding() {
    state = State.END_GAME;
    ship.backToHome();
    addScore(left * leftBonus, ship.pos);
    left = 0;
    credit = 0;
    letters.add("MISSION COMPLETED!", 110, 400, 12, 500, 1);
    SoundManager.playBgmOnce(SoundManager.Bgm.ENDING);
  }

  public void drawWarning() {
    letters.add("WARNING", 132, 210, 32, 250, 0);
    letters.add("HERE COMES A GIGANTIC TOY", 80, 280, 10, 250, -3);
  }

  public void setInGame() {
    state = State.IN_GAME;
  }

  private const string[] stageMessage = 
    [
     "WE ARE TUMIKI FIGHTERS!",
     "JUST OVER THE HORIZON",
     "PANIC ON MEADOW",
     "COASTLINE UNDER FIRE",
     "JUNK CITY CENTRAL"
    ];
  private void startStage() {
    fragments.clear();
    particles.clear();
    letters.clear();
    enemies.clear();
    bullets.clear();
    splinters.clear();
    ship.start();
    stageManager.start(stage);
    field.start(stage);
    gauge.init();
    bossTimer = BOSSTIMER_FREEZED;
    bossDstCnt = -1;
    letters.add("STAGE " ~ to!string(stage + 1), 180, 150, 24, 240, -4);
    letters.add(stageMessage[stage], 320 - stageMessage[stage].length * 10, 270, 10, 240, -2);
    int si = stage % (SoundManager.STAGE_BGM_NUM * 2 - 1);
    if (si >= SoundManager.STAGE_BGM_NUM)
      si = SoundManager.STAGE_BGM_NUM * 2 - si - 2;
    SoundManager.playBgm(SoundManager.Bgm.STG1 + si);
  }

  public void startGameover() {
    state = State.GAMEOVER;
    setScreenShake(0, 0);
    letters.clear();
    interval = mainLoop.INTERVAL_BASE;
    mainLoop.interval = cast(int) interval;
    cnt = 0;
    prefManager.setHiScore(score, stage);
    Music.fadeMusic();
  }

  private void startPause() {
    state = State.PAUSE;
    pauseCnt = 0;
  }

  private void resumePause() {
    state = State.IN_GAME;
  }

  public void addScore(int sc, Vector pos) {
    if (sc <= 0)
      return;
    score += sc;
    ScoreSign ss = cast(ScoreSign) signs.getInstanceForced();
    float s = 0.3 + cast(float) sc / 3000;
    if (s > 1.2)
      s = 1.2;
    ss.set(pos, sc, s);
    if (score > extendScore) {
      SoundManager.playSe(SoundManager.Se.EXTEND);
      left++;
      if (extendScore <= firstExtend)
	extendScore = everyExtend;
      else 
	extendScore += everyExtend;
    }
  }

  public void shipDestroyed() {
    bullets.clearVisible();
    left--;
    if (left < 0)
      startGameover();
  }

  public int bossDestroyed() {
    Music.fadeMusic();
    bullets.clearVisible();
    ship.breakStuckEnemies();
    bullets.clear();
    if (bossDstCnt < 0)
      bossDstCnt = 0;
    // Boss destroyed bonus score.
    int bs = (cast(int) bossTimer / 60) * 10 + 10000;
    if (bs > 20000)
      bs = 20000;
    else if (bs < 10000)
      bs = 10000;
    if (mode == Mode.EXTRA)
      bs *= 3;
    return bs;
  }

  public void bossInAttack(int tm) {
    bossTimer = cast(int) (tm * 16.66667);
  }

  public void setRank(float r) {
    stageManager.setRank(r);
  }

  // Move actors in game(called once per frame).
  public override void move() {
    if (pad.keys[SDLK_ESCAPE] == SDL_PRESSED) {
      mainLoop.breakLoop();
      return;
    }
    switch (state) { default: break;
    case State.START_GAME:
    case State.IN_GAME:
    case State.END_GAME:
      moveInGame();
      break;
    case State.GAMEOVER:
      moveGameover();
      break;
    case State.PAUSE:
      movePause();
      break;
    case State.TITLE:
      moveTitle();
      break;
    }
    cnt++;
  }

  private bool pPrsd = true;

  private void moveInGame() {
    if (bossDstCnt > STAGE_END_CNT - 120 && stage >= StageManager.STAGE_NUM - 1) {
      stage++;
      bossDstCnt = -1;
      bossTimer = BOSSTIMER_FREEZED;
      startEnding();
      return;
    }
    if (bossDstCnt > STAGE_END_CNT) {
      stage++;
      startStage();
      return;
    } else if (bossDstCnt == STAGE_END_CNT - 119) {
      SoundManager.playSe(SoundManager.Se.PROPELLER);
    }
    if (state == State.IN_GAME)
      stageManager.move();
    field.move();
    splinters.move();
    if (bossDstCnt > STAGE_END_CNT - 120) {
      bullets.clearVisible();
      bullets.clear();
      ship.endMove();
    } else if (state == State.IN_GAME) {
      ship.move();
    } else if (state == State.START_GAME) {
      ship.startMove();
    } else {
      ship.backToHomeMove();
    }
    BulletActor.resetTotalBulletsSpeed();
    bullets.move();
    Enemy.resetTotalNum();
    enemies.move();
    particles.move();
    fragments.move();
    signs.move();
    gauge.move();
    letters.move();
    moveScreenShake();
    Tumiki.move();
    if (bossDstCnt >= 0) {
      bossDstCnt++;
      ship.breakStuckEnemies();
    } else if (bossTimer < BOSSTIMER_FREEZED) {
      bossTimer -= 17;
      if (bossTimer < 0) {
	bullets.clearVisible();
	bullets.clear();
	ship.breakStuckEnemies();
	bossTimer = 0;
	bossDstCnt = 0;
	Music.fadeMusic();
      }
    }
    if (pad.keys[SDLK_p] == SDL_PRESSED) {
      if (!pPrsd) {
	pPrsd = true;
	if (state == State.IN_GAME)
	  startPause();
      }
    } else {
      pPrsd = false;
    }
    /*if (!nowait) {
      // Intentional slowdown when the total speed of bullets is over SLOWDOWN_START_BULLETS_SPEED
      if (BulletActor.totalBulletsSpeed > SLOWDOWN_START_BULLETS_SPEED) {
	float sm = BulletActor.totalBulletsSpeed / SLOWDOWN_START_BULLETS_SPEED;
	if (sm > 1.75)
	  sm = 1.75;
	interval += (sm * mainLoop.INTERVAL_BASE - interval) * 0.1;
	mainLoop.interval = (int) interval;
      } else {
	interval += (mainLoop.INTERVAL_BASE - interval) * 0.08;
	mainLoop.interval = (int) interval;
      }
      }*/
  }

  private bool btnPrsd, arwPrsd;
  private bool isContinue;

  private void moveGameover() {
    bool gotoNextState = false;
    if (cnt == 48)
      letters.add("GAME OVER", 100, 230, 28, 400, 4);
    if (cnt <= 64) {
      btnPrsd = arwPrsd = true;
      isContinue = false;
    } else {
      if (pad.getButtonState() & (Pad.PAD_BUTTON1 | Pad.PAD_BUTTON2)) {
	if (!btnPrsd)
	  gotoNextState = true;
      } else {
	btnPrsd = false;
      }
      if (credit > 0) {
	int key = (pad.getPadState() & (Pad.PAD_LEFT | Pad.PAD_RIGHT));
	if (key != 0) {
	  if (!arwPrsd) {
	    arwPrsd = true;
	    if ((key & Pad.PAD_LEFT) != 0)
	      isContinue = true;
	    if ((key & Pad.PAD_RIGHT) != 0)
	      isContinue = false;
	  }
	} else {
	  arwPrsd = false;
	}
      }
    }
    if ((cnt > 64 && gotoNextState) || cnt > 700) {
      if (isContinue && cnt <= 700) {
	credit--;
	startInGame();
      } else {
	startTitle();
      }
    }
    field.move();
    bullets.move();
    enemies.move();
    particles.move();
    fragments.move();
    letters.move();
    Tumiki.move();
  }

  private void movePause() {
    pauseCnt++;
    if (pad.keys[SDLK_p] == SDL_PRESSED) {
      if (!pPrsd) {
	pPrsd = true;
	resumePause();
      }
    } else {
      pPrsd = false;
    }
  }

  private void moveTitle() {
    attractManager.moveTitle();
    field.move();
    Tumiki.move();
  }

  // Draw actors in game(called once per frame).
  public override void draw() {
    SDL_Event e = mainLoop.event;
    if (e.type == SDL_VIDEORESIZE) {
      SDL_ResizeEvent re = e.resize;
      if (re.w > 150 && re.h > 100)
	screen.resized(re.w, re.h);
    }
    screen.clear();
    screen.viewOrthoFixed();
    glDisable(GL_CULL_FACE);
    field.drawBack();
    glEnable(GL_CULL_FACE);
    screen.viewPerspective();

    glPushMatrix();
    setEyepos();
    switch (state) { default: break;
    case State.START_GAME:
    case State.IN_GAME:
    case State.PAUSE:
    case State.END_GAME:
      drawInGame();
      break;
    case State.GAMEOVER:
      drawGameover();
      break;
    case State.TITLE:
      drawTitle();
      break;
    }
    glPopMatrix();

    screen.viewOrthoFixed();
    glDisable(GL_CULL_FACE);
    letters.draw();
    switch (state) { default: break;
    case State.IN_GAME:
      drawStatusInGame();
      break;
    case State.GAMEOVER:
    case State.END_GAME:
      drawStatusGameover();
      break;
    case State.PAUSE:
      drawStatusPause();
      break;
    case State.TITLE:
      drawStatusTitle();
      break;
    case State.START_GAME:
      break;
    }
    glEnable(GL_CULL_FACE);
    screen.viewPerspective();
  }

  private void drawInGame() {
    glEnable(GL_DEPTH_TEST);
    field.draw();
    enemies.draw();
    splinters.draw();
    if (state == State.START_GAME)
      ship.drawFriendly();
    else if (state == State.END_GAME)
      ship.drawFriendlyBack();
    ship.draw();
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBegin(GL_QUADS);
    particles.draw();
    glEnd();
    glDisable(GL_BLEND);
    fragments.draw();
    bullets.drawShots();
    signs.draw();
    glEnable(GL_DEPTH_TEST);
    gauge.draw();
    drawLeft();
    glDisable(GL_DEPTH_TEST);
    glLineWidth(2);
    bullets.drawBullets();
    glLineWidth(1);
  }

  private void drawGameover() {
    glEnable(GL_DEPTH_TEST);
    field.draw();
    enemies.draw();
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBegin(GL_QUADS);
    particles.draw();
    glEnd();
    glDisable(GL_BLEND);
    fragments.draw();
    bullets.drawShots();
  }

  private void drawTitle() {
    glEnable(GL_DEPTH_TEST);
    field.draw();
    glDisable(GL_DEPTH_TEST);
  }

  private void drawInfo() {
    LetterRender.drawString("SCORE", 4, 4, 12, LetterRender.Direction.TO_RIGHT, -3);
    LetterRender.drawNum(score, 300, 4, 12, LetterRender.Direction.TO_RIGHT, 3);
    if (bossTimer < BOSSTIMER_FREEZED && (bossDstCnt & 31) > 8) {
      LetterRender.drawTime(bossTimer, 600, 32, 10, 3);
    }
  }

  private void drawLeft() {
    float x = -field.size.x * 0.85, sz = 0.4;
    for (int i = 0; i < left; i++, x += 2) {
      glPushMatrix();
      glScalef(sz, sz, sz);
      ship.drawLeft(x / sz, -field.size.y * 0.82 / sz, 1 / sz);
      glPopMatrix();
    }
  }

  private void drawStatusInGame() {
    drawInfo();
  }

  private void drawStatusGameover() {
    drawInfo();
    if (credit > 0 && cnt > 64) {
      LetterRender.drawString("CONTINUE", 280, 400, 10, LetterRender.Direction.TO_RIGHT, 1);
      if (isContinue) {
	LetterRender.drawString("YES", 480, 400, 12, LetterRender.Direction.TO_RIGHT, 0);
	LetterRender.drawString("NO", 562, 402, 8, LetterRender.Direction.TO_RIGHT, 2);
      } else {
	LetterRender.drawString("YES", 483, 402, 8, LetterRender.Direction.TO_RIGHT, 2);
	LetterRender.drawString("NO", 560, 400, 12, LetterRender.Direction.TO_RIGHT, 0);
      }
      LetterRender.drawString("CREDIT " ~ to!string(credit), 32, 420, 8, 
			      LetterRender.Direction.TO_RIGHT, 3);
    }
  }

  private void drawStatusPause() {
    drawInfo();
    if ((pauseCnt % 60) < 30)
      LetterRender.drawString("PAUSE", 280, 220, 12, LetterRender.Direction.TO_RIGHT, -5);
  }

  private void drawStatusTitle() {
    attractManager.drawTitle();
  }

  private int screenShakeCnt;
  private float screenShakeIntense;

  public void setScreenShake(int cnt, float intense) {
    screenShakeCnt = cnt;
    screenShakeIntense = intense;
  }

  private void moveScreenShake() {
    if (screenShakeCnt > 0)
      screenShakeCnt--;
  }

  private void setEyepos() {
    float x = 0, y = 0;
    if (screenShakeCnt > 0) {
      x = rand.nextSignedFloat(screenShakeIntense * (screenShakeCnt + 10));
      y = rand.nextSignedFloat(screenShakeIntense * (screenShakeCnt + 10));
    }
    glTranslatef(x, y, -field.eyeZ);
  }
}