feat: Collisions Dataclass eingefuehrt und in PhysicsEntity verwendet
- Neue @dataclass Collisions mit Feldern top/right/bottom/left - collisions-Dict in PhysicsEntity durch Collisions-Instanz ersetzt - Attribut-Zugriff (collisions.top) statt Dict-Indexierung - Typisierung von Feldern und Methoden in PhysicsEntity und Player
This commit is contained in:
parent
da973dd968
commit
d796b47c46
1 changed files with 36 additions and 25 deletions
|
|
@ -1,26 +1,37 @@
|
||||||
import pygame
|
import pygame
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from scripts.animation import Animation
|
||||||
from scripts.tilemap import Tilemap
|
from scripts.tilemap import Tilemap
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Collisions:
|
||||||
|
top: bool = False
|
||||||
|
right: bool = False
|
||||||
|
bottom: bool = False
|
||||||
|
left: bool = False
|
||||||
|
|
||||||
|
|
||||||
class PhysicsEntity:
|
class PhysicsEntity:
|
||||||
def __init__(self, game, e_type, pos: tuple, size):
|
def __init__(self, game, e_type: str, pos: tuple[float, float], size: tuple[int, int]):
|
||||||
self.game = game
|
self.game = game
|
||||||
self.e_type = e_type
|
self.e_type: str = e_type
|
||||||
self.pos = list(pos)
|
self.pos: list[float] = list(pos)
|
||||||
self.size = size
|
self.size: tuple[int, int] = size
|
||||||
self.velocity = [0, 0]
|
self.velocity: list[float] = [0, 0]
|
||||||
self.collisions = {"top": False, "bottom": False, "left": False, "right": False}
|
self.collisions: Collisions = Collisions()
|
||||||
self.speed = 3
|
self.speed: int = 3
|
||||||
|
|
||||||
# Animationen
|
# Animationen
|
||||||
self.action = ""
|
self.action: str = ""
|
||||||
self.flip = False
|
self.flip: bool = False
|
||||||
self.animation_offset = (-3, -3)
|
self.animation_offset: tuple[int, int] = (-3, -3)
|
||||||
|
self.animation: Animation
|
||||||
self.set_action("idle")
|
self.set_action("idle")
|
||||||
|
|
||||||
def update(self, tilemap: Tilemap, movement: tuple = (0, 0)):
|
def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None:
|
||||||
self.collisions = {"top": False, "bottom": False, "left": False, "right": False}
|
self.collisions = Collisions()
|
||||||
frame_movement = (
|
frame_movement: tuple[float, float] = (
|
||||||
(movement[0] + self.velocity[0]) * self.speed,
|
(movement[0] + self.velocity[0]) * self.speed,
|
||||||
movement[1] + self.velocity[1],
|
movement[1] + self.velocity[1],
|
||||||
)
|
)
|
||||||
|
|
@ -31,12 +42,12 @@ class PhysicsEntity:
|
||||||
if entity_rect.colliderect(recto):
|
if entity_rect.colliderect(recto):
|
||||||
if frame_movement[0] > 0:
|
if frame_movement[0] > 0:
|
||||||
entity_rect.right = recto.left
|
entity_rect.right = recto.left
|
||||||
self.collisions["right"] = True
|
self.collisions.right = True
|
||||||
self.game.isJumping = False
|
self.game.isJumping = False
|
||||||
|
|
||||||
if frame_movement[0] < 0:
|
if frame_movement[0] < 0:
|
||||||
entity_rect.left = recto.right
|
entity_rect.left = recto.right
|
||||||
self.collisions["left"] = True
|
self.collisions.left = True
|
||||||
self.game.isJumping = False
|
self.game.isJumping = False
|
||||||
|
|
||||||
self.pos[0] = entity_rect.x
|
self.pos[0] = entity_rect.x
|
||||||
|
|
@ -48,12 +59,12 @@ class PhysicsEntity:
|
||||||
if entity_rect.colliderect(rectolino):
|
if entity_rect.colliderect(rectolino):
|
||||||
if frame_movement[1] > 0:
|
if frame_movement[1] > 0:
|
||||||
entity_rect.bottom = rectolino.top
|
entity_rect.bottom = rectolino.top
|
||||||
self.collisions["bottom"] = True
|
self.collisions.bottom = True
|
||||||
self.game.isJumping = False
|
self.game.isJumping = False
|
||||||
|
|
||||||
if frame_movement[1] < 0:
|
if frame_movement[1] < 0:
|
||||||
entity_rect.top = rectolino.bottom
|
entity_rect.top = rectolino.bottom
|
||||||
self.collisions["top"] = True
|
self.collisions.top = True
|
||||||
|
|
||||||
self.pos[1] = entity_rect.y
|
self.pos[1] = entity_rect.y
|
||||||
|
|
||||||
|
|
@ -62,14 +73,14 @@ class PhysicsEntity:
|
||||||
if movement[0] < 0:
|
if movement[0] < 0:
|
||||||
self.flip = True
|
self.flip = True
|
||||||
|
|
||||||
if self.collisions["bottom"] or self.collisions["top"]:
|
if self.collisions.bottom or self.collisions.top:
|
||||||
self.velocity[1] = 0
|
self.velocity[1] = 0
|
||||||
|
|
||||||
self.velocity[1] = min(5, self.velocity[1] + 0.1)
|
self.velocity[1] = min(5, self.velocity[1] + 0.1)
|
||||||
|
|
||||||
self.animation.update()
|
self.animation.update()
|
||||||
|
|
||||||
def render(self, surface: pygame.Surface, offset: tuple = (0, 0)):
|
def render(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
|
||||||
surface.blit(
|
surface.blit(
|
||||||
pygame.transform.flip(self.animation.img(), self.flip, False),
|
pygame.transform.flip(self.animation.img(), self.flip, False),
|
||||||
(
|
(
|
||||||
|
|
@ -78,10 +89,10 @@ class PhysicsEntity:
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
def rect(self):
|
def rect(self) -> pygame.Rect:
|
||||||
return pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])
|
return pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])
|
||||||
|
|
||||||
def set_action(self, action):
|
def set_action(self, action: str) -> None:
|
||||||
if action != self.action:
|
if action != self.action:
|
||||||
self.action = action
|
self.action = action
|
||||||
image = self.e_type + "/" + self.action
|
image = self.e_type + "/" + self.action
|
||||||
|
|
@ -89,16 +100,16 @@ class PhysicsEntity:
|
||||||
|
|
||||||
|
|
||||||
class Player(PhysicsEntity):
|
class Player(PhysicsEntity):
|
||||||
def __init__(self, game, pos, size):
|
def __init__(self, game, pos: tuple[float, float], size: tuple[int, int]):
|
||||||
super().__init__(game, "player", pos, size)
|
super().__init__(game, "player", pos, size)
|
||||||
self.air_time = 0 # Neu! Zählt, wie lange wir fallen/springe
|
self.air_time: int = 0 # Neu! Zählt, wie lange wir fallen/springe
|
||||||
|
|
||||||
def update(self, tilemap, movement=(0, 0)):
|
def update(self, tilemap: Tilemap, movement: tuple[float, float] = (0, 0)) -> None:
|
||||||
super().update(tilemap, movement)
|
super().update(tilemap, movement)
|
||||||
|
|
||||||
self.air_time += 1
|
self.air_time += 1
|
||||||
|
|
||||||
if self.collisions["bottom"]:
|
if self.collisions.bottom:
|
||||||
self.air_time = 0
|
self.air_time = 0
|
||||||
# Wenn wir länger als 4 Frames in der Luft sind -> Springen!
|
# Wenn wir länger als 4 Frames in der Luft sind -> Springen!
|
||||||
if self.air_time > 4:
|
if self.air_time > 4:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue