File: title.d

package info (click to toggle)
torus-trooper 0.22.dfsg1-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,384 kB
  • ctags: 5
  • sloc: xml: 839; makefile: 28
file content (399 lines) | stat: -rw-r--r-- 11,887 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
/*
 * $Id: title.d,v 1.3 2005/01/01 12:40:28 kenta Exp $
 *
 * Copyright 2004 Kenta Cho. Some rights reserved.
 */
module abagames.tt.title;

private import std.math;
private import opengl;
private import openglu;
private import abagames.util.vector;
private import abagames.util.sdl.displaylist;
private import abagames.util.sdl.texture;
private import abagames.util.sdl.pad;
private import abagames.tt.screen;
private import abagames.tt.prefmanager;
private import abagames.tt.ship;
private import abagames.tt.letter;
private import abagames.tt.gamemanager;

/**
 * Title screen.
 */
public class TitleManager {
 private:
  static const int REPLAY_CHANGE_DURATION = 30;
  static const int AUTO_REPEAT_START_TIME = 30;
  static const int AUTO_REPEAT_CNT = 5;
  PrefManager prefManager;
  Pad pad;
  Ship ship;
  GameManager gameManager;
  DisplayList displayList;
  int cnt;
  Texture titleTexture;
  int grade, level;
  bool dirPressed, btnPressed;
  int keyRepeatCnt;
  int replayCnt;
  bool _replayMode;
  float _replayChangeRatio;

  public this(PrefManager pm, Pad p, Ship s, GameManager gm) {
    prefManager = pm;
    pad = p;
    ship = s;
    gameManager = gm;
    createTorusShape();
    titleTexture = new Texture("title.bmp");
  }

  public void close() {
    displayList.close();
  }

  public void start() {
    cnt = 0;
    grade = prefManager.prefData.selectedGrade;
    level = prefManager.prefData.selectedLevel;
    keyRepeatCnt = 0;
    dirPressed = btnPressed = true;
    replayCnt = 0;
    _replayMode = false;
  }

  public void move(bool hasReplayData) {
    int dir = pad.getDirState();
    if (!replayMode) {
      if (dir & (Pad.Dir.RIGHT | Pad.Dir.LEFT)) {
        if (!dirPressed) {
          dirPressed = true;
          int oldGrade = grade;
          if (dir & Pad.Dir.RIGHT) {
            grade++;
            if (grade >= Ship.GRADE_NUM)
              grade = 0;
          }
          if (dir & Pad.Dir.LEFT) {
            grade--;
            if (grade < 0)
              grade = Ship.GRADE_NUM - 1;
          }
          if (level == prefManager.prefData.getMaxLevel(oldGrade)
              || level > prefManager.prefData.getMaxLevel(grade))
            level = prefManager.prefData.getMaxLevel(grade);
        }
      }
      if (dir & (Pad.Dir.UP | Pad.Dir.DOWN)) {
        int mv = 0;
        if (!dirPressed) {
          dirPressed = true;
          mv = 1;
        } else {
          keyRepeatCnt++;
          if (keyRepeatCnt >= AUTO_REPEAT_START_TIME) {
            if (keyRepeatCnt % AUTO_REPEAT_CNT == 0) {
              mv = (keyRepeatCnt / AUTO_REPEAT_START_TIME) *
                (keyRepeatCnt / AUTO_REPEAT_START_TIME);
            }
          }
        }
        if (dir & Pad.Dir.DOWN) {
          level += mv;
          if (level > prefManager.prefData.getMaxLevel(grade)) {
            if (keyRepeatCnt >= AUTO_REPEAT_START_TIME)
              level = prefManager.prefData.getMaxLevel(grade);
            else
              level = 1;
            keyRepeatCnt = 0;
          }
        }
        if (dir & Pad.Dir.UP) {
          level -= mv;
          if (level < 1) {
            if (keyRepeatCnt >= AUTO_REPEAT_START_TIME)
              level = 1;
            else
              level = prefManager.prefData.getMaxLevel(grade);
            keyRepeatCnt = 0;
          }
        }
      }
    } else {
      if (dir & (Pad.Dir.RIGHT | Pad.Dir.LEFT)) {
        if (!dirPressed) {
          dirPressed = true;
          if (dir & Pad.Dir.RIGHT) {
            ship.cameraMode = false;
          }
          if (dir & Pad.Dir.LEFT) {
            ship.cameraMode = true;
          }
        }
      }
      if (dir & (Pad.Dir.UP | Pad.Dir.DOWN)) {
        if (!dirPressed) {
          dirPressed = true;
          if (dir & Pad.Dir.UP) {
            ship.drawFrontMode = true;
          }
          if (dir & Pad.Dir.DOWN) {
            ship.drawFrontMode = false;
          }
        }
      }
    }
    if (dir == 0) {
      dirPressed = false;
      keyRepeatCnt = 0;
    }
    int btn = pad.getButtonState();
    if (btn & Pad.Button.ANY) {
      if (!btnPressed) {
        btnPressed = true;
        if (btn & Pad.Button.A) {
          if (!replayMode) {
            prefManager.prefData.recordStartGame(grade, level);
            gameManager.startInGame();
          }
        }
        if (hasReplayData)
          if (btn & Pad.Button.B)
            _replayMode = !_replayMode;
      }
    } else {
      btnPressed = false;
    }
    cnt++;
    if (_replayMode) {
      if (replayCnt < REPLAY_CHANGE_DURATION)
        replayCnt++;
    } else {
      if (replayCnt > 0)
        replayCnt--;
    }
    _replayChangeRatio = cast(float) replayCnt / REPLAY_CHANGE_DURATION;
  }

