sep-pm-platformer/project/sprite/DynamicSprite.py

38 lines
1.3 KiB
Python
Raw Permalink Normal View History

2023-03-27 11:46:03 +02:00
from typing import Optional
2023-03-26 17:01:28 +02:00
from physics.CollisionDirection import CollisionDirection
2023-03-25 15:41:32 +01:00
from sprite.Spritesheet import Spritesheet
from sprite.StaticSprite import StaticSprite
from physics.TickData import TickData
2023-03-25 15:41:32 +01:00
class DynamicSprite(StaticSprite):
2023-03-27 11:46:03 +02:00
def __init__(self, spritesheet: Optional[Spritesheet] = None):
2023-03-25 15:41:32 +01:00
super().__init__(spritesheet)
self.motion = (0, 0)
self.last_effective_motion = (1, 1)
2023-03-25 18:14:47 +01:00
2023-03-26 17:01:28 +02:00
self.apply_base_deceleration = True
2023-03-25 18:14:47 +01:00
self.deceleration_horizontal_air = 0.02
self.deceleration_horizontal_ground = 0.3
2023-03-25 17:18:43 +01:00
self.gravity = 9.81 / 10
2023-03-25 15:41:32 +01:00
2023-03-27 18:52:49 +02:00
self.bounce_factor = 0
def tick(self, tick_data: TickData):
super().tick(tick_data)
2023-03-26 17:01:28 +02:00
if self.apply_base_deceleration:
deceleration_horizontal = 0
if abs(self.motion[0]) > 0:
if self.get_collides_with_direction(CollisionDirection.BOTTOM):
deceleration_horizontal = self.deceleration_horizontal_ground
else:
deceleration_horizontal = self.deceleration_horizontal_air
self.motion = (
self.motion[0] - deceleration_horizontal * self.motion[0] * tick_data.dt,
self.motion[1] + self.gravity * tick_data.dt
)