forked from fogleman/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
237 lines (216 loc) · 8.44 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Imports, sorted alphabetically.
# Python packages
from math import cos, sin, atan2, radians
import random
# Third-party packages
# Nothing for now...
# Modules from this project
from blocks import torch_block
from entity import Entity
import globals as G
from inventory import Inventory
from items import stick_item
from utils import normalize, FACES
damage_block = 0, 100, 0 # records the last block to do damage
__all__ = (
'Player',
)
class Player(Entity):
def __init__(self, position, rotation, flying=False,
game_mode=G.GAME_MODE):
super(Player, self).__init__(position, rotation, health=7,
max_health=10, attack_power=2.0 / 3,
attack_range=4)
self.inventory = Inventory()
self.quick_slots = Inventory(9)
self.armor = Inventory(4)
self.flying = flying
self.game_mode = game_mode
self.strafe = [0, 0]
self.dy = 0
self.current_density = 1 # Current density of the block we're colliding with
self.last_sector = None
self.damage_block = 0, 100, 0
initial_items = [torch_block, stick_item]
for item in initial_items:
quantity = random.randint(2, 10)
if random.choice((True, False)):
self.inventory.add_item(item.id, quantity)
#else:
# self.quick_slots.add_item(item.id, quantity)
def add_item(self, item_id):
if self.quick_slots.add_item(item_id):
return True
elif self.inventory.add_item(item_id):
return True
return False
def change_health(self, change):
self.health += change
if self.health > self.max_health:
self.health = self.max_health
def on_deactivate(self):
self.strafe = [0, 0]
def on_key_release(self, symbol, modifiers):
if symbol == G.MOVE_FORWARD_KEY:
self.strafe[0] += 1
G.BIOME_BLOCK_COUNT +=1
#print ('north' + str(G.BIOME_BLOCK_COUNT))
elif symbol == G.MOVE_BACKWARD_KEY:
self.strafe[0] -= 1
G.BIOME_BLOCK_COUNT -=1
elif symbol == G.MOVE_LEFT_KEY:
self.strafe[1] += 1
G.BIOME_BLOCK_COUNT +=1
elif symbol == G.MOVE_RIGHT_KEY:
self.strafe[1] -= 1
G.BIOME_BLOCK_COUNT +=1
elif (symbol == G.JUMP_KEY
or symbol == G.CROUCH_KEY) and self.flying:
self.dy = 0
def on_key_press(self, symbol, modifiers):
if symbol == G.MOVE_FORWARD_KEY:
self.strafe[0] -= 1
G.BIOME_BLOCK_COUNT +=1
elif symbol == G.MOVE_BACKWARD_KEY:
self.strafe[0] += 1
G.BIOME_BLOCK_COUNT -=1
elif symbol == G.MOVE_LEFT_KEY:
self.strafe[1] -= 1
G.BIOME_BLOCK_COUNT +=1
elif symbol == G.MOVE_RIGHT_KEY:
self.strafe[1] += 1
G.BIOME_BLOCK_COUNT -=1
elif symbol == G.JUMP_KEY:
if self.flying:
self.dy = 0.045 # jump speed
elif self.dy == 0:
self.dy = 0.016 # jump speed
elif symbol == G.CROUCH_KEY:
if self.flying:
self.dy = -0.045 # inversed jump speed
elif symbol == G.FLY_KEY and self.game_mode == G.CREATIVE_MODE:
self.dy = 0
self.flying = not self.flying
def get_motion_vector(self):
if any(self.strafe):
x, y = self.rotation
y_r = radians(y)
x_r = radians(x)
strafe = atan2(*self.strafe)
if self.flying:
m = cos(y_r)
dy = sin(y_r)
if self.strafe[1]:
dy = 0.0
m = 1
if self.strafe[0] > 0:
dy *= -1
x_r += strafe
dx = cos(x_r) * m
dz = sin(x_r) * m
else:
dy = 0.0
x_r += strafe
dx = cos(x_r)
dz = sin(x_r)
else:
dy = 0.0
dx = 0.0
dz = 0.0
return dx, dy, dz
def get_sight_vector(self):
x, y = self.rotation
y_r = radians(y)
x_r = radians(x)
m = cos(y_r)
dy = sin(y_r)
x_r -= G.HALF_PI
dx = cos(x_r) * m
dz = sin(x_r) * m
return dx, dy, dz
def update(self, dt, parent):
# walking
speed = 15 if self.flying else 5*self.current_density
d = dt * speed
dx, dy, dz = self.get_motion_vector()
dx, dy, dz = dx * d, dy * d, dz * d
# gravity
if not self.flying:
self.dy -= dt * 0.022 # g force, should be = jump_speed * 0.5 / max_jump_height
self.dy = max(self.dy, -0.5) # terminal velocity
dy += self.dy
else:
self.dy = max(self.dy, -0.5) # terminal velocity
dy += self.dy
# collisions
x, y, z = self.position
x, y, z = self.collide(parent, (x + dx, y + dy, z + dz), 2)
self.position = (x, y, z)
def collide(self, parent, position, height):
pad = 0.25
p = list(position)
np = normalize(position)
self.current_density = 1 # Reset it, incase we don't hit any water
for face in FACES: # check all surrounding blocks
for i in xrange(3): # check each dimension independently
if not face[i]:
continue
d = (p[i] - np[i]) * face[i]
if d < pad:
continue
for dy in xrange(height): # check each height
op = list(np)
op[1] -= dy
op[i] += face[i]
op = tuple(op)
# If there is no block or if the block is not a cube,
# we can walk there.
if op not in parent.world \
or parent.world[op].vertex_mode != G.VERTEX_CUBE:
continue
# if density <= 1 then we can walk through it. (water)
if parent.world[op].density < 1:
self.current_density = parent.world[op].density
continue
if parent.world[op].player_damage > 0:
if self.damage_block != np:
# this keeps a player from taking the same damage over and over...
self.change_health( - parent.world[op].player_damage)
parent.item_list.update_health()
self.damage_block = np
continue
else:
#do nothing
continue
# if height <= 1 then we can walk on top of it. (carpet, half-blocks, etc...)
if parent.world[op].height < 0.5:
#current_height = parent.world[op].height
#x, y, z = self.position
#new_pos = (x, y + current_height, z)
#self.position = new_pos
continue
p[i] -= (d - pad) * face[i]
if face == (0, -1, 0) or face == (0, 1, 0):
# jump damage
if self.game_mode == G.SURVIVAL_MODE:
damage = self.dy * -1000.0
damage = 3.0 * damage / 22.0
damage -= 2.0
if damage >= 0.0:
health_change = 0
if damage <= 0.839:
health_change = 0
elif damage <= 1.146:
health_change = -1
elif damage <= 1.44:
health_change = -2
elif damage <= 2.26:
health_change = -2
else:
health_change = -3
if health_change != 0:
self.change_health(health_change)
parent.item_list.update_health()
self.dy = 0
break
return tuple(p)