LIBS = -Llibft -lft -lm -LMLX42/build -lmlx42 -ldl -lglfw -pthread
VPATH := src
-SRC = main.c init.c loop.c input.c draw.c tilemap.c
+SRC = main.c init.c loop.c input.c draw.c tilemap.c player_process.c \
+ collision.c
OBJ_DIR := obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */
-/* Updated: 2024/05/11 16:10:26 by dkaiser ### ########.fr */
+/* Updated: 2024/05/14 12:41:08 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
int init(t_game *game);
void loop(void *params);
+void player_process(t_game *game);
int draw(t_game *game);
int draw_map(t_game *game);
void on_key_input(mlx_key_data_t event, void *params);
+int check_collision(t_vector a_pos, t_vector a_size, t_vector b_pos,
+ t_vector b_size);
+
#endif // SO_LONG_H
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* collision.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */
+/* Updated: 2024/05/14 12:26:02 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "so_long.h"
+
+int check_collision(t_vector a_pos, t_vector a_size, t_vector b_pos,
+ t_vector b_size)
+{
+ int result;
+
+ result = 0;
+ if (a_pos.x < b_size.x + b_pos.x && a_pos.x + a_size.x > b_pos.x
+ && a_pos.y < b_size.y + b_pos.y && a_pos.y + a_size.y > b_pos.y)
+ {
+ if (a_pos.x < b_pos.x)
+ result |= RIGHT;
+ if (a_pos.x > b_pos.x)
+ result |= LEFT;
+ if (a_pos.y < b_pos.y)
+ result |= DOWN;
+ if (a_pos.y > b_pos.y)
+ result |= UP;
+ }
+ return (result);
+}
+
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:09:24 by dkaiser #+# #+# */
-/* Updated: 2024/05/10 13:18:51 by dkaiser ### ########.fr */
+/* Updated: 2024/05/14 12:45:26 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "so_long.h"
void loop(void *params)
t_game *game;
game = (t_game *)params;
- game->player.direction.x = ((game->input_direction & RIGHT) != 0)
- - ((game->input_direction & LEFT) != 0);
- game->player.direction.y = ((game->input_direction & DOWN) != 0)
- - ((game->input_direction & UP) != 0);
- game->player.position.x += game->player.direction.x * PLAYER_MOVE_SPEED
- * game->mlx->delta_time;
- game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED
- * game->mlx->delta_time;
+ player_process(game);
draw(game);
}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* player_process.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */
+/* Updated: 2024/05/14 12:44:17 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "so_long.h"
+
+static t_vector get_direction_from_input(t_game *game);
+
+void player_process(t_game *game)
+{
+ t_player player;
+
+ player = game->player;
+ player.direction = get_direction_from_input(game);
+ player.velocity.x = player.direction.x * PLAYER_MOVE_SPEED
+ * game->mlx->delta_time;
+ player.velocity.y = player.direction.y * PLAYER_MOVE_SPEED
+ * game->mlx->delta_time;
+ player.position.x += player.velocity.x;
+ player.position.y += player.velocity.y;
+}
+
+static t_vector get_direction_from_input(t_game *game)
+{
+ t_vector result;
+
+ result.x = ((game->input_direction & RIGHT) != 0)
+ - ((game->input_direction & LEFT) != 0);
+ result.y = ((game->input_direction & DOWN) != 0)
+ - ((game->input_direction & UP) != 0);
+ return (result);
+}