Code clean: improve modules independance:
[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_buttn(int b, int d)
81 {
82     if (d)
83     {
84         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
85             return goto_state(&st_play_loop);
86         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
87             return goto_state(&st_over);
88     }
89     return 1;
90 }
91
92 /*---------------------------------------------------------------------------*/
93
94 static int play_set_enter(void)
95 {
96     int id;
97
98     if ((id = gui_label(0, _("Set?"), GUI_LRG, GUI_ALL, 0, 0)))
99     {
100         gui_layout(id, 0, 0);
101         gui_pulse(id, 1.2f);
102     }
103
104     audio_play(AUD_SET, 1.f);
105
106     return id;
107 }
108
109 static void play_set_leave(int id)
110 {
111     gui_delete(id);
112 }
113
114 static void play_set_paint(int id, float st)
115 {
116     game_draw(0, st);
117     gui_paint(id);
118 }
119
120 static void play_set_timer(int id, float dt)
121 {
122     float t = time_state();
123
124     game_set_fly(0.5f - 0.5f * t);
125
126     if (dt > 0.0f && t > 1.0f)
127         goto_state(&st_play_loop);
128
129     game_step_fade(dt);
130     gui_timer(id, dt);
131 }
132
133 static int play_set_click(int b, int d)
134 {
135     if (b < 0 && d == 1)
136     {
137         game_set_fly(0.0f);
138         return goto_state(&st_play_loop);
139     }
140     return 1;
141 }
142
143 static int play_set_buttn(int b, int d)
144 {
145     if (d)
146     {
147         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
148             return goto_state(&st_play_loop);
149         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
150             return goto_state(&st_over);
151     }
152     return 1;
153 }
154
155 /*---------------------------------------------------------------------------*/
156
157 static int play_loop_enter(void)
158 {
159     int id;
160
161     if ((id = gui_label(0, _("GO!"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
162     {
163         gui_layout(id, 0, 0);
164         gui_pulse(id, 1.2f);
165     }
166
167     audio_play(AUD_GO, 1.f);
168
169     game_set_fly(0.f);
170     view_rotate = 0;
171
172     hud_update(0);
173
174     return id;
175 }
176
177 static void play_loop_leave(int id)
178 {
179     gui_delete(id);
180 }
181
182 static void play_loop_paint(int id, float st)
183 {
184     game_draw(0, st);
185     hud_paint();
186
187     if (time_state() < 1.f)
188         gui_paint(id);
189 }
190
191 static void play_loop_timer(int id, float dt)
192 {
193     float k = (((SDL_GetModState() & KMOD_LSHIFT) ||
194                 (SDL_GetModState() & KMOD_RSHIFT)) ?
195                (float) config_get_d(CONFIG_ROTATE_FAST) / 100.f:
196                (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.f);
197
198     static float at = 0;
199
200     float g[3] = { 0.0f, -9.8f, 0.0f };
201
202     at = (7 * at + dt) / 8;
203
204     gui_timer(id, at);
205     hud_timer(at);
206     game_set_rot(view_rotate * k);
207
208     switch (game_step(g, at, 1))
209     {
210     case GAME_TIME: level_stop(GAME_TIME); goto_state(&st_time_out); break;
211     case GAME_FALL: level_stop(GAME_FALL); goto_state(&st_fall_out); break;
212     case GAME_GOAL: level_stop(GAME_GOAL); goto_state(&st_goal);     break;
213     }
214
215     game_step_fade(dt);
216     demo_play_step(at);
217     audio_timer(dt);
218 }
219
220 static void play_loop_point(int id, int x, int y, int dx, int dy)
221 {
222     game_set_pos(dx, dy);
223 }
224
225 static void play_loop_stick(int id, int a, int k)
226 {
227     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
228         game_set_z(k);
229     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
230         game_set_x(k);
231 }
232
233 static int play_loop_click(int b, int d)
234 {
235     view_rotate = d ? b : 0;
236     return 1;
237 }
238
239 static int play_loop_keybd(int c, int d)
240 {
241     if (d)
242     {
243         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
244             view_rotate = +1;
245         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
246             view_rotate = -1;
247
248         if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
249         {
250             config_set_d(CONFIG_CAMERA, 0);
251             hud_view_pulse(0);
252         }
253         if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
254         {
255             config_set_d(CONFIG_CAMERA, 1);
256             hud_view_pulse(1);
257         }
258         if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
259         {
260             config_set_d(CONFIG_CAMERA, 2);
261             hud_view_pulse(2);
262         }
263     }
264     else
265     {
266         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
267             view_rotate = 0;
268         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
269             view_rotate = 0;
270     }
271
272     if (d && c == SDLK_F12)
273         return goto_state(&st_look);
274     
275     /* Cheat */
276     if (d && c == SDLK_c && config_get_d(CONFIG_CHEAT))
277     {
278         level_stop(GAME_GOAL);
279         return goto_state(&st_goal);
280     }
281     return 1;
282 }
283
284 static int play_loop_buttn(int b, int d)
285 {
286     if (d == 1)
287     {
288         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
289         {
290             level_stop(GAME_NONE);
291             return goto_state(&st_over);
292         }
293
294         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
295             view_rotate = +1;
296         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
297             view_rotate = -1;
298
299         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
300         {
301             config_set_d(CONFIG_CAMERA, 0);
302             hud_view_pulse(0);
303         }
304         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
305         {
306             config_set_d(CONFIG_CAMERA, 1);
307             hud_view_pulse(1);
308         }
309         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
310         {
311             config_set_d(CONFIG_CAMERA, 2);
312             hud_view_pulse(2);
313         }
314     }
315     else
316     {
317         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
318             view_rotate = 0;
319         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
320             view_rotate = 0;
321     }
322     return 1;
323 }
324
325 /*---------------------------------------------------------------------------*/
326
327 static float phi;
328 static float theta;
329
330 static int look_enter(void)
331 {
332     phi   = 0;
333     theta = 0;
334     return 0;
335 }
336
337 static void look_leave(int id)
338 {
339 }
340
341 static void look_paint(int id, float st)
342 {
343     game_draw(0, st);
344 }
345
346 static void look_point(int id, int x, int y, int dx, int dy)
347 {
348     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
349     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
350
351     if (phi > +90.0f) phi = +90.0f;
352     if (phi < -90.0f) phi = -90.0f;
353
354     if (theta > +180.0f) theta -= 360.0f;
355     if (theta < -180.0f) theta += 360.0f;
356
357     game_look(phi, theta);
358 }
359
360 static int look_keybd(int c, int d)
361 {
362     if (d && c == SDLK_F12)
363         return goto_state(&st_play_loop);
364
365     return 1;
366 }
367
368 static int look_buttn(int b, int d)
369 {
370     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
371         return goto_state(&st_play_loop);
372
373     return 1;
374 }
375
376 /*---------------------------------------------------------------------------*/
377
378 struct state st_play_ready = {
379     play_ready_enter,
380     play_ready_leave,
381     play_ready_paint,
382     play_ready_timer,
383     NULL,
384     NULL,
385     play_ready_click,
386     NULL,
387     play_ready_buttn,
388     1, 0
389 };
390
391 struct state st_play_set = {
392     play_set_enter,
393     play_set_leave,
394     play_set_paint,
395     play_set_timer,
396     NULL,
397     NULL,
398     play_set_click,
399     NULL,
400     play_set_buttn,
401     1, 0
402 };
403
404 struct state st_play_loop = {
405     play_loop_enter,
406     play_loop_leave,
407     play_loop_paint,
408     play_loop_timer,
409     play_loop_point,
410     play_loop_stick,
411     play_loop_click,
412     play_loop_keybd,
413     play_loop_buttn,
414     0, 0
415 };
416
417 struct state st_look = {
418     look_enter,
419     look_leave,
420     look_paint,
421     NULL,
422     look_point,
423     NULL,
424     NULL,
425     look_keybd,
426     look_buttn,
427     0, 0
428 };