Merge 'progress' branch.
[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
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 resume;
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         progress_stop();
51         return goto_state(&st_over);
52
53     case FALL_OUT_SAVE:
54         resume = 1;
55
56         progress_stop();
57         return goto_save(&st_fall_out, &st_fall_out);
58
59     case FALL_OUT_NEXT:
60         if (progress_next())
61             return goto_state(&st_level);
62         break;
63
64     case FALL_OUT_SAME:
65         if (progress_same())
66             return goto_state(&st_level);
67         break;
68     }
69
70     return 1;
71 }
72
73 static int fall_out_enter(void)
74 {
75     int id, jd, kd;
76
77     /* Reset hack. */
78     resume = 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             if (progress_dead())
89                 gui_start(jd, _("Exit"), GUI_SML, FALL_OUT_OVER, 0);
90
91             if (progress_next_avail())
92                 gui_start(jd, _("Next Level"),  GUI_SML, FALL_OUT_NEXT, 0);
93
94             if (progress_same_avail())
95                 gui_start(jd, _("Retry Level"), GUI_SML, FALL_OUT_SAME, 0);
96
97             if (demo_saved())
98                 gui_state(jd, _("Save Replay"), GUI_SML, FALL_OUT_SAVE, 0);
99         }
100
101         gui_space(id);
102
103         gui_pulse(kd, 1.2f);
104         gui_layout(id, 0, 0);
105     }
106
107     audio_music_fade_out(2.0f);
108     /* audio_play(AUD_FALL, 1.0f); */
109
110     config_clr_grab();
111
112     return id;
113 }
114
115 static void fall_out_timer(int id, float dt)
116 {
117     float g[3] = { 0.0f, -9.8f, 0.0f };
118
119     if (time_state() < 2.f)
120     {
121         demo_play_step();
122         game_step(g, dt, 0);
123     }
124
125     gui_timer(id, dt);
126 }
127
128 static int fall_out_keybd(int c, int d)
129 {
130     if (d)
131     {
132         if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
133             return fall_out_action(FALL_OUT_SAME);
134     }
135     return 1;
136 }
137
138 static int fall_out_buttn(int b, int d)
139 {
140     if (d)
141     {
142         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
143             return fall_out_action(gui_token(gui_click()));
144         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
145             return fall_out_action(FALL_OUT_BACK);
146     }
147     return 1;
148 }
149
150 static void fall_out_leave(int id)
151 {
152     /* HACK:  don't run animation if only "visiting" a state. */
153     st_fall_out.timer = resume ? shared_timer : fall_out_timer;
154
155     gui_delete(id);
156 }
157
158 /*---------------------------------------------------------------------------*/
159
160 struct state st_fall_out = {
161     fall_out_enter,
162     fall_out_leave,
163     shared_paint,
164     fall_out_timer,
165     shared_point,
166     shared_stick,
167     NULL,
168     shared_click,
169     fall_out_keybd,
170     fall_out_buttn,
171     1, 0
172 };
173