Import from neverball-1.4.0.tar.gz
[neverball-archive.git] / ball / st_play.c
blob362b3286d413b667882f3b97c8efcf1868551863
1 /*
2 * Copyright (C) 2003 Robert Kooima
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.
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.
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"
23 #include "st_play.h"
24 #include "st_fail.h"
25 #include "st_goal.h"
26 #include "st_over.h"
28 /*---------------------------------------------------------------------------*/
30 static int view_rotate;
32 /*---------------------------------------------------------------------------*/
34 static int play_ready_enter(void)
36 int id;
38 if ((id = gui_label(0, "Ready?", GUI_LRG, GUI_ALL, 0, 0)))
40 gui_layout(id, 0, 0);
41 gui_pulse(id, 1.2f);
44 audio_play(AUD_READY, 1.0f);
45 config_set_grab();
47 return id;
50 static void play_ready_leave(int id)
52 gui_delete(id);
55 static void play_ready_paint(int id, float st)
57 game_draw(0, st);
58 gui_paint(id);
61 static void play_ready_timer(int id, float dt)
63 float t = time_state();
65 game_set_fly(1.0f - 0.5f * t);
67 if (dt > 0.0f && t > 1.0f)
68 goto_state(&st_play_set);
70 game_step_fade(dt);
71 gui_timer(id, dt);
72 audio_timer(dt);
75 static int play_ready_click(int b, int d)
77 return (b < 0 && d == 1) ? goto_state(&st_play_loop) : 1;
80 static int play_ready_keybd(int c, int d)
82 return (d && c == SDLK_ESCAPE) ? goto_state(&st_over) : 1;
85 static int play_ready_buttn(int b, int d)
87 if (d)
89 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
90 return goto_state(&st_play_loop);
91 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
92 return goto_state(&st_over);
94 return 1;
97 /*---------------------------------------------------------------------------*/
99 static int play_set_enter(void)
101 int id;
103 if ((id = gui_label(0, "Set?", GUI_LRG, GUI_ALL, 0, 0)))
105 gui_layout(id, 0, 0);
106 gui_pulse(id, 1.2f);
109 audio_play(AUD_SET, 1.f);
111 return id;
114 static void play_set_leave(int id)
116 gui_delete(id);
119 static void play_set_paint(int id, float st)
121 game_draw(0, st);
122 gui_paint(id);
125 static void play_set_timer(int id, float dt)
127 float t = time_state();
129 game_set_fly(0.5f - 0.5f * t);
131 if (dt > 0.0f && t > 1.0f)
132 goto_state(&st_play_loop);
134 game_step_fade(dt);
135 gui_timer(id, dt);
138 static int play_set_click(int b, int d)
140 if (b < 0 && d == 1)
142 game_set_fly(0.0f);
143 return goto_state(&st_play_loop);
145 return 1;
148 static int play_set_keybd(int c, int d)
150 return (d && c == SDLK_ESCAPE) ? goto_state(&st_over) : 1;
153 static int play_set_buttn(int b, int d)
155 if (d)
157 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
158 return goto_state(&st_play_loop);
159 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
160 return goto_state(&st_over);
162 return 1;
165 /*---------------------------------------------------------------------------*/
167 static int play_loop_enter(void)
169 int id;
171 if ((id = gui_label(0, "GO!", GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
173 gui_layout(id, 0, 0);
174 gui_pulse(id, 1.2f);
177 audio_play(AUD_GO, 1.f);
179 game_set_fly(0.f);
180 view_rotate = 0;
182 return id;
185 static void play_loop_leave(int id)
187 gui_delete(id);
190 static void play_loop_paint(int id, float st)
192 game_draw(0, st);
193 hud_paint();
195 if (time_state() < 1.f)
196 gui_paint(id);
199 static void play_loop_timer(int id, float dt)
201 float k = (((SDL_GetModState() & KMOD_LSHIFT) ||
202 (SDL_GetModState() & KMOD_RSHIFT)) ?
203 (float) config_get_d(CONFIG_ROTATE_FAST) / 100.f:
204 (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.f);
206 static float at = 0;
208 float g[3] = { 0.0f, -9.8f, 0.0f };
210 at = (7 * at + dt) / 8;
212 gui_timer(id, at);
213 hud_timer(at);
214 game_set_rot(view_rotate * k);
216 switch (game_step(g, at, 1))
218 case GAME_TIME: level_stat(GAME_TIME); goto_state(&st_time_out); break;
219 case GAME_FALL: level_stat(GAME_FALL); goto_state(&st_fall_out); break;
220 case GAME_GOAL: level_stat(GAME_GOAL); goto_state(&st_goal); break;
223 game_step_fade(dt);
224 demo_play_step(at);
225 audio_timer(dt);
228 static void play_loop_point(int id, int x, int y, int dx, int dy)
230 game_set_pos(dx, dy);
233 static void play_loop_stick(int id, int a, int k)
235 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
236 game_set_z(k);
237 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
238 game_set_x(k);
241 static int play_loop_click(int b, int d)
243 view_rotate = d ? b : 0;
244 return 1;
247 static int play_loop_keybd(int c, int d)
249 if (d)
251 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
252 view_rotate = +1;
253 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
254 view_rotate = -1;
256 if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
258 config_set_d(CONFIG_CAMERA, 0);
259 hud_view_pulse(0);
261 if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
263 config_set_d(CONFIG_CAMERA, 1);
264 hud_view_pulse(1);
266 if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
268 config_set_d(CONFIG_CAMERA, 2);
269 hud_view_pulse(2);
272 else
274 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
275 view_rotate = 0;
276 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
277 view_rotate = 0;
280 if (d && c == SDLK_ESCAPE)
281 goto_state(&st_over);
282 if (d && c == SDLK_F12)
283 goto_state(&st_look);
284 return 1;
287 static int play_loop_buttn(int b, int d)
289 if (d == 1)
291 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
292 return goto_state(&st_over);
294 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
295 view_rotate = +1;
296 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
297 view_rotate = -1;
299 if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
301 config_set_d(CONFIG_CAMERA, 0);
302 hud_view_pulse(0);
304 if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
306 config_set_d(CONFIG_CAMERA, 1);
307 hud_view_pulse(1);
309 if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
311 config_set_d(CONFIG_CAMERA, 2);
312 hud_view_pulse(2);
315 else
317 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
318 view_rotate = 0;
319 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
320 view_rotate = 0;
322 return 1;
325 /*---------------------------------------------------------------------------*/
327 static float phi;
328 static float theta;
330 static int look_enter(void)
332 phi = 0;
333 theta = 0;
334 return 0;
337 static void look_leave(int id)
341 static void look_paint(int id, float st)
343 game_draw(0, st);
346 static void look_point(int id, int x, int y, int dx, int dy)
348 phi += 90.0f * dy / config_get_d(CONFIG_HEIGHT);
349 theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
351 if (phi > +90.0f) phi = +90.0f;
352 if (phi < -90.0f) phi = -90.0f;
354 if (theta > +180.0f) theta -= 360.0f;
355 if (theta < -180.0f) theta += 360.0f;
357 game_look(phi, theta);
360 static int look_keybd(int c, int d)
362 if (d && c == SDLK_ESCAPE) goto_state(&st_play_loop);
363 if (d && c == SDLK_F12) goto_state(&st_play_loop);
365 return 1;
368 /*---------------------------------------------------------------------------*/
370 struct state st_play_ready = {
371 play_ready_enter,
372 play_ready_leave,
373 play_ready_paint,
374 play_ready_timer,
375 NULL,
376 NULL,
377 play_ready_click,
378 play_ready_keybd,
379 play_ready_buttn,
380 1, 0
383 struct state st_play_set = {
384 play_set_enter,
385 play_set_leave,
386 play_set_paint,
387 play_set_timer,
388 NULL,
389 NULL,
390 play_set_click,
391 play_set_keybd,
392 play_set_buttn,
393 1, 0
396 struct state st_play_loop = {
397 play_loop_enter,
398 play_loop_leave,
399 play_loop_paint,
400 play_loop_timer,
401 play_loop_point,
402 play_loop_stick,
403 play_loop_click,
404 play_loop_keybd,
405 play_loop_buttn,
406 0, 0
409 struct state st_look = {
410 look_enter,
411 look_leave,
412 look_paint,
413 NULL,
414 look_point,
415 NULL,
416 NULL,
417 look_keybd,
418 NULL,
419 0, 0