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