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