bonus ball for bonus levels. Fix #56
[neverball] / ball / st_fail.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 "game.h"
17 #include "levels.h"
18 #include "audio.h"
19 #include "config.h"
20 #include "st_shared.h"
21
22 #include "st_fail.h"
23 #include "st_over.h"
24 #include "st_done.h"
25 #include "st_save.h"
26 #include "st_level.h"
27 #include "st_start.h"
28
29 /*---------------------------------------------------------------------------*/
30
31 #define FAIL_BACK  0
32 #define FAIL_OVER  1
33 #define FAIL_RETRY 2
34 #define FAIL_SAVE  3
35 #define FAIL_NEXT  4
36 #define FAIL_DONE  5
37
38 static int fail_action(int i)
39 {
40     struct state *next;
41     const struct level_game *lg = curr_lg();
42     switch (i)
43     {
44     case FAIL_BACK:
45         return goto_end_level();
46             
47     case FAIL_OVER:
48         return goto_state(&st_over);
49
50     case FAIL_RETRY:
51         return goto_state(&st_level);
52         
53     case FAIL_DONE:
54         return goto_state(&st_over);
55         
56     case FAIL_NEXT:
57         level_next();
58         return goto_state(&st_level);
59
60     case FAIL_SAVE:
61         if (lg->next_level)
62         {
63             level_next();
64             next = &st_level;
65         }
66         else if (lg->dead)
67             next = &st_over;
68         else if (lg->win)
69             next = &st_done;
70         else
71             next = &st_level;
72                 
73         return goto_save(next, next);
74     }
75     return 1;
76 }
77
78 static int fail_buttn(int b, int d)
79 {
80     if (d)
81     {
82         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
83             return fail_action(gui_token(gui_click()));
84         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
85             return fail_action(FAIL_BACK);
86     }
87     return 1;
88 }
89
90 static int gui_fail(const char *title)
91 {
92     int id, jd, kd;
93     const struct level_game *lg = curr_lg();
94
95     if ((id = gui_vstack(0)))
96     {
97         kd = gui_label(id, title, GUI_LRG, GUI_ALL, gui_gry, gui_red);
98     
99         if ((jd = gui_harray(id)))
100         {
101             gui_state(jd, _("Save Replay"),     GUI_SML, FAIL_SAVE,  0);
102
103             if (lg->next_level != NULL && lg->mode != MODE_CHALLENGE)
104                 gui_state(jd, _("Retry Level"), GUI_SML, FAIL_RETRY, 0);
105             
106             if (lg->dead)
107                 gui_start(jd, _("Game Over"),   GUI_SML, FAIL_OVER,  0);
108             else if (lg->win)
109                 gui_start(jd, _("Finish"),        GUI_SML, FAIL_DONE,  0);
110             else if (lg->next_level != NULL)
111                 gui_start(jd, _("Next Level"),  GUI_SML, FAIL_NEXT,  0);
112             else
113                 gui_start(jd, _("Retry Level"), GUI_SML, FAIL_RETRY, 0);
114         }
115
116         gui_pulse(kd, 1.2f);
117         gui_layout(id, 0, 0);
118     }
119
120     return id;
121 }
122
123 /*---------------------------------------------------------------------------*/
124
125 static int fall_out_enter(void)
126 {
127     int id = gui_fail(_("Fall-out!"));
128
129     audio_music_fade_out(2.0f);
130     audio_play(AUD_FALL, 1.0f);
131
132     config_clr_grab();
133
134     return id;
135 }
136
137 static void fall_out_timer(int id, float dt)
138 {
139     float g[3] = { 0.0f, -9.8f, 0.0f };
140
141     if (time_state() < 2.f)
142         game_step(g, dt, NULL);
143
144     gui_timer(id, dt);
145     audio_timer(dt);
146 }
147
148
149 /*---------------------------------------------------------------------------*/
150
151 static int time_out_enter(void)
152 {
153     int id = gui_fail(_("Time's Up!"));
154
155     audio_music_fade_out(2.0f);
156     audio_play(AUD_TIME, 1.0f);
157
158     config_clr_grab();
159
160     return id;
161 }
162
163 /*---------------------------------------------------------------------------*/
164
165 struct state st_fall_out = {
166     fall_out_enter,
167     shared_leave,
168     shared_paint,
169     fall_out_timer,
170     shared_point,
171     shared_stick,
172     shared_click,
173     NULL,
174     fail_buttn,
175     1, 0
176 };
177
178 struct state st_time_out = {
179     time_out_enter,
180     shared_leave,
181     shared_paint,
182     shared_timer,
183     shared_point,
184     shared_stick,
185     shared_click,
186     NULL,
187     fail_buttn,
188     1, 0
189 };