-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha.py
More file actions
177 lines (174 loc) · 5.29 KB
/
a.py
File metadata and controls
177 lines (174 loc) · 5.29 KB
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
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
import json
import hashlib
import time
app = Ursina()
textures = ['mc/bedrock', 'mc/planks', 'mc/wood', 'mc/dirt', 'mc/stone', 'mc/grass', 'mc/diamond']
selected_tex = textures[1]
Sky()
size = (0.5, 0.5, 0.5)
#size = (1, 1, 1)
i = 0
mode = 'Block.obj'
# Define a Voxel class.
# By setting the parent to scene and the model to 'cube' it becomes a 3d button.
iventory = Entity(
parent = camera.ui,
model = 'quad',
scale = (.9, .1),
origin = (-.5, .5),
position = (-.4,-.3),
texture = 'white_cube',
texture_scale = (9,1),
color = color.dark_gray
)
class Chunk():
global selected_tex, textures
def __init__(self, x, z):
self.cx = x
self.cz = z
self.block = []
self.voxels = []
self.textures = []
self.loaded = False
dd = "[" + str(x) + "|" + str(z) + "]"
self.name = hashlib.md5(dd.encode()).hexdigest()
for i in range (0,8):
m = []
t = []
for j in range (0,8):
sx = i + x * 8
sy = j + z * 8
position = (sx, 0, sy)
m.append(position)
t.append(textures[0])
self.textures.append(t)
#voxel = Voxel(position=position)
self.block.append(m)
print("Initialized Chunk " + self.name)
def load(self):
selected_tex = textures[0]
for i in range (0,8):
m = i
for j in range (0,8):
selected_tex = self.textures[m][j]
voxel = Voxel(position=self.block[m][j])
self.voxels.append(voxel)
self.loaded=True
print("Loaded Chunk " + self.name)
selected_tex = 1
def unload(self):
indexer = 0
for i in range (0, 8):
for j in range (0, 8):
self.textures[i][j] = self.voxels[indexer].stexture
indexer = indexer + 1
for i in range (0,len(self.voxels)):
destroy(self.voxels[i])
self.voxels = []
print("Unloaded Chunk" + self.name)
self.loaded = False
class Voxel(Button):
global selected_tex
def __init__(self, position=(0,0,0)):
self.stexture = selected_tex
super().__init__(
parent = scene,
position = position,
model = mode,
origin_y = .5,
texture = selected_tex,
scale = size,
color = color.color(0, 0, random.uniform(.9, 1.0)),
#highlight_color = color.limegreen,
)
def input(self, key):
global textures, selected_tex, i, le
if self.hovered:
#print(t)
if key == 'right mouse down':
voxel = Voxel(position=self.position + mouse.normal)
if key == 'left mouse down':
if str(self.texture) == 'bedrock.png':
return
else:
destroy(self)
if key == 'escape':
mouse.visible = True
mouse.position = position
if key == '1':
i = 1
selected_tex = textures[1]
if key == '2':
i = 2
selected_tex = textures[2]
if key == '3':
i = 3
selected_tex = textures[3]
if key == '4':
i = 4
selected_tex = textures[4]
if key == '5':
i = 5
selected_tex = textures[5]
if key == '6':
i = 6
selected_tex = textures[6]
if key == 'scroll up':
if i <= 5:
i += 1
selected_tex = textures[i]
if key == 'scroll down':
if i >= 2:
i -= 1
selected_tex = textures[i]
def input(key):
if key == 'r':
m[0].unload()
if key == 'm':
m[0].load()
m = []
z_chunks = 80
x_chunks = 80
for z in range(-1*int(z_chunks / 2), int((z_chunks / 2)+1)):
for x in range(-1* int(x_chunks / 2), int((x_chunks / 2)+1)):
a = Chunk(z,x)
if (z == 0 and x == 0):
a.load()
m.append(a)
def update():
global m
p = player.position
x, y, z = p
a = str(x/8)
b = str(z/8)
ccx = 0
ccz = 0
if (float(a) < 0):
ccx = int(a.split(".")[0]) - 1
else:
cx = int(a.split(".")[0])
if (float(b) < 0):
ccz = int(b.split(".")[0]) - 1
else:
ccz = int(b.split(".")[0])
#print (str(cx) + str(cz))
ul = []
l = []
for x in range (-2 , 3):
for z in range (-2, 3):
for i in range (0, len(m)):
if (m[i].cx == ccx + x and m[i].cz == ccz + z):
if ((x < 2 and x > -2) and (z<2 and z > -2)):
l.append(m[i])
else:
ul.append(m[i])
for i in range (0, len(l)):
if (l[i].loaded == False):
l[i].load()
for i in range (0, len(ul)):
if (ul[i].loaded == True):
ul[i].unload()
player = FirstPersonController()
app.run()