diff --git a/Makefile b/Makefile index 85a99b6..537ca79 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/includes/data_assets.h b/includes/data_assets.h index aed8bec..8250f9e 100644 --- a/includes/data_assets.h +++ b/includes/data_assets.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/includes/data_sprites.h b/includes/data_sprites.h index dbeadd2..3cf3b71 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/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 diff --git a/includes/env.h b/includes/env.h index e361158..7f503c4 100644 --- a/includes/env.h +++ b/includes/env.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 { diff --git a/includes/object.h b/includes/object.h index 841085f..6d168d7 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/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 diff --git a/includes/point.h b/includes/point.h index 7e4f83d..f7c75c4 100644 --- a/includes/point.h +++ b/includes/point.h @@ -6,22 +6,24 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/includes/room.h b/includes/room.h index b9863ff..4870a94 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/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); diff --git a/includes/room_utils.h b/includes/room_utils.h index 53c04a9..4f695cd 100644 --- a/includes/room_utils.h +++ b/includes/room_utils.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/includes/sprite.h b/includes/sprite.h index dbf9f8c..05e0c01 100644 --- a/includes/sprite.h +++ b/includes/sprite.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/src/data_sprites.c b/src/data_sprites.c index f58b15a..66660f7 100644 --- a/src/data_sprites.c +++ b/src/data_sprites.c @@ -6,14 +6,18 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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}, }; diff --git a/src/main.c b/src/main.c index 18d2d54..63afb0c 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) diff --git a/src/object_character.c b/src/object_character.c new file mode 100644 index 0000000..6c1f8f7 --- /dev/null +++ b/src/object_character.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* object_character.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/src/object.c b/src/object_snas.c similarity index 53% rename from src/object.c rename to src/object_snas.c index 30cdb31..1943ef1 100644 --- a/src/object.c +++ b/src/object_snas.c @@ -1,42 +1,62 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* object.c :+: :+: :+: */ +/* object_snas.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/src/point.c b/src/point.c index 76b0daa..bbb3d0c 100644 --- a/src/point.c +++ b/src/point.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); +} diff --git a/src/room.c b/src/room.c index 113879f..9e05ea8 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/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); diff --git a/src/room_utils.c b/src/room_utils.c index 01e0c32..df9db01 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/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); +} diff --git a/src/sprite.c b/src/sprite.c index b3cecc7..8926931 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -6,18 +6,19 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; }