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