]> git.lizzy.rs Git - nothing.git/blob - src/game/level.c
Merge pull request #179 from tsoding/renderer-inside-of-camera
[nothing.git] / src / game / level.c
1 #include <SDL2/SDL.h>
2 #include <assert.h>
3
4 #include "game/camera.h"
5 #include "game/level.h"
6 #include "game/level/background.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     player_focus_camera(level->player, camera);
104
105     if (camera_clear_background(camera, level->background_color) < 0) {
106         return -1;
107     }
108
109     const rect_t view_port = camera_view_port(camera);
110
111     background_move_to(level->background, vec(view_port.x + view_port.w * 0.5f,
112                                               view_port.y + view_port.h * 0.5f));
113
114     if (background_render(level->background, camera) < 0) {
115         return -1;
116     }
117
118     if (platforms_render(level->back_platforms, camera) < 0) {
119         return -1;
120     }
121
122     if (player_render(level->player, camera) < 0) {
123         return -1;
124     }
125
126     if (lava_render(level->lava, camera) < 0) {
127         return -1;
128     }
129
130     if (platforms_render(level->platforms, camera) < 0) {
131         return -1;
132     }
133
134     if (goals_render(level->goals, camera) < 0) {
135         return -1;
136     }
137
138     /* TODO(#157): goals_cue is not supposed to be invoked in level_render
139      *
140      * But I simply couldn't find a better place for it.
141      */
142     goals_cue(level->goals, camera);
143
144     return 0;
145 }
146
147 int level_update(level_t *level, float delta_time)
148 {
149     assert(level);
150     assert(delta_time > 0);
151
152     player_update(level->player, level->platforms, delta_time);
153
154     player_hide_goals(level->player, level->goals);
155     player_die_from_lava(level->player, level->lava);
156
157     goals_update(level->goals, delta_time);
158     goals_checkpoint(level->goals, level->player);
159     lava_update(level->lava, delta_time);
160
161     return 0;
162 }
163
164 int level_event(level_t *level, const SDL_Event *event)
165 {
166     assert(level);
167     assert(event);
168
169     switch (event->type) {
170     case SDL_KEYDOWN:
171         switch (event->key.keysym.sym) {
172         case SDLK_SPACE:
173             player_jump(level->player);
174             break;
175         }
176         break;
177
178     case SDL_JOYBUTTONDOWN:
179         if (event->jbutton.button == 1) {
180             player_jump(level->player);
181         }
182         break;
183     }
184
185     return 0;
186 }
187
188 int level_input(level_t *level,
189                 const Uint8 *const keyboard_state,
190                 SDL_Joystick *the_stick_of_joy)
191 {
192     assert(level);
193     assert(keyboard_state);
194     (void) the_stick_of_joy;
195
196     if (keyboard_state[SDL_SCANCODE_A]) {
197         player_move_left(level->player);
198     } else if (keyboard_state[SDL_SCANCODE_D]) {
199         player_move_right(level->player);
200     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) < 0) {
201         player_move_left(level->player);
202     } else if (the_stick_of_joy && SDL_JoystickGetAxis(the_stick_of_joy, 0) > 0) {
203         player_move_right(level->player);
204     } else {
205         player_stop(level->player);
206     }
207
208     return 0;
209 }
210
211 int level_reload_preserve_player(level_t *level, const char *file_name)
212 {
213     lt_t * const lt = create_lt();
214     if (lt == NULL) {
215         return -1;
216     }
217
218     /* TODO(#104): duplicate code in create_level_from_file and level_reload_preserve_player */
219
220     FILE * const level_file = PUSH_LT(lt, fopen(file_name, "r"), fclose);
221     if (level_file == NULL) {
222         throw_error(ERROR_TYPE_LIBC);
223         RETURN_LT(lt, -1);
224     }
225
226     char color[7];
227     if (fscanf(level_file, "%6s", color) == EOF) {
228         throw_error(ERROR_TYPE_LIBC);
229         RETURN_LT(lt, -1);
230     }
231     level->background_color = color_from_hexstr(color);
232
233     player_t * const skipped_player = create_player_from_stream(level_file);
234     if (skipped_player == NULL) {
235         RETURN_LT(lt, -1);
236     }
237     destroy_player(skipped_player);
238
239     platforms_t * const platforms = create_platforms_from_stream(level_file);
240     if (platforms == NULL) {
241         RETURN_LT(lt, -1);
242     }
243     level->platforms = RESET_LT(level->lt, level->platforms, platforms);
244
245     goals_t * const goals = create_goals_from_stream(level_file);
246     if (goals == NULL) {
247         RETURN_LT(lt, -1);
248     }
249     level->goals = RESET_LT(level->lt, level->goals, goals);
250
251     lava_t * const lava = create_lava_from_stream(level_file);
252     if (lava == NULL) {
253         RETURN_LT(lt, -1);
254     }
255     level->lava = RESET_LT(level->lt, level->lava, lava);
256
257     platforms_t * const back_platforms = create_platforms_from_stream(level_file);
258     if (back_platforms == NULL) {
259         RETURN_LT(lt, -1);
260     }
261     level->back_platforms = RESET_LT(level->lt, level->back_platforms, back_platforms);
262
263     RETURN_LT(lt, 0);
264 }
265
266 int level_sound(level_t *level, sound_medium_t *sound_medium)
267 {
268     if (goals_sound(level->goals, sound_medium) < 0) {
269         return -1;
270     }
271
272     if (player_sound(level->player, sound_medium) < 0) {
273         return -1;
274     }
275
276     return 0;
277 }