Each module now includes its own header first. Removed sstream.h and the definition...
[crack-attack.git] / src / WinRecord.cxx
blobe30421145d14ac8c8f891b40a708ceb479fc67b1
1 /*
2 * WinRecord.cxx
3 * Daniel Nelson - 10/22/0
5 * Copyright (C) 2000 Daniel Nelson
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Daniel Nelson - aluminumangel.org
22 * 174 W. 18th Ave.
23 * Columbus, OH 43210
25 * Handles the win/loss record.
28 #include "WinRecord.h"
30 #include <GL/glut.h>
32 #include "glext.h"
34 #include "Game.h"
35 #include "Displayer.h"
36 #include "Random.h"
38 int WinRecord::current_game;
39 bool WinRecord::won;
40 bool WinRecord::concession;
41 int WinRecord::games_won;
42 int WinRecord::games_lost;
43 int WinRecord::record[GC_GAMES_PER_MATCH];
45 Star WinRecord::stars[GC_GAMES_PER_MATCH];
46 int WinRecord::dynamic_star;
47 int WinRecord::displaced_star;
48 GLfloat WinRecord::win_star_x;
49 GLfloat WinRecord::win_star_y;
50 GLfloat WinRecord::win_star_v_x;
51 GLfloat WinRecord::win_star_v_y;
52 GLfloat WinRecord::old_star_a;
53 GLfloat WinRecord::old_star_size;
54 bool WinRecord::draw_old_star;
56 void WinRecord::initialize ( )
58 for (int n = GC_GAMES_PER_MATCH; n--; )
59 record[n] = GR_NOT_PLAYED;
60 current_game = -1;
61 concession = false;
62 games_won = 0;
63 games_lost = 0;
65 dynamic_star = -1;
66 displaced_star = -1;
67 for (int n = GC_GAMES_PER_MATCH; n--; )
68 stars[n].a = 0.0f;
71 void WinRecord::gameStart ( )
73 if (MetaState::mode & CM_SOLO)
74 current_game = 1;
75 else
76 current_game++;
78 record[current_game] = GR_BEING_PLAYED;
81 void WinRecord::gameWon ( )
83 won = true;
84 games_won++;
85 record[current_game] = GR_WON;
87 if (MetaState::mode & CM_REALLY_LOW_GRAPHICS) {
88 // The star will disappear and pop into existance in the play area. After a
89 // few moments, it will fly to it's original location.
91 // record star's old location; for a time, two stars will be drawn, as one
92 // pops in and the other pops away
93 draw_old_star = true;
94 old_star_a = stars[current_game].a;
95 old_star_size = 5.0f;
97 // displace the star into the play area and set it's starting parameters
98 displaced_star = current_game;
99 stars[current_game].a = DC_STAR_WIN_MIN_ANGULAR_DEVIATION
100 + DC_STAR_WIN_SPREAD_ANGULAR_DEVIATION * Random::number();
101 if (Random::chanceIn2(2))
102 stars[current_game].a = -stars[current_game].a;
103 stars[current_game].v_a = 0.0f;
104 stars[current_game].size = 0.0f;
105 stars[current_game].v_size = 0.0f;
106 win_star_x = (-DC_LEFT_EXTERNAL_CENTER + DC_STAR_DISPLACEMENT
107 * (GC_GAMES_PER_MATCH - 1) / 2.0f + DC_STAR_WIN_OFFSET_X)
108 - DC_STAR_DISPLACEMENT * current_game;
110 if (MetaState::mode & CM_SOLO)
111 win_star_y = -DC_STAR_OFFSET_Y + DC_STAR_WIN_SOLO_OFFSET_Y;
112 else
113 win_star_y = -DC_STAR_OFFSET_Y + DC_STAR_WIN_OFFSET_Y;
115 // set the kick velocity; choice of two aesthetically pleasing preset values
116 // or, for variety, a random 270 degree arc
117 switch (Random::number(3)) {
118 case 0:
119 win_star_v_x = DC_STAR_WIN_PRESET_1_VELOCITY_X;
120 win_star_v_y = DC_STAR_WIN_PRESET_1_VELOCITY_Y;
121 break;
122 case 1:
123 win_star_v_x = DC_STAR_WIN_PRESET_2_VELOCITY_X;
124 win_star_v_y = DC_STAR_WIN_PRESET_2_VELOCITY_Y;
125 break;
126 default:
127 // too infrequent to warrent a random direction table
128 float v = DC_STAR_WIN_MIN_VELOCITY
129 + DC_STAR_WIN_SPREAD_VELOCITY * Random::number();
130 float angle = Random::number() * (3.0f * PI / 2.0f) - (PI / 2.0f);
131 win_star_v_x = v * cos(angle);
132 win_star_v_y = v * sin(angle);
133 break;
138 void WinRecord::gameLoss ( )
140 won = false;
141 games_lost++;
142 record[current_game] = GR_LOST;
145 void WinRecord::timeStep ( )
147 for (int n = GC_GAMES_PER_MATCH; n--; ) {
148 Star &star = stars[n];
150 switch (record[n]) {
152 // game being played
153 case GR_BEING_PLAYED:
154 if (Game::time_step >= GC_START_PAUSE_DELAY)
155 star.a += DC_STAR_PLAY_ANGULAR_VELOCITY;
156 else
157 star.a += Game::time_step
158 * (DC_STAR_PLAY_ANGULAR_VELOCITY / (float) GC_START_PAUSE_DELAY);
159 break;
161 // game has been lost
162 case GR_LOST:
163 if (current_game == n && Game::time_step < DC_CELEBRATION_TIME)
164 star.a += (DC_CELEBRATION_TIME - Game::time_step)
165 * (DC_STAR_PLAY_ANGULAR_VELOCITY / (float) DC_CELEBRATION_TIME);
166 break;
168 // game has been won
169 case GR_WON:
170 // wait for win message to hit before dynamics
171 if (current_game != n || Game::time_step > DC_WIN_FADE_TIME) {
173 star.size += star.v_size;
174 if (star.size < 0.0f) star.size = 0.0f;
175 star.v_size += -DC_STAR_WIN_SIZE_DRAG * star.v_size
176 - DC_STAR_WIN_SIZE_SPRING * (star.size - DC_STAR_SIZE_EQUILIBRIUM);
178 if (fabs(star.v_size) < DC_STAR_WIN_SIZE_EPSILON
179 && Random::chanceIn(DC_STAR_WIN_SIZE_PULSE_CHANCE_IN))
180 star.v_size += DC_STAR_WIN_SIZE_PULSE_VELOCITY * Random::number();
182 star.v_a += -DC_STAR_WIN_ANGULAR_SPRING * star.a;
184 star.a += star.v_a;
186 // begin motion
187 if (Game::time_step == DC_WIN_FADE_TIME + DC_STAR_WIN_KICK_DELAY
188 && current_game == n) {
189 displaced_star = -1;
190 dynamic_star = n;
193 // if we're dynamic;
194 if (dynamic_star == n) {
195 win_star_x += win_star_v_x;
196 win_star_y += win_star_v_y;
198 win_star_v_x += -DC_STAR_WIN_SPRING * win_star_x
199 - DC_STAR_WIN_DRAG * win_star_v_x;
200 win_star_v_y += -DC_STAR_WIN_SPRING * win_star_y
201 - DC_STAR_WIN_DRAG * win_star_v_y;
203 // if we're there, stop being dynamic
204 if (fabs(win_star_v_x) < DC_STAR_WIN_VELOCITY_EPSILON
205 && fabs(win_star_v_y) < DC_STAR_WIN_VELOCITY_EPSILON
206 && fabs(win_star_x) < DC_STAR_WIN_EPSILON
207 && fabs(win_star_y) < DC_STAR_WIN_EPSILON)
208 dynamic_star = -1;
212 // shrink the old star as the new one apears
213 if (draw_old_star && current_game == n) {
214 if (star.size < DC_STAR_SIZE_EQUILIBRIUM) {
215 old_star_size = DC_STAR_SIZE_EQUILIBRIUM - star.size;
216 old_star_a += DC_STAR_PLAY_ANGULAR_VELOCITY;
217 } else
218 draw_old_star = false;
221 break;