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.

player.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import bge
  2. import FSM
  3. from mathutils import Vector
  4. import math
  5. import mathutils
  6. dict = bge.logic.globalDict
  7. class Player:
  8. def __init__(self, cont):
  9. self.cont = cont
  10. self.obj = cont.owner
  11. self.FSM = FSM.PlayerFSM(self)
  12. self.walking = True
  13. self.last_pos = self.obj.worldPosition.copy()
  14. self.throw_timer = 0
  15. self.throw_deck = None
  16. self.throw_deck_empty = self.obj.childrenRecursive["throw_deck_empty"]
  17. self.step = True
  18. self.dropin_obj = None
  19. #self.throw_deck_obj = dict['level_scene'].objects['throw_deck']
  20. dict['walking'] = True
  21. #print(self.obj.childrenRecursive)
  22. #self.arm = self.obj.childrenRecursive['Char4']
  23. self.arm = None
  24. def update(self):
  25. self.FSM.Execute()
  26. self.last_pos = self.obj.worldPosition.copy()
  27. def get_ground_ray(self):
  28. v = self.obj.worldOrientation.col[0]
  29. tv = v.normalized()
  30. axis = 2
  31. distance = -50
  32. start = self.obj.worldPosition.copy()
  33. back_start = start + 0.15 * tv
  34. start.z += .1
  35. end = Vector.copy(self.obj.worldPosition + (self.obj.worldOrientation.col[axis] * distance))
  36. ray = self.obj.rayCast(end, back_start, 0, 'ground', 1, 0, 0, 53247)
  37. v = self.obj.linearVelocity.copy()
  38. v = self.obj.worldOrientation.col[0]
  39. tv = v.normalized()
  40. front_start = start - 0.30 * tv
  41. start.z += .1
  42. front_end = Vector(front_start + (self.obj.worldOrientation.col[axis] * distance))
  43. frontray = self.obj.rayCast(front_end, front_start, 0, 'ground', 1, 0, 0, 53247)
  44. #if ray[0] == None and frontray[0] == None:
  45. cray = self.obj.rayCast(end, start, 0, 'ground', 1, 0, 0, 53247)
  46. #bge.render.drawLine(front_start, front_end, [1,0,0])
  47. #bge.render.drawLine(back_start, end, [0,1,0])
  48. #bge.render.drawLine(start, end, [0,0,1])
  49. return [ray, frontray, cray]
  50. def get_dropin_rays(self):
  51. v = self.obj.worldOrientation.col[0]
  52. tv = v.normalized()
  53. axis = 2
  54. distance = -50
  55. start = self.obj.worldPosition.copy()
  56. back_start = start + 0.1 * tv
  57. start.z += .1
  58. end = Vector.copy(self.obj.worldPosition + (self.obj.worldOrientation.col[axis] * distance))
  59. ray = self.obj.rayCast(end, back_start, 0, 'ground', 1, 0, 0, 53247)
  60. v = self.obj.linearVelocity.copy()
  61. v = self.obj.worldOrientation.col[0]
  62. tv = v.normalized()
  63. front_start = start - 0.1 * tv
  64. start.z += .1
  65. front_end = Vector(front_start + (self.obj.worldOrientation.col[axis] * distance))
  66. frontray = self.obj.rayCast(front_end, front_start, 0, 'ground', 1, 0, 0, 53247)
  67. cray = self.obj.rayCast(end, start, 0, 'ground', 1, 0, 0, 53247)
  68. return [ray, frontray, cray]
  69. def get_hang_ray(self):
  70. v = self.obj.worldOrientation.col[0]
  71. tv = v.normalized()
  72. axis = 2
  73. distance = -5
  74. start = self.obj.worldPosition.copy()
  75. start.z += 1.8
  76. mstart = start - 0.40 * tv
  77. end = Vector(mstart + (self.obj.worldOrientation.col[axis] * distance))
  78. ray = self.obj.rayCast(end, mstart, 0, 'ground', 1, 1, 0, 53247)
  79. #ray = self.obj.rayCast(end, mstart, 0, '', 1, 1, 0, 53247)
  80. #bge.render.drawLine(mstart, end, [0,0,1])
  81. return ray
  82. def get_hang_align_ray(self):
  83. v = self.obj.worldOrientation.col[0]
  84. tv = v.normalized()
  85. axis = 0
  86. distance = -2
  87. start = self.obj.worldPosition.copy()
  88. start.z += 1
  89. #mstart = start - 0.40 * tv
  90. #mstart = start * tv
  91. mstart = start
  92. end = Vector(mstart + (self.obj.worldOrientation.col[axis] * distance))
  93. #ray = self.obj.rayCast(end, mstart, 0, 'ground', 1, 0, 0, 53247)
  94. ray = self.obj.rayCast(end, mstart, 0, '', 1, 0, 0, 53247)
  95. #if ray[0] != None:
  96. #print(ray[0], 'player funct')
  97. #bge.render.drawLine(mstart, end, [0,1,0])
  98. return ray
  99. def get_ground_dist(self, groundray):
  100. gr = groundray[0]
  101. fgr = groundray[1]
  102. cgr = groundray[2]
  103. d = None
  104. if gr[0] != None:
  105. p_z = self.obj.worldPosition.z
  106. g_z = gr[1].z
  107. distance_to_ground = p_z - g_z
  108. p2 = None
  109. if fgr[0] != None:
  110. p2 = p_z - fgr[1].z
  111. if p2 < distance_to_ground:
  112. distance_to_ground = p2
  113. if cgr[0] != None:
  114. p2 = p_z - cgr[1].z
  115. if p2 < distance_to_ground:
  116. distance_to_ground = p2
  117. return distance_to_ground
  118. def get_hang_dist(self, groundray):
  119. if groundray[0] != None:
  120. p_z = self.obj.worldPosition.z
  121. g_z = groundray[1].z
  122. distance_to_ground = p_z - g_z
  123. return distance_to_ground
  124. def set_walk_z(self, dist):
  125. if dist < .6:
  126. target_dist = .3
  127. self.obj.worldPosition.z += (target_dist - dist)
  128. self.obj.linearVelocity.z = 0
  129. def align_walk_z(self):
  130. self.obj.alignAxisToVect([0,0,1], 2, .08)
  131. def walk_movement(self):
  132. moving = False
  133. o = self.obj
  134. o.linearVelocity.y *= .9
  135. o.linearVelocity.x *= .95
  136. if dict['kb_w'] == 2 or dict['lUD'] < -.05:
  137. #walk_blend(self)
  138. #run
  139. if (dict['kb_lsh'] == 2 or dict['aBut'] == 1) and o.linearVelocity.x > -dict['max_run_vel']:
  140. #self.run_speed = 1.5
  141. #o.applyForce([-dict['run_force'], 0, 0], True)
  142. o.linearVelocity.x -= .2
  143. #walk
  144. elif o.linearVelocity.x > -dict['max_walk_vel']:
  145. #o.applyForce([-dict['walk_force'], 0, 0], True)
  146. o.linearVelocity.x -= .1
  147. if dict['kb_s'] == 2 or dict['lUD'] > .06:
  148. #o.applyForce([10, 0, 0], True)
  149. if o.linearVelocity.x < (dict['max_walk_vel'] / 2):
  150. o.linearVelocity.x += .05
  151. #print('w')
  152. if dict['kb_a'] == 2 or dict['lLR'] < -.03:
  153. o.applyRotation([0, 0, dict['walk_turn_amt']], True)
  154. #print('w')
  155. if dict['kb_d'] == 2 or dict['lLR'] > .03:
  156. o.applyRotation([0, 0, -dict['walk_turn_amt']], True)
  157. #print('linvel', o.linearVelocity)
  158. return moving
  159. def move_walk_cam(self):
  160. amt = -.05
  161. amt2 = -.07
  162. if dict['rLR'] > .04:
  163. dict['camera'].applyMovement([amt, 0, 0], True)
  164. elif dict['rLR'] < -.04:
  165. dict['camera'].applyMovement([-amt, 0, 0], True)
  166. if dict['rUD'] > .04:
  167. dict['camera'].applyMovement([0, -amt2, 0], True)
  168. elif dict['rUD'] < -.04:
  169. dict['camera'].applyMovement([0, amt2, 0], True)
  170. def check_reset_point(self):
  171. if dict['ddPad'] == 1:
  172. spawn_pos = self.obj['spawn_pos']
  173. spawn_rot = self.obj['spawn_rot']
  174. spawn_cam_pos = self.obj['spawn_cam_pos']
  175. spawn_cam_rot = self.obj['spawn_cam_rot']
  176. try:
  177. self.obj.worldPosition = (spawn_pos[0], spawn_pos[1], (spawn_pos[2] + .1))
  178. self.obj.worldOrientation = [[spawn_rot[0][0], spawn_rot[0][1],spawn_rot[0][2]], [spawn_rot[1][0],spawn_rot[1][1], spawn_rot[1][2]], [0.0, 0.0, 1.0]]
  179. dict['camera'].worldPosition = (spawn_cam_pos[0], spawn_cam_pos[1], (spawn_cam_pos[2]))
  180. dict['camera'].worldOrientation = [[spawn_cam_rot[0][0], spawn_cam_rot[0][1], spawn_cam_rot[0][2]], [spawn_cam_rot[1][0], spawn_cam_rot[1][1], spawn_cam_rot[1][2]], [0.0, 0.0, 1.0]]
  181. except:
  182. self.obj.worldPosition = (0, 0, .1)
  183. self.obj.worldOrientation = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
  184. if self.obj["spawn_stance"] == 1:
  185. self.obj.setLinearVelocity([.1,0,0], 1)
  186. else:
  187. self.obj.setLinearVelocity([-.1,0,0], 1)
  188. if dict['udPad'] == 1:
  189. self.obj['spawn_pos'] = [self.obj.worldPosition[0], self.obj.worldPosition[1], self.obj.worldPosition[2]]
  190. self.obj['spawn_rot'] = [[self.obj.worldOrientation[0][0], self.obj.worldOrientation[0][1],self.obj.worldOrientation[0][2]], [self.obj.worldOrientation[1][0], self.obj.worldOrientation[1][1], self.obj.worldOrientation[1][2]], self.obj.worldOrientation[2][2]]
  191. self.obj['spawn_cam_pos'] = [dict['camera'].worldPosition[0], dict['camera'].worldPosition[1], dict['camera'].worldPosition[2]]
  192. self.obj['spawn_cam_rot'] = [[dict['camera'].worldOrientation[0][0], dict['camera'].worldOrientation[0][1],dict['camera'].worldOrientation[0][2]], [dict['camera'].worldOrientation[1][0], dict['camera'].worldOrientation[1][1], dict['camera'].worldOrientation[1][2]], dict['camera'].worldOrientation[2][2]]
  193. stance = self.obj["stance"]
  194. self.obj["spawn_stance"] = stance
  195. def hang_move(self):
  196. if dict['lLR'] > .04:
  197. #self.arm.playAction('c_hanghopleft', 0, 45, layer=3, play_mode=0, speed=.75, blendin=10)
  198. self.arm.playAction('c_shimmy_l', 0, 34, layer=3, play_mode=0, speed=.75, blendin=10)
  199. self.obj.applyForce([0,235,0], True)
  200. if dict['lLR'] < -.04:
  201. #self.arm.playAction('c_hanghopright', 0, 45, layer=3, play_mode=0, speed=.75, blendin=10)
  202. self.arm.playAction('c_shimmy_r', 0, 34, layer=3, play_mode=0, speed=.75, blendin=10)
  203. self.obj.applyForce([0,-235,0], True)
  204. def check_throw(self):
  205. def throw(strength):
  206. print('throwing with strength', strength)
  207. if self.throw_deck != None:
  208. self.throw_deck.endObject()
  209. self.throw_deck = None
  210. self.throw_deck = dict['level_scene'].addObject('throw_deck', self.throw_deck_empty, 0)
  211. self.throw_deck.worldOrientation = self.throw_deck_empty.worldOrientation
  212. #self.throw_deck.applyForce([0, 0, -300], True)
  213. #-z = forward +y = height
  214. self.throw_deck.applyForce([0, 150, -200], True)
  215. self.hide_deck()
  216. if self.throw_deck == None:
  217. strength = self.throw_timer
  218. if strength > 19:
  219. strength = 19
  220. if dict['bBut'] == 0 and dict['last_bBut'] == 1:
  221. self.throw_timer = 0
  222. self.arm.playAction('c_throw', 19, 45, layer=7, play_mode=0, speed=.75, blendin=5)
  223. self.deck_arm.playAction('b_throw', 19, 45, layer=7, play_mode=0, speed=.75, blendin=5)
  224. throw(strength)
  225. elif dict['bBut'] == 1:
  226. self.arm.playAction('c_throw', 0, 45, layer=7, play_mode=1, speed=.75, blendin=5)
  227. self.deck_arm.playAction('b_throw', 0, 45, layer=7, play_mode=1, speed=.75, blendin=5)
  228. self.arm.setActionFrame(strength, 7)
  229. self.deck_arm.setActionFrame(strength, 7)
  230. self.throw_timer += 1
  231. else:
  232. self.throw_timer = 0
  233. else:
  234. if dict['bBut'] == 0 and dict['last_bBut'] == 1:
  235. self.show_deck()
  236. self.throw_deck.endObject()
  237. self.throw_deck = None
  238. def drop_deck(self):
  239. if self.throw_deck == None:
  240. self.throw_deck = dict['level_scene'].addObject('throw_deck', self.throw_deck_empty, 0)
  241. self.throw_deck.worldOrientation = self.throw_deck_empty.worldOrientation
  242. #self.throw_deck.applyForce([0, 0, -300], True)
  243. #-z = forward +y = height
  244. self.throw_deck.applyMovement([-.5, 0, .5], True)
  245. self.throw_deck.worldPosition.z += .4
  246. self.throw_deck.applyForce([0, 50, 75], True)
  247. self.hide_deck()
  248. #if self.throw_timer > 0:
  249. def hide_deck(self):
  250. for x in self.deck_arm.childrenRecursive:
  251. x.visible = False
  252. def show_deck(self):
  253. for x in self.deck_arm.childrenRecursive:
  254. x.visible = True
  255. def get_vert_rot(self, own, object):
  256. if 'inited' not in object:
  257. object['inited'] = True
  258. for mesh in object.meshes:
  259. red_verts = []
  260. green_verts = []
  261. for m_index in range(len(mesh.materials)):
  262. for v_index in range(mesh.getVertexArrayLength(m_index)):
  263. vertex = mesh.getVertex(m_index, v_index)
  264. if vertex.color[0] > .8:
  265. loc = object.worldTransform * vertex.XYZ
  266. red_verts.append(loc.freeze())
  267. if vertex.color[1] > .8:
  268. loc = object.worldTransform * vertex.XYZ
  269. green_verts.append(loc.freeze())
  270. red_verts = set(red_verts)
  271. #for v in red_verts:
  272. #print(v, 'red_vert')
  273. green_verts = set(green_verts)
  274. object['red_verts'] = red_verts
  275. object['green_verts'] = green_verts
  276. size_red = len(object['red_verts'])
  277. kd_red = mathutils.kdtree.KDTree(size_red)
  278. size_green = len(object['green_verts'])
  279. kd_green = mathutils.kdtree.KDTree(size_green)
  280. for i, v in enumerate(object['red_verts']):
  281. kd_red.insert(v, i)
  282. for i, v in enumerate(object['green_verts']):
  283. kd_green.insert(v, i)
  284. kd_red.balance()
  285. kd_green.balance()
  286. object['kd_red'] = kd_red
  287. object['kd_green'] = kd_green
  288. #print('kd built -------')
  289. #print(own['kd_red'])
  290. # Find the closest points to the player
  291. #co_find = control_bottom.worldPosition
  292. co_find = own.worldPosition
  293. found_red = object['kd_red'].find_n(co_find, 2)
  294. found_green = object['kd_green'].find_n(co_find, 1)
  295. primary = Vector(found_red[0][0])
  296. secondary = Vector(found_red[1][0])
  297. lineVector = secondary - primary
  298. lineVector2 = primary - secondary
  299. if found_green != []:
  300. lv_green = Vector(found_green[0][0])
  301. eul = Vector((1, 0, 0)).rotation_difference(lineVector).to_euler()
  302. te = dict['level_scene'].objects['temp_empty']
  303. rotation = te.worldOrientation.to_euler()
  304. if abs(rotation.z) > 3:
  305. te.applyRotation([0, 0, 1.570796*2], True)
  306. xyz = te.localOrientation.to_euler()
  307. xyz[0] = math.radians(0)
  308. te.localOrientation = xyz.to_matrix()
  309. if lineVector == Vector([0.0, 0.0, 0.0]):
  310. #print('vector is empty')
  311. pass
  312. else:
  313. te.alignAxisToVect(lineVector, 0, 1)
  314. te.worldPosition = primary
  315. if found_green != []:
  316. local = te.worldOrientation.inverted() * (lv_green - te.worldPosition)
  317. if local.y < 0:
  318. #print('flip vector')
  319. eul = Vector((1, 0, 0)).rotation_difference(lineVector2).to_euler()
  320. if lineVector2 == Vector([0.0, 0.0, 0.0]):
  321. #print('linVector2 is empty')
  322. pass
  323. else:
  324. te.alignAxisToVect(lineVector2, 0, 1)
  325. #print('align2', lineVector2)
  326. te.worldPosition = secondary
  327. else:
  328. #print('align1', lineVector)
  329. pass
  330. #flip z axis
  331. #print(local, 'local')
  332. myhead = te.worldOrientation.col[2]
  333. worldUp = Vector([0,0,1])
  334. #worldY = Vector([0,1,0])
  335. dot_p = myhead.dot(worldUp)
  336. if dot_p > 0.9:
  337. pass
  338. else:
  339. #print('flip grind object rotation')
  340. te.applyRotation([1.570796*2, 0, 0], True)
  341. return eul
  342. def move_to_te(self):
  343. o = self.obj
  344. te = dict['level_scene'].objects['temp_empty']
  345. te.applyRotation([0,0,-3.14/2], True)
  346. te.worldPosition.z += .3
  347. o.worldPosition = te.worldPosition
  348. o.worldOrientation = te.worldOrientation
  349. o.linearVelocity = [0,0,0]
  350. def update(cont):
  351. #print('-------------player updating----------------')
  352. dict = bge.logic.globalDict
  353. #own = cont.owner
  354. if 'player_class' not in dict:
  355. #dict['playerFSM'] = FSM.PlayerFSM(own)
  356. dict['player_class'] = Player(cont)
  357. #dict['playerFSM'].Execute()
  358. dict['player_class'].update()