Fixed bug: not being able to rotate with specific collisions

This commit is contained in:
Marcin Kostrzewski 2020-04-05 20:47:24 +02:00
parent 8557eabb97
commit c91c82d80a
2 changed files with 28 additions and 24 deletions

View File

@ -13,24 +13,20 @@ class Player(Entity):
# Move in a desired direction # Move in a desired direction
def move(self, rotation): 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 rotation.value == Rotations.NORTH.value:
if self.rotation.value != rotation.value: self.rect.y -= self.rect.w
self.rotate(rotation) elif rotation.value == Rotations.EAST.value:
# Otherwise, move one tile to a given direction self.rect.x += self.rect.w
else: elif rotation.value == Rotations.SOUTH.value:
# TODO: Collision checks "and not to events" self.rect.y += self.rect.w
if rotation.value == Rotations.NORTH.value: elif rotation.value == Rotations.WEST.value:
self.rect.y -= self.rect.w self.rect.x -= 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): def rotate(self, rotation):
self.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90)) # If the player is not facing given direction, it will not move the first time, it will only get rotated
self.rotation = rotation 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): class Rotations(Enum):
NORTH = 0 NORTH = 0

View File

@ -34,14 +34,22 @@ class EventManager:
# Key names are temporary # Key names are temporary
# TODO: Load key bindings from JSON # 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): if keys[pygame.K_w]:
self.player.move(Rotations.NORTH) self.player.rotate(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): if not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w):
self.player.move(Rotations.SOUTH) self.player.move(Rotations.NORTH)
if keys[pygame.K_d] and not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y): if keys[pygame.K_s]:
self.player.move(Rotations.EAST) self.player.rotate(Rotations.SOUTH)
if keys[pygame.K_a] and not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y): if not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w):
self.player.move(Rotations.WEST) 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)