Replay pause/end screen: removed info and reordered buttons.
[neverball] / ball / st_goal.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_goal.h"
24 #include "st_save.h"
25 #include "st_over.h"
26 #include "st_done.h"
27 #include "st_start.h"
28 #include "st_level.h"
29 #include "st_name.h"
30 #include "st_shared.h"
31
32 /*---------------------------------------------------------------------------*/
33
34 #define GOAL_NEXT 1
35 #define GOAL_SAME 2
36 #define GOAL_SAVE 3
37 #define GOAL_BACK 4
38 #define GOAL_DONE 5
39 #define GOAL_NAME 6
40 #define GOAL_OVER 7
41
42 static int balls_id;
43 static int coins_id;
44 static int score_id;
45
46 /* Bread crumbs. */
47
48 static int new_name;
49 static int be_back_soon;
50
51 static int goal_action(int i)
52 {
53     audio_play(AUD_MENU, 1.0f);
54
55     switch (i)
56     {
57     case GOAL_BACK:
58         /* Fall through. */
59
60     case GOAL_OVER:
61         level_stop();
62         return goto_state(&st_over);
63
64     case GOAL_SAVE:
65         be_back_soon = 1;
66
67         level_stop();
68         return goto_save(&st_goal, &st_goal);
69
70     case GOAL_NAME:
71         new_name = 1;
72         be_back_soon = 1;
73
74         level_stop();
75         return goto_name(&st_goal, &st_goal);
76
77     case GOAL_DONE:
78         level_stop();
79         return goto_state(&st_done);
80
81     case GOAL_NEXT:
82         level_next();
83         return goto_state(&st_level);
84
85     case GOAL_SAME:
86         level_same();
87         return goto_state(&st_level);
88     }
89
90     return 1;
91 }
92
93 static int goal_enter(void)
94 {
95     const char *s1 = _("New Record");
96     const char *s2 = _("GOAL");
97
98     int id, jd, kd;
99
100     const struct level_game *lg = curr_lg();
101     const struct level *l = lg->level;
102
103     int high;
104
105     high = (lg->time_rank < 3) || (lg->goal_rank < 3) || (lg->coin_rank < 3);
106
107     /* Reset hack. */
108     be_back_soon = 0;
109
110     if (new_name)
111     {
112         level_update_player_name();
113         new_name = 0;
114     }
115
116     if ((id = gui_vstack(0)))
117     {
118         int gid;
119
120         if (high)
121             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
122         else
123             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
124
125         gui_space(id);
126
127         if (lg->mode == MODE_CHALLENGE)
128         {
129             int coins = lg->coins;
130             int score = lg->score - coins;
131             int balls = lg->balls - count_extra_balls(score, coins);
132
133             if ((jd = gui_hstack(id)))
134             {
135                 if ((kd = gui_harray(jd)))
136                 {
137                     balls_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
138                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
139                 }
140                 if ((kd = gui_harray(jd)))
141                 {
142                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
143                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
144                 }
145                 if ((kd = gui_harray(jd)))
146                 {
147                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
148                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
149                 }
150
151                 gui_set_count(balls_id, balls);
152                 gui_set_count(score_id, score);
153                 gui_set_count(coins_id, coins);
154             }
155         }
156         else
157         {
158             balls_id = score_id = coins_id = 0;
159         }
160
161         gui_space(id);
162
163         if ((jd = gui_harray(id)))
164         {
165             gui_most_coins(jd, 1);
166             gui_best_times(jd, 1);
167         }
168
169         gui_space(id);
170
171         if ((jd = gui_harray(id)))
172         {
173             int next_id = 0, retry_id = 0;
174
175             if (lg->win)
176                 gui_start(jd, _("Finish"), GUI_SML, GOAL_DONE, 0);
177             else
178                 next_id = gui_maybe(jd, _("Next Level"),  GOAL_NEXT,
179                                     lg->next_level != NULL);
180
181             if (lg->dead)
182                 gui_start(jd, _("Game Over"), GUI_SML, GOAL_OVER, 0);
183             else
184             {
185                 retry_id = gui_maybe(jd, _("Retry Level"), GOAL_SAME,
186                                      lg->mode != MODE_CHALLENGE);
187             }
188
189             gui_maybe(jd, _("Save Replay"), GOAL_SAVE, demo_saved());
190
191             /* Default is next if the next level is newly unlocked. */
192
193             if (next_id && lg->unlock)
194                 gui_focus(next_id);
195             else if (lg->mode != MODE_CHALLENGE)
196                 gui_focus(retry_id);
197         }
198
199         /* FIXME, I'm ugly. */
200
201         if (high)
202             gui_state(id, _("Change Player Name"),  GUI_SML, GOAL_NAME, 0);
203
204         gui_pulse(gid, 1.2f);
205         gui_layout(id, 0, 0);
206
207     }
208
209     set_most_coins(&l->score.most_coins, lg->coin_rank);
210
211     if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
212         set_best_times(&l->score.unlock_goal, lg->goal_rank, 1);
213     else
214         set_best_times(&l->score.best_times, lg->time_rank, 0);
215
216     audio_music_fade_out(2.0f);
217
218     config_clr_grab();
219
220     return id;
221 }
222
223 static void goal_timer(int id, float dt)
224 {
225     static float DT = 0.0f;
226
227     float g[3] = { 0.0f, 9.8f, 0.0f };
228
229     DT += dt;
230
231     if (time_state() < 1.f)
232     {
233         game_step(g, dt, 0);
234         demo_play_step(dt);
235     }
236     else if (DT > 0.05f && coins_id)
237     {
238         int coins = gui_value(coins_id);
239
240         if (coins > 0)
241         {
242             int score = gui_value(score_id);
243             int balls = gui_value(balls_id);
244
245             gui_set_count(coins_id, coins - 1);
246             gui_pulse(coins_id, 1.1f);
247
248             gui_set_count(score_id, score + 1);
249             gui_pulse(score_id, 1.1f);
250
251             if ((score + 1) % 100 == 0)
252             {
253                 gui_set_count(balls_id, balls + 1);
254                 gui_pulse(balls_id, 2.0f);
255                 audio_play(AUD_BALL, 1.0f);
256             }
257         }
258         DT = 0.0f;
259     }
260
261     gui_timer(id, dt);
262     audio_timer(dt);
263 }
264
265 static int goal_buttn(int b, int d)
266 {
267     if (d)
268     {
269         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
270             return goal_action(gui_token(gui_click()));
271         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
272             return goal_action(GOAL_BACK);
273     }
274     return 1;
275 }
276
277 static void goal_leave(int id)
278 {
279     /* HACK:  don't run animation if only "visiting" a state. */
280     st_goal.timer = be_back_soon ? shared_timer : goal_timer;
281
282     gui_delete(id);
283 }
284
285 /*---------------------------------------------------------------------------*/
286
287 struct state st_goal = {
288     goal_enter,
289     goal_leave,
290     shared_paint,
291     goal_timer,
292     shared_point,
293     shared_stick,
294     shared_click,
295     NULL,
296     goal_buttn,
297     1, 0
298 };
299