-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPSCameraPlayer.py
79 lines (54 loc) · 2.06 KB
/
FPSCameraPlayer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
import pygame as pg
import glm
import math
import random
from engine.actor.Camera import Camera
from pyrr import Vector3, Matrix44
class FPSCameraPlayer(Camera):
def __init__(self) -> None:
super().__init__()
self.mouse_sensivity = 0.3
self.move_speed = 1
self.anim_i = 0
self.anim_pos = Vector3([0, 0, 0])
self.step_sounds = []
self.step_i = 0
for step_snd_idx in range(1, 8):
step_snd_name = f"assets/sounds/Step{step_snd_idx}.wav"
snd_obj = pg.mixer.Sound(step_snd_name)
snd_obj.set_volume(0.1)
self.step_sounds.append(snd_obj)
def get_view_matrix(self):
base_view_matrix = super().get_view_matrix()
anim_matrix = Matrix44.from_translation(self.anim_pos)
return anim_matrix @ base_view_matrix
def move(self, additional_vector: Vector3, dt: float):
keys = pg.key.get_pressed()
sprint = 1
if keys[pg.K_LALT]:
sprint = 3
self.position = (
self.position + additional_vector * self.move_speed * dt * sprint
)
self.anim_i += dt * 10 * sprint
self.step_i += dt * 10 * sprint
self.anim_pos = Vector3([0, -0.1 + math.cos(self.anim_i) * 0.03, 0])
self.rollDeg = math.cos(self.anim_i / 2) * 0.25
if self.step_i > 6:
self.step_i = 0
snd = random.choice(self.step_sounds)
snd.play()
def execute(self, dt: float):
rel_x, rel_y = pg.mouse.get_rel()
self.pitchDeg += rel_y * -self.mouse_sensivity
self.yawDeg += rel_x * self.mouse_sensivity
self.pitchDeg = glm.clamp(self.pitchDeg, -89, 89)
keys = pg.key.get_pressed()
if keys[pg.K_w]:
self.move(self.get_forward_vector(ignore_pitch=True), dt)
if keys[pg.K_s]:
self.move(-self.get_forward_vector(ignore_pitch=True), dt)
if keys[pg.K_a]:
self.move(-self.get_right_vector(), dt)
if keys[pg.K_d]:
self.move(self.get_right_vector(), dt)