feat: snas can move!!!!!1!

This commit is contained in:
mcolonna 2024-03-26 16:54:10 +01:00
parent 4bd12ffcf1
commit ef0941e44f
17 changed files with 296 additions and 58 deletions

View file

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

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/29 17:43:01 by mcolonna #+# #+# */
/* Updated: 2024/03/13 15:43:09 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 15:09:30 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -84,7 +84,8 @@ typedef enum e_direction
DOWN,
UP,
LEFT,
RIGHT
RIGHT,
NO_DIRECTION
} t_direction;
# define OBJECT_SNAS OBJECT_SNAS_DOWN

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 15:56:31 by mcolonna #+# #+# */
/* Updated: 2024/03/15 18:49:12 by mcolonna ### ########.fr */
/* Updated: 2024/03/29 13:55:24 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,13 @@
# include "data_assets.h"
# define NB_SPRITES 3
# define NB_SPRITES 7
typedef enum e_assettype
{
ANIMATION, // loops through all the sprites
CHARACTER_STILL, // down up left and right positions
} t_assettype;
typedef struct s_spriteinfo
{
@ -23,6 +29,7 @@ typedef struct s_spriteinfo
int nb_frames;
int origin_x;
int origin_y;
t_assettype type;
} t_spriteinfo;
extern const t_spriteinfo g_spritesmap[NB_SPRITES];
@ -31,7 +38,11 @@ typedef enum e_spriteid
{
CASE_WALL,
CASE_FLOOR,
SPR_SNAS_DOWN
SPR_SNAS,
SPR_SNAS_DOWN,
SPR_SNAS_UP,
SPR_SNAS_LEFT,
SPR_SNAS_RIGHT,
} t_spriteid;
#endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */
/* Updated: 2024/03/26 15:48:24 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 12:50:14 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,8 @@
# define WINDOW_WIDTH 600
# define WINDOW_HEIGHT 400
# define WINDOW_TITLE "undretale"
# define FPS 10
# define FPS 20
# define WALK_STEPS_NB 4
typedef struct s_env
{

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:58:25 by mcolonna #+# #+# */
/* Updated: 2024/03/15 20:06:45 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 17:51:55 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
# include "display.h"
# include "sprite.h"
/**** OBJECT ****/
typedef struct s_object t_object;
typedef struct s_objecttype
@ -31,16 +32,39 @@ typedef struct s_object
{
t_objecttype type;
void *data;
} t_object;
typedef struct s_snas_data
{
t_direction direction;
t_sprite spr;
} t_snas_data;
} t_object;
typedef t_object (*t_object_init)(t_memclass);
t_object snas_init(t_memclass mc);
/**** CHARACTER ****/
typedef struct s_character_sprites
{
t_spriteid still;
t_spriteid walk_left;
t_spriteid walk_right;
t_spriteid walk_up;
t_spriteid walk_down;
} t_character_sprites;
typedef struct s_character
{
t_direction direction;
t_sprite spr;
int walk_remaining_steps;
t_character_sprites *sprites;
} t_character;
void character_draw(t_character *character, int x, int y);
t_point character_loop(t_character *character, t_point pos,
t_direction (*brain)(void));
t_character character_init(t_character_sprites *sprites);
/**** SNAS ****/
typedef struct s_snas_data
{
t_character character;
} t_snas_data;
t_object snas_init(t_memclass mc);
#endif

View file

