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