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

35 lines
1.2 KiB
Python

from typing import Optional
from physics.CollisionDirection import CollisionDirection
from sprite.Spritesheet import Spritesheet
from sprite.StaticSprite import StaticSprite
from physics.TickData import TickData
class DynamicSprite(StaticSprite):
def __init__(self, spritesheet: Optional[Spritesheet] = None):
super().__init__(spritesheet)
self.motion = (0, 0)
self.apply_base_deceleration = True
self.deceleration_horizontal_air = 0.02
self.deceleration_horizontal_ground = 0.3
self.gravity = 9.81 / 10
def tick(self, tick_data: TickData):
super().tick(tick_data)
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
)