@ -6,22 +6,24 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:05:03 by mcolonna #+# #+# */
/* Updated: 2024/03/15 19:38:46 by mcolonna ### ########.fr */
/* Updated: 2024/03/27 18:22:51 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef POINT_H
# define POINT_H
# include "data_assets.h"
typedef struct s_point
{
int x;
int y;
} t_point;
/*
t_point point_init(int x, int y);
void point_addto(t_point *dest, t_point src);
*/
t_point point_fromdirection(t_direction direction);
t_point point_multiply(t_point point, int x);
#endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:00:45 by mcolonna #+# #+# */
/* Updated: 2024/03/15 18:51:14 by mcolonna ### ########.fr */
/* Updated: 2024/03/26 16:06:02 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,6 +35,8 @@ typedef struct s_roomcase
t_room room_fromfile(t_const_string path);
void room_loop(t_room room);
void room_draw(t_room room);
void room_free(t_room room);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:34:01 by mcolonna #+# #+# */
/* Updated: 2024/03/13 16:46:44 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 17:54:32 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,5 +19,7 @@
int getlinelen(t_const_string line);
void room_getsize(t_memclass mc, t_room *r, t_const_string path);
t_roomcase *getroomcase(t_const_string path, char c);
void moveobject(t_room room, t_point start, t_point move);
bool isinlist(void *addr, t_list list);
#endif

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 22:13:20 by mcolonna #+# #+# */
/* Updated: 2024/03/15 19:55:46 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 15:59:30 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,12 +18,14 @@
typedef struct s_sprite
{
const t_spriteinfo *info;
int index;
} t_sprite;
t_spriteinfo *info;
int index;
} t_sprite;
t_sprite sprite_init(t_spriteid spr);
void sprite_draw(int x, int y, t_sprite *spr);
void sprite_character_set_direction(t_sprite *spr, t_direction value);
#endif

View file

@ -6,14 +6,18 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/11 16:02:29 by mcolonna #+# #+# */
/* Updated: 2024/03/15 15:31:48 by mcolonna ### ########.fr */
/* Updated: 2024/03/29 14:32:57 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "data_sprites.h"
const t_spriteinfo g_spritesmap[NB_SPRITES] = {
{WALL_BORDER, 1, 50, 50},
{FLOOR_1, 1, 50, 50},
{OBJECT_SNAS_DOWN, 1, 50, 50}
{WALL_BORDER, 1, 50, 50, ANIMATION},
{FLOOR_1, 1, 50, 50, ANIMATION},
{OBJECT_SNAS, 0, 50, 50, CHARACTER_STILL},
{OBJECT_SNAS_DOWN, 1, 50, 50, ANIMATION},
{OBJECT_SNAS_UP, 1, 50, 50, ANIMATION},
{OBJECT_SNAS_LEFT, 1, 50, 50, ANIMATION},
{OBJECT_SNAS_RIGHT, 1, 50, 50, ANIMATION},
};

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
/* Updated: 2024/03/26 15:59:54 by mcolonna ### ########.fr */
/* Updated: 2024/03/26 16:42:18 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,15 +33,12 @@ int close_hook(void)
return (0);
}
static bool g_tac = false;
static void loop(void)
{
if (g_tac)
print_line(err, 1, " tac");
else
print_line(err, 1, "tic");
g_tac = !g_tac;
room_loop(g_env.room);
display_erase();
room_draw(g_env.room);
display_flush();
}
int loop_hook(void)

84
src/object_character.c Normal file
View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* object_character.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/03/29 13:56:58 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "object.h"
#include "error.h"
#include "sprite.h"
#include "libtf.h"
#include "env.h"
static void character_initstate(t_character *character)
{
const t_spriteid walk_sprites[] = {
character->sprites->walk_down,
character->sprites->walk_up,
character->sprites->walk_left,
character->sprites->walk_right,
};
if (character->walk_remaining_steps)
character->spr = sprite_init(walk_sprites[character->direction]);
else
{
character->spr = sprite_init(SPR_SNAS);
sprite_character_set_direction(&character->spr, character->direction);
}
}
void character_draw(t_character *character, int x, int y)
{
t_point pos;
pos = point_init(x, y);
point_addto(&pos,
point_multiply(
point_fromdirection(character->direction),
character->walk_remaining_steps * 50 / -WALK_STEPS_NB
));
sprite_draw(pos.x, pos.y, &character->spr);
}
t_point character_loop(
t_character *character, t_point pos, t_direction (*brain)(void))
{
t_direction move;
(void)pos;
move = NO_DIRECTION;
if (character->walk_remaining_steps)
if (!--character->walk_remaining_steps)
character_initstate(character);
if (!character->walk_remaining_steps)
{
move = brain();
if (move != NO_DIRECTION)
{
character->walk_remaining_steps = WALK_STEPS_NB - 1;
character->direction = move;
character_initstate(character);
}
}
if (move != NO_DIRECTION)
return (point_fromdirection(character->direction));
return (point_init(0, 0));
}
t_character character_init(t_character_sprites *sprites)
{
t_character r;
r.direction = DOWN;
r.sprites = sprites;
r.walk_remaining_steps = 0;
character_initstate(&r);
return (r);
}

View file

@ -1,42 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* object.c :+: :+: :+: */
/* object_snas.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */
/* Updated: 2024/03/15 20:00:25 by mcolonna ### ########.fr */
/* Updated: 2024/03/29 13:54:55 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "object.h"
#include "error.h"
#include "sprite.h"
#include "libtf.h"
#include "env.h"
static t_direction snas_brain(void)
{
if (g_env.up && !g_env.down)
return (UP);
if (g_env.down && !g_env.up)
return (DOWN);
if (g_env.left && !g_env.right)
return (LEFT);
if (g_env.right && !g_env.left)
return (RIGHT);
return (NO_DIRECTION);
}
static t_point snas_loop(t_object *obj, t_point pos)
{
const t_point r = {0, 0};
(void)((void)obj, pos);
return (r);
return (character_loop(
&((t_snas_data *)obj->data)->character, pos, snas_brain));
}
static void snas_draw(t_object *obj, int x, int y)
{
sprite_draw(x, y, &((t_snas_data *)obj->data)->spr);
return (character_draw(&((t_snas_data *)obj->data)->character, x, y));
}
t_object snas_init(t_memclass mc)
{
static const t_objecttype type = {loop: snas_loop, draw: snas_draw};
t_object r;
t_snas_data *data;
static const t_objecttype type
= {loop: snas_loop, draw: snas_draw};
static const t_character_sprites sprites = {
still: SPR_SNAS,
walk_left: SPR_SNAS_LEFT,
walk_right: SPR_SNAS_RIGHT,
walk_up: SPR_SNAS_UP,
walk_down: SPR_SNAS_DOWN,
};
t_object r;
t_snas_data *data;
r.type = type;
data = mem_alloc(err, mc, sizeof(t_snas_data));
data->direction = DOWN;
data->spr = sprite_init(SPR_SNAS_DOWN);
data->character = character_init((t_character_sprites *)&sprites);
r.data = data;
return (r);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 15:24:37 by mcolonna #+# #+# */
/* Updated: 2024/03/15 15:26:34 by mcolonna ### ########.fr */
/* Updated: 2024/03/27 18:23:24 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,3 +24,23 @@ void point_addto(t_point *dest, t_point src)
dest->x += src.x;
dest->y += src.y;
}
t_point point_fromdirection(t_direction direction)
{
if (direction == LEFT)
return (point_init(-1, 0));
if (direction == RIGHT)
return (point_init(+1, 0));
if (direction == UP)
return (point_init(0, -1));
if (direction == DOWN)
return (point_init(0, +1));
return (point_init(0, 0));
}
t_point point_multiply(t_point point, int x)
{
point.x *= x;
point.y *= x;
return (point);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 23:31:22 by mcolonna #+# #+# */
/* Updated: 2024/03/25 14:42:55 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 17:53:28 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -94,6 +94,35 @@ void room_draw(t_room room)
}
}
void room_loop(t_room room)
{
t_point pos;
t_object *object;
t_point move;
const t_memclass mc = mem_subclass(err, g_env.mc);
t_list objects_done;
objects_done = list_createempty(mc);
pos.x = 0;
while (pos.x < room.width)
{
pos.y = 0;
while (pos.y < room.height)
{
object = room.objects[pos.y * room.height + pos.x];
if (object && !isinlist(object, objects_done))
{
move = object->type.loop(object, pos);
moveobject(room, pos, move);
list_add(err, &objects_done, object);
}
pos.y++;
}
pos.x++;
}
mem_freeall(mc);
}
void room_free(t_room room)
{
mem_freeall(room.mc);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:26:28 by mcolonna #+# #+# */
/* Updated: 2024/03/15 18:55:48 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 17:54:58 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -73,3 +73,30 @@ t_roomcase *getroomcase(t_const_string path, char c)
err_perror_str(path, "unknown char");
return (NULL);
}
void moveobject(t_room room, t_point start, t_point move)
{
t_point end;
end = start;
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;
}
bool isinlist(void *addr, t_list list)
{
t_list_element *el;
el = list.first;
while (el)
{
if (el->value == addr)
return (true);
el = el->next;
}
return (false);
}

View file

@ -6,18 +6,19 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 22:28:52 by mcolonna #+# #+# */
/* Updated: 2024/03/11 16:36:45 by mcolonna ### ########.fr */
/* Updated: 2024/03/28 16:02:01 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "sprite.h"
#include "display.h"
#include "error.h"
t_sprite sprite_init(t_spriteid id)
{
t_sprite r;
r.info = g_spritesmap + id;
r.info = (t_spriteinfo *)(g_spritesmap + id);
r.index = 0;
return (r);
}
@ -27,7 +28,17 @@ void sprite_draw(int x, int y, t_sprite *spr)
display_draw(
x - spr->info->origin_x, y - spr->info->origin_y,
spr->info->first_frame + spr->index);
spr->index++;
if (spr->index >= spr->info->nb_frames)
spr->index = 0;
if (spr->info->type == ANIMATION)
{
spr->index++;
if (spr->index >= spr->info->nb_frames)
spr->index = 0;
}
}
void sprite_character_set_direction(t_sprite *spr, t_direction value)
{
if (spr->info->type != CHARACTER_STILL)
err("the sprite must be a CHARACTER_STILL sprite.");
spr->index = value;
}