]> git.lizzy.rs Git - nothing.git/blob - src/game/level.c
94e14ef31daf94758f1c9b2fa87066a0b3c90d1f
[nothing.git] / src / game / level.c
1 #include <SDL2/SDL.h>
2 #include <assert.h>
3
4 #include "game/level.h"
5 #include "game/level/background.h"
6 #include "game/level/camera.h"
7 #include "game/level/goals.h"
8 #include "game/level/lava.h"
9 #include "game/level/platforms.h"
10 #include "game/level/player.h"
11 #include "system/error.h"
12 #include "system/lt.h"
13
14 /* TODO(#118): Level doesn't play the Nothing and Something sounds when the goal get into view */
15
16 struct level_t
17 {
18     lt_t *lt;
19     player_t *player;
20     platforms_t *platforms;
21     goals_t *goals;
22     lava_t *lava;
23     color_t background_color;
24     platforms_t *back_platforms;
25     background_t *background;
26 };
27
28 level_t *create_level_from_file(const char *file_name)
29 {
30     assert(file_name);
31
32     lt_t *const lt = create_lt();
33     if (lt == NULL) {
34         return NULL;
35     }
36
37     level_t *const level = PUSH_LT(lt, malloc(sizeof(level_t)), free);
38     if (level == NULL) {
39         throw_error(ERROR_TYPE_LIBC);
40         RETURN_LT(lt, NULL);
41     }
42
43     FILE *level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose);
44     if (level_file == NULL) {
45         throw_error(ERROR_TYPE_LIBC);
46         RETURN_LT(lt, NULL);
47     }
48
49     char color[7];
50     if (fscanf(level_file, "%6s", color) == EOF) {
51         throw_error(ERROR_TYPE_LIBC);
52         RETURN_LT(lt, NULL);
53     }
54     level->background_color = color_from_hexstr(color);
55
56     level->player = PUSH_LT(lt, create_player_from_stream(level_file), destroy_player);
57     if (level->player == NULL) {
58         RETURN_LT(lt, NULL);
59     }
60
61     level->platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
62     if (level->platforms == NULL) {
63         RETURN_LT(lt, NULL);
64     }
65
66     level->goals = PUSH_LT(lt, create_goals_from_stream(level_file), destroy_goals);
67     if (level->goals == NULL) {
68         RETURN_LT(lt, NULL);
69     }
70
71     level->lava = PUSH_LT(lt, create_lava_from_stream(level_file), destroy_lava);
72     if (level->lava == NULL) {
73         RETURN_LT(lt, NULL);
74     }
75
76     level->back_platforms = PUSH_LT(lt, create_platforms_from_stream(level_file), destroy_platforms);
77     if (level->back_platforms == NULL) {
78         RETURN_LT(lt, NULL);
79     }
80
81     level->background = PUSH_LT(lt, create_background(level->background_color), destroy_background);
82     if (level->background == NULL) {
83         RETURN_LT(lt, NULL);
84     }
85
86     level->lt = lt;
87
88     fclose(RELEASE_LT(lt, level_file));
89
90     return level;
91 }
92
93 void destroy_level(level_t *level)
94 {
95     assert(level);
96     RETURN_LT0(level->lt);
97 }
98
99 int level_render(const level_t *level, camera_t *camera)
100 {
101     assert(level);
102
103     if (camera_clear_background(camera, level->background_color) < 0) {
104         return -1;
105     }
106
107     const rect_t view_port = camera_view_port(camera);
108
109     background_move_to(level->background, vec(view_port.x + view_port.w * 0.5f,
110                                               view_port.y + view_port.h * 0.5f));
111
112     if (background_render(level->background, camera) < 0) {
113         return -1;
114     }
115
116     if (platforms_render(level->back_platforms, camera) < 0) {
117         return -1;
118     }
119
120     if (player_render(level->player, camera) < 0) {
121         return -1;
122     }
123
124     if (lava_render(level->lava, camera) < 0) {
125         return -1;
126     }
127
128     if (platforms_render(level->platforms, camera) < 0) {
129         return -1;
130     }
131
132     if (goals_render(level->goals, camera) < 0) {
133         return -1;
134     }
135
136     /* TODO: player_focus_camera is not supposed to be invoked in level_render */
137     player_focus_camera(level->player, camera);
138
139     /* TODO(#157): goals_cue is not supposed to be invoked in level_render
140      *
141      * But I simply couldn't find a better place for it.
142      */
143     goals_cue(level->goals, camera);
144
145     return 0;
146 }
147
148 int level_update(level_t *level, float delta_time)
149 {
150     assert(level);
151     assert(delta_time > 0);
152
153     player_update(level->player, level->platforms, delta_time);
154
155     player_hide_goals(level->player, level->goals);
156     player_die_from_lava(level->player, level->lava);
157
158     goals_update(level->goals, delta_time);
159     goals_checkpoint(level->goals, level->player);
160     lava_update(level->lava, delta_time);
161
162     return 0;
163 }
164
165 int level_event(level_t *level, const SDL_Event *event)
166 {
167     assert(level);
168     assert(event);
169
170     switch (event->type) {
171     case SDL_KEYDOWN:
172         switch (event->key.keysym.sym) {
173         case SDLK_SPACE:
174             player_jump(level->player);
175             break;
176         }
177         break;
178
179     case SDL_JOYBUTTONDOWN:
180         if (event->jbutton.button == 1) {
181             player_jump(level->player);
182         }
183         break;
184     }
185
186     return 0;
187 }
188
189 int level_input(level_t *level,
190                 const Uint8 *const keyboard_state,
191                 SDL_Joystick *the_stick_of_joy)
192 {
193     assert(level);
194     assert(keyboard_state);
195     (void) the_stick_of_joy;
196
197     if (keyboard_state[SDL_SCANCODE_A]) {
198         player_move_left(level->player);
199     } else if (keyboard_state[SDL_SCANCODE_D]) {
200         player_move_right(level->player);
201     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
202         player_move_left(level->player);
203     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
204         player_move_right(level->player);
205     } else {
206         player_stop(level->player);
207     }
208
209     return 0;
210 }
211
212 int level_reload_preserve_player(level_t *level, const char *file_name)
213 {
214     lt_t * const lt = create_lt();
215     if (lt == NULL) {
216         return -1;
217     }
218
219     /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
220
221     FILE * const level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose);
222     if (level_file == NULL) {
223         throw_error(ERROR_TYPE_LIBC);
224         RETURN_LT(lt, -1);
225     }
226
227     char color[7];
228     if (fscanf(level_file, "%6s", color) == EOF) {
229         throw_error(ERROR_TYPE_LIBC);
230         RETURN_LT(lt, -1);
231     }
232     level->background_color = color_from_hexstr(color);
233
234     player_t * const skipped_player = create_player_from_stream(level_file);
235     if (skipped_player == NULL) {
236         RETURN_LT(lt, -1);
237     }
238     destroy_player(skipped_player);
239
240     platforms_t * const platforms = create_platforms_from_stream(level_file);
241     if (platforms == NULL) {
242         RETURN_LT(lt, -1);
243     }
244     level->platforms = RESET_LT(level->lt, level->platforms, platforms);
245
246     goals_t * const goals = create_goals_from_stream(level_file);
247     if (goals == NULL) {
248         RETURN_LT(lt, -1);
249     }
250     level->goals = RESET_LT(level->lt, level->goals, goals);
251
252     lava_t * const lava = create_lava_from_stream(level_file);
253     if (lava == NULL) {
254         RETURN_LT(lt, -1);
255     }
256     level->lava = RESET_LT(level->lt, level->lava, lava);
257
258     platforms_t * const back_platforms = create_platforms_from_stream(level_file);
259     if (back_platforms == NULL) {
260         RETURN_LT(lt, -1);
261     }
262     level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
263
264     RETURN_LT(lt, 0);
265 }
266
267 int level_sound(level_t *level, sound_medium_t *sound_medium)
268 {
269     if (goals_sound(level->goals, sound_medium) < 0) {
270         return -1;
271     }
272
273     if (player_sound(level->player, sound_medium) < 0) {
274         return -1;
275     }
276
277     return 0;
278 }