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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. c.deck_arm = o.childrenRecursive['deck_arm']
  57. self.walk_weight = 1
  58. self.run_weight = 1
  59. self.run_speed = 0.0
  60. self.turn_weight = 0
  61. self.FSM.owner.walking = True
  62. dict['walking'] = True
  63. dict['walk'] = 1
  64. print('fsm enter walk')
  65. super(Walk, self).Enter()
  66. def Execute(self):
  67. self.FSM.stateLife += 1
  68. o = self.FSM.owner.obj
  69. c = self.FSM.owner
  70. #print(o.worldPosition.z)
  71. #print(o['getonboard'], 'dict onboard')
  72. #print(dict['walk'], 'fsm walk walk')
  73. if self.FSM.stateLife == 2:
  74. if o['stance']:
  75. o.applyRotation([0,0,3.14], True)
  76. o['stance'] = False
  77. # o['requestAction'] = 'fak_offboard'
  78. # print('request fak offboard')
  79. # else:
  80. # o['requestAction'] = 'reg_offboard'
  81. # print('request reg offboard')
  82. # if self.FSM.stateLife > 5:
  83. # o['requestAction'] = 'reg_idle'
  84. c.align_walk_z()
  85. ground_rays = c.get_ground_ray()
  86. #77777777777777777777777777777777777777777777777777777777777777777777777777777777777
  87. c.get_hang_align_ray()
  88. c.move_walk_cam()
  89. c.check_reset_point()
  90. c.check_throw()
  91. if ground_rays[0][0] != None:
  92. dist = c.get_ground_dist(ground_rays)
  93. if dist > .7:
  94. self.FSM.ToTransition('toWalkAir')
  95. print('---falling')
  96. else:
  97. c.set_walk_z(dist)
  98. #print(dist, 'setting height')
  99. if self.FSM.stateLife > 4:
  100. self.check_onboard()
  101. self.check_jump()
  102. moving = c.walk_movement()
  103. self.get_walk_weight()
  104. self.idle_blend()
  105. else:
  106. dict['walk'] = 1
  107. def check_onboard(self):
  108. o = self.FSM.owner.obj
  109. #print(o['getonboard'], 'getonboard')
  110. if dict['walk'] == 0:
  111. o['getonboard'] = True
  112. self.FSM.ToTransition('toRoll')
  113. #print('onboard', dict['walk'], o['getonboard'])
  114. if dict['yBut'] == 1 and dict['last_yBut'] == 0:
  115. o['getonboard'] = True
  116. self.FSM.ToTransition('toRoll')
  117. def check_jump(self):
  118. if dict['xBut'] == True or dict['kb_space'] == 1:
  119. if dict['last_xBut'] == 0:
  120. self.FSM.ToTransition('toWalkJump')
  121. def idle_blend(self):
  122. arm = self.FSM.owner.arm
  123. deck = self.FSM.owner.deck_arm
  124. arm.playAction('reg_idle1', 1,120, layer=1, play_mode=1, speed=1, blendin=10)
  125. deck.playAction('b_reg_idle1', 1,120, layer=1, play_mode=1, speed=1, blendin=10)
  126. if self.FSM.stateLife > 20:
  127. frame = arm.getActionFrame(2) + .5 * self.run_speed
  128. if frame > 30:
  129. frame = 0
  130. arm.stopAction(2)
  131. deck.stopAction(2)
  132. arm.playAction('reg_nwalk3', 0,30, layer=2, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.walk_weight)
  133. deck.playAction('b_reg_walk', 0,30, layer=2, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.walk_weight)
  134. #b_reg_walk
  135. arm.setActionFrame(frame, 2)
  136. deck.setActionFrame(frame, 2)
  137. #print(self.walk_weight, frame)
  138. frame = arm.getActionFrame(3) + .5 * self.run_speed
  139. if frame > 30:
  140. frame = 0
  141. arm.stopAction(3)
  142. deck.stopAction(3)
  143. arm.playAction('reg_run.003', 0,30, layer=3, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.run_weight)
  144. deck.playAction('b_reg_run', 0,30, layer=3, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=self.run_weight)
  145. arm.setActionFrame(frame, 3)
  146. deck.setActionFrame(frame, 3)
  147. #print(frame, 'frame')
  148. #print(self.run_weight, frame)
  149. # if self.turn_weight > 0:
  150. # tw = abs(self.turn_weight - 1)
  151. # #tw = self.turn_weight
  152. # arm.stopAction(4)
  153. # arm.playAction('bwalk_right', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw)
  154. # elif self.turn_weight < 0:
  155. # tw = abs(abs(self.turn_weight) - 1)
  156. # #tw = self.turn_weight
  157. # arm.stopAction(4)
  158. # arm.playAction('bwalk_left', 1,60, layer=4, play_mode=1, speed=self.run_speed, blendin=0, layer_weight=tw)
  159. def get_walk_weight(self):
  160. #print(self.FSM.owner.linearVelocity.y)
  161. o = self.FSM.owner.obj
  162. w = abs(o.linearVelocity.x)
  163. #wt = 3.5
  164. #yt = 8
  165. wt = 1.7
  166. yt = 3
  167. out2 = 0
  168. if w < wt:
  169. out = w / wt
  170. else:
  171. out = 1
  172. #print('running', w)
  173. out2 = w / yt
  174. out = abs(1 - out)
  175. out2 = abs(1 - out2)
  176. tgww = round(out, 3)
  177. tgrw = round(out2, 3)
  178. incer = .05
  179. if self.walk_weight < tgww:
  180. self.walk_weight += incer
  181. if self.walk_weight > tgww:
  182. self.walk_weight -= incer
  183. if self.run_weight <= tgrw:
  184. self.run_weight += incer
  185. if self.run_weight > tgrw:
  186. self.run_weight -= incer
  187. if self.walk_weight <= 0:
  188. self.walk_weight = 0
  189. if self.walk_weight > .95:
  190. self.walk_weight = 1
  191. if self.run_weight <= 0:
  192. self.run_weight = 0
  193. if self.run_weight > .95:
  194. self.run_weight = 1
  195. if dict['kb_lsh'] == 2 or dict['aBut'] == 1:
  196. self.run_speed = 1.3
  197. else:
  198. self.run_speed = .8
  199. #print(self.run_speed, '---', self.walk_weight, 'walk weight', self.run_weight, 'run weight')
  200. def Exit(self):
  201. self.FSM.owner.arm.stopAction(0)
  202. self.FSM.owner.arm.stopAction(1)
  203. self.FSM.owner.arm.stopAction(2)
  204. self.FSM.owner.arm.stopAction(3)
  205. self.FSM.owner.deck_arm.stopAction(0)
  206. self.FSM.owner.deck_arm.stopAction(1)
  207. self.FSM.owner.deck_arm.stopAction(2)
  208. self.FSM.owner.deck_arm.stopAction(3)
  209. dict['walk'] = 0
  210. #====================================
  211. class WalkAir(State):
  212. def __init__(self,FSM):
  213. super(WalkAir, self).__init__(FSM)
  214. def Enter(self):
  215. self.FSM.stateLife = 1
  216. super(WalkAir, self).Enter()
  217. def Execute(self):
  218. self.FSM.stateLife += 1
  219. self.FSM.owner.check_reset_point()
  220. arm = self.FSM.owner.arm
  221. deck_arm = self.FSM.owner.deck_arm
  222. #arm.playAction('a_jump_t', 23, 23, layer=3, play_mode=1, speed=1, blendin=5)
  223. arm.playAction('c_jump_up', 23, 23, layer=3, play_mode=1, speed=1, blendin=15)
  224. deck_arm.playAction('b_walk_jump', 23, 23, layer=3, play_mode=1, speed=1, blendin=15)
  225. if self.FSM.owner.obj.linearVelocity.z < -10:
  226. self.FSM.owner.obj.linearVelocity.z = -10
  227. moving = self.FSM.owner.walk_movement()
  228. ground_rays = self.FSM.owner.get_ground_ray()
  229. self.FSM.owner.check_throw()
  230. if ground_rays[0][0] != None:
  231. dist = self.FSM.owner.get_ground_dist(ground_rays)
  232. if dist < .4:
  233. self.FSM.ToTransition('toWalkLand')
  234. if dist > .5 and self.FSM.owner.obj.worldPosition.z < self.FSM.owner.last_pos.z:
  235. hang_ray = self.FSM.owner.get_hang_ray()
  236. hang_align_ray = self.FSM.owner.get_hang_align_ray()
  237. if hang_ray[0] != None and hang_align_ray[0] != None and self.FSM.prevState != 'WalkHang' and dict['lUD'] < .04:
  238. hr_dist = self.FSM.owner.get_hang_dist(hang_ray)
  239. if hr_dist < .1:
  240. self.FSM.owner.obj.linearVelocity = [0,0,0]
  241. self.FSM.owner.obj.worldPosition.z = hang_ray[1].z - 1
  242. self.FSM.ToTransition('toWalkHang')
  243. def Exit(self):
  244. self.FSM.owner.arm.stopAction(0)
  245. self.FSM.owner.arm.stopAction(1)
  246. self.FSM.owner.arm.stopAction(2)
  247. self.FSM.owner.arm.stopAction(3)
  248. self.FSM.owner.deck_arm.stopAction(0)
  249. self.FSM.owner.deck_arm.stopAction(1)
  250. self.FSM.owner.deck_arm.stopAction(2)
  251. self.FSM.owner.deck_arm.stopAction(3)
  252. #====================================
  253. class WalkJump(State):
  254. def __init__(self,FSM):
  255. super(WalkJump, self).__init__(FSM)
  256. def Enter(self):
  257. self.FSM.stateLife = 1
  258. arm = self.FSM.owner.arm
  259. deck = self.FSM.owner.deck_arm
  260. #arm.playAction('a_jump_t', 1, 23, layer=3, play_mode=0, speed=1, blendin=5)
  261. arm.playAction('c_jump_up', 1, 23, layer=3, play_mode=0, speed=1, blendin=5)
  262. deck.playAction('b_walk_jump', 1, 23, layer=3, play_mode=0, speed=1, blendin=5)
  263. super(WalkJump, self).Enter()
  264. def Execute(self):
  265. self.FSM.stateLife += 1
  266. arm = self.FSM.owner.arm
  267. o = self.FSM.owner.obj
  268. moving = self.FSM.owner.walk_movement()
  269. if self.FSM.stateLife == 10:
  270. force = [ 0.0, 0.0, dict['walk_jump_force']]
  271. if o.linearVelocity.z < 10:
  272. o.applyForce(force, True)
  273. if self.FSM.stateLife > 27:
  274. self.FSM.ToTransition('toWalkAir')
  275. def Exit(self):
  276. pass
  277. #====================================
  278. class WalkLand(State):
  279. def __init__(self,FSM):
  280. super(WalkLand, self).__init__(FSM)
  281. def Enter(self):
  282. self.FSM.stateLife = 1
  283. #self.FSM.owner.arm.playAction('a_jump_t', 23, 50, layer=5, play_mode=0, speed=1, blendin=5)
  284. self.FSM.owner.arm.playAction('c_land', 0, 50, layer=1, play_mode=0, speed=1, blendin=5)
  285. self.FSM.owner.deck_arm.playAction('b_walk_land', 0, 50, layer=1, play_mode=0, speed=1, blendin=5)
  286. #c_land
  287. super(WalkLand, self).Enter()
  288. def Execute(self):
  289. self.FSM.stateLife += 1
  290. self.FSM.owner.obj.linearVelocity.x *= .8
  291. self.FSM.owner.obj.linearVelocity.y *= .8
  292. # arm = self.FSM.owner.arm
  293. # print('af', arm.getActionFrame(5))
  294. # if arm.getActionFrame(5) > 30:
  295. # print('stopping land')
  296. # arm.stopAction(5)
  297. # arm.playAction('reg_idle1', 1,120, layer=1, play_mode=1, speed=1, blendin=2)
  298. if self.FSM.stateLife > 30:
  299. self.FSM.ToTransition('toWalk')
  300. ground_rays = self.FSM.owner.get_ground_ray()
  301. if ground_rays[0][0] != None:
  302. dist = self.FSM.owner.get_ground_dist(ground_rays)
  303. self.FSM.owner.set_walk_z(dist)
  304. def Exit(self):
  305. pass
  306. #====================================
  307. class WalkHang(State):
  308. def __init__(self,FSM):
  309. super(WalkHang, self).__init__(FSM)
  310. def Enter(self):
  311. self.FSM.stateLife = 1
  312. self.FSM.owner.arm.playAction('c_braced_hang', 0, 36, layer=2, play_mode=0, speed=1, blendin=10)
  313. self.moveable = False
  314. #self.FSM.owner.arm.playAction('c_braced_hang', 36, 50, layer=1, play_mode=1, speed=1, blendin=10)
  315. super(WalkHang, self).Enter()
  316. def Execute(self):
  317. self.FSM.stateLife += 1
  318. self.FSM.owner.obj.linearVelocity = [0,0,0]
  319. #self.FSM.owner.arm.playAction('c_braced_hang', 0, 36, layer=2, play_mode=0, speed=1, blendin=10)
  320. self.FSM.owner.arm.playAction('c_braced_hang', 36, 50, layer=1, play_mode=1, speed=1, blendin=10)
  321. if dict['lUD'] > .04:
  322. self.FSM.ToTransition('toWalkAir')
  323. self.FSM.owner.move_walk_cam()
  324. if self.FSM.stateLife > 25:
  325. self.moveable = True
  326. if self.moveable:
  327. if dict['lUD'] < -.04:
  328. self.FSM.ToTransition('toWalkClimb')
  329. self.FSM.owner.hang_move()
  330. ground_rays = self.FSM.owner.get_ground_ray()
  331. if ground_rays[0][0] != None:
  332. dist = self.FSM.owner.get_ground_dist(ground_rays)
  333. if dist < .4:
  334. self.FSM.ToTransition('toWalkLand')
  335. hang_ray = self.FSM.owner.get_hang_ray()
  336. hang_align_ray = self.FSM.owner.get_hang_align_ray()
  337. #print(hang_align_ray, 'hang_align_ray')
  338. #self.FSM.owner.hang_move()
  339. if hang_ray[0] != None:
  340. hr_dist = self.FSM.owner.get_hang_dist(hang_ray)
  341. print(hr_dist, 'hang dist')
  342. if hr_dist < .5:
  343. self.FSM.owner.obj.linearVelocity = [0,0,0]
  344. self.FSM.owner.obj.worldPosition.z = hang_ray[1].z - 1
  345. if hang_align_ray[0] != None:
  346. print(hang_align_ray[0])
  347. v = hang_align_ray[2]
  348. self.FSM.owner.obj.alignAxisToVect(v, 0, .5)
  349. else:
  350. self.FSM.ToTransition('toWalkAir')
  351. if hang_align_ray[0] == None:
  352. self.FSM.ToTransition('toWalkAir')
  353. self.FSM.owner.align_walk_z()
  354. def Exit(self):
  355. pass
  356. #====================================
  357. class WalkClimb(State):
  358. def __init__(self,FSM):
  359. super(WalkClimb, self).__init__(FSM)
  360. def Enter(self):
  361. self.FSM.stateLife = 1
  362. self.FSM.owner.arm.playAction('c_wallclimb', 1, 50, layer=3, play_mode=0, speed=1, blendin=10)
  363. super(WalkClimb, self).Enter()
  364. def Execute(self):
  365. self.FSM.stateLife += 1
  366. #self.FSM.ToTransition('toLand')
  367. if self.FSM.stateLife > 25:
  368. self.FSM.owner.obj.applyForce([-300, 0, 80], True)
  369. else:
  370. self.FSM.owner.obj.applyForce([-20, 0, 80], True)
  371. if self.FSM.stateLife > 35:
  372. self.FSM.ToTransition('toWalkLand')
  373. def Exit(self):
  374. pass
  375. #====================================
  376. class WalkHurdle(State):
  377. def __init__(self,FSM):
  378. super(WalkHurdle, self).__init__(FSM)
  379. def Enter(self):
  380. self.FSM.stateLife = 1
  381. super(WalkHurdle, self).Enter()
  382. def Execute(self):
  383. self.FSM.stateLife += 1
  384. #self.FSM.ToTransition('toLand')
  385. def Exit(self):
  386. pass
  387. #====================================
  388. class Example(State):
  389. def __init__(self,FSM):
  390. super(Example, self).__init__(FSM)
  391. def Enter(self):
  392. self.FSM.stateLife = 1
  393. super(Example, self).Enter()
  394. def Execute(self):
  395. self.FSM.stateLife += 1
  396. #self.FSM.ToTransition('toExample')
  397. def Exit(self):
  398. pass
  399. #====================================
  400. class Example(State):
  401. def __init__(self,FSM):
  402. super(Example, self).__init__(FSM)
  403. def Enter(self):
  404. self.FSM.stateLife = 1
  405. super(Example, self).Enter()
  406. def Execute(self):
  407. self.FSM.stateLife += 1
  408. #self.FSM.ToTransition('toExample')
  409. def Exit(self):
  410. pass
  411. #====================================
  412. class Example(State):
  413. def __init__(self,FSM):
  414. super(Example, self).__init__(FSM)
  415. def Enter(self):
  416. self.FSM.stateLife = 1
  417. super(Example, self).Enter()
  418. def Execute(self):
  419. self.FSM.stateLife += 1
  420. #self.FSM.ToTransition('toExample')
  421. def Exit(self):
  422. pass
  423. #====================================
  424. class Roll(State):
  425. def __init__(self,FSM):
  426. super(Roll, self).__init__(FSM)
  427. def Enter(self):
  428. self.FSM.stateLife = 1
  429. self.FSM.owner.walking = False
  430. self.FSM.owner.obj['getonboard'] = False
  431. dict['walking'] = False
  432. super(Roll, self).Enter()
  433. def Execute(self):
  434. self.FSM.stateLife += 1
  435. if dict['walk'] == 1 or self.FSM.owner.obj['getoffboard'] == True:
  436. dict['walk'] = 1
  437. self.FSM.owner.obj['getoffboard'] = True
  438. self.FSM.ToTransition('toWalk')
  439. print('fsm to walk')
  440. if self.FSM.owner.obj['fall'] == True:
  441. self.FSM.ToTransition('toRagdoll')
  442. print('rolling')
  443. #self.FSM.ToTransition('toLand')
  444. def Exit(self):
  445. pass
  446. #====================================
  447. class Ragdoll(State):
  448. def __init__(self,FSM):
  449. super(Ragdoll, self).__init__(FSM)
  450. def Enter(self):
  451. self.FSM.stateLife = 1
  452. super(Ragdoll, self).Enter()
  453. def Execute(self):
  454. self.FSM.stateLife += 1
  455. print('ragdolling')
  456. if dict['yBut'] == 1:
  457. self.FSM.owner.obj['fall'] = False
  458. self.FSM.owner.cont.activate(self.FSM.owner.cont.actuators['walk'])
  459. self.FSM.ToTransition('toWalk')
  460. def Exit(self):
  461. pass
  462. #====================================