Import from neverball-1.3.1.tar.gz
[neverball-archive.git] / ball / st_play.c
blobf13bfaf58d975a9d20db15cb4449672329577391
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 static float at = 0;
203 float g[3] = { 0.0f, -9.8f, 0.0f };
205 at = (7 * at + dt) / 8;
207 gui_timer(id, at);
208 hud_timer(at);
209 game_set_rot(view_rotate);
211 switch (game_step(g, at, 1))
213 case GAME_TIME: level_stat(GAME_TIME); goto_state(&st_time_out); break;
214 case GAME_FALL: level_stat(GAME_FALL); goto_state(&st_fall_out); break;
215 case GAME_GOAL: level_stat(GAME_GOAL); goto_state(&st_goal); break;
218 game_step_fade(dt);
219 demo_play_step(at);
220 audio_timer(dt);
223 static void play_loop_point(int id, int x, int y, int dx, int dy)
225 game_set_pos(dx, dy);
228 static void play_loop_stick(int id, int a, int k)
230 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
231 game_set_z(k);
232 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
233 game_set_x(k);
236 static int play_loop_click(int b, int d)
238 view_rotate = d ? b : 0;
239 return 1;
242 static int play_loop_keybd(int c, int d)
244 if (d)
246 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
247 view_rotate = +1;
248 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
249 view_rotate = -1;
251 if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
253 config_set_d(CONFIG_CAMERA, 0);
254 hud_view_pulse(0);
256 if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
258 config_set_d(CONFIG_CAMERA, 1);
259 hud_view_pulse(1);
261 if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
263 config_set_d(CONFIG_CAMERA, 2);
264 hud_view_pulse(2);
267 else
269 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
270 view_rotate = 0;
271 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
272 view_rotate = 0;
275 if (d && c == SDLK_ESCAPE)
276 goto_state(&st_over);
277 if (d && c == SDLK_F12)
278 goto_state(&st_look);
279 return 1;
282 static int play_loop_buttn(int b, int d)
284 if (d == 1)
286 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
287 return goto_state(&st_over);
289 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
290 view_rotate = +1;
291 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
292 view_rotate = -1;
294 if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
296 config_set_d(CONFIG_CAMERA, 0);
297 hud_view_pulse(0);
299 if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
301 config_set_d(CONFIG_CAMERA, 1);
302 hud_view_pulse(1);
304 if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
306 config_set_d(CONFIG_CAMERA, 2);
307 hud_view_pulse(2);
310 else
312 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
313 view_rotate = 0;
314 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
315 view_rotate = 0;
317 return 1;
320 /*---------------------------------------------------------------------------*/
322 static float phi;
323 static float theta;
325 static int look_enter(void)
327 phi = 0;
328 theta = 0;
329 return 0;
332 static void look_leave(int id)
336 static void look_paint(int id, float st)
338 game_draw(0, st);
341 static void look_point(int id, int x, int y, int dx, int dy)
343 phi += 90.0f * dy / config_get_d(CONFIG_HEIGHT);
344 theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
346 if (phi > +90.0f) phi = +90.0f;
347 if (phi < -90.0f) phi = -90.0f;
349 if (theta > +180.0f) theta -= 360.0f;
350 if (theta < -180.0f) theta += 360.0f;
352 game_look(phi, theta);
355 static int look_keybd(int c, int d)
357 if (d && c == SDLK_ESCAPE) goto_state(&st_play_loop);
358 if (d && c == SDLK_F12) goto_state(&st_play_loop);
360 return 1;
363 /*---------------------------------------------------------------------------*/
365 struct state st_play_ready = {
366 play_ready_enter,
367 play_ready_leave,
368 play_ready_paint,
369 play_ready_timer,
370 NULL,
371 NULL,
372 play_ready_click,
373 play_ready_keybd,
374 play_ready_buttn,
375 1, 0
378 struct state st_play_set = {
379 play_set_enter,
380 play_set_leave,
381 play_set_paint,
382 play_set_timer,
383 NULL,
384 NULL,
385 play_set_click,
386 play_set_keybd,
387 play_set_buttn,
388 1, 0
391 struct state st_play_loop = {
392 play_loop_enter,
393 play_loop_leave,
394 play_loop_paint,
395 play_loop_timer,
396 play_loop_point,
397 play_loop_stick,
398 play_loop_click,
399 play_loop_keybd,
400 play_loop_buttn,
401 0, 0
404 struct state st_look = {
405 look_enter,
406 look_leave,
407 look_paint,
408 NULL,
409 look_point,
410 NULL,
411 NULL,
412 look_keybd,
413 NULL,
414 0, 0