X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fgame%2Flevel%2Fboxes.c;h=5c747f8d64e138ecce5a0ea84d10900260d68bdd;hb=9d160a8438d75a51b8cb3c7ab727f65dfe2593d3;hp=f6893037f32c90d78e972c988740ede64083dd4d;hpb=3934426232228039aa9e32eadfd5d749440618ce;p=nothing.git diff --git a/src/game/level/boxes.c b/src/game/level/boxes.c index f6893037..5c747f8d 100644 --- a/src/game/level/boxes.c +++ b/src/game/level/boxes.c @@ -5,37 +5,42 @@ #include "game/level/player.h" #include "game/level/player/rigid_rect.h" #include "system/error.h" +#include "system/line_stream.h" #include "system/lt.h" +#include "system/nth_alloc.h" -struct boxes_t +struct Boxes { - lt_t *lt; + Lt *lt; size_t count; - rigid_rect_t **bodies; + Rigid_rect **bodies; }; -boxes_t *create_boxes_from_stream(FILE *stream) +Boxes *create_boxes_from_line_stream(LineStream *line_stream) { - assert(stream); + assert(line_stream); - lt_t *lt = create_lt(); + Lt *lt = create_lt(); if (lt == NULL) { return NULL; } - boxes_t *boxes = PUSH_LT(lt, malloc(sizeof(boxes_t)), free); + Boxes *boxes = PUSH_LT(lt, nth_alloc(sizeof(Boxes)), free); if (boxes == NULL) { throw_error(ERROR_TYPE_LIBC); RETURN_LT(lt, NULL); } - if (fscanf(stream, "%lu", &boxes->count) == EOF) { + if (sscanf( + line_stream_next(line_stream), + "%lu", + &boxes->count) == EOF) { throw_error(ERROR_TYPE_LIBC); RETURN_LT(lt, NULL); } - boxes->bodies = PUSH_LT(lt, malloc(sizeof(rigid_rect_t*) * boxes->count), free); + boxes->bodies = PUSH_LT(lt, nth_alloc(sizeof(Rigid_rect*) * boxes->count), free); if (boxes->bodies == NULL) { throw_error(ERROR_TYPE_LIBC); RETURN_LT(lt, NULL); @@ -44,7 +49,7 @@ boxes_t *create_boxes_from_stream(FILE *stream) for (size_t i = 0; i < boxes->count; ++i) { boxes->bodies[i] = PUSH_LT( lt, - create_rigid_rect_from_stream(stream), + create_rigid_rect_from_line_stream(line_stream), destroy_rigid_rect); if (boxes->bodies[i] == NULL) { RETURN_LT(lt, NULL); @@ -56,13 +61,13 @@ boxes_t *create_boxes_from_stream(FILE *stream) return boxes; } -void destroy_boxes(boxes_t *boxes) +void destroy_boxes(Boxes *boxes) { assert(boxes); RETURN_LT0(boxes->lt); } -int boxes_render(boxes_t *boxes, camera_t *camera) +int boxes_render(Boxes *boxes, Camera *camera) { assert(boxes); assert(camera); @@ -76,7 +81,7 @@ int boxes_render(boxes_t *boxes, camera_t *camera) return 0; } -int boxes_update(boxes_t *boxes, +int boxes_update(Boxes *boxes, float delta_time) { assert(boxes); @@ -91,8 +96,8 @@ int boxes_update(boxes_t *boxes, return 0; } -int boxes_add_to_physical_world(const boxes_t *boxes, - physical_world_t *physical_world) +int boxes_add_to_physical_world(const Boxes *boxes, + Physical_world *physical_world) { assert(boxes); assert(physical_world); @@ -108,9 +113,26 @@ int boxes_add_to_physical_world(const boxes_t *boxes, return 0; } -void boxes_float_in_lava(boxes_t *boxes, lava_t *lava) +void boxes_float_in_lava(Boxes *boxes, Lava *lava) { + assert(boxes); + assert(lava); + for (size_t i = 0; i < boxes->count; ++i) { lava_float_rigid_rect(lava, boxes->bodies[i]); } } + +Rigid_rect *boxes_rigid_rect(Boxes *boxes, const char *id) +{ + assert(boxes); + assert(id); + + for (size_t i = 0; i < boxes->count; ++i) { + if (rigid_rect_has_id(boxes->bodies[i], id)) { + return boxes->bodies[i]; + } + } + + return 0; +}