-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventory.py
213 lines (155 loc) · 8.32 KB
/
Inventory.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
import helpers
from helpers import *
""" We're using lists to store items, and we need to construct a wrapper for the inventory
that allows both for access into it as well as encapsulation of the draw functions.
"""
class Inventory():
def __init__ (self, batch, group):
self.item_list = []
self.item_count = []
self.InventoryWidth = WIDTH * 0.7
self.InventoryHeight = HEIGHT * 0.1
self.InventoryXPos = (WIDTH - self.InventoryWidth ) / 2.0
self.InventoryYPos = 40
self.index = 0
self.batch = pyglet.graphics.Batch()
self.group = group
self.item_v = []
self.pos_z = 0
def scrollIndex (self, scrollValue):
self.index = (self.index - scrollValue) % len (self.item_list)
def draw_3D_inventory_block (self, x, y, z, texture):
vertex_data = cube_vertices (x, y, z, 1)
self.batch.invalidate()
# glOrtho(0, width, 0, height, -1, 1)
v = self.batch.add (24, GL_QUADS, self.group, ('v3f/static', vertex_data), ('t2f/static', texture))
return v
def draw (self):
self.batch.draw()
return
#convert this to a separate HUD class later with its own batch
def drawHUD (self, current_health):
glColor3f (1,0,0)
draw_rect (WIDTH - 220, HEIGHT - 40, 200 * current_health / 100., 30)
def drawIndividualItem (self, index, group, player_pos, camera_rot, item_vertices):
""" I think that inventory items should be drawn either as 2D sprites or 3D icons. I'm not
sure yet. However I am sure that I don't like the sprite sheet method of loading
sprites. However we may not have another choice due to speed constraints, I guess?
3D is the way to gooo
"""
x = self.InventoryXPos + 37 + self.InventoryWidth / 9 * index
y = self.InventoryYPos
yaw = camera_rot[0]
pitch = camera_rot[1]
rot_offsetx = index * 16 - 32
rot_offsety = -18
forward_vector = getForwardVector (rot_offsetx, rot_offsety)
transform_vector = Vector (0, 1, 0)
player_vector = Vector (player_pos[0], player_pos[1], player_pos[2])
right_vector = CrossProduct (forward_vector, transform_vector)
up_vector = CrossProduct (right_vector, forward_vector)
offset_forward_vector = getForwardVector (yaw, pitch)
if index == self.index: #maybe change self.index to self.currently_selected_index or something
projection_distance_factor = 8
else:
projection_distance_factor = 10
transformed_x = player_pos[0] + forward_vector.x * projection_distance_factor
transformed_y = player_pos[1] + forward_vector.y * projection_distance_factor
transformed_z = player_pos[2] - forward_vector.z * projection_distance_factor
item_vertices.vertices = cube_vertices (transformed_x, transformed_y, transformed_z, 1)
num_vertices = item_vertices.get_size() * 3
center_pos = getCenterOfVertices (item_vertices, num_vertices)
self.pos_z += 1
for index in range (0, num_vertices, 3):
xvert = index
yvert = index + 1
zvert = index + 2
#rotates cube by offset
rotated_vector = rotatePoint ((item_vertices.vertices[xvert],
item_vertices.vertices[yvert],
item_vertices.vertices[zvert]),
(rot_offsety, -rot_offsetx, 0),
center_pos)
item_vertices.vertices[xvert] = rotated_vector[0]
item_vertices.vertices[yvert] = rotated_vector[1]
item_vertices.vertices[zvert] = rotated_vector[2]
#rotates cube to face player
rotated_vector = rotatePoint ((item_vertices.vertices[xvert],
item_vertices.vertices[yvert],
item_vertices.vertices[zvert]),
(pitch, -yaw, 0),
player_pos)
item_vertices.vertices[xvert] = rotated_vector[0]
item_vertices.vertices[yvert] = rotated_vector[1]
item_vertices.vertices[zvert] = rotated_vector[2]
# self.v.vertices[xvert] = self.v.vertices[xvert] - up_vector.x * 5
# self.v.vertices[yvert] = self.v.vertices[yvert] - up_vector.y * 5
# self.v.vertices[zvert] = self.v.vertices[zvert] + up_vector.z * 5
#Opposite signage because left
# self.v.vertices[xvert] = self.v.vertices[xvert] + right_vector.x * 5
# self.v.vertices[yvert] = self.v.vertices[yvert] + right_vector.y * 5
# self.v.vertices[zvert] = self.v.vertices[zvert] - right_vector.z * 5
positioning_vector = right_vector * 10
# print("Pitch: " + str(pitch))
# print("Yaw:" + str(yaw))
#rotates cube with player as center as a pseudo positioning
#i need to calculate new pitch and yaw from line of sight, pitch and yaw
#yaw and pitch only provide rotational information in 360 degrees. I need to make sure
#that the rotation from x axis is always kept in mind, and then add rotation based on that
xcomp = math.cos (math.radians(pitch)) * math.cos(math.radians(-yaw)) * 20
ycomp = math.sin (math.radians(pitch)) * 20
zcomp = math.sin (math.radians(-yaw)) * math.cos(math.radians(pitch)) * 20
# print("xcomp: " + str(xcomp))
# print("ycomp: " + str(ycomp))
# print("zcomp: " + str(zcomp))
# print("Rotated_x" + str(rotated_vector[0]))
# print("Rotated_y" + str(rotated_vector[1]))
# print("Rotated_z" + str(rotated_vector[2]))
# print("right_vector_x: " + str(right_vector.z))
# self.v.vertices[xvert] = self.v.vertices[xvert] + right_vector.x * 5
# self.v.vertices[yvert] = self.v.vertices[yvert] + right_vector.y * 5
# self.v.vertices[zvert] = self.v.vertices[zvert] - right_vector.z * 5
# item_vertices.draw(pyglet.gl.GL_POINTS)
# """ now we have to move the cube into its respective slot """
# self.v.vertices[xvert] = transform_to_player_view (self.v.vertices[xvert], -30, -30)
# print(a)
# batch.draw()
# draw_rect (self.InventoryXPos + 37+self.InventoryWidth/9*index, self.InventoryYPos, self.InventoryWidth / 10.0, self.InventoryHeight)
def drawItems (self, batch, group, player_pos, camera_rot):
""" maybe I should wrap batch and group as another object"""
self.player_pos = player_pos
self.camera_rot = camera_rot
for index in range (0, len (self.item_list)):
self.drawIndividualItem (index, group, player_pos, camera_rot, self.item_v[index])
return
def getCurrentlyIndexedItem (self):
if (len (self.item_list) <= self.index):
return None
else:
return (self.item_list [self.index])
def addItem (self, item_name):
for i in range (0, len (self.item_list)):
if (self.item_list [i] == item_name):
self.item_count [i] = self.item_count [i] + 1
return
""" if the code doesn't return, we then add the item and its count to their respective lists """
self.item_list.append (item_name)
self.item_count.append (1)
self.item_v.append (self.draw_3D_inventory_block (len (self.item_count), 1, 0, item_name))
def removeItem (self, item_name):
for i in range (0, len (self.item_list)):
if (self.item_list[i] == item_name):
self.item_count[i] = self.item_count[i] - 1
if self.item_count[i] <= 0:
self.item_v.clear()
del self.item_list[i]
del self.item_count[i]
self.batch = None
self.batch = pyglet.graphics.Batch()
for i in range (0, len (self.item_list)):
self.item_v.append (self.draw_3D_inventory_block (len (self.item_list), 1, 0, self.item_list[i]))
return
def setIndex (self, i):
self.index = i
def getSize (self):
return len (self.item_list)