  public void draw() {
    if (_replayChangeRatio >= 1.0f)
      return;
    glPopMatrix();
    Screen.viewOrthoFixed();
    glDisable(GL_BLEND);
    Screen.setColor(0, 0, 0);
    float rcr = _replayChangeRatio * 2;
    if (rcr > 1)
      rcr = 1;
    glBegin(GL_QUADS);
    glVertex3f(450 + (640 - 450) * rcr, 0, 0);
    glVertex3f(640, 0, 0);
    glVertex3f(640, 480, 0);
    glVertex3f(450 + (640 - 450) * rcr, 480, 0);
    glEnd();
    glEnable(GL_BLEND);
    Screen.viewPerspective();
    glPushMatrix();
    gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
    glPushMatrix();
    glTranslatef(3 - _replayChangeRatio * 2.4f, 1.8f, 3.5f - _replayChangeRatio * 1.5f);
    glRotatef(30, 1, 0, 0);
    glRotatef(sin(cnt * 0.005f) * 12, 0, 1, 0);
    glRotatef(cnt * 0.2f, 0, 0, 1);
    glDisable(GL_BLEND);
    Screen.setColor(0, 0, 0);
    displayList.call(1);
    glEnable(GL_BLEND);
    Screen.setColor(1, 1, 1, 0.5f);
    displayList.call(0);
    glPopMatrix();
  }

  public void drawFront() {
    if (_replayChangeRatio > 0)
      return;
    glPushMatrix();
    glTranslatef(508, 400, 0);
    glRotatef(-20, 0, 0, 1);
    glScalef(128, 64, 1);
    glLineWidth(2);
    displayList.call(2);
    glLineWidth(1);
    glPopMatrix();
    Screen.setColor(1, 1, 1);
    glEnable(GL_TEXTURE_2D);
    titleTexture.bind();
    glBegin(GL_TRIANGLE_FAN);
    glTexCoord2f(0, 0);
    glVertex3f(470, 380, 0);
    glTexCoord2f(1, 0);
    glVertex3f(598, 380, 0);
    glTexCoord2f(1, 1);
    glVertex3f(598, 428, 0);
    glTexCoord2f(0, 1);
    glVertex3f(470, 428, 0);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    float cx, cy;
    for (int i = 0; i < Ship.GRADE_NUM; i++) {
      glLineWidth(2);
      calcCursorPos(cx, cy, i, 1);
      drawCursorRing(cx, cy, 15);
      Letter.drawString(Ship.GRADE_LETTER[i], cx - 4, cy - 10, 7);
      glLineWidth(1);
      int ml = prefManager.prefData.getMaxLevel(i);
      if (ml > 1) {
        float ecx, ecy;
        calcCursorPos(ecx, ecy, i, ml);
        drawCursorRing(ecx, ecy, 15);
        Letter.drawNum(ml, ecx + 7, ecy - 8, 6);
        float l2cx, l2cy;
        calcCursorPos(l2cx, l2cy, i, 2);
        glBegin(GL_LINES);
        glVertex3f(cx - 29, cy + 7, 0);
        glVertex3f(l2cx - 29, l2cy + 7, 0);
        glVertex3f(l2cx - 29, l2cy + 7, 0);
        glVertex3f(ecx - 29, ecy + 7, 0);
        glVertex3f(cx + 29, cy - 7, 0);
        glVertex3f(l2cx + 29, l2cy - 7, 0);
        glVertex3f(l2cx + 29, l2cy - 7, 0);
        glVertex3f(ecx + 29, ecy - 7, 0);
        glEnd();
      }
    }
    Letter.drawString(Ship.GRADE_STR[grade],
                      560 - Ship.GRADE_STR[grade].length * 19, 4, 9);
    Letter.drawNum(level, 620, 10, 6);
    Letter.drawString("LV", 570, 10, 6);
    GradeData gd = prefManager.prefData.getGradeData(grade);
    Letter.drawNum(gd.hiScore, 620, 45, 8);
    Letter.drawNum(gd.startLevel, 408, 54, 5);
    Letter.drawNum(gd.endLevel, 453, 54, 5);
    Letter.drawString("-", 423, 54, 5);
    calcCursorPos(cx, cy, grade, level);
    drawCursorRing(cx, cy, 18 + sin(cnt * 0.1f) * 3);
  }

