player animation will now update based on player input
parent
00c051de02
commit
2280e81629
|
@ -19,17 +19,22 @@ class PlayerSprite(DynamicSprite):
|
|||
self.gravity = 9.81 / 10
|
||||
|
||||
self.max_motion_horizontal_via_input = 5
|
||||
|
||||
self.id = 'player'
|
||||
|
||||
# animation properties
|
||||
self.no_motion_since = 100
|
||||
self.last_directional_input = KeyManager.KEY_RIGHT
|
||||
|
||||
def tick(self, tick_data: TickData):
|
||||
super().tick(tick_data)
|
||||
|
||||
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_RIGHT):
|
||||
self.last_directional_input = 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):
|
||||
self.last_directional_input = KeyManager.KEY_LEFT
|
||||
if self.motion[0] > -self.max_motion_horizontal_via_input:
|
||||
self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1])
|
||||
|
||||
|
@ -41,3 +46,20 @@ class PlayerSprite(DynamicSprite):
|
|||
self.motion = (self.motion[0], self.motion[1] - 0.55)
|
||||
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)
|
||||
|
|
|
@ -48,6 +48,9 @@ class Sprite(UiElement):
|
|||
return None
|
||||
|
||||
def tick(self, tick_data: TickData):
|
||||
self.update_image(tick_data)
|
||||
|
||||
def update_image(self, tick_data: TickData):
|
||||
if self.spritesheet is None:
|
||||
self.image = None
|
||||
return
|
||||
|
@ -64,23 +67,22 @@ class Sprite(UiElement):
|
|||
self.last_image = self.image
|
||||
self.image = animation['images'][self.animation_frame % len(animation['images'])]
|
||||
|
||||
empty_tick_data = TickData(0, [], KeyManager(), [], PositionScale())
|
||||
|
||||
def set_animation_state(self, state: str):
|
||||
if self.spritesheet is None:
|
||||
if self.spritesheet is None or self.animation_state == state:
|
||||
return
|
||||
|
||||
if state in self.spritesheet.animations:
|
||||
self.animation_state = state
|
||||
self.animation_delay = 0
|
||||
self.tick(self.empty_tick_data())
|
||||
self.update_image(self.empty_tick_data)
|
||||
else:
|
||||
print('Unknown animation state: ' + state)
|
||||
|
||||
def set_animation_frame(self, frame: int):
|
||||
self.animation_frame = frame
|
||||
self.tick(self.empty_tick_data())
|
||||
|
||||
def empty_tick_data(self) -> TickData:
|
||||
return TickData(0, [], KeyManager(), [], PositionScale())
|
||||
self.update_image(self.empty_tick_data)
|
||||
|
||||
def render_sprite_image(self) -> pygame.Surface:
|
||||
return self.image
|
||||
|
|
Loading…
Reference in New Issue