-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventory.py
87 lines (70 loc) · 2.75 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
from ursina import *
class Inventory(Entity):
def __init__(self, **kwargs):
super().__init__(
parent=camera.ui,
model=Quad(radius=.015),
texture='white_cube',
texture_scale=(5, 8),
scale=(.5, .8),
origin=(-.5, .5),
position=(-.3, .4),
color=color.color(0, 0, .1, .9),
)
for key, value in kwargs.items():
setattr(self, key, value)
def find_free_spot(self):
for y in range(8):
for x in range(5):
grid_positions = [(int(e.x * self.texture_scale[0]), int(e.y * self.texture_scale[1])) for e in
self.children]
print(grid_positions)
if not (x, -y) in grid_positions:
print('found free spot:', x, y)
return x, y
def append(self, item, x=0, y=0):
print('add item:', item)
if len(self.children) >= 5 * 8:
print('inventory full')
error_message = Text('<red>Inventory is full!', origin=(0, -1.5), x=-.5, scale=2)
destroy(error_message, delay=1)
return
x, y = self.find_free_spot()
icon = Draggable(
parent=self,
model='quad',
texture=item,
color=color.white,
scale_x=1 / self.texture_scale[0],
scale_y=1 / self.texture_scale[1],
origin=(-.5, .5),
x=x * 1 / self.texture_scale[0],
y=-y * 1 / self.texture_scale[1],
z=-.5,
)
name = item.replace('_', ' ').title()
if random.random() < .25:
icon.color = color.gold
name = '<orange>Rare ' + name
icon.tooltip = Tooltip(name)
icon.tooltip.background.color = color.color(0, 0, 0, .8)
def drag():
icon.org_pos = (icon.x, icon.y)
icon.z -= .01 # ensure the dragged item overlaps the rest
def drop():
icon.x = int((icon.x + (icon.scale_x / 2)) * 5) / 5
icon.y = int((icon.y - (icon.scale_y / 2)) * 8) / 8
icon.z += .01
# if outside, return to original position
if icon.x < 0 or icon.x >= 1 or icon.y > 0 or icon.y <= -1:
icon.position = (icon.org_pos)
return
# if the spot is taken, swap positions
for c in self.children:
if c == icon:
continue
if c.x == icon.x and c.y == icon.y:
print('swap positions')
c.position = icon.org_pos
icon.drag = drag
icon.drop = drop