sep-pm-platformer/project/physics/sprites/PlayerSprite.py

50 lines
2.0 KiB
Python
Raw Normal View History

2023-03-26 17:01:28 +02:00
from physics.CollisionDirection import CollisionDirection
from physics.TickData import TickData
from sprite.DynamicSprite import DynamicSprite
from sprite.Spritesheet import Spritesheet
from ui_elements.KeyManager import KeyManager
from ui_elements.TextLabel import TextLabel
class PlayerSprite(DynamicSprite):
def __init__(self, spritesheet: Spritesheet):
super().__init__(spritesheet)
2023-03-27 12:51:44 +02:00
self.cheat_fly = False
2023-03-26 17:01:28 +02:00
self.jump_time = -1
self.allowed_jump_time = 12
self.acceleration_horizontal = 2
self.deceleration_horizontal_air = 0.02
self.deceleration_horizontal_ground = 0.3
self.gravity = 9.81 / 10
self.max_motion_horizontal_via_input = 5
def tick(self, tick_data: TickData):
super().tick(tick_data)
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_RIGHT):
if self.motion[0] < self.max_motion_horizontal_via_input:
self.motion = (self.motion[0] + self.acceleration_horizontal, self.motion[1])
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_LEFT):
if self.motion[0] > -self.max_motion_horizontal_via_input:
self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1])
2023-03-27 12:51:44 +02:00
if not self.cheat_fly:
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP):
if self.jump_time < 0 and self.get_collides_with_direction(CollisionDirection.BOTTOM):
self.jump_time = self.allowed_jump_time
self.motion = (self.motion[0], self.motion[1] - 7)
if self.jump_time >= 0:
self.motion = (self.motion[0], self.motion[1] - 0.5)
2023-03-26 17:01:28 +02:00
if self.jump_time >= 0:
2023-03-27 12:51:44 +02:00
self.jump_time -= 1
else:
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP):
self.motion = (self.motion[0], self.motion[1] - 2)
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_DOWN):
self.motion = (self.motion[0], self.motion[1] + 2)