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 17KB

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