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.

StatesPlayer.py 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import bge
  2. dict = bge.logic.globalDict
  3. #====================================
  4. State = type("State", (object,), {})
  5. #====================================
  6. class State(object):
  7. def __init__(self, FSM):
  8. self.FSM = FSM
  9. self.timer = 0
  10. self.startTime = 0
  11. def Enter(self):
  12. self.timer = 0
  13. self.startTime = 0
  14. def Execute(self):
  15. print('Executing')
  16. def Exit(self):
  17. print('Exiting')
  18. #====================================
  19. class Example(State):
  20. def __init__(self,FSM):
  21. super(Example, self).__init__(FSM)
  22. def Enter(self):
  23. self.FSM.stateLife = 1
  24. super(Example, self).Enter()
  25. def Execute(self):
  26. self.FSM.stateLife += 1
  27. #self.FSM.ToTransition('toExample')
  28. def Exit(self):
  29. pass
  30. #====================================
  31. class Startup(State):
  32. def __init__(self,FSM):
  33. super(Startup, self).__init__(FSM)
  34. def Enter(self):
  35. self.FSM.stateLife = 1
  36. super(Startup, self).Enter()
  37. def Execute(self):
  38. self.FSM.stateLife += 1
  39. if self.FSM.stateLife == 5:
  40. #Startup.main(self.FSM.owner['cont'])
  41. self.FSM.ToTransition('toWalk')
  42. print('player FSM')
  43. def Exit(self):
  44. pass
  45. #====================================
  46. class Walk(State):
  47. def __init__(self,FSM):
  48. super(Walk, self).__init__(FSM)
  49. def Enter(self):
  50. self.FSM.stateLife = 1
  51. o = self.FSM.owner.obj
  52. c = self.FSM.owner
  53. if c.arm == None:
  54. print(o.childrenRecursive)
  55. c.arm = o.childrenRecursive['Char4']
  56. self.walk_weight = 1
  57. self.run_weight = 1
  58. self.run_speed = 1.0
  59. self.turn_weight = 0
  60. super(Walk, self).Enter()
  61. def Execute(self):
  62. self.FSM.stateLife += 1
  63. o = self.FSM.owner.obj
  64. c = self.FSM.owner
  65. #print('walking')
  66. if self.FSM.stateLife == 2:
  67. if o['stance']:
  68. o.applyRotation([0,0,3], True)
  69. o['stance'] = False
  70. o['requestAction'] = 'fak_offboard'
  71. print('request fak offboard')
  72. else:
  73. o['requestAction'] = 'reg_offboard'
  74. print('request reg offboard')
  75. if self.FSM.stateLife > 5:
  76. o['requestAction'] = 'reg_idle'
  77. self.check_onboard()
  78. self.check_jump()
  79. self.check_exit()
  80. ray = c.get_ground_ray()
  81. moving = c.walk_movement()
  82. self.idle_blend()
  83. self.get_walk_weight()
  84. #self.FSM.ToTransition('toLand')
  85. def check_onboard(self):
  86. if dict['walk'] == 0:
  87. self.FSM.ToTransition('toRoll')
  88. if dict['yBut'] == 1 and dict['last_yBut'] == 0:
  89. print('do the onboard')
  90. def check_jump(self):
  91. o = self.FSM.owner.obj
  92. #limit fall speed
  93. if o.linearVelocity.z < -10:
  94. o.linearVelocity.z = -10
  95. if dict['xBut'] == True or dict['kb_space'] == 1:
  96. if dict['last_xBut'] == 0:
  97. o['requestAction'] ='reg_jump'
  98. JUMPHEIGHT = 1100
  99. force = [ 0.0, 0.0, JUMPHEIGHT]
  100. # use local axis
  101. local = False
  102. # apply force -- limit jump speed
  103. if o.linearVelocity.z < 10:
  104. o.applyForce(force, local)
  105. o.linearVelocity.z += 5
  106. def check_exit(self):
  107. pass
  108. #own['walk'] == 1
  109. def idle_blend(self):
  110. arm = self.FSM.owner.arm
  111. arm.playAction('reg_idle1', 1,120, layer=6, play_mode=1, speed=1, blendin=10)
  112. if self.FSM.stateLife > 20:
  113. frame = arm.getActionFrame(7) + .5 * self.run_speed
  114. if frame > 30:
  115. frame = 0
  116. arm.stopAction(7)
  117. arm.playAction('reg_nwalk2', 0,30, layer=7, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.walk_weight)
  118. arm.setActionFrame(frame, 7)
  119. #print(self.walk_weight, frame)
  120. frame = arm.getActionFrame(8) + .5 * self.run_speed
  121. if frame > 30:
  122. frame = 0
  123. arm.stopAction(8)
  124. arm.playAction('reg_run.002', 0,30, layer=8, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.run_weight)
  125. arm.setActionFrame(frame, 8)
  126. #print(frame, 'frame')
  127. #print(self.run_weight, frame)
  128. # if self.turn_weight > 0:
  129. # tw = abs(self.turn_weight - 1)
  130. # #tw = self.turn_weight
  131. # arm.stopAction(4)
  132. # arm.playAction('bwalk_right', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw)
  133. # elif self.turn_weight < 0:
  134. # tw = abs(abs(self.turn_weight) - 1)
  135. # #tw = self.turn_weight
  136. # arm.stopAction(4)
  137. # arm.playAction('bwalk_left', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw)
  138. def get_walk_weight(self):
  139. #print(self.FSM.owner.linearVelocity.y)
  140. o = self.FSM.owner.obj
  141. w = abs(o.linearVelocity.x)
  142. #wt = 3.5
  143. #yt = 8
  144. wt = 1.1
  145. yt = 3
  146. out2 = 0
  147. if w < wt:
  148. out = w / wt
  149. else:
  150. out = 1
  151. #print('running', w)
  152. out2 = w / yt
  153. out = abs(1 - out)
  154. out2 = abs(1 - out2)
  155. tgww = round(out, 3)
  156. tgrw = round(out2, 3)
  157. incer = .05
  158. if self.walk_weight < tgww:
  159. self.walk_weight += incer
  160. if self.walk_weight > tgww:
  161. self.walk_weight -= incer
  162. if self.run_weight <= tgrw:
  163. self.run_weight += incer
  164. if self.run_weight > tgrw:
  165. self.run_weight -= incer
  166. if self.walk_weight <= 0:
  167. self.walk_weight = 0
  168. if self.walk_weight > .95:
  169. self.walk_weight = 1
  170. if self.run_weight <= 0:
  171. self.run_weight = 0
  172. if self.run_weight > .95:
  173. self.run_weight = 1
  174. if dict['kb_lsh'] == 2 or dict['aBut'] == 1:
  175. self.run_speed = 1.3
  176. else:
  177. self.run_speed = .8
  178. print(self.run_speed, '---', self.walk_weight, 'walk weight', self.run_weight, 'run weight')
  179. def Exit(self):
  180. pass
  181. #====================================
  182. class WalkAir(State):
  183. def __init__(self,FSM):
  184. super(WalkAir, self).__init__(FSM)
  185. def Enter(self):
  186. self.FSM.stateLife = 1
  187. super(WalkAir, self).Enter()
  188. def Execute(self):
  189. self.FSM.stateLife += 1
  190. #self.FSM.ToTransition('toLand')
  191. def Exit(self):
  192. pass
  193. #====================================
  194. class WalkJump(State):
  195. def __init__(self,FSM):
  196. super(WalkJump, self).__init__(FSM)
  197. def Enter(self):
  198. self.FSM.stateLife = 1
  199. super(WalkJump, self).Enter()
  200. def Execute(self):
  201. self.FSM.stateLife += 1
  202. #self.FSM.ToTransition('toLand')
  203. def Exit(self):
  204. pass
  205. #====================================
  206. class WalkLand(State):
  207. def __init__(self,FSM):
  208. super(WalkLand, self).__init__(FSM)
  209. def Enter(self):
  210. self.FSM.stateLife = 1
  211. super(WalkLand, self).Enter()
  212. def Execute(self):
  213. self.FSM.stateLife += 1
  214. #self.FSM.ToTransition('toLand')
  215. def Exit(self):
  216. pass
  217. #====================================
  218. class WalkHang(State):
  219. def __init__(self,FSM):
  220. super(WalkHang, self).__init__(FSM)
  221. def Enter(self):
  222. self.FSM.stateLife = 1
  223. super(WalkHang, self).Enter()
  224. def Execute(self):
  225. self.FSM.stateLife += 1
  226. #self.FSM.ToTransition('toLand')
  227. def Exit(self):
  228. pass
  229. #====================================
  230. class WalkClimb(State):
  231. def __init__(self,FSM):
  232. super(WalkClimb, self).__init__(FSM)
  233. def Enter(self):
  234. self.FSM.stateLife = 1
  235. super(WalkClimb, self).Enter()
  236. def Execute(self):
  237. self.FSM.stateLife += 1
  238. #self.FSM.ToTransition('toLand')
  239. def Exit(self):
  240. pass
  241. #====================================
  242. class WalkHurdle(State):
  243. def __init__(self,FSM):
  244. super(WalkHurdle, self).__init__(FSM)
  245. def Enter(self):
  246. self.FSM.stateLife = 1
  247. super(WalkHurdle, self).Enter()
  248. def Execute(self):
  249. self.FSM.stateLife += 1
  250. #self.FSM.ToTransition('toLand')
  251. def Exit(self):
  252. pass
  253. #====================================
  254. class Roll(State):
  255. def __init__(self,FSM):
  256. super(Roll, self).__init__(FSM)
  257. def Enter(self):
  258. self.FSM.stateLife = 1
  259. super(Roll, self).Enter()
  260. def Execute(self):
  261. self.FSM.stateLife += 1
  262. if dict['walk'] == 1:
  263. self.FSM.ToTransition('toWalk')
  264. print('rolling')
  265. #self.FSM.ToTransition('toLand')
  266. def Exit(self):
  267. pass
  268. #====================================