Fix des bugs de collision

This commit is contained in:
Yannis 2023-12-29 19:47:50 +01:00
parent acca811311
commit 350064792c

View file

@ -97,9 +97,15 @@ class Entity:
def move(self, x: float, y: float, map_manager: MapManager): def move(self, x: float, y: float, map_manager: MapManager):
"""Fait bouger l'entité en tenant compte des collisions.""" """Fait bouger l'entité en tenant compte des collisions."""
# On normalise la vitesse
initial_speed = math.sqrt(x**2+y**2)
x = x/initial_speed*self.max_speed
y = y/initial_speed*self.max_speed
# On simule le mouvement. Si on ne rencontre pas de collision, on applique le mouvement # On simule le mouvement. Si on ne rencontre pas de collision, on applique le mouvement
if not self.get_collisions(self.x + x, self.y, map_manager): if not self.get_collisions(self.x + x, self.y, map_manager):
final_x = x self.x += x
else: else:
# Si on a une collision, on avance pixel par pixel jusqu'à atteindre la collision # Si on a une collision, on avance pixel par pixel jusqu'à atteindre la collision
i = 0 i = 0
@ -112,11 +118,11 @@ class Entity:
i -= 1 i -= 1
i += 1 i += 1
final_x = i self.x += i
# On répète le procédé avec l'ordonnée # On répète le procédé avec l'ordonnée
if not self.get_collisions(self.x, self.y + y, map_manager): if not self.get_collisions(self.x, self.y + y, map_manager):
final_y = y self.y += y
else: else:
i = 0 i = 0
if y > 0: if y > 0:
@ -128,16 +134,8 @@ class Entity:
i -= 1 i -= 1
i += 1 i += 1
final_y = i self.y += i
# On normalise le vecteur x, y pour ne pas dépacer la vitesse maximale de l'entité
total_len = math.sqrt(final_x**2+final_y**2)
if total_len == 0.:
return
self.x += final_x/total_len*self.max_speed
self.y += final_y/total_len*self.max_speed
def link_animation(self, name: str): def link_animation(self, name: str):
"""Met à jour l'animation en cours de l'entité.""" """Met à jour l'animation en cours de l'entité."""