Browse Source

world drawing

shuvit 2 years ago
parent
commit
5b07e7279f
1 changed files with 209 additions and 0 deletions
  1. 209
    0
      scripts/draw_world.py

+ 209
- 0
scripts/draw_world.py View File

@@ -0,0 +1,209 @@
1
+import bge
2
+import ast
3
+import glob
4
+import random
5
+import os
6
+
7
+def read_files(own):
8
+
9
+	file = bge.logic.expandPath('//assets/bsp/bsp_00-00.gob')
10
+
11
+
12
+
13
+	directory = bge.logic.expandPath("//assets/bsp/")
14
+	file_name = directory + "*.gob" # Test directory "Test\\Music\\*.mp3"
15
+	file_list = glob.glob(file_name)
16
+	selected = random.choice(file_list)
17
+	full_path = os.path.join(directory, selected)
18
+	print('glob got', file_list)
19
+
20
+
21
+
22
+	for f in file_list:
23
+		load = open(file, 'r')
24
+		lines = load.readlines()
25
+		load.close()
26
+		blist = []
27
+		for x in lines:
28
+			x = str(x)
29
+			x = x.replace(","," ")
30
+			ostr = str(x.split()[0])
31
+			pX = float(x.split()[1])
32
+			pY = float(x.split()[2])
33
+			pZ = float(x.split()[3])
34
+			pR = float(x.split()[4])
35
+			pS = float(x.split()[5])		
36
+			#x = x.replace("'", "")
37
+			#x = ast.literal_eval(x)
38
+			#print(x, pX, pY, pZ)
39
+			blist.append([ostr, pX, pY, pZ, pR, pS])
40
+
41
+		bn = os.path.basename(f)
42
+		bn = bn[:-4]
43
+		print(bn, 'basename')
44
+		own[bn] = blist
45
+		bna = bn + '_active'		
46
+		own[bna] = True
47
+		bno = bn + '_obj'
48
+		own[bno] = []
49
+
50
+
51
+
52
+# 	load = open(file, 'r')
53
+# 	lines = load.readlines()
54
+# 	load.close()
55
+# 	#line2 = lines[0]
56
+# 	#line = line2.split()
57
+# 	#lines = ast.literal_eval(lines)
58
+# 	#l2 = lines.split()
59
+# 	blist = []
60
+# 	for x in lines:
61
+# 		x = str(x)
62
+# 		x = x.replace(","," ")
63
+# 		ostr = str(x.split()[0])
64
+# 		pX = float(x.split()[1])
65
+# 		pY = float(x.split()[2])
66
+# 		pZ = float(x.split()[3])
67
+# 		pR = float(x.split()[4])
68
+# 		pS = float(x.split()[5])		
69
+# 		#x = x.replace("'", "")
70
+# 		#x = ast.literal_eval(x)
71
+# 		#print(x, pX, pY, pZ)
72
+# 		blist.append([ostr, pX, pY, pZ, pR, pS])
73
+
74
+# 	own['bsp_00-00'] = blist
75
+# 	own['bsp_00-00_active'] = True
76
+# 	own['bsp_00-00_obj'] = []
77
+# #-----------------------
78
+# 	file = bge.logic.expandPath('//assets/bsp/bsp_00-01.gob')
79
+
80
+# 	load = open(file, 'r')
81
+# 	lines = load.readlines()
82
+# 	load.close()
83
+# 	#line2 = lines[0]
84
+# 	#line = line2.split()
85
+# 	#lines = ast.literal_eval(lines)
86
+# 	#l2 = lines.split()
87
+# 	blist = []
88
+# 	for x in lines:
89
+# 		x = str(x)
90
+# 		x = x.replace(","," ")
91
+# 		ostr = str(x.split()[0])
92
+# 		pX = float(x.split()[1])
93
+# 		pY = float(x.split()[2])
94
+# 		pZ = float(x.split()[3])
95
+# 		pR = float(x.split()[4])
96
+# 		pS = float(x.split()[5])
97
+# 		#x = x.replace("'", "")
98
+# 		#x = ast.literal_eval(x)
99
+# 		#print(x, pX, pY, pZ)
100
+# 		blist.append([ostr, pX, pY, pZ, pR, pS])
101
+
102
+# 	own['bsp_00-01'] = blist	
103
+# 	own['bsp_00-01_active'] = False	
104
+# 	own['bsp_00-01_obj'] = []
105
+
106
+def draw(own, tile):
107
+	scene = bge.logic.getCurrentScene()
108
+	de = scene.objects['dot_empty']
109
+	de.applyRotation((0, 0, 0), False) 
110
+	de.scaling = [1, 1, 1]
111
+	yMult = tile[4:6]
112
+	xMult = tile[7:9]
113
+
114
+	print(yMult, xMult, 'multers')
115
+
116
+	xOffset = 128 * int(xMult)
117
+	yOffset = 128 * int(yMult)
118
+
119
+	# if 'bsp_00-03' in tile:
120
+	# 	xOffset = 128
121
+	# 	yOffset = 0
122
+	# 	de.applyRotation((0, 0, 0), False) 
123
+	for x in own[tile]:
124
+		objer = tile + '_obj'
125
+		obna = scene.objectsInactive[x[0]]
126
+		#if x[1] != 0 and x[2] != 0:
127
+		de.scaling = [x[5], x[5], x[5]]
128
+		de.applyRotation((0, 0, x[4]), False) 
129
+		de.worldPosition.x = x[1] + xOffset
130
+		de.worldPosition.y = x[2] + yOffset
131
+		de.worldPosition.z = x[3]
132
+		#de.applyRotation((0, 0, x[4]), False) 
133
+		
134
+		#de.rotation = [x[4], x[4], x[4]]
135
+		obj = scene.addObject(obna, de, 0)
136
+		#obj.applyRotation((x[4], x[4], x[4]), True) 
137
+		#obj.scaling = [x[5], x[5], x[5]]
138
+		de.applyRotation((0, 0, -x[4]), False) 
139
+		own[objer].append(obj)
140
+
141
+def remove_tile(own, tile):
142
+	nm = tile + '_obj'
143
+	for x in own[nm]:
144
+		x.endObject()
145
+	own[nm] = []	
146
+
147
+def update_position(own):
148
+	pX = own.worldPosition.x
149
+	pY = own.worldPosition.y
150
+	gsize = 128
151
+	size = gsize / 2
152
+	mX = int(pX / size)
153
+	mY = int(pY / size)
154
+	if mX == 1:
155
+		if 'bsp_00-01' in own['active_tiles']:
156
+			pass
157
+		else:
158
+			draw(own, 'bsp_00-01')
159
+			own['bsp_00-01_active'] = True
160
+			own['active_tiles'].append('bsp_00-01')
161
+			print('drawing')
162
+	if mX == 0:
163
+		if 'bsp_00-01' in own['active_tiles']:
164
+			own['bsp_00-01_active'] = False
165
+			own['active_tiles'].remove('bsp_00-01')
166
+			remove_tile(own, 'bsp_00-01')
167
+			print('removing')		
168
+	#print('uPos', mX, mY)
169
+
170
+	sX = round(own .worldPosition.x / gsize ,1)
171
+	sY = round(own.worldPosition.y / gsize, 1)
172
+	own['sX'] = sX
173
+	own['sY'] = sY
174
+	print('sx, sy', sX, sY)
175
+	ssx = str(int(sX))
176
+	if len(ssx) < 2:
177
+		ssx = '0' + ssx
178
+	ssy = str(int(sY))
179
+	if len(ssy) < 2:
180
+		ssy = '0' + ssy	
181
+	ct = 'bsp_' + ssx + '-' + ssy
182
+	#print(ct) 
183
+	own['current_tile'] = ct
184
+
185
+	ssxp1 = str(int(sX) + 1)	
186
+	if len(ssxp1) < 2: ssxp1 = '0' + ssxp1
187
+	ssxm1 = str(int(sX) - 1)
188
+	if len(ssxm1) < 2: ssxm1 = '0' + ssxm1
189
+
190
+
191
+	ssyp1 = str(int(sY) + 1)
192
+	if len(ssyp1) < 2: ssyp1 = '0' + ssyp1
193
+	ssym1 = str(int(sY) - 1)
194
+	if len(ssym1) < 2: ssym1 = '0' + symp1
195
+	nname = 'bsp_' + ssy +'-' + ssxp1
196
+	print(nname)
197
+
198
+
199
+def main(cont):
200
+	own = cont.owner
201
+	if 'draw_inited' not in own:
202
+		own['draw_inited'] = True
203
+		own['active_tiles'] = []
204
+		read_files(own)
205
+
206
+		draw(own, 'bsp_00-00')
207
+		own['active_tiles'].append('bsp_00-00')
208
+	update_position(own)	
209
+	

Loading…
Cancel
Save