  private void calcCursorPos(ref float x, ref float y, int gd, int lv) {
    x = 460 + gd * 70;
    y = 90;
    if (lv > 180)
      lv = 180;
    if (lv > 1) {
      y += 30 + lv;
      x -= lv * 0.33f;
    }
  }

  private void drawCursorRing(float x, float y, float s) {
    glPushMatrix();
    glTranslatef(x, y, 0);
    glRotatef(-20, 0, 0, 1);
    glScalef(s * 2, s, 1);
    displayList.call(2);
    glPopMatrix();
  }

  private void createTorusShape() {
    Vector3 cp = new Vector3;
    cp.z = 0;
    Vector3 ringOfs = new Vector3;
    float torusRad = 5;
    float ringRad = 0.7;
    displayList = new DisplayList(3);
    displayList.beginNewList();
    float d1 = 0;
    for (int i = 0; i < 32; i++, d1 += PI * 2 / 32) {
      float d2 = 0;
      for (int j = 0; j < 16; j++, d2 += PI * 2 / 16) {
        cp.x = sin(d1) * torusRad;
        cp.y = cos(d1) * torusRad;
        glBegin(GL_LINE_STRIP);
        createRingOffset(ringOfs, cp, ringRad, d1, d2);
        Screen.glVertex(ringOfs);
        createRingOffset(ringOfs, cp, ringRad, d1, d2 + PI * 2 / 16);
        Screen.glVertex(ringOfs);
        cp.x = sin(d1 + PI * 2 / 32) * torusRad;
        cp.y = cos(d1 + PI * 2 / 32) * torusRad;
        createRingOffset(ringOfs, cp, ringRad, d1 + PI * 2 / 32, d2 + PI * 2 / 16);
        Screen.glVertex(ringOfs);
        glEnd();
      }
    }
    displayList.nextNewList();
    d1 = 0;
    glBegin(GL_QUADS);
    for (int i = 0; i < 32; i++, d1 += PI * 2 / 32) {
      cp.x = sin(d1) * (torusRad + ringRad);
      cp.y = cos(d1) * (torusRad + ringRad);
      Screen.glVertex(cp);
      cp.x = sin(d1) * (torusRad + ringRad * 10);
      cp.y = cos(d1) * (torusRad + ringRad * 10);
      Screen.glVertex(cp);
      cp.x = sin(d1 + PI * 2 / 32) * (torusRad + ringRad * 10);
      cp.y = cos(d1 + PI * 2 / 32) * (torusRad + ringRad * 10);
      Screen.glVertex(cp);
      cp.x = sin(d1 + PI * 2 / 32) * (torusRad + ringRad);
      cp.y = cos(d1 + PI * 2 / 32) * (torusRad + ringRad);
      Screen.glVertex(cp);
    }
    d1 = 0;
    for (int i = 0; i < 32; i++, d1 += PI * 2 / 32) {
      float d2 = 0;
      for (int j = 0; j < 16; j++, d2 += PI * 2 / 16) {
        cp.x = sin(d1) * torusRad;
        cp.y = cos(d1) * torusRad;
        createRingOffset(ringOfs, cp, ringRad, d1, d2);
        Screen.glVertex(ringOfs);
        createRingOffset(ringOfs, cp, ringRad, d1, d2 + PI * 2 / 16);
        Screen.glVertex(ringOfs);
        cp.x = sin(d1 + PI * 2 / 32) * torusRad;
        cp.y = cos(d1 + PI * 2 / 32) * torusRad;
        createRingOffset(ringOfs, cp, ringRad, d1 + PI * 2 / 32, d2 + PI * 2 / 16);
        Screen.glVertex(ringOfs);
        createRingOffset(ringOfs, cp, ringRad, d1 + PI * 2 / 32, d2);
        Screen.glVertex(ringOfs);
      }
    }
    glEnd();
    displayList.nextNewList();
    d1 = 0;
    Screen.setColor(1, 1, 1);
    glBegin(GL_LINE_LOOP);
    for (int i = 0; i < 128; i++, d1 += PI * 2 / 128) {
      cp.x = sin(d1);
      cp.y = cos(d1);
      Screen.glVertex(cp);
    }
    glEnd();
    Screen.setColor(1, 1, 1, 0.3f);
    glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0, 0, 0);
    for (int i = 0; i <= 128; i++, d1 += PI * 2 / 128) {
      cp.x = sin(d1);
      cp.y = cos(d1);
      Screen.glVertex(cp);
    }
    glEnd();
    displayList.endNewList();
  }

  public void createRingOffset(Vector3 ringOfs, Vector3 centerPos,
                               float rad, float d1, float d2) {
    ringOfs.x = 0;
    ringOfs.y = rad;
    ringOfs.z = 0;
    ringOfs.rollX(d2);
    ringOfs.rollZ(-d1);
    ringOfs += centerPos;
  }

  public bool replayMode() {
    return _replayMode;
  }

  public float replayChangeRatio() {
    return _replayChangeRatio;
  }
}