again more code clean, thing become more and more simpler
[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;
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, 1);
193     if (state)
194     {
195         level_stop(state, curr_clock(), curr_coins());
196         switch (state)
197         {
198         case GAME_TIME: goto_state(&st_time_out); break;
199         case GAME_FALL: goto_state(&st_fall_out); break;
200         case GAME_GOAL: goto_state(&st_goal);     break;
201         }
202     }
203
204     game_step_fade(dt);
205     demo_play_step(at);
206     audio_timer(dt);
207 }
208
209 static void play_loop_point(int id, int x, int y, int dx, int dy)
210 {
211     game_set_pos(dx, dy);
212 }
213
214 static void play_loop_stick(int id, int a, int k)
215 {
216     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
217         game_set_z(k);
218     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
219         game_set_x(k);
220 }
221
222 static int play_loop_click(int b, int d)
223 {
224     view_rotate = d ? b : 0;
225     return 1;
226 }
227
228 static int play_loop_keybd(int c, int d)
229 {
230     if (d)
231     {
232         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
233             view_rotate = +1;
234         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
235             view_rotate = -1;
236
237         if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
238         {
239             config_set_d(CONFIG_CAMERA, 0);
240             hud_view_pulse(0);
241         }
242         if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
243         {
244             config_set_d(CONFIG_CAMERA, 1);
245             hud_view_pulse(1);
246         }
247         if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
248         {
249             config_set_d(CONFIG_CAMERA, 2);
250             hud_view_pulse(2);
251         }
252     }
253     else
254     {
255         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
256             view_rotate = 0;
257         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
258             view_rotate = 0;
259     }
260
261     if (d && c == SDLK_F12)
262         return goto_state(&st_look);
263     
264     /* Cheat */
265     if (d && c == SDLK_c && config_get_d(CONFIG_CHEAT))
266     {
267         level_stop(GAME_GOAL, curr_clock(), curr_coins());
268         return goto_state(&st_goal);
269     }
270     return 1;
271 }
272
273 static int play_loop_buttn(int b, int d)
274 {
275     if (d == 1)
276     {
277         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
278         {
279             level_stop(GAME_NONE, curr_clock(), curr_coins());
280             return abort_play();
281         }
282
283         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
284             view_rotate = +1;
285         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
286             view_rotate = -1;
287
288         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
289         {
290             config_set_d(CONFIG_CAMERA, 0);
291             hud_view_pulse(0);
292         }
293         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
294         {
295             config_set_d(CONFIG_CAMERA, 1);
296             hud_view_pulse(1);
297         }
298         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
299         {
300             config_set_d(CONFIG_CAMERA, 2);
301             hud_view_pulse(2);
302         }
303     }
304     else
305     {
306         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
307             view_rotate = 0;
308         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
309             view_rotate = 0;
310     }
311     return 1;
312 }
313
314 /*---------------------------------------------------------------------------*/
315
316 static float phi;
317 static float theta;
318
319 static int look_enter(void)
320 {
321     phi   = 0;
322     theta = 0;
323     return 0;
324 }
325
326 static void look_leave(int id)
327 {
328 }
329
330 static void look_paint(int id, float st)
331 {
332     game_draw(0, st);
333 }
334
335 static void look_point(int id, int x, int y, int dx, int dy)
336 {
337     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
338     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
339
340     if (phi > +90.0f) phi = +90.0f;
341     if (phi < -90.0f) phi = -90.0f;
342
343     if (theta > +180.0f) theta -= 360.0f;
344     if (theta < -180.0f) theta += 360.0f;
345
346     game_look(phi, theta);
347 }
348
349 static int look_keybd(int c, int d)
350 {
351     if (d && c == SDLK_F12)
352         return goto_state(&st_play_loop);
353
354     return 1;
355 }
356
357 static int look_buttn(int b, int d)
358 {
359     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
360         return goto_state(&st_play_loop);
361
362     return 1;
363 }
364
365 /*---------------------------------------------------------------------------*/
366
367 struct state st_play_ready = {
368     play_ready_enter,
369     shared_leave,
370     shared_paint,
371     play_ready_timer,
372     NULL,
373     NULL,
374     play_ready_click,
375     NULL,
376     play_ready_buttn,
377     1, 0
378 };
379
380 struct state st_play_set = {
381     play_set_enter,
382     shared_leave,
383     shared_paint,
384     play_set_timer,
385     NULL,
386     NULL,
387     play_set_click,
388     NULL,
389     play_set_buttn,
390     1, 0
391 };
392
393 struct state st_play_loop = {
394     play_loop_enter,
395     shared_leave,
396     play_loop_paint,
397     play_loop_timer,
398     play_loop_point,
399     play_loop_stick,
400     play_loop_click,
401     play_loop_keybd,
402     play_loop_buttn,
403     0, 0
404 };
405
406 struct state st_look = {
407     look_enter,
408     look_leave,
409     look_paint,
410     NULL,
411     look_point,
412     NULL,
413     NULL,
414     look_keybd,
415     look_buttn,
416     0, 0
417 };