again more code clean, thing become more and more simpler
[neverball] / ball / st_goal.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 <string.h>
16
17 #include "gui.h"
18 #include "set.h"
19 #include "game.h"
20 #include "util.h"
21 #include "levels.h"
22 #include "audio.h"
23 #include "config.h"
24 #include "demo.h"
25 #include "st_shared.h"
26
27 #include "st_goal.h"
28 #include "st_save.h"
29 #include "st_over.h"
30 #include "st_done.h"
31 #include "st_start.h"
32 #include "st_level.h"
33 #include "st_name.h"
34
35 /*---------------------------------------------------------------------------*/
36
37 #define GOAL_NEXT 2
38 #define GOAL_SAME 3
39 #define GOAL_SAVE 4
40 #define GOAL_BACK 5
41 #define GOAL_DONE 6
42 #define GOAL_NAME 7
43
44 static int high;
45 static int time_i;
46 static int coin_i;
47     
48 static int balls_id;
49 static int coins_id;
50 static int score_id;
51
52 extern struct state st_goal_bis;
53
54 static int goal_action(int i)
55 {
56     audio_play(AUD_MENU, 1.0f);
57
58     switch (i)
59     {
60     case GOAL_BACK:
61         return goto_end_level();
62
63     case GOAL_SAVE:
64         return goto_save(&st_goal_bis, &st_goal_bis);
65
66     case GOAL_NAME:
67         return goto_name(&st_goal_bis, &st_goal_bis);
68         
69     case GOAL_DONE:
70         return goto_state(&st_done);
71         
72     case GOAL_NEXT:
73         level_next();
74         return goto_state(&st_level);
75
76     case GOAL_SAME:
77         return goto_state(&st_level);
78     }
79
80     return 1;
81 }
82
83 static int goal_init(int * gidp)
84 {
85     const char *s1 = _("New Record");
86     const char *s2 = _("GOAL");
87     const struct level_game * lg = curr_lg();
88     int mode = curr_lg()->mode;
89
90     int id, jd, kd;
91
92     if ((id = gui_vstack(0)))
93     {
94         int gid;
95
96         if (high)
97             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
98         else
99             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
100
101         gui_space(id);
102
103         if (mode == MODE_CHALLENGE)
104         {
105             int coins = lg->coins;
106             int score = lg->score - coins;
107             int balls = lg->balls - count_extra_balls(score, coins);
108             if ((jd = gui_hstack(id)))
109             {
110                 if ((kd = gui_harray(jd)))
111                 {
112                     balls_id = gui_count(kd,  100, GUI_MED, GUI_RGT);
113                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
114                 }
115                 if ((kd = gui_harray(jd)))
116                 {
117                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
118                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
119                 }
120                 if ((kd = gui_harray(jd)))
121                 {
122                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
123                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
124                 }
125
126                 gui_set_count(balls_id, balls);
127                 gui_set_count(score_id, score);
128                 gui_set_count(coins_id, coins);
129             }
130         }
131         
132         gui_space(id);
133
134         if ((jd = gui_harray(id)))
135         {
136             gui_most_coins(jd, 3, 1);
137             gui_best_times(jd, 3, 1);
138         }
139
140         gui_space(id);
141
142         if ((jd = gui_harray(id)))
143         {
144             if (demo_play_saved())
145                 gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
146             else
147                 gui_label(jd, _("Save Replay"), GUI_SML, GUI_ALL, gui_blk, gui_blk);
148             
149             if (mode != MODE_CHALLENGE)
150                 gui_start(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
151             
152             if (mode == MODE_CHALLENGE && level_last())
153                 gui_start(jd, _("Finish"),      GUI_SML, GOAL_DONE, 0);
154             else if (level_opened(lg->level+1))
155                 gui_state(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
156             else if (mode != MODE_SINGLE)
157                 gui_label(jd, _("Next Level"),  GUI_SML, GUI_ALL, gui_blk, gui_blk);
158         }
159
160         if (high)
161             gui_state(id, _("Change Player Name"),  GUI_SML, GOAL_NAME, 0);
162
163         gui_layout(id, 0, 0);
164         if (gidp) *gidp = gid;
165     }
166
167     set_most_coins(lg->level, coin_i);
168     set_best_times(lg->level, time_i);
169
170     config_clr_grab();
171
172     return id;
173 }
174
175 static int goal_enter(void)
176 {
177     int gid;
178     int r;
179     
180     time_i = 3;
181     coin_i = 3;
182     high   = level_sort(&time_i, &coin_i);
183
184     r = goal_init(&gid);
185     
186     gui_pulse(gid, 1.2f);
187     audio_music_fade_out(2.0f);
188     return r; 
189 }
190
191 static int goal_bis_enter(void)
192 {
193     char player[MAXNAM];
194     config_get_s(CONFIG_PLAYER, player, MAXNAM);
195     level_name(curr_lg()->level, player, time_i, coin_i);
196     return goal_init(NULL);
197 }
198
199 static void goal_timer(int id, float dt)
200 {
201     static float DT = 0.0f;
202
203     float g[3] = { 0.0f, 9.8f, 0.0f };
204
205     DT += dt;
206
207     if (time_state() < 1.f)
208         game_step(g, dt, 0);
209     else if (DT > 0.05f && curr_lg()->mode == MODE_CHALLENGE)
210     {
211         int coins = gui_value(coins_id);
212         if (coins > 0)
213         {
214             int score = gui_value(score_id);
215             int balls = gui_value(balls_id);
216
217             gui_set_count(coins_id, coins - 1);
218             gui_pulse(coins_id, 1.1f);
219             
220             gui_set_count(score_id, score + 1);
221             gui_pulse(score_id, 1.1f);
222             if ((score+1) % 100 == 0)
223             {
224                 gui_set_count(balls_id, balls + 1);
225                 gui_pulse(balls_id, 2.0f);
226                 audio_play(AUD_BALL, 1.0f);
227             }
228         }
229
230         DT = 0.0f;
231     }
232
233     gui_timer(id, dt);
234     audio_timer(dt);
235 }
236
237 static int goal_buttn(int b, int d)
238 {
239     if (d)
240     {
241         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
242             return goal_action(gui_token(gui_click()));
243         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
244             return goal_action(GOAL_BACK);
245     }
246     return 1;
247 }
248
249 /*---------------------------------------------------------------------------*/
250
251 struct state st_goal = {
252     goal_enter,
253     shared_leave,
254     shared_paint,
255     goal_timer,
256     shared_point,
257     shared_stick,
258     shared_click,
259     NULL,
260     goal_buttn,
261     1, 0
262 };
263
264 struct state st_goal_bis = {
265     goal_bis_enter,
266     shared_leave,
267     shared_paint,
268     shared_timer,
269     shared_point,
270     shared_stick,
271     shared_click,
272     NULL,
273     goal_buttn,
274     1, 0
275 };