From a76965cd9105937c4bd3b68ae40aed5d2d0b8115 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sat, 9 May 2020 22:54:15 +0200 Subject: [PATCH] Player now keeps track of the movement delay --- src/AI/AutomaticMovement.py | 22 +++++++++++----------- src/entities/Player.py | 26 ++++++++++++++++++++------ src/game/Game.py | 8 ++------ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/AI/AutomaticMovement.py b/src/AI/AutomaticMovement.py index 70161d8..ba12b8e 100644 --- a/src/AI/AutomaticMovement.py +++ b/src/AI/AutomaticMovement.py @@ -49,17 +49,17 @@ class AutomaticMovement: def updatePlayerCoords(self): if self.actualTarget is not None and self.nextMove is not None: - self.player.move(self.nextMove) - self.movesList.remove(self.nextMove) - if len(self.movesList) != 0: - self.nextMove = self.movesList[0] - else: - if self.canPickup: - self.pickUp() - self.canPickup = False - self.movesList = None - self.nextMove = None - self.actualTarget = None + if self.player.move(self.nextMove): + self.movesList.remove(self.nextMove) + if len(self.movesList) != 0: + self.nextMove = self.movesList[0] + else: + if self.canPickup: + self.pickUp() + self.canPickup = False + self.movesList = None + self.nextMove = None + self.actualTarget = None def a_Star(self): print("A* in progress ...") diff --git a/src/entities/Player.py b/src/entities/Player.py index a950359..304b68c 100644 --- a/src/entities/Player.py +++ b/src/entities/Player.py @@ -24,14 +24,27 @@ class Player(Entity): # If a player dies, the death reason is stored here self.deathReason = None + # Tracks time between every move + self.movementTimer = 0 + + # Player can move only so fast + self.moveTimeout = 100 + # Move; movement - Enum + # Returns true if the move is successful def move(self, movement): - # Rotation - if movement.value != Movement.FORWARD.value: - self.updateRotation(movement) - # Else move + # Can move if timeout has elapsed + if self.movementTimer > self.moveTimeout: + self.movementTimer = 0 + # Rotation + if movement.value != Movement.FORWARD.value: + self.updateRotation(movement) + # Else move + else: + self.moveForward() + return True else: - self.moveForward() + return False # Deprecated - use move() instead def moveForward(self): @@ -130,7 +143,8 @@ class Player(Entity): self.timeAlive += self.timer.get_time() # Player gets tired every once in a while #self.applyTimeFatigue(self.timer.get_time()) # COMMENTED FOR A_STAR_TEST - self.timer.tick() + # Adds frametime to movementTimer + self.movementTimer += self.timer.tick() self.determineLife() diff --git a/src/game/Game.py b/src/game/Game.py index abd6503..5186e74 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -102,12 +102,8 @@ class Game: # Call update() method for each entity self.spritesList.update() - # TODO: Move this to player.update() - if self.moveTimer > 0: - self.moveTimer -= self.deltaTime - else: - self.movement.updatePlayerCoords() - self.moveTimer = self.moveTime + # A* + self.movement.updatePlayerCoords() # Draw all sprites self.spritesList.draw(self.screen.pygameScreen)