major menu, demo and game state clean
[neverball] / ball / st_play.c
1 /*   
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include "gui.h"
16 #include "hud.h"
17 #include "game.h"
18 #include "demo.h"
19 #include "level.h"
20 #include "audio.h"
21 #include "config.h"
22
23 #include "st_play.h"
24 #include "st_fail.h"
25 #include "st_goal.h"
26 #include "st_over.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 static int view_rotate;
31
32 /*---------------------------------------------------------------------------*/
33
34 static int play_ready_enter(void)
35 {
36     int id;
37
38     if ((id = gui_label(0, _("Ready?"), GUI_LRG, GUI_ALL, 0, 0)))
39     {
40         gui_layout(id, 0, 0);
41         gui_pulse(id, 1.2f);
42     }
43
44     audio_play(AUD_READY, 1.0f);
45     config_set_grab();
46
47     return id;
48 }
49
50 static void play_ready_leave(int id)
51 {
52     gui_delete(id);
53 }
54
55 static void play_ready_paint(int id, float st)
56 {
57     game_draw(0, st);
58     gui_paint(id);
59 }
60
61 static void play_ready_timer(int id, float dt)
62 {
63     float t = time_state();
64
65     game_set_fly(1.0f - 0.5f * t);
66
67     if (dt > 0.0f && t > 1.0f)
68         goto_state(&st_play_set);
69
70     game_step_fade(dt);
71     gui_timer(id, dt);
72     audio_timer(dt);
73 }
74
75 static int play_ready_click(int b, int d)
76 {
77     return (b < 0 && d == 1) ? goto_state(&st_play_loop) : 1;
78 }
79
80 static int play_ready_keybd(int c, int d)
81 {
82     return (d && c == SDLK_ESCAPE) ? goto_state(&st_over) : 1;
83 }
84
85 static int play_ready_buttn(int b, int d)
86 {
87     if (d)
88     {
89         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
90             return goto_state(&st_play_loop);
91         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
92             return goto_state(&st_over);
93     }
94     return 1;
95 }
96
97 /*---------------------------------------------------------------------------*/
98
99 static int play_set_enter(void)
100 {
101     int id;
102
103     if ((id = gui_label(0, _("Set?"), GUI_LRG, GUI_ALL, 0, 0)))
104     {
105         gui_layout(id, 0, 0);
106         gui_pulse(id, 1.2f);
107     }
108
109     audio_play(AUD_SET, 1.f);
110
111     return id;
112 }
113
114 static void play_set_leave(int id)
115 {
116     gui_delete(id);
117 }
118
119 static void play_set_paint(int id, float st)
120 {
121     game_draw(0, st);
122     gui_paint(id);
123 }
124
125 static void play_set_timer(int id, float dt)
126 {
127     float t = time_state();
128
129     game_set_fly(0.5f - 0.5f * t);
130
131     if (dt > 0.0f && t > 1.0f)
132         goto_state(&st_play_loop);
133
134     game_step_fade(dt);
135     gui_timer(id, dt);
136 }
137
138 static int play_set_click(int b, int d)
139 {
140     if (b < 0 && d == 1)
141     {
142         game_set_fly(0.0f);
143         return goto_state(&st_play_loop);
144     }
145     return 1;
146 }
147
148 static int play_set_keybd(int c, int d)
149 {
150     return (d && c == SDLK_ESCAPE) ? goto_state(&st_over) : 1;
151 }
152
153 static int play_set_buttn(int b, int d)
154 {
155     if (d)
156     {
157         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
158             return goto_state(&st_play_loop);
159         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
160             return goto_state(&st_over);
161     }
162     return 1;
163 }
164
165 /*---------------------------------------------------------------------------*/
166
167 static int play_loop_enter(void)
168 {
169     int id;
170
171     if ((id = gui_label(0, _("GO!"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
172     {
173         gui_layout(id, 0, 0);
174         gui_pulse(id, 1.2f);
175     }
176
177     audio_play(AUD_GO, 1.f);
178
179     game_set_fly(0.f);
180     view_rotate = 0;
181
182     return id;
183 }
184
185 static void play_loop_leave(int id)
186 {
187     gui_delete(id);
188 }
189
190 static void play_loop_paint(int id, float st)
191 {
192     game_draw(0, st);
193     hud_paint();
194
195     if (time_state() < 1.f)
196         gui_paint(id);
197 }
198
199 static void play_loop_timer(int id, float dt)
200 {
201     float k = (((SDL_GetModState() & KMOD_LSHIFT) ||
202                 (SDL_GetModState() & KMOD_RSHIFT)) ?
203                (float) config_get_d(CONFIG_ROTATE_FAST) / 100.f:
204                (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.f);
205
206     static float at = 0;
207
208     float g[3] = { 0.0f, -9.8f, 0.0f };
209
210     at = (7 * at + dt) / 8;
211
212     gui_timer(id, at);
213     hud_timer(at);
214     game_set_rot(view_rotate * k);
215
216     switch (game_step(g, at, 1))
217     {
218     case GAME_TIME: level_stop(GAME_TIME); goto_state(&st_time_out); break;
219     case GAME_FALL: level_stop(GAME_FALL); goto_state(&st_fall_out); break;
220     case GAME_GOAL: level_stop(GAME_GOAL); goto_state(&st_goal);     break;
221     }
222
223     game_step_fade(dt);
224     demo_play_step(at);
225     audio_timer(dt);
226 }
227
228 static void play_loop_point(int id, int x, int y, int dx, int dy)
229 {
230     game_set_pos(dx, dy);
231 }
232
233 static void play_loop_stick(int id, int a, int k)
234 {
235     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
236         game_set_z(k);
237     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
238         game_set_x(k);
239 }
240
241 static int play_loop_click(int b, int d)
242 {
243     view_rotate = d ? b : 0;
244     return 1;
245 }
246
247 static int play_loop_keybd(int c, int d)
248 {
249     if (d)
250     {
251         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
252             view_rotate = +1;
253         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
254             view_rotate = -1;
255
256         if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
257         {
258             config_set_d(CONFIG_CAMERA, 0);
259             hud_view_pulse(0);
260         }
261         if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
262         {
263             config_set_d(CONFIG_CAMERA, 1);
264             hud_view_pulse(1);
265         }
266         if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
267         {
268             config_set_d(CONFIG_CAMERA, 2);
269             hud_view_pulse(2);
270         }
271     }
272     else
273     {
274         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
275             view_rotate = 0;
276         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
277             view_rotate = 0;
278     }
279
280     if (d && c == SDLK_ESCAPE)
281     {
282         level_stop(GAME_NONE);
283         goto_state(&st_over);
284     }
285     if (d && c == SDLK_F12)
286         goto_state(&st_look);
287     
288     /* Cheat */
289     if (d && c == SDLK_c)
290     {
291         level_stop(GAME_GOAL);
292         goto_state(&st_goal);
293     }
294     return 1;
295 }
296
297 static int play_loop_buttn(int b, int d)
298 {
299     if (d == 1)
300     {
301         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
302             return goto_state(&st_over);
303
304         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
305             view_rotate = +1;
306         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
307             view_rotate = -1;
308
309         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
310         {
311             config_set_d(CONFIG_CAMERA, 0);
312             hud_view_pulse(0);
313         }
314         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
315         {
316             config_set_d(CONFIG_CAMERA, 1);
317             hud_view_pulse(1);
318         }
319         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
320         {
321             config_set_d(CONFIG_CAMERA, 2);
322             hud_view_pulse(2);
323         }
324     }
325     else
326     {
327         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
328             view_rotate = 0;
329         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
330             view_rotate = 0;
331     }
332     return 1;
333 }
334
335 /*---------------------------------------------------------------------------*/
336
337 static float phi;
338 static float theta;
339
340 static int look_enter(void)
341 {
342     phi   = 0;
343     theta = 0;
344     return 0;
345 }
346
347 static void look_leave(int id)
348 {
349 }
350
351 static void look_paint(int id, float st)
352 {
353     game_draw(0, st);
354 }
355
356 static void look_point(int id, int x, int y, int dx, int dy)
357 {
358     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
359     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
360
361     if (phi > +90.0f) phi = +90.0f;
362     if (phi < -90.0f) phi = -90.0f;
363
364     if (theta > +180.0f) theta -= 360.0f;
365     if (theta < -180.0f) theta += 360.0f;
366
367     game_look(phi, theta);
368 }
369
370 static int look_keybd(int c, int d)
371 {
372     if (d && c == SDLK_ESCAPE) goto_state(&st_play_loop);
373     if (d && c == SDLK_F12)    goto_state(&st_play_loop);
374
375     return 1;
376 }
377
378 /*---------------------------------------------------------------------------*/
379
380 struct state st_play_ready = {
381     play_ready_enter,
382     play_ready_leave,
383     play_ready_paint,
384     play_ready_timer,
385     NULL,
386     NULL,
387     play_ready_click,
388     play_ready_keybd,
389     play_ready_buttn,
390     1, 0
391 };
392
393 struct state st_play_set = {
394     play_set_enter,
395     play_set_leave,
396     play_set_paint,
397     play_set_timer,
398     NULL,
399     NULL,
400     play_set_click,
401     play_set_keybd,
402     play_set_buttn,
403     1, 0
404 };
405
406 struct state st_play_loop = {
407     play_loop_enter,
408     play_loop_leave,
409     play_loop_paint,
410     play_loop_timer,
411     play_loop_point,
412     play_loop_stick,
413     play_loop_click,
414     play_loop_keybd,
415     play_loop_buttn,
416     0, 0
417 };
418
419 struct state st_look = {
420     look_enter,
421     look_leave,
422     look_paint,
423     NULL,
424     look_point,
425     NULL,
426     NULL,
427     look_keybd,
428     NULL,
429     0, 0
430 };