From 97097c25294b8a51c9e9c5b2073e45ff2328189b Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 29 Mar 2024 16:02:17 +0100 Subject: [PATCH] feat: add walls and borders * Also fix: getting an object from t_room.objects --- Makefile | 2 +- includes/data_sprites.h | 5 +++-- includes/object.h | 13 ++++++++++-- includes/room.h | 16 ++++++++++----- room/room.ber | 10 +++++----- src/data_sprites.c | 3 ++- src/object_character.c | 12 ++++++----- src/object_snas.c | 3 ++- src/object_wall.c | 44 +++++++++++++++++++++++++++++++++++++++++ src/room.c | 24 ++++++++++++++++++++-- src/room_utils.c | 10 +++++----- 11 files changed, 113 insertions(+), 29 deletions(-) create mode 100644 src/object_wall.c diff --git a/Makefile b/Makefile index 537ca79..c13d7d2 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CODE = main error env input timedloop \ data_assets data_sprites \ display1 display2 display_utils sprite \ room room_utils point \ - object_character object_snas + object_character object_snas object_wall LIBRARIES = mlx libtf LIBRARIES_FILES = libtf/libtf.a LIBRARIES_LINK = mlx diff --git a/includes/data_sprites.h b/includes/data_sprites.h index 3cf3b71..f9cae7f 100644 --- a/includes/data_sprites.h +++ b/includes/data_sprites.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/11 15:56:31 by mcolonna #+# #+# */ -/* Updated: 2024/03/29 13:55:24 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 15:02:34 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ # include "data_assets.h" -# define NB_SPRITES 7 +# define NB_SPRITES 8 typedef enum e_assettype { @@ -43,6 +43,7 @@ typedef enum e_spriteid SPR_SNAS_UP, SPR_SNAS_LEFT, SPR_SNAS_RIGHT, + SPR_WALL, } t_spriteid; #endif diff --git a/includes/object.h b/includes/object.h index 6d168d7..fa016a2 100644 --- a/includes/object.h +++ b/includes/object.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/15 14:58:25 by mcolonna #+# #+# */ -/* Updated: 2024/03/28 17:51:55 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 15:29:58 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,8 @@ typedef struct s_object { t_objecttype type; void *data; -} t_object; + bool solid; +} t_object; typedef t_object (*t_object_init)(t_memclass); @@ -67,4 +68,12 @@ typedef struct s_snas_data t_object snas_init(t_memclass mc); +/**** WALL ****/ +typedef struct s_wall_data +{ + t_sprite spr; +} t_wall_data; + +t_object wall_init(t_memclass mc); + #endif diff --git a/includes/room.h b/includes/room.h index 4870a94..f7be26b 100644 --- a/includes/room.h +++ b/includes/room.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/07 23:00:45 by mcolonna #+# #+# */ -/* Updated: 2024/03/26 16:06:02 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 15:44:11 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,12 +33,18 @@ typedef struct s_roomcase t_object_init object; } t_roomcase; -t_room room_fromfile(t_const_string path); +t_room room_fromfile(t_const_string path); -void room_loop(t_room room); +void room_loop(t_room room); -void room_draw(t_room room); +void room_draw(t_room room); -void room_free(t_room room); +void room_free(t_room room); + +t_object *room_getobjectfaced( + t_room room, t_character *character, t_point pos); + +bool room_canwalk( + t_room room, t_character *character, t_point pos); #endif diff --git a/room/room.ber b/room/room.ber index 7b2d1c6..99eb0d9 100644 --- a/room/room.ber +++ b/room/room.ber @@ -1,5 +1,5 @@ -11111 -1P011 -11111 -10001 -11111 +0011111 +0010000 +0011P00 +0000000 +0000000 diff --git a/src/data_sprites.c b/src/data_sprites.c index 66660f7..f9023ec 100644 --- a/src/data_sprites.c +++ b/src/data_sprites.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/11 16:02:29 by mcolonna #+# #+# */ -/* Updated: 2024/03/29 14:32:57 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 15:02:13 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,4 +20,5 @@ const t_spriteinfo g_spritesmap[NB_SPRITES] = { {OBJECT_SNAS_UP, 1, 50, 50, ANIMATION}, {OBJECT_SNAS_LEFT, 1, 50, 50, ANIMATION}, {OBJECT_SNAS_RIGHT, 1, 50, 50, ANIMATION}, +{WALL_INNER, 1, 50, 50, ANIMATION}, }; diff --git a/src/object_character.c b/src/object_character.c index 6c1f8f7..c739564 100644 --- a/src/object_character.c +++ b/src/object_character.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ -/* Updated: 2024/03/29 13:56:58 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 15:51:38 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,6 @@ t_point character_loop( { t_direction move; - (void)pos; move = NO_DIRECTION; if (character->walk_remaining_steps) if (!--character->walk_remaining_steps) @@ -62,13 +61,16 @@ t_point character_loop( move = brain(); if (move != NO_DIRECTION) { - character->walk_remaining_steps = WALK_STEPS_NB - 1; character->direction = move; + if (room_canwalk(g_env.room, character, pos)) + { + character->walk_remaining_steps = WALK_STEPS_NB - 1; + character_initstate(character); + return (point_fromdirection(character->direction)); + } character_initstate(character); } } - if (move != NO_DIRECTION) - return (point_fromdirection(character->direction)); return (point_init(0, 0)); } diff --git a/src/object_snas.c b/src/object_snas.c index 1943ef1..ca29185 100644 --- a/src/object_snas.c +++ b/src/object_snas.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ -/* Updated: 2024/03/29 13:54:55 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 15:52:13 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,5 +58,6 @@ t_object snas_init(t_memclass mc) data = mem_alloc(err, mc, sizeof(t_snas_data)); data->character = character_init((t_character_sprites *)&sprites); r.data = data; + r.solid = true; return (r); } diff --git a/src/object_wall.c b/src/object_wall.c new file mode 100644 index 0000000..533da27 --- /dev/null +++ b/src/object_wall.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* object_wall.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ +/* Updated: 2024/03/29 15:52:32 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "object.h" +#include "error.h" +#include "sprite.h" +#include "libtf.h" +#include "env.h" + +static t_point wall_loop(t_object *obj, t_point pos) +{ + (void)obj; + (void)pos; + return (point_init(0, 0)); +} + +static void wall_draw(t_object *obj, int x, int y) +{ + sprite_draw(x, y, &((t_wall_data *)obj->data)->spr); +} + +t_object wall_init(t_memclass mc) +{ + static const t_objecttype type + = {loop: wall_loop, draw: wall_draw}; + t_object r; + t_wall_data *data; + + r.type = type; + data = mem_alloc(err, mc, sizeof(t_wall_data)); + data->spr = sprite_init(SPR_WALL); + r.data = data; + r.solid = true; + return (r); +} diff --git a/src/room.c b/src/room.c index 9e05ea8..9ea40ba 100644 --- a/src/room.c +++ b/src/room.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */ -/* Updated: 2024/03/28 17:53:28 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 17:21:58 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -109,7 +109,7 @@ void room_loop(t_room room) pos.y = 0; while (pos.y < room.height) { - object = room.objects[pos.y * room.height + pos.x]; + object = room.objects[pos.y * room.width + pos.x]; if (object && !isinlist(object, objects_done)) { move = object->type.loop(object, pos); @@ -127,3 +127,23 @@ void room_free(t_room room) { mem_freeall(room.mc); } + +t_object *room_getobjectfaced(t_room room, t_character *character, t_point pos) +{ + point_addto(&pos, point_fromdirection(character->direction)); + return (room.objects[pos.y * room.width + pos.x]); +} + +bool room_canwalk(t_room room, t_character *character, t_point pos) +{ + t_point after; + t_object *obj; + + after = pos; + point_addto(&after, point_fromdirection(character->direction)); + if (after.y <= 0 || after.y >= room.height - 1 + || after.x <= 0 || after.x >= room.width - 1) + return (false); + obj = room_getobjectfaced(room, character, pos); + return (!obj || !obj->solid); +} diff --git a/src/room_utils.c b/src/room_utils.c index df9db01..ce64ff4 100644 --- a/src/room_utils.c +++ b/src/room_utils.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */ -/* Updated: 2024/03/28 17:54:58 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 17:21:17 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ static t_roomcase g_roomcases[] = { {c: '0', surface_spr: CASE_FLOOR, object: NULL}, -{c: '1', surface_spr: CASE_WALL, object: NULL}, +{c: '1', surface_spr: CASE_FLOOR, object: wall_init}, {c: 'P', surface_spr: CASE_FLOOR, object: snas_init}, {c: '\0'} }; @@ -82,9 +82,9 @@ void moveobject(t_room room, t_point start, t_point move) point_addto(&end, move); if (start.x == end.x && start.y == end.y) return ; - room.objects[end.y * room.height + end.x] - = room.objects[start.y * room.height + start.x]; - room.objects[start.y * room.height + start.x] = NULL; + room.objects[end.y * room.width + end.x] + = room.objects[start.y * room.width + start.x]; + room.objects[start.y * room.width + start.x] = NULL; } bool isinlist(void *addr, t_list list)