902260e1bc7c968256bda5ed786a8eea26d37918
[neverball] / 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 "levels.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "st_shared.h"
23
24 #include "st_play.h"
25 #include "st_goal.h"
26 #include "st_fall_out.h"
27 #include "st_time_out.h"
28 #include "st_over.h"
29 #include "st_pause.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 static int view_rotate;
34
35 static int pause_or_exit(void)
36 {
37     if (SDL_GetModState() & KMOD_SHIFT)
38     {
39         level_stat(GAME_NONE, curr_clock(), curr_coins());
40         level_stop();
41         config_clr_grab();
42
43         return goto_state(&st_over);
44     }
45     return goto_pause();
46 }
47
48 /*---------------------------------------------------------------------------*/
49
50 static int play_ready_enter(void)
51 {
52     int id;
53
54     if ((id = gui_label(0, _("Ready?"), GUI_LRG, GUI_ALL, 0, 0)))
55     {
56         gui_layout(id, 0, 0);
57         gui_pulse(id, 1.2f);
58     }
59
60     audio_play(AUD_READY, 1.0f);
61     config_set_grab(1);
62
63     return id;
64 }
65
66 static void play_ready_timer(int id, float dt)
67 {
68     float t = time_state();
69
70     game_set_fly(1.0f - 0.5f * t);
71
72     if (dt > 0.0f && t > 1.0f)
73         goto_state(&st_play_set);
74
75     game_step_fade(dt);
76     gui_timer(id, dt);
77 }
78
79 static int play_ready_click(int b, int d)
80 {
81     return (b < 0 && d == 1) ? goto_state(&st_play_loop) : 1;
82 }
83
84 static int play_ready_keybd(int c, int d)
85 {
86     if (d)
87         if (config_tst_d(CONFIG_KEY_PAUSE, c))
88             goto_pause();
89     return 1;
90 }
91
92 static int play_ready_buttn(int b, int d)
93 {
94     if (d)
95     {
96         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
97             return goto_state(&st_play_loop);
98         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
99             return pause_or_exit();
100     }
101     return 1;
102 }
103
104 /*---------------------------------------------------------------------------*/
105
106 static int play_set_enter(void)
107 {
108     int id;
109
110     if ((id = gui_label(0, _("Set?"), GUI_LRG, GUI_ALL, 0, 0)))
111     {
112         gui_layout(id, 0, 0);
113         gui_pulse(id, 1.2f);
114     }
115
116     audio_play(AUD_SET, 1.f);
117
118     clear_pause();
119
120     return id;
121 }
122
123 static void play_set_timer(int id, float dt)
124 {
125     float t = time_state();
126
127     game_set_fly(0.5f - 0.5f * t);
128
129     if (dt > 0.0f && t > 1.0f)
130         goto_state(&st_play_loop);
131
132     game_step_fade(dt);
133     gui_timer(id, dt);
134 }
135
136 static int play_set_click(int b, int d)
137 {
138     if (b < 0 && d == 1)
139     {
140         game_set_fly(0.0f);
141         return goto_state(&st_play_loop);
142     }
143     return 1;
144 }
145
146 static int play_set_keybd(int c, int d)
147 {
148     if (d)
149         if (config_tst_d(CONFIG_KEY_PAUSE, c))
150             goto_pause();
151     return 1;
152 }
153
154 static int play_set_buttn(int b, int d)
155 {
156     if (d)
157     {
158         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
159             return goto_state(&st_play_loop);
160         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
161             return pause_or_exit();
162     }
163     return 1;
164 }
165
166 /*---------------------------------------------------------------------------*/
167
168 static int nohud = 0;
169
170 static int play_loop_enter(void)
171 {
172     int id;
173
174     if (is_paused())
175     {
176         clear_pause();
177         view_rotate = 0;
178         return 0;
179     }
180
181     if ((id = gui_label(0, _("GO!"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
182     {
183         gui_layout(id, 0, 0);
184         gui_pulse(id, 1.2f);
185     }
186
187     audio_play(AUD_GO, 1.f);
188
189     game_set_fly(0.f);
190     view_rotate = 0;
191
192     hud_view_pulse(config_get_d(CONFIG_CAMERA));
193
194     nohud = 0;
195
196     hud_update(0);
197
198     return id;
199 }
200
201 static void play_loop_paint(int id, float st)
202 {
203     game_draw(0, st);
204
205     if (!nohud)
206         hud_paint();
207
208     if (time_state() < 1.f)
209         gui_paint(id);
210 }
211
212 static void play_loop_timer(int id, float dt)
213 {
214     float k = ((SDL_GetModState() & KMOD_SHIFT) ?
215                (float) config_get_d(CONFIG_ROTATE_FAST) / 100.0f :
216                (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.0f);
217
218     float g[3] = { 0.0f, -9.8f, 0.0f };
219
220     gui_timer(id, dt);
221     hud_timer(dt);
222     game_set_rot(view_rotate * k);
223     game_set_cam(config_get_d(CONFIG_CAMERA));
224
225     game_step_fade(dt);
226     demo_play_step();
227
228     switch (game_step(g, dt, 1))
229     {
230     case GAME_GOAL:
231         level_stat(GAME_GOAL, curr_clock(), curr_coins());
232         gui_stuck();
233         goto_state(&st_goal);
234         break;
235
236     case GAME_FALL:
237         level_stat(GAME_FALL, curr_clock(), curr_coins());
238         gui_stuck();
239         goto_state(&st_fall_out);
240         break;
241
242     case GAME_TIME:
243         level_stat(GAME_TIME, curr_clock(), curr_coins());
244         gui_stuck();
245         goto_state(&st_time_out);
246         break;
247
248     default:
249         break;
250     }
251 }
252
253 static void play_loop_point(int id, int x, int y, int dx, int dy)
254 {
255     game_set_pos(dx, dy);
256 }
257
258 static void play_loop_stick(int id, int a, int k)
259 {
260     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
261         game_set_z(k);
262     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
263         game_set_x(k);
264 }
265
266 static void play_loop_angle(int id, int x, int z)
267 {
268     game_set_x(x);
269     game_set_z(z);
270 }
271
272 static int play_loop_click(int b, int d)
273 {
274     view_rotate = d ? b : 0;
275     return 1;
276 }
277
278 static int play_loop_keybd(int c, int d)
279 {
280     if (d)
281     {
282         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
283             view_rotate = +1;
284         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
285             view_rotate = -1;
286
287         if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
288         {
289             config_set_d(CONFIG_CAMERA, 0);
290             hud_view_pulse(0);
291         }
292         if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
293         {
294             config_set_d(CONFIG_CAMERA, 1);
295             hud_view_pulse(1);
296         }
297         if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
298         {
299             config_set_d(CONFIG_CAMERA, 2);
300             hud_view_pulse(2);
301         }
302         if (config_tst_d(CONFIG_KEY_RESTART, c) &&
303             curr_lg()->mode != MODE_CHALLENGE)
304         {
305             level_same();
306             goto_state(&st_play_ready);
307         }
308         if (config_tst_d(CONFIG_KEY_PAUSE, c))
309             goto_pause();
310     }
311     else
312     {
313         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
314             view_rotate = 0;
315         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
316             view_rotate = 0;
317     }
318
319     if (d && c == SDLK_F12 && config_cheat())
320         return goto_state(&st_look);
321
322     if (d && c == SDLK_F6)
323         nohud = !nohud;
324
325     if (d && c == SDLK_c && config_cheat())
326     {
327         level_stat(GAME_GOAL, curr_clock(), curr_coins());
328         return goto_state(&st_goal);
329     }
330     return 1;
331 }
332
333 static int play_loop_buttn(int b, int d)
334 {
335     if (d == 1)
336     {
337         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
338             pause_or_exit();
339
340         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
341             view_rotate = +1;
342         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
343             view_rotate = -1;
344
345         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
346         {
347             config_set_d(CONFIG_CAMERA, 0);
348             hud_view_pulse(0);
349         }
350         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
351         {
352             config_set_d(CONFIG_CAMERA, 1);
353             hud_view_pulse(1);
354         }
355         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
356         {
357             config_set_d(CONFIG_CAMERA, 2);
358             hud_view_pulse(2);
359         }
360     }
361     else
362     {
363         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
364             view_rotate = 0;
365         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
366             view_rotate = 0;
367     }
368     return 1;
369 }
370
371 /*---------------------------------------------------------------------------*/
372
373 static float phi;
374 static float theta;
375
376 static int look_enter(void)
377 {
378     phi   = 0;
379     theta = 0;
380     return 0;
381 }
382
383 static void look_leave(int id)
384 {
385 }
386
387 static void look_paint(int id, float st)
388 {
389     game_draw(0, st);
390 }
391
392 static void look_point(int id, int x, int y, int dx, int dy)
393 {
394     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
395     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
396
397     if (phi > +90.0f) phi = +90.0f;
398     if (phi < -90.0f) phi = -90.0f;
399
400     if (theta > +180.0f) theta -= 360.0f;
401     if (theta < -180.0f) theta += 360.0f;
402
403     game_look(phi, theta);
404 }
405
406 static int look_keybd(int c, int d)
407 {
408     if (d && c == SDLK_F12)
409         return goto_state(&st_play_loop);
410
411     return 1;
412 }
413
414 static int look_buttn(int b, int d)
415 {
416     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
417         return goto_state(&st_play_loop);
418
419     return 1;
420 }
421
422 /*---------------------------------------------------------------------------*/
423
424 struct state st_play_ready = {
425     play_ready_enter,
426     shared_leave,
427     shared_paint,
428     play_ready_timer,
429     NULL,
430     NULL,
431     NULL,
432     play_ready_click,
433     play_ready_keybd,
434     play_ready_buttn,
435     1, 0
436 };
437
438 struct state st_play_set = {
439     play_set_enter,
440     shared_leave,
441     shared_paint,
442     play_set_timer,
443     NULL,
444     NULL,
445     NULL,
446     play_set_click,
447     play_set_keybd,
448     play_set_buttn,
449     1, 0
450 };
451
452 struct state st_play_loop = {
453     play_loop_enter,
454     shared_leave,
455     play_loop_paint,
456     play_loop_timer,
457     play_loop_point,
458     play_loop_stick,
459     shared_angle,
460     play_loop_click,
461     play_loop_keybd,
462     play_loop_buttn,
463     0, 0
464 };
465
466 struct state st_look = {
467     look_enter,
468     look_leave,
469     look_paint,
470     NULL,
471     look_point,
472     NULL,
473     NULL,
474     NULL,
475     look_keybd,
476     look_buttn,
477     0, 0
478 };