-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
151 lines (90 loc) · 3.47 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Module for player's structures."""
from __future__ import print_function
import copy
import data
import script
class Body(script.Controllable):
"""Represents body in world."""
def __init__(self, camera, renderer, collision_offset, height):
self.camera = camera
self.camera_fall_collision = False
self.renderer = renderer
self.collision_offset = collision_offset
self.height = height
self.camera_height = self.height * 0.9
# static for now
self.vertical_offsets = [0.2, 1.2]
def fall(self):
position = data.Point(
self.camera.x_pos,
self.camera.y_pos - self.camera_height,
self.camera.z_pos)
position2 = data.Point(
self.camera.x_pos,
self.camera.y_pos - (self.height * 0.8),
self.camera.z_pos)
if self.renderer.ground_collision(position2):
# print("helper")
self.camera.collision_helper()
self.camera.stop_falling()
self.camera_fall_collision = True
elif self.renderer.ground_collision(position):
self.camera.stop_falling()
self.camera_fall_collision = True
else:
self.camera_fall_collision = False
if self.camera.gravity and not self.camera_fall_collision:
self.camera.fall()
else:
self.camera.jump_counter = 0
def is_collide(self, point):
"""Check collision for point and all its vertical offsets.
Args:
point (data.Point): point
Return:
bool: collision
"""
for offset in self.vertical_offsets:
temp = copy.copy(point)
temp.y += offset - self.camera_height
if self.renderer.ground_collision(temp):
return True
return False
def forward(self):
next_x = self.camera.next_fw_x_point(self.collision_offset)
next_z = self.camera.next_fw_z_point(self.collision_offset)
if not self.is_collide(next_x):
self.camera.forward_x()
if not self.is_collide(next_z):
self.camera.forward_z()
def backward(self):
next_x = self.camera.next_bw_x_point(self.collision_offset)
next_z = self.camera.next_bw_z_point(self.collision_offset)
if not self.is_collide(next_x):
self.camera.backward_x()
if not self.is_collide(next_z):
self.camera.backward_z()
def left(self):
next_x = self.camera.next_left_x_point(self.collision_offset)
next_z = self.camera.next_left_z_point(self.collision_offset)
if not self.is_collide(next_x):
self.camera.left_x()
if not self.is_collide(next_z):
self.camera.left_z()
def right(self):
next_x = self.camera.next_right_x_point(self.collision_offset)
next_z = self.camera.next_right_z_point(self.collision_offset)
if not self.is_collide(next_x):
self.camera.right_x()
if not self.is_collide(next_z):
self.camera.right_z()
def jump(self):
# camera_position = self.camera.get_position_inverse_z()
camera_position = self.camera.get_position()
camera_position.y += 0.6
new_position = camera_position
if not self.renderer.ground_collision(new_position):
self.camera.jump()
class PlayerBody(Body):
"""Represents player's body in world."""
pass