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