feat: add walls and borders *

Also fix: getting an object from t_room.objects
This commit is contained in:
mcolonna 2024-03-29 16:02:17 +01:00
parent ef0941e44f
commit 97097c2529
11 changed files with 113 additions and 29 deletions

View file

@ -6,7 +6,7 @@ CODE = main error env input timedloop \
data_assets data_sprites \ data_assets data_sprites \
display1 display2 display_utils sprite \ display1 display2 display_utils sprite \
room room_utils point \ room room_utils point \
object_character object_snas object_character object_snas object_wall
LIBRARIES = mlx libtf LIBRARIES = mlx libtf
LIBRARIES_FILES = libtf/libtf.a LIBRARIES_FILES = libtf/libtf.a
LIBRARIES_LINK = mlx LIBRARIES_LINK = mlx

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 15:56:31 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" # include "data_assets.h"
# define NB_SPRITES 7 # define NB_SPRITES 8
typedef enum e_assettype typedef enum e_assettype
{ {
@ -43,6 +43,7 @@ typedef enum e_spriteid
SPR_SNAS_UP, SPR_SNAS_UP,
SPR_SNAS_LEFT, SPR_SNAS_LEFT,
SPR_SNAS_RIGHT, SPR_SNAS_RIGHT,
SPR_WALL,
} t_spriteid; } t_spriteid;
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:58:25 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; t_objecttype type;
void *data; void *data;
} t_object; bool solid;
} t_object;
typedef t_object (*t_object_init)(t_memclass); typedef t_object (*t_object_init)(t_memclass);
@ -67,4 +68,12 @@ typedef struct s_snas_data
t_object snas_init(t_memclass mc); 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 #endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:00:45 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_object_init object;
} t_roomcase; } 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 #endif

View file

@ -1,5 +1,5 @@
11111 0011111
1P011 0010000
11111 0011P00
10001 0000000
11111 0000000

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 16:02:29 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_UP, 1, 50, 50, ANIMATION},
{OBJECT_SNAS_LEFT, 1, 50, 50, ANIMATION}, {OBJECT_SNAS_LEFT, 1, 50, 50, ANIMATION},
{OBJECT_SNAS_RIGHT, 1, 50, 50, ANIMATION}, {OBJECT_SNAS_RIGHT, 1, 50, 50, ANIMATION},
{WALL_INNER, 1, 50, 50, ANIMATION},
}; };

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 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; t_direction move;
(void)pos;
move = NO_DIRECTION; move = NO_DIRECTION;
if (character->walk_remaining_steps) if (character->walk_remaining_steps)
if (!--character->walk_remaining_steps) if (!--character->walk_remaining_steps)
@ -62,13 +61,16 @@ t_point character_loop(
move = brain(); move = brain();
if (move != NO_DIRECTION) if (move != NO_DIRECTION)
{ {
character->walk_remaining_steps = WALK_STEPS_NB - 1;
character->direction = move; 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); character_initstate(character);
} }
} }
if (move != NO_DIRECTION)
return (point_fromdirection(character->direction));
return (point_init(0, 0)); return (point_init(0, 0));
} }

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 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 = mem_alloc(err, mc, sizeof(t_snas_data));
data->character = character_init((t_character_sprites *)&sprites); data->character = character_init((t_character_sprites *)&sprites);
r.data = data; r.data = data;
r.solid = true;
return (r); return (r);
} }

44
src/object_wall.c Normal file
View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* object_wall.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:31:22 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; pos.y = 0;
while (pos.y < room.height) 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)) if (object && !isinlist(object, objects_done))
{ {
move = object->type.loop(object, pos); move = object->type.loop(object, pos);
@ -127,3 +127,23 @@ void room_free(t_room room)
{ {
mem_freeall(room.mc); 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);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:26:28 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[] = { static t_roomcase g_roomcases[] = {
{c: '0', surface_spr: CASE_FLOOR, object: NULL}, {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: 'P', surface_spr: CASE_FLOOR, object: snas_init},
{c: '\0'} {c: '\0'}
}; };
@ -82,9 +82,9 @@ void moveobject(t_room room, t_point start, t_point move)
point_addto(&end, move); point_addto(&end, move);
if (start.x == end.x && start.y == end.y) if (start.x == end.x && start.y == end.y)
return ; return ;
room.objects[end.y * room.height + end.x] room.objects[end.y * room.width + end.x]
= room.objects[start.y * room.height + start.x]; = room.objects[start.y * room.width + start.x];
room.objects[start.y * room.height + start.x] = NULL; room.objects[start.y * room.width + start.x] = NULL;
} }
bool isinlist(void *addr, t_list list) bool isinlist(void *addr, t_list list)