rotation field and getFacingCoord method moved from Player to Entity

This commit is contained in:
Marcin Kostrzewski 2020-05-13 17:33:45 +02:00
parent 16bb2cd311
commit 45e7c412f8
2 changed files with 28 additions and 24 deletions

View File

@ -1,3 +1,4 @@
from enum import Enum
from pathlib import Path
import pygame
@ -20,8 +21,12 @@ class Entity(pygame.sprite.Sprite):
self.image.set_colorkey((255, 255, 255))
self.rect.x = pos[0]
self.rect.y = pos[1]
# Unique ID
self.id = self.setNewId()
# Where the entity is facing
self.rotation = Rotations.NORTH
@staticmethod
def setNewId():
"""
@ -53,3 +58,24 @@ class Entity(pygame.sprite.Sprite):
image = pygame.transform.scale(image, (tileSize, tileSize))
rect = image.get_rect()
return image, rect
def getFacingCoord(self):
"""
Get coordinates forward to the player.
:return: Position tuple
"""
if self.rotation.value == Rotations.NORTH.value:
return self.rect.x, self.rect.y - self.rect.h
elif self.rotation.value == Rotations.SOUTH.value:
return self.rect.x, self.rect.y + self.rect.h
elif self.rotation.value == Rotations.EAST.value:
return self.rect.x + self.rect.h, self.rect.y
elif self.rotation.value == Rotations.WEST.value:
return self.rect.x - self.rect.h, self.rect.y
class Rotations(Enum):
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3

View File

@ -3,6 +3,7 @@ from enum import Enum
import pygame
from entities.Entity import Rotations
from src.entities.Entity import Entity
from src.entities.Statistics import Statistics
@ -16,8 +17,7 @@ class Player(Entity):
# Entity constructor
super().__init__("player.png", size, (spawnpoint[0] * size, spawnpoint[1] * size))
# Where the player is facing, 0 - north, 1
self.rotation = Rotations.NORTH
self.statistics = Statistics(100, 0, 0, 100)
# How many steps has the player taken through its lifetime
self.movePoints = 0
@ -126,21 +126,6 @@ class Player(Entity):
self.statistics.set_stamina(2)
self.fatigueTimeout = 0
# TODO: Remove
def getFacingCoord(self):
"""
Get coordinates forward to the player.
:return: Position tuple
"""
if self.rotation == Rotations.NORTH:
return self.rect.x, self.rect.y - self.rect.h
elif self.rotation == Rotations.SOUTH:
return self.rect.x, self.rect.y + self.rect.h
elif self.rotation == Rotations.EAST:
return self.rect.x + self.rect.h, self.rect.y
elif self.rotation == Rotations.WEST:
return self.rect.x - self.rect.h, self.rect.y
def getStatistic(self, stat):
"""
Get the specified statistic as an integer.
@ -199,13 +184,6 @@ class Player(Entity):
self.determineLife()
class Rotations(Enum):
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
class StatisticNames(Enum):
HP = 0
STAMINA = 1