From 02f0e429daf9b32b4a1916b7e6d9e5bfb6db31b3 Mon Sep 17 00:00:00 2001 From: yannis300307 Date: Tue, 26 Dec 2023 11:08:56 +0100 Subject: [PATCH] =?UTF-8?q?Collisions=20plus=20pr=C3=A9cises?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entity.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/entity.py b/src/entity.py index 9322178..484350d 100644 --- a/src/entity.py +++ b/src/entity.py @@ -1,3 +1,5 @@ +import math + from src.map_manager import MapManager @@ -8,7 +10,7 @@ class Entity: self.x = 8 self.y = 8 - self.collision_rect = [-7, -7, 40, 40] # x1, y1, x2, y2 + self.collision_rect = [-7, -7, 7, 7] # x1, y1, x2, y2 # Time utilisé pour les IA self.time = 0 @@ -29,13 +31,9 @@ class Entity: top_left_corner_tile = (int((x + self.collision_rect[0]) / 16), int((y + self.collision_rect[1]) / 16)) - top_right_corner_tile = (int((x + self.collision_rect[2]) / 16), - int((y + self.collision_rect[1]) / 16)) - bottom_left_corner_tile = (int((x + self.collision_rect[0]) / 16), - int((y + self.collision_rect[3]) / 16)) - bottom_right_corner_tile = (int((x + self.collision_rect[2]) / 16), - int((y + self.collision_rect[3]) / 16)) + bottom_right_corner_tile = (int((x + self.collision_rect[2]-1) / 16), + int((y + self.collision_rect[3]-1) / 16)) collision = False @@ -55,8 +53,33 @@ class Entity: if not collision_x: self.x += x + else: + i = 0 + if x > 0: + while not self.get_collisions(self.x + i, self.y, map_manager): + i += 1 + i -= 1 + else: + while not self.get_collisions(self.x + i, self.y, map_manager): + i -= 1 + i += 1 + + self.x += i + if not collision_y: self.y += y + else: + i = 0 + if y > 0: + while not self.get_collisions(self.x, self.y + i, map_manager): + i += 1 + i -= 1 + else: + while not self.get_collisions(self.x, self.y + i, map_manager): + i -= 1 + i += 1 + + self.y += i #print(top_left_corner_tile, top_right_corner_tile, bottom_left_corner_tile, bottom_right_corner_tile)