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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. self.FSM.ToTransition('toWalk')
  41. print('player FSM')
  42. def Exit(self):
  43. pass
  44. #====================================
  45. class Walk(State):
  46. def __init__(self,FSM):
  47. super(Walk, self).__init__(FSM)
  48. def Enter(self):
  49. self.FSM.stateLife = 1
  50. o = self.FSM.owner.obj
  51. c = self.FSM.owner
  52. o['getoffboard'] = False
  53. o['getonboard'] = False
  54. if c.arm == None:
  55. c.arm = o.childrenRecursive['Char4']
  56. self.walk_weight = 1
  57. self.run_weight = 1
  58. self.run_speed = 0.0
  59. self.turn_weight = 0
  60. self.FSM.owner.walking = True
  61. dict['walking'] = True
  62. dict['walk'] = 1
  63. print('fsm enter walk')
  64. super(Walk, self).Enter()
  65. def Execute(self):
  66. self.FSM.stateLife += 1
  67. o = self.FSM.owner.obj
  68. c = self.FSM.owner
  69. #print(o.worldPosition.z)
  70. #print(o['getonboard'], 'dict onboard')
  71. #print(dict['walk'], 'fsm walk walk')
  72. if self.FSM.stateLife == 2:
  73. if o['stance']:
  74. o.applyRotation([0,0,3.14], True)
  75. o['stance'] = False
  76. # o['requestAction'] = 'fak_offboard'
  77. # print('request fak offboard')
  78. # else:
  79. # o['requestAction'] = 'reg_offboard'
  80. # print('request reg offboard')
  81. # if self.FSM.stateLife > 5:
  82. # o['requestAction'] = 'reg_idle'
  83. ground_rays = c.get_ground_ray()
  84. if ground_rays[0][0] != None:
  85. dist = c.get_ground_dist(ground_rays)
  86. c.set_walk_z(dist)
  87. #print(dist, 'setting height')
  88. if self.FSM.stateLife > 4:
  89. self.check_onboard()
  90. self.check_jump()
  91. moving = c.walk_movement()
  92. self.get_walk_weight()
  93. self.idle_blend()
  94. else:
  95. dict['walk'] = 1
  96. def check_onboard(self):
  97. o = self.FSM.owner.obj
  98. print(o['getonboard'], 'getonboard')
  99. if dict['walk'] == 0:
  100. o['getonboard'] = True
  101. self.FSM.ToTransition('toRoll')
  102. print('onboard', dict['walk'], o['getonboard'])
  103. if dict['yBut'] == 1 and dict['last_yBut'] == 0:
  104. o['getonboard'] = True
  105. self.FSM.ToTransition('toRoll')
  106. def check_jump(self):
  107. if dict['xBut'] == True or dict['kb_space'] == 1:
  108. if dict['last_xBut'] == 0:
  109. self.FSM.ToTransition('toWalkJump')
  110. def idle_blend(self):
  111. arm = self.FSM.owner.arm
  112. arm.playAction('reg_idle1', 1,120, layer=1, play_mode=1, speed=1, blendin=10)
  113. if self.FSM.stateLife > 20:
  114. frame = arm.getActionFrame(2) + .5 * self.run_speed
  115. if frame > 30:
  116. frame = 0
  117. arm.stopAction(2)
  118. arm.playAction('reg_nwalk3', 0,30, layer=2, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.walk_weight)
  119. arm.setActionFrame(frame, 2)
  120. #print(self.walk_weight, frame)
  121. frame = arm.getActionFrame(3) + .5 * self.run_speed
  122. if frame > 30:
  123. frame = 0
  124. arm.stopAction(3)
  125. arm.playAction('reg_run.003', 0,30, layer=3, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.run_weight)
  126. arm.setActionFrame(frame, 3)
  127. #print(frame, 'frame')
  128. #print(self.run_weight, frame)
  129. # if self.turn_weight > 0:
  130. # tw = abs(self.turn_weight - 1)
  131. # #tw = self.turn_weight
  132. # arm.stopAction(4)
  133. # arm.playAction('bwalk_right', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw)
  134. # elif self.turn_weight < 0:
  135. # tw = abs(abs(self.turn_weight) - 1)
  136. # #tw = self.turn_weight
  137. # arm.stopAction(4)
  138. # arm.playAction('bwalk_left', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw)
  139. def get_walk_weight(self):
  140. #print(self.FSM.owner.linearVelocity.y)
  141. o = self.FSM.owner.obj
  142. w = abs(o.linearVelocity.x)
  143. #wt = 3.5
  144. #yt = 8
  145. wt = 1.7
  146. yt = 3
  147. out2 = 0
  148. if w < wt:
  149. out = w / wt
  150. else:
  151. out = 1
  152. #print('running', w)
  153. out2 = w / yt
  154. out = abs(1 - out)
  155. out2 = abs(1 - out2)
  156. tgww = round(out, 3)
  157. tgrw = round(out2, 3)
  158. incer = .05
  159. if self.walk_weight < tgww:
  160. self.walk_weight += incer
  161. if self.walk_weight > tgww:
  162. self.walk_weight -= incer
  163. if self.run_weight <= tgrw:
  164. self.run_weight += incer
  165. if self.run_weight > tgrw:
  166. self.run_weight -= incer
  167. if self.walk_weight <= 0:
  168. self.walk_weight = 0
  169. if self.walk_weight > .95:
  170. self.walk_weight = 1
  171. if self.run_weight <= 0:
  172. self.run_weight = 0
  173. if self.run_weight > .95:
  174. self.run_weight = 1
  175. if dict['kb_lsh'] == 2 or dict['aBut'] == 1:
  176. self.run_speed = 1.3
  177. else:
  178. self.run_speed = .8
  179. #print(self.run_speed, '---', self.walk_weight, 'walk weight', self.run_weight, 'run weight')
  180. def Exit(self):
  181. self.FSM.owner.arm.stopAction(0)
  182. self.FSM.owner.arm.stopAction(1)
  183. self.FSM.owner.arm.stopAction(2)
  184. self.FSM.owner.arm.stopAction(3)
  185. dict['walk'] = 0
  186. #====================================
  187. class WalkAir(State):
  188. def __init__(self,FSM):
  189. super(WalkAir, self).__init__(FSM)
  190. def Enter(self):
  191. self.FSM.stateLife = 1
  192. #arm = self.FSM.owner.arm
  193. #arm.playAction('a_jump_t', 27, 27, layer=1, play_mode=0, speed=1, blendin=5)
  194. super(WalkAir, self).Enter()
  195. def Execute(self):
  196. self.FSM.stateLife += 1
  197. #self.FSM.ToTransition('toLand')
  198. arm = self.FSM.owner.arm
  199. arm.playAction('a_jump_t', 23, 23, layer=3, play_mode=1, speed=1, blendin=5)
  200. #if self.FSM.stateLife > 50:
  201. #self.FSM.ToTransition('toWalk')
  202. #limit fall speed
  203. if self.FSM.owner.obj.linearVelocity.z < -10:
  204. self.FSM.owner.obj.linearVelocity.z = -10
  205. #ground_rays = self.FSM.owner.get_ground_ray()
  206. ground_rays = self.FSM.owner.get_ground_ray()
  207. if ground_rays[0][0] != None:
  208. dist = self.FSM.owner.get_ground_dist(ground_rays)
  209. if dist < .4:
  210. self.FSM.ToTransition('toWalkLand')
  211. moving = self.FSM.owner.walk_movement()
  212. def Exit(self):
  213. self.FSM.owner.arm.stopAction(0)
  214. self.FSM.owner.arm.stopAction(1)
  215. self.FSM.owner.arm.stopAction(2)
  216. self.FSM.owner.arm.stopAction(3)
  217. #====================================
  218. class WalkJump(State):
  219. def __init__(self,FSM):
  220. super(WalkJump, self).__init__(FSM)
  221. def Enter(self):
  222. self.FSM.stateLife = 1
  223. arm = self.FSM.owner.arm
  224. arm.playAction('a_jump_t', 1, 23, layer=3, play_mode=0, speed=1, blendin=5)
  225. super(WalkJump, self).Enter()
  226. def Execute(self):
  227. self.FSM.stateLife += 1
  228. arm = self.FSM.owner.arm
  229. o = self.FSM.owner.obj
  230. moving = self.FSM.owner.walk_movement()
  231. if self.FSM.stateLife == 10:
  232. force = [ 0.0, 0.0, dict['walk_jump_force']]
  233. if o.linearVelocity.z < 10:
  234. o.applyForce(force, True)
  235. if self.FSM.stateLife > 27:
  236. self.FSM.ToTransition('toWalkAir')
  237. def Exit(self):
  238. pass
  239. #====================================
  240. class WalkLand(State):
  241. def __init__(self,FSM):
  242. super(WalkLand, self).__init__(FSM)
  243. def Enter(self):
  244. self.FSM.stateLife = 1
  245. self.FSM.owner.arm.playAction('a_jump_t', 23, 50, layer=5, play_mode=0, speed=1, blendin=5)
  246. super(WalkLand, self).Enter()
  247. def Execute(self):
  248. self.FSM.stateLife += 1
  249. self.FSM.owner.obj.linearVelocity.x *= .8
  250. self.FSM.owner.obj.linearVelocity.y *= .8
  251. # arm = self.FSM.owner.arm
  252. # print('af', arm.getActionFrame(5))
  253. # if arm.getActionFrame(5) > 30:
  254. # print('stopping land')
  255. # arm.stopAction(5)
  256. # arm.playAction('reg_idle1', 1,120, layer=1, play_mode=1, speed=1, blendin=2)
  257. if self.FSM.stateLife > 8:
  258. self.FSM.ToTransition('toWalk')
  259. ground_rays = self.FSM.owner.get_ground_ray()
  260. if ground_rays[0][0] != None:
  261. dist = self.FSM.owner.get_ground_dist(ground_rays)
  262. self.FSM.owner.set_walk_z(dist)
  263. def Exit(self):
  264. pass
  265. #====================================
  266. class WalkHang(State):
  267. def __init__(self,FSM):
  268. super(WalkHang, self).__init__(FSM)
  269. def Enter(self):
  270. self.FSM.stateLife = 1
  271. super(WalkHang, self).Enter()
  272. def Execute(self):
  273. self.FSM.stateLife += 1
  274. #self.FSM.ToTransition('toLand')
  275. def Exit(self):
  276. pass
  277. #====================================
  278. class WalkClimb(State):
  279. def __init__(self,FSM):
  280. super(WalkClimb, self).__init__(FSM)
  281. def Enter(self):
  282. self.FSM.stateLife = 1
  283. super(WalkClimb, self).Enter()
  284. def Execute(self):
  285. self.FSM.stateLife += 1
  286. #self.FSM.ToTransition('toLand')
  287. def Exit(self):
  288. pass
  289. #====================================
  290. class WalkHurdle(State):
  291. def __init__(self,FSM):
  292. super(WalkHurdle, self).__init__(FSM)
  293. def Enter(self):
  294. self.FSM.stateLife = 1
  295. super(WalkHurdle, self).Enter()
  296. def Execute(self):
  297. self.FSM.stateLife += 1
  298. #self.FSM.ToTransition('toLand')
  299. def Exit(self):
  300. pass
  301. #====================================
  302. class Roll(State):
  303. def __init__(self,FSM):
  304. super(Roll, self).__init__(FSM)
  305. def Enter(self):
  306. self.FSM.stateLife = 1
  307. self.FSM.owner.walking = False
  308. dict['walking'] = False
  309. super(Roll, self).Enter()
  310. def Execute(self):
  311. self.FSM.stateLife += 1
  312. if dict['walk'] == 1 or self.FSM.owner.obj['getoffboard'] == True:
  313. dict['walk'] = 1
  314. self.FSM.owner.obj['getoffboard'] = True
  315. self.FSM.ToTransition('toWalk')
  316. print('fsm to walk')
  317. print('rolling')
  318. #self.FSM.ToTransition('toLand')
  319. def Exit(self):
  320. pass
  321. #====================================