File size: 1,952 Bytes
f92c150 |
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 |
import math
import entity
import matrix
WALKING_SPEED = 4.317
SPRINTING_SPEED = 7 # faster than in Minecraft, feels better
class Player(entity.Entity):
def __init__(self, world, shader, width, height):
super().__init__(world)
self.view_width = width
self.view_height = height
# create matrices
self.mv_matrix = matrix.Matrix()
self.p_matrix = matrix.Matrix()
# shaders
self.shader = shader
self.shader_matrix_location = self.shader.find_uniform(b"matrix")
# camera variables
self.eyelevel = self.height - 0.2
self.input = [0, 0, 0]
self.target_speed = WALKING_SPEED
self.speed = self.target_speed
def update(self, delta_time):
# process input
if delta_time * 20 > 1:
self.speed = self.target_speed
else:
self.speed += (self.target_speed - self.speed) * delta_time * 20
multiplier = self.speed * (1, 2)[self.flying]
if self.flying and self.input[1]:
self.accel[1] = self.input[1] * multiplier
if self.input[0] or self.input[2]:
angle = self.rotation[0] - math.atan2(self.input[2], self.input[0]) + math.tau / 4
self.accel[0] = math.cos(angle) * multiplier
self.accel[2] = math.sin(angle) * multiplier
if not self.flying and self.input[1] > 0:
self.jump()
# process physics & collisions &c
super().update(delta_time)
def update_matrices(self):
# create projection matrix
self.p_matrix.load_identity()
self.p_matrix.perspective(
90 + 10 * (self.speed - WALKING_SPEED) / (SPRINTING_SPEED - WALKING_SPEED),
float(self.view_width) / self.view_height, 0.1, 500)
# create modelview matrix
self.mv_matrix.load_identity()
self.mv_matrix.rotate_2d(self.rotation[0] + math.tau / 4, self.rotation[1])
self.mv_matrix.translate(-self.position[0], -self.position[1] - self.eyelevel, -self.position[2])
# modelviewprojection matrix
mvp_matrix = self.p_matrix * self.mv_matrix
self.shader.uniform_matrix(self.shader_matrix_location, mvp_matrix) |