Fixed a no-default-button-on-fail bug in Challenge mode.
[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 "levels.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
30 /*---------------------------------------------------------------------------*/
31
32 #define FALL_OUT_NEXT 1
33 #define FALL_OUT_SAME 2
34 #define FALL_OUT_SAVE 3
35 #define FALL_OUT_BACK 4
36 #define FALL_OUT_OVER 5
37
38 static int be_back_soon;
39
40 static int fall_out_action(int i)
41 {
42     audio_play(AUD_MENU, 1.0f);
43
44     switch (i)
45     {
46     case FALL_OUT_BACK:
47         /* Fall through. */
48
49     case FALL_OUT_OVER:
50         level_stop();
51         return goto_state(&st_over);
52
53     case FALL_OUT_SAVE:
54         be_back_soon = 1;
55
56         level_stop();
57         return goto_save(&st_fall_out, &st_fall_out);
58
59     case FALL_OUT_NEXT:
60         level_next();
61         return goto_state(&st_level);
62
63     case FALL_OUT_SAME:
64         level_same();
65         return goto_state(&st_level);
66     }
67
68     return 1;
69 }
70
71 static int fall_out_enter(void)
72 {
73     int id, jd, kd;
74
75     const struct level_game *lg = curr_lg();
76
77     /* Reset hack. */
78     be_back_soon = 0;
79
80     if ((id = gui_vstack(0)))
81     {
82         kd = gui_label(id, _("Fall-out!"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
83
84         gui_space(id);
85
86         if ((jd = gui_harray(id)))
87         {
88             int next_id = 0, retry_id = 0;
89
90             next_id = gui_maybe(jd, _("Next Level"), FALL_OUT_NEXT,
91                                 lg->next_level != NULL);
92
93             if (lg->dead)
94             {
95                 gui_start(jd, _("Game Over"), GUI_SML, FALL_OUT_OVER, 0);
96             }
97             else
98             {
99                 retry_id = gui_state(jd, _("Retry Level"), GUI_SML,
100                                      FALL_OUT_SAME, 0);
101             }
102
103             gui_maybe(jd, _("Save Replay"), FALL_OUT_SAVE, demo_saved());
104
105             /* Default is next if the next level is newly unlocked. */
106
107             if (next_id && lg->unlock)
108                 gui_focus(next_id);
109             else if (retry_id)
110                 gui_focus(retry_id);
111         }
112
113         gui_space(id);
114
115         gui_pulse(kd, 1.2f);
116         gui_layout(id, 0, 0);
117     }
118
119     audio_music_fade_out(2.0f);
120     /* audio_play(AUD_FALL, 1.0f); */
121
122     config_clr_grab();
123
124     return id;
125 }
126
127 static void fall_out_timer(int id, float dt)
128 {
129     float g[3] = { 0.0f, -9.8f, 0.0f };
130
131     if (time_state() < 2.f)
132     {
133         game_step(g, dt, 0);
134         demo_play_step(dt);
135     }
136
137     gui_timer(id, dt);
138     audio_timer(dt);
139 }
140
141 static int fall_out_buttn(int b, int d)
142 {
143     if (d)
144     {
145         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
146             return fall_out_action(gui_token(gui_click()));
147         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
148             return fall_out_action(FALL_OUT_BACK);
149     }
150     return 1;
151 }
152
153 static void fall_out_leave(int id)
154 {
155     /* HACK:  don't run animation if only "visiting" a state. */
156     st_fall_out.timer = be_back_soon ? shared_timer : fall_out_timer;
157
158     gui_delete(id);
159 }
160
161 /*---------------------------------------------------------------------------*/
162
163 struct state st_fall_out = {
164     fall_out_enter,
165     fall_out_leave,
166     shared_paint,
167     fall_out_timer,
168     shared_point,
169     shared_stick,
170     shared_click,
171     NULL,
172     fall_out_buttn,
173     1, 0
174 };
175