]> git.dkaiser.de - 42/so_long.git/commitdiff
Outsource player processing and add collision func
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 14 May 2024 10:45:49 +0000 (12:45 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 14 May 2024 10:45:49 +0000 (12:45 +0200)
Makefile
include/so_long.h
src/collision.c [new file with mode: 0644]
src/loop.c
src/player_process.c [new file with mode: 0644]

index 763c89798b5ff8ccc65a26c6098ab0695976b463..84d93f3257639b633f395cdfae76b93da111fa99 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,8 @@ HEADERS =       -Iinclude -Ilibft -IMLX42/include
 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))
index e9097d1587acdcefdc01f67b9245bbeb991d94d0..bd403711c46351496cf4eaa532ba039a84d6d312 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -80,8 +80,12 @@ int                          load_map_from_file(t_tilemap *tilemap, char *filename);
 
 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
diff --git a/src/collision.c b/src/collision.c
new file mode 100644 (file)
index 0000000..fee1452
--- /dev/null
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
+
index 62b5ed9b0b6195b3f419265e3b4ec89473f38d56..c4f95b3dd6941b7732a5f9f327329881555bc8fb 100644 (file)
@@ -6,11 +6,10 @@
 /*   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)
@@ -18,13 +17,6 @@ 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);
 }
diff --git a/src/player_process.c b/src/player_process.c
new file mode 100644 (file)
index 0000000..8cabe9a
--- /dev/null
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}