From c91c82d80aae8d932947745337dcbfe463633b71 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 5 Apr 2020 20:47:24 +0200 Subject: [PATCH] Fixed bug: not being able to rotate with specific collisions --- src/entities/Player.py | 28 ++++++++++++---------------- src/game/EventManager.py | 24 ++++++++++++++++-------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/entities/Player.py b/src/entities/Player.py index 6472d46..439f38b 100644 --- a/src/entities/Player.py +++ b/src/entities/Player.py @@ -13,24 +13,20 @@ class Player(Entity): # Move in a desired direction def move(self, rotation): - # If the player is not facing given direction, it will not move the first time, it will only get rotated - if self.rotation.value != rotation.value: - self.rotate(rotation) - # Otherwise, move one tile to a given direction - else: - # TODO: Collision checks "and not to events" - if rotation.value == Rotations.NORTH.value: - self.rect.y -= self.rect.w - elif rotation.value == Rotations.EAST.value: - self.rect.x += self.rect.w - elif rotation.value == Rotations.SOUTH.value: - self.rect.y += self.rect.w - elif rotation.value == Rotations.WEST.value: - self.rect.x -= self.rect.w + if rotation.value == Rotations.NORTH.value: + self.rect.y -= self.rect.w + elif rotation.value == Rotations.EAST.value: + self.rect.x += self.rect.w + elif rotation.value == Rotations.SOUTH.value: + self.rect.y += self.rect.w + elif rotation.value == Rotations.WEST.value: + self.rect.x -= self.rect.w def rotate(self, rotation): - self.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90)) - self.rotation = rotation + # If the player is not facing given direction, it will not move the first time, it will only get rotated + if self.rotation.value != rotation.value: + self.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90)) + self.rotation = rotation class Rotations(Enum): NORTH = 0 diff --git a/src/game/EventManager.py b/src/game/EventManager.py index f093015..540b60d 100644 --- a/src/game/EventManager.py +++ b/src/game/EventManager.py @@ -34,14 +34,22 @@ class EventManager: # Key names are temporary # TODO: Load key bindings from JSON - if keys[pygame.K_w] and not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w): - self.player.move(Rotations.NORTH) - if keys[pygame.K_s] and not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w): - self.player.move(Rotations.SOUTH) - if keys[pygame.K_d] and not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y): - self.player.move(Rotations.EAST) - if keys[pygame.K_a] and not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y): - self.player.move(Rotations.WEST) + if keys[pygame.K_w]: + self.player.rotate(Rotations.NORTH) + if not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w): + self.player.move(Rotations.NORTH) + if keys[pygame.K_s]: + self.player.rotate(Rotations.SOUTH) + if not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w): + self.player.move(Rotations.SOUTH) + if keys[pygame.K_d]: + self.player.rotate(Rotations.EAST) + if not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y): + self.player.move(Rotations.EAST) + if keys[pygame.K_a]: + self.player.rotate(Rotations.WEST) + if not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y): + self.player.move(Rotations.WEST)