Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

draw_world.py 4.8KB

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