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

95 lines
3.7 KiB
Python
Raw Normal View History

2023-03-29 16:42:10 +02:00
from level.elements.dynamic.PushableBoxLevelElement import PushableBoxLevelElement
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
class PlayerSprite(DynamicSprite):
def __init__(self, spritesheet: Spritesheet):
super().__init__(spritesheet)
self.jump_time = -1
self.allowed_jump_time = 12
self.acceleration_horizontal = 2
2023-03-27 18:07:21 +02:00
self.deceleration_horizontal_air = 0.2
self.deceleration_horizontal_ground = 0.4
2023-03-29 09:40:09 +02:00
self.gravity = 9.81 / 11
2023-03-26 17:01:28 +02:00
self.max_motion_horizontal_via_input = 5
2023-03-28 20:08:52 +02:00
self.id = 'player'
# animation properties
self.no_motion_since = 100
self.last_directional_input = KeyManager.KEY_RIGHT
2023-03-29 16:42:10 +02:00
self.is_potentially_stuck = 0
2023-03-26 17:01:28 +02:00
def tick(self, tick_data: TickData):
super().tick(tick_data)
2023-03-29 16:42:10 +02:00
left = self.get_collides_with_direction(CollisionDirection.LEFT)
right = self.get_collides_with_direction(CollisionDirection.RIGHT)
bottom = self.get_collides_with_direction(CollisionDirection.BOTTOM)
if self.is_potentially_stuck == 0 and left and bottom:
if left.secondary_sprite == bottom.secondary_sprite:
if isinstance(left.secondary_sprite, PushableBoxLevelElement):
self.is_potentially_stuck = 1
else:
self.is_potentially_stuck = 0
else:
self.is_potentially_stuck = 0
elif self.is_potentially_stuck == 1 and right and bottom:
if right.secondary_sprite == bottom.secondary_sprite:
if isinstance(right.secondary_sprite, PushableBoxLevelElement):
self.is_potentially_stuck = 2
else:
self.is_potentially_stuck = 0
else:
self.is_potentially_stuck = 0
if self.is_potentially_stuck == 2:
print('stuck')
self.position_scale.position = (self.position_scale.position[0] - 5, self.position_scale.position[1])
2023-03-29 16:48:09 +02:00
self.is_potentially_stuck = 0
2023-03-29 16:42:10 +02:00
2023-03-26 17:01:28 +02:00
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_RIGHT):
self.last_directional_input = KeyManager.KEY_RIGHT
2023-03-26 17:01:28 +02:00
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):
self.last_directional_input = KeyManager.KEY_LEFT
2023-03-26 17:01:28 +02:00
if self.motion[0] > -self.max_motion_horizontal_via_input:
self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1])
2023-03-27 13:08:22 +02:00
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
2023-03-29 09:40:09 +02:00
self.motion = (self.motion[0], self.motion[1] - 7)
2023-03-26 17:01:28 +02:00
if self.jump_time >= 0:
2023-03-29 12:29:00 +02:00
self.motion = (self.motion[0], self.motion[1] - 0.65)
2023-03-27 13:08:22 +02:00
if self.jump_time >= 0:
self.jump_time -= 1
# update animation state
if self.last_directional_input == KeyManager.KEY_RIGHT:
animation_dir = 'r'
else:
animation_dir = 'l'
if abs(self.motion[0]) < 0.1:
self.no_motion_since += tick_data.dt
else:
self.no_motion_since = 0
if self.no_motion_since > 20:
self.set_animation_state('idle_' + animation_dir)
else:
self.set_animation_state('walk_' + animation_dir)