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.

actionsFSM.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import bge
  2. from random import randint
  3. import actionsFSMlist
  4. MAIN_LAYER = 10
  5. TRANS_LAYER = 12
  6. def getOwn():
  7. cont = bge.logic.getCurrentController()
  8. own = cont.owner
  9. return own
  10. def transPlaying():
  11. scene = bge.logic.getCurrentScene()
  12. skater = scene.objects['Char4']
  13. if skater.isPlayingAction(TRANS_LAYER):
  14. return True
  15. else:
  16. return False
  17. def stopTrans():
  18. scene = bge.logic.getCurrentScene()
  19. skater = scene.objects['Char4']
  20. if skater.isPlayingAction(TRANS_LAYER):
  21. skater.stopAction(TRANS_LAYER)
  22. def getCurFrame():
  23. scene = bge.logic.getCurrentScene()
  24. skater = scene.objects['Char4']
  25. if skater.isPlayingAction(TRANS_LAYER):
  26. num = round(skater.getActionFrame(TRANS_LAYER), 3)
  27. print('playing frame ', num)
  28. return num
  29. else:
  30. return None
  31. def interupt_request():
  32. cont = bge.logic.getCurrentController()
  33. own = cont.owner
  34. print(own['requestAction'])
  35. if own['requestAction'] in actionsFSMlist.reg_fliplist or own['requestAction'] in actionsFSMlist.fak_fliplist:
  36. print('flip is true')
  37. return True
  38. else:
  39. return False
  40. def actionPlayer(ac, FSM):
  41. scene = bge.logic.getCurrentScene()
  42. skater = scene.objects['Char4']
  43. deck = scene.objects['deck']
  44. trucks = scene.objects['trucks']
  45. name = ac.FSM.action.s_action
  46. start = ac.FSM.action.s_se[0]
  47. end = ac.FSM.action.s_se[1]
  48. layer = ac.FSM.action.layer
  49. mode = ac.FSM.action.mode
  50. speed = ac.FSM.action.speed
  51. skater.playAction(name, start,end, layer=layer, play_mode=mode, speed=speed)
  52. def transEndPlayer(ac, FSM):
  53. scene = bge.logic.getCurrentScene()
  54. skater = scene.objects['Char4']
  55. deck = scene.objects['deck']
  56. trucks = scene.objects['trucks']
  57. name = ac.FSM.action.s_action
  58. end = ac.FSM.action.s_se[1]
  59. skater.playAction(name, end,end, layer=TRANS_LAYER, play_mode=1, speed=1)
  60. #====================================
  61. class Transition(object):
  62. def __init__(self, toState):
  63. self.toState = toState
  64. def Execute(self):
  65. print('Transitioning ...', self.toState)
  66. #====================================
  67. #====================================
  68. #====================================
  69. #====================================
  70. #====================================
  71. #====================================
  72. State = type("State", (object,), {})
  73. class State(object):
  74. def __init__(self, FSM):
  75. self.FSM = FSM
  76. self.timer = 0
  77. self.startTime = 0
  78. def Enter(self):
  79. self.timer = randint(0,5)
  80. self.startTime = 0
  81. def Execute(self):
  82. print('Executing')
  83. def Exit(self):
  84. print('Exiting')
  85. #====================================
  86. #====================================
  87. class reg_roll(State):
  88. def __init__(self,FSM):
  89. super(reg_roll, self).__init__(FSM)
  90. def Enter(self):
  91. print('entering: ', type(self).__name__)
  92. self.FSM.action = getattr(actionsFSMlist, type(self).__name__)
  93. super(reg_roll, self).Enter()
  94. def Execute(self):
  95. print('executing: ', type(self).__name__)
  96. self.FSM.stateLife += 1
  97. actionPlayer(self, FSM)
  98. own = getOwn()
  99. if own['requestAction'] == 'reg_opos':
  100. self.FSM.ToTransition('toreg_roll_to_opos')
  101. if own['requestAction'] == 'reg_turnLeft':
  102. self.FSM.ToTransition('toreg_roll_to_left')
  103. def Exit(self):
  104. print('exiting: ', type(self).__name__)
  105. class reg_opos(State):
  106. def __init__(self,FSM):
  107. super(reg_opos, self).__init__(FSM)
  108. def Enter(self):
  109. print('entering: ', type(self).__name__)
  110. self.FSM.action = getattr(actionsFSMlist, type(self).__name__)
  111. super(reg_opos, self).Enter()
  112. def Execute(self):
  113. print('executing: ', type(self).__name__)
  114. self.FSM.stateLife += 1
  115. dict = bge.logic.globalDict
  116. actionPlayer(self, FSM)
  117. own = getOwn()
  118. if own['requestAction'] != 'reg_opos':
  119. self.FSM.ToTransition(self.FSM.action.parentTrans)
  120. def Exit(self):
  121. print('exiting: ', type(self).__name__)
  122. class reg_left(State):
  123. def __init__(self,FSM):
  124. super(reg_left, self).__init__(FSM)
  125. def Enter(self):
  126. print('entering: ', type(self).__name__)
  127. self.FSM.action = getattr(actionsFSMlist, type(self).__name__)
  128. super(reg_left, self).Enter()
  129. def Execute(self):
  130. print('executing: ', type(self).__name__)
  131. self.FSM.stateLife += 1
  132. actionPlayer(self, FSM)
  133. own = getOwn()
  134. if own['requestAction'] != self.FSM.action.requestAction:
  135. self.FSM.ToTransition(self.FSM.action.parentTrans)
  136. print('initing left')
  137. def Exit(self):
  138. print('exiting: ', type(self).__name__)
  139. #====================================
  140. #====================================
  141. class reg_roll_to_opos(State):
  142. def __init__(self,FSM):
  143. super(reg_roll_to_opos, self).__init__(FSM)
  144. def Enter(self):
  145. print('entering: ', type(self).__name__)
  146. self.FSM.action = getattr(actionsFSMlist, type(self).__name__)
  147. super(reg_roll_to_opos, self).Enter()
  148. def Execute(self):
  149. print('executing: ', type(self).__name__)
  150. dict = bge.logic.globalDict
  151. self.FSM.stateLife += 1
  152. own = getOwn()
  153. if own['requestAction'] == 'reg_roll':
  154. print('requesting early out')
  155. self.FSM.ToTransition('toreg_opos_to_roll')
  156. if transPlaying() == False and self.FSM.stateLife > 2:
  157. self.FSM.ToTransition(self.FSM.action.parentTrans)
  158. transEndPlayer(self, FSM)
  159. else:
  160. actionPlayer(self, FSM)
  161. def Exit(self):
  162. print('exiting: ', type(self).__name__)
  163. class reg_opos_to_roll(State):
  164. def __init__(self,FSM):
  165. super(reg_opos_to_roll, self).__init__(FSM)
  166. def Enter(self):
  167. print('entering: ', type(self).__name__)
  168. self.FSM.action = getattr(actionsFSMlist, type(self).__name__)
  169. super(reg_opos_to_roll, self).Enter()
  170. old_state = self.FSM.prevState
  171. if getCurFrame() != None:
  172. print('do the things', old_state.FSM.currentFrame, getCurFrame())
  173. self.FSM.action.s_se[0] = getCurFrame()
  174. def Execute(self):
  175. print('executing: ', type(self).__name__)
  176. self.FSM.stateLife += 1
  177. if transPlaying() == False and self.FSM.stateLife > 2:
  178. self.FSM.ToTransition('toreg_roll')
  179. transEndPlayer(self, FSM)
  180. else:
  181. actionPlayer(self, FSM)
  182. def Exit(self):
  183. print('exiting: ', type(self).__name__)
  184. class reg_roll_to_left(State):
  185. def __init__(self,FSM):
  186. super(reg_roll_to_left, self).__init__(FSM)
  187. def Enter(self):
  188. print('entering: ', type(self).__name__)
  189. self.FSM.action = getattr(actionsFSMlist, type(self).__name__)
  190. super(reg_roll_to_left, self).Enter()
  191. old_state = self.FSM.prevState
  192. if getCurFrame() != None:
  193. print('current frame is', getCurFrame())
  194. self.FSM.action.s_se[0] = getCurFrame()
  195. def Execute(self):
  196. print('executing: ', type(self).__name__)
  197. dict = bge.logic.globalDict
  198. self.FSM.stateLife += 1
  199. own = getOwn()
  200. if own['requestAction'] == 'reg_roll':
  201. self.FSM.ToTransition('toreg_left_to_roll')
  202. if transPlaying() == False and self.FSM.stateLife > 2:
  203. self.FSM.ToTransition('toreg_left')
  204. transEndPlayer(self, FSM)
  205. else:
  206. actionPlayer(self, FSM)
  207. def Exit(self):
  208. print('exiting: ', type(self).__name__)
  209. class reg_left_to_roll(State):
  210. def __init__(self,FSM):
  211. super(reg_left_to_roll, self).__init__(FSM)
  212. def Enter(self):
  213. print('entering: ', type(self).__name__)
  214. self.FSM.action = getattr(actionsFSMlist, type(self).__name__)
  215. super(reg_left_to_roll, self).Enter()
  216. old_state = self.FSM.prevState
  217. if getCurFrame() != None:
  218. print('do the things', old_state.FSM.currentFrame, getCurFrame())
  219. self.FSM.action.s_se[0] = getCurFrame()
  220. def Execute(self):
  221. print('executing: ', type(self).__name__)
  222. dict = bge.logic.globalDict
  223. self.FSM.stateLife += 1
  224. if transPlaying() == False and self.FSM.stateLife > 2:
  225. self.FSM.ToTransition('toreg_roll')
  226. transEndPlayer(self, FSM)
  227. else:
  228. actionPlayer(self, FSM)
  229. def Exit(self):
  230. print('exiting: ', type(self).__name__)
  231. #===================================
  232. #====================================
  233. class FSM(object):
  234. def __init__ (self, character):
  235. try:
  236. self.char = self.char
  237. except:
  238. self.char = character
  239. self.states = {}
  240. self.transitions = {}
  241. self.curState = None
  242. self.prevState = None
  243. self.stateLife = 0
  244. self.currentFrame = 0.0
  245. self.name = ''
  246. self.action = []
  247. print('always init')
  248. def AddTransition(self, transName, transition):
  249. self.transitions[transName] = transition
  250. def AddState(self, stateName, state):
  251. self.states[stateName] = state
  252. def SetState(self, stateName):
  253. self.prevState = self.curState
  254. self.curState = self.states[stateName]
  255. def ToTransition(self, toTrans):
  256. self.trans = self.transitions[toTrans]
  257. def Execute(self):
  258. if (self.trans):
  259. self.curState.Exit()
  260. self.trans.Execute()
  261. self.SetState(self.trans.toState)
  262. self.curState.Enter()
  263. self.trans = None
  264. self.curState.Execute()
  265. #====================================
  266. Char = type("Char",(object,),{})
  267. class StateMachine(Char):
  268. def __init__(self):
  269. self.FSM = FSM(self)
  270. ##STATES
  271. self.FSM.AddState('reg_roll', reg_roll(self.FSM))
  272. self.FSM.AddState('reg_opos', reg_opos(self.FSM))
  273. self.FSM.AddState('reg_roll_to_opos', reg_roll_to_opos(self.FSM))
  274. self.FSM.AddState('reg_opos_to_roll', reg_opos_to_roll(self.FSM))
  275. self.FSM.AddState('reg_left', reg_left(self.FSM))
  276. self.FSM.AddState('reg_left_to_roll', reg_left_to_roll(self.FSM))
  277. self.FSM.AddState('reg_roll_to_left', reg_roll_to_left(self.FSM))
  278. #TRANSITIONS
  279. self.FSM.AddTransition('toreg_roll', Transition('reg_roll'))
  280. self.FSM.AddTransition('toreg_opos', Transition('reg_opos'))
  281. self.FSM.AddTransition('toreg_roll_to_opos', Transition('reg_roll_to_opos'))
  282. self.FSM.AddTransition('toreg_opos_to_roll', Transition('reg_opos_to_roll'))
  283. self.FSM.AddTransition('toreg_left', Transition('reg_left'))
  284. self.FSM.AddTransition('toreg_left_to_roll', Transition('reg_left_to_roll'))
  285. self.FSM.AddTransition('toreg_roll_to_left', Transition('reg_roll_to_left'))
  286. if self.FSM.curState == None:
  287. self.FSM.SetState('reg_roll')
  288. self.FSM.ToTransition('toreg_roll')
  289. print(' -= Initializing Action Player State Machine =-')
  290. def Execute(self):
  291. self.FSM.Execute()
  292. #====================================
  293. r = StateMachine()
  294. def main(cont):
  295. own = cont.owner
  296. if 'inited' not in own:
  297. own['inited'] = True
  298. own['frame'] = 0
  299. own['state'] = 'On'
  300. print('initing')
  301. r.Execute()
  302. own['frame'] += 1
  303. if interupt_request():
  304. print('interupt request')