Restart updates
[neverball] / ball / st_fall_out.c
1 /*
2  * Copyright (C) 2007 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 "game.h"
17 #include "util.h"
18 #include "progress.h"
19 #include "audio.h"
20 #include "config.h"
21 #include "demo.h"
22
23 #include "st_fall_out.h"
24 #include "st_save.h"
25 #include "st_over.h"
26 #include "st_start.h"
27 #include "st_level.h"
28 #include "st_shared.h"
29 #include "st_play.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 #define FALL_OUT_NEXT 1
34 #define FALL_OUT_SAME 2
35 #define FALL_OUT_SAVE 3
36 #define FALL_OUT_BACK 4
37 #define FALL_OUT_OVER 5
38
39 static int resume;
40
41 static int fall_out_action(int i)
42 {
43     audio_play(AUD_MENU, 1.0f);
44
45     switch (i)
46     {
47     case FALL_OUT_BACK:
48         /* Fall through. */
49
50     case FALL_OUT_OVER:
51         progress_stop();
52         return goto_state(&st_over);
53
54     case FALL_OUT_SAVE:
55         resume = 1;
56
57         progress_stop();
58         return goto_save(&st_fall_out, &st_fall_out);
59
60     case FALL_OUT_NEXT:
61         if (progress_next())
62             return goto_state(&st_level);
63         break;
64
65     case FALL_OUT_SAME:
66         if (progress_same())
67             return goto_state(&st_level);
68         break;
69     }
70
71     return 1;
72 }
73
74 static int fall_out_enter(void)
75 {
76     int id, jd, kd;
77
78     /* Reset hack. */
79     resume = 0;
80
81     if ((id = gui_vstack(0)))
82     {
83         kd = gui_label(id, _("Fall-out!"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
84
85         gui_space(id);
86
87         if ((jd = gui_harray(id)))
88         {
89             if (progress_dead())
90                 gui_start(jd, _("Exit"), GUI_SML, FALL_OUT_OVER, 0);
91
92             if (progress_next_avail())
93                 gui_start(jd, _("Next Level"),  GUI_SML, FALL_OUT_NEXT, 0);
94
95             if (progress_same_avail())
96                 gui_start(jd, _("Retry Level"), GUI_SML, FALL_OUT_SAME, 0);
97
98             if (demo_saved())
99                 gui_state(jd, _("Save Replay"), GUI_SML, FALL_OUT_SAVE, 0);
100         }
101
102         gui_space(id);
103
104         gui_pulse(kd, 1.2f);
105         gui_layout(id, 0, 0);
106     }
107
108     audio_music_fade_out(2.0f);
109     /* audio_play(AUD_FALL, 1.0f); */
110
111     config_clr_grab();
112
113     return id;
114 }
115
116 static void fall_out_timer(int id, float dt)
117 {
118     float g[3] = { 0.0f, -9.8f, 0.0f };
119
120     if (time_state() < 2.f)
121     {
122         demo_play_step();
123         game_step(g, dt, 0);
124     }
125
126     gui_timer(id, dt);
127 }
128
129 static int fall_out_keybd(int c, int d)
130 {
131     if (d)
132     {
133         if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
134         {
135             if (progress_same())
136                 goto_state(&st_play_ready);
137         }
138     }
139     return 1;
140 }
141
142 static int fall_out_buttn(int b, int d)
143 {
144     if (d)
145     {
146         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
147             return fall_out_action(gui_token(gui_click()));
148         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
149             return fall_out_action(FALL_OUT_BACK);
150     }
151     return 1;
152 }
153
154 static void fall_out_leave(int id)
155 {
156     /* HACK:  don't run animation if only "visiting" a state. */
157     st_fall_out.timer = resume ? shared_timer : fall_out_timer;
158
159     gui_delete(id);
160 }
161
162 /*---------------------------------------------------------------------------*/
163
164 struct state st_fall_out = {
165     fall_out_enter,
166     fall_out_leave,
167     shared_paint,
168     fall_out_timer,
169     shared_point,
170     shared_stick,
171     NULL,
172     shared_click,
173     fall_out_keybd,
174     fall_out_buttn,
175     1, 0
176 };
177