-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
66 lines (49 loc) · 2.63 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import glob
import math
import pygame
from gametools import ImageLoader
import entity
class Player(entity.Entity):
def __init__(self, position: pygame.Vector2, world):
player_img = ImageLoader.ImageLoader.GetImage("assets/imgs/BikePlayer.png", alpha=True)
entity.Entity.__init__(self, position, player_img, world)
self.on_bike = True
def load_animations(self) -> None:
self.animations = {}
self.current_animation = None
path_start = "assets/imgs/Player/"
for fname in glob.iglob(path_start + "*.png"):
ImageLoader.ImageLoader.GetImage(fname, alpha=True) # Cache all the images
self.animations['falling'] = ImageLoader.ImageLoader.return_image_set("player_fall", None, True)
self.animations['biking'] = ImageLoader.ImageLoader.return_image_set("player_biking", None, True)
self.animations['walking'] = ImageLoader.ImageLoader.return_image_set("player_walking", None, True)
self.current_animation = self.animations['biking']
self.time_between_animation_frames = 100
def update(self, acceleration: pygame.Vector2, rotation: float, delta: float):
self.velocity += acceleration * delta
if self.on_bike:
self.movement_on_bike(acceleration, rotation, delta)
else:
self.movement_on_foot(acceleration, rotation, delta)
move_velocity = pygame.Vector2()
orientation_rad = (self.orientation * math.pi) / 180.0
move_velocity.x = math.cos(orientation_rad) * self.velocity.y
move_velocity.y = -math.sin(orientation_rad) * self.velocity.y
self.position += self.world.lock_to_mask(self, move_velocity * delta)
self.rect.center = self.position
self.update_animation(delta)
self.update_children()
self.update_rect()
def movement_on_bike(self, acceleration: pygame.Vector2, rotation: float, delta: float) -> None:
# On bike you have a higher max speed but have a slower turn speed
if acceleration.y == 0.0:
self.velocity *= 0.5 ** delta
# If the player is on a bike they will move only in the direction they are facing, no strafing.
self.velocity.y = max(-50, min(self.velocity.y, 200))
self.orientation += rotation * delta
def movement_on_foot(self, acceleration: pygame.Vector2, rotation: float, delta: float) -> None:
# On foot you turn faster but have a lower max speed
if acceleration.y == 0:
self.velocity *= 0.05 ** delta
self.velocity.y = max(-10, min(self.velocity.y, 50))
self.orientation += rotation * 4 * delta