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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import bge
  2. import FSM
  3. from mathutils import Vector
  4. dict = bge.logic.globalDict
  5. class Player:
  6. def __init__(self, cont):
  7. self.cont = cont
  8. self.obj = cont.owner
  9. self.FSM = FSM.PlayerFSM(self)
  10. self.walking = True
  11. self.last_pos = self.obj.worldPosition.copy()
  12. self.throw_timer = 0
  13. self.throw_deck = None
  14. self.throw_deck_empty = self.obj.childrenRecursive["throw_deck_empty"]
  15. #self.throw_deck_obj = dict['level_scene'].objects['throw_deck']
  16. dict['walking'] = True
  17. #print(self.obj.childrenRecursive)
  18. #self.arm = self.obj.childrenRecursive['Char4']
  19. self.arm = None
  20. def update(self):
  21. self.FSM.Execute()
  22. self.last_pos = self.obj.worldPosition.copy()
  23. def get_ground_ray(self):
  24. v = self.obj.worldOrientation.col[0]
  25. tv = v.normalized()
  26. axis = 2
  27. distance = -50
  28. start = self.obj.worldPosition.copy()
  29. back_start = start + 0.15 * tv
  30. start.z += .1
  31. end = Vector.copy(self.obj.worldPosition + (self.obj.worldOrientation.col[axis] * distance))
  32. ray = self.obj.rayCast(end, back_start, 0, 'ground', 1, 0, 0, 53247)
  33. v = self.obj.linearVelocity.copy()
  34. v = self.obj.worldOrientation.col[0]
  35. tv = v.normalized()
  36. front_start = start - 0.30 * tv
  37. start.z += .1
  38. front_end = Vector(front_start + (self.obj.worldOrientation.col[axis] * distance))
  39. frontray = self.obj.rayCast(front_end, front_start, 0, 'ground', 1, 1, 0, 53247)
  40. #bge.render.drawLine(front_start, front_end, [1,0,0])
  41. return [ray, frontray]
  42. def get_hang_ray(self):
  43. v = self.obj.worldOrientation.col[0]
  44. tv = v.normalized()
  45. axis = 2
  46. distance = -5
  47. start = self.obj.worldPosition.copy()
  48. start.z += 1.8
  49. mstart = start - 0.40 * tv
  50. end = Vector(mstart + (self.obj.worldOrientation.col[axis] * distance))
  51. ray = self.obj.rayCast(end, mstart, 0, 'ground', 1, 1, 0, 53247)
  52. #ray = self.obj.rayCast(end, mstart, 0, '', 1, 1, 0, 53247)
  53. #bge.render.drawLine(mstart, end, [0,0,1])
  54. return ray
  55. def get_hang_align_ray(self):
  56. v = self.obj.worldOrientation.col[0]
  57. tv = v.normalized()
  58. axis = 0
  59. distance = -2
  60. start = self.obj.worldPosition.copy()
  61. start.z += 1
  62. #mstart = start - 0.40 * tv
  63. #mstart = start * tv
  64. mstart = start
  65. end = Vector(mstart + (self.obj.worldOrientation.col[axis] * distance))
  66. #ray = self.obj.rayCast(end, mstart, 0, 'ground', 1, 0, 0, 53247)
  67. ray = self.obj.rayCast(end, mstart, 0, '', 1, 0, 0, 53247)
  68. #if ray[0] != None:
  69. #print(ray[0], 'player funct')
  70. #bge.render.drawLine(mstart, end, [0,1,0])
  71. return ray
  72. def get_ground_dist(self, groundray):
  73. gr = groundray[0]
  74. fgr = groundray[1]
  75. d = None
  76. if gr[0] != None:
  77. p_z = self.obj.worldPosition.z
  78. g_z = gr[1].z
  79. distance_to_ground = p_z - g_z
  80. p2 = None
  81. if fgr[0] != None:
  82. p2 = p_z - fgr[1].z
  83. if p2 < distance_to_ground:
  84. distance_to_ground = p2
  85. return distance_to_ground
  86. def get_hang_dist(self, groundray):
  87. if groundray[0] != None:
  88. p_z = self.obj.worldPosition.z
  89. g_z = groundray[1].z
  90. distance_to_ground = p_z - g_z
  91. return distance_to_ground
  92. def set_walk_z(self, dist):
  93. if dist < .6:
  94. target_dist = .3
  95. self.obj.worldPosition.z += (target_dist - dist)
  96. self.obj.linearVelocity.z = 0
  97. def align_walk_z(self):
  98. self.obj.alignAxisToVect([0,0,1], 2, .05)
  99. def walk_movement(self):
  100. moving = False
  101. o = self.obj
  102. o.linearVelocity.y *= .9
  103. o.linearVelocity.x *= .95
  104. if dict['kb_w'] == 2 or dict['lUD'] < -.05:
  105. #walk_blend(self)
  106. #run
  107. if (dict['kb_lsh'] == 2 or dict['aBut'] == 1) and o.linearVelocity.x > -dict['max_run_vel']:
  108. #self.run_speed = 1.5
  109. #o.applyForce([-dict['run_force'], 0, 0], True)
  110. o.linearVelocity.x -= .2
  111. #walk
  112. elif o.linearVelocity.x > -dict['max_walk_vel']:
  113. #o.applyForce([-dict['walk_force'], 0, 0], True)
  114. o.linearVelocity.x -= .1
  115. if dict['kb_s'] == 2 or dict['lUD'] > .06:
  116. #o.applyForce([10, 0, 0], True)
  117. if o.linearVelocity.x < (dict['max_walk_vel'] / 2):
  118. o.linearVelocity.x += .05
  119. #print('w')
  120. if dict['kb_a'] == 2 or dict['lLR'] < -.03:
  121. o.applyRotation([0, 0, dict['walk_turn_amt']], True)
  122. #print('w')
  123. if dict['kb_d'] == 2 or dict['lLR'] > .03:
  124. o.applyRotation([0, 0, -dict['walk_turn_amt']], True)
  125. #print('linvel', o.linearVelocity)
  126. return moving
  127. def move_walk_cam(self):
  128. amt = -.05
  129. amt2 = -.07
  130. if dict['rLR'] > .04:
  131. dict['camera'].applyMovement([amt, 0, 0], True)
  132. elif dict['rLR'] < -.04:
  133. dict['camera'].applyMovement([-amt, 0, 0], True)
  134. if dict['rUD'] > .04:
  135. dict['camera'].applyMovement([0, -amt2, 0], True)
  136. elif dict['rUD'] < -.04:
  137. dict['camera'].applyMovement([0, amt2, 0], True)
  138. def check_reset_point(self):
  139. print(dict['udPad'], dict['ddPad'])
  140. if dict['udPad'] == 1:
  141. print('set reset')
  142. elif dict['ddPad'] == 1:
  143. print('go to reset')
  144. #reset
  145. if dict['ddPad'] == 1:
  146. spawn_pos = self.obj['spawn_pos']
  147. spawn_rot = self.obj['spawn_rot']
  148. spawn_cam_pos = self.obj['spawn_cam_pos']
  149. spawn_cam_rot = self.obj['spawn_cam_rot']
  150. try:
  151. self.obj.worldPosition = (spawn_pos[0], spawn_pos[1], (spawn_pos[2] + .1))
  152. 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]]
  153. dict['camera'].worldPosition = (spawn_cam_pos[0], spawn_cam_pos[1], (spawn_cam_pos[2]))
  154. 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]]
  155. except:
  156. self.obj.worldPosition = (0, 0, .1)
  157. self.obj.worldOrientation = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
  158. if self.obj["spawn_stance"] == 1:
  159. self.obj.setLinearVelocity([.1,0,0], 1)
  160. else:
  161. self.obj.setLinearVelocity([-.1,0,0], 1)
  162. if dict['udPad'] == 1:
  163. self.obj['spawn_pos'] = [self.obj.worldPosition[0], self.obj.worldPosition[1], self.obj.worldPosition[2]]
  164. 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]]
  165. self.obj['spawn_cam_pos'] = [dict['camera'].worldPosition[0], dict['camera'].worldPosition[1], dict['camera'].worldPosition[2]]
  166. 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]]
  167. stance = self.obj["stance"]
  168. self.obj["spawn_stance"] = stance
  169. def hang_move(self):
  170. if dict['lLR'] > .04:
  171. #self.arm.playAction('c_hanghopleft', 0, 45, layer=3, play_mode=0, speed=.75, blendin=10)
  172. self.arm.playAction('c_shimmy_l', 0, 34, layer=3, play_mode=0, speed=.75, blendin=10)
  173. self.obj.applyForce([0,235,0], True)
  174. if dict['lLR'] < -.04:
  175. #self.arm.playAction('c_hanghopright', 0, 45, layer=3, play_mode=0, speed=.75, blendin=10)
  176. self.arm.playAction('c_shimmy_r', 0, 34, layer=3, play_mode=0, speed=.75, blendin=10)
  177. self.obj.applyForce([0,-235,0], True)
  178. def check_throw(self):
  179. def throw(strength):
  180. print('throwing with strength', strength)
  181. if self.throw_deck != None:
  182. self.throw_deck.endObject()
  183. self.throw_deck = None
  184. self.throw_deck = dict['level_scene'].addObject('throw_deck', self.throw_deck_empty, 0)
  185. self.throw_deck.worldOrientation = self.throw_deck_empty.worldOrientation
  186. #self.throw_deck.applyForce([0, 0, -300], True)
  187. #-z = forward +y = height
  188. self.throw_deck.applyForce([0, 150, -200], True)
  189. self.hide_deck()
  190. if self.throw_deck == None:
  191. strength = self.throw_timer
  192. if strength > 19:
  193. strength = 19
  194. if dict['bBut'] == 0 and dict['last_bBut'] == 1:
  195. self.throw_timer = 0
  196. self.arm.playAction('c_throw', 19, 45, layer=7, play_mode=0, speed=.75, blendin=5)
  197. self.deck_arm.playAction('b_throw', 19, 45, layer=7, play_mode=0, speed=.75, blendin=5)
  198. throw(strength)
  199. elif dict['bBut'] == 1:
  200. self.arm.playAction('c_throw', 0, 45, layer=7, play_mode=1, speed=.75, blendin=5)
  201. self.deck_arm.playAction('b_throw', 0, 45, layer=7, play_mode=1, speed=.75, blendin=5)
  202. self.arm.setActionFrame(strength, 7)
  203. self.deck_arm.setActionFrame(strength, 7)
  204. self.throw_timer += 1
  205. else:
  206. self.throw_timer = 0
  207. else:
  208. if dict['bBut'] == 0 and dict['last_bBut'] == 1:
  209. self.show_deck()
  210. self.throw_deck.endObject()
  211. self.throw_deck = None
  212. #if self.throw_timer > 0:
  213. def hide_deck(self):
  214. for x in self.deck_arm.childrenRecursive:
  215. x.visible = False
  216. def show_deck(self):
  217. for x in self.deck_arm.childrenRecursive:
  218. x.visible = True
  219. def update(cont):
  220. #print('-------------player updating----------------')
  221. dict = bge.logic.globalDict
  222. #own = cont.owner
  223. if 'player_class' not in dict:
  224. #dict['playerFSM'] = FSM.PlayerFSM(own)
  225. dict['player_class'] = Player(cont)
  226. #dict['playerFSM'].Execute()
  227. dict['player_class'].update()