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.

camFSM.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import bge
  2. from random import randint
  3. from time import clock
  4. dict = bge.logic.globalDict
  5. def get_request_state(cur_state):
  6. if dict['cam_state'] != cur_state:
  7. print(cur_state)
  8. print('change cam')
  9. return 'to' + str(dict['cam_state'])
  10. else:
  11. return None
  12. #camera - (Target Object, Min Distance, Max Distance, Height)
  13. def activate_camera(cobj, cmin, cmax, cheight):
  14. pass
  15. # cont = bge.logic.getCurrentController()
  16. # scene = bge.logic.getCurrentScene()
  17. # camActu = cont.actuators['Camera']
  18. # camActu.object = scene.objects[cobj]
  19. # camActu.min = cmin
  20. # camActu.max = cmax
  21. # camActu.height = cheight
  22. # cont.activate(camActu)
  23. #====================================
  24. class Transition(object):
  25. def __init__(self, toState):
  26. self.toState = toState
  27. def Execute(self):
  28. print('Transitioning ...', self.toState)
  29. #====================================
  30. State = type("State", (object,), {})
  31. class State(object):
  32. def __init__(self, FSM):
  33. self.FSM = FSM
  34. self.timer = 0
  35. self.startTime = 0
  36. #cam settings
  37. self.target = None
  38. self.min = 0
  39. self.max = 0
  40. def Enter(self):
  41. self.timer = randint(0,5)
  42. self.startTime = int(clock())
  43. def Execute(self):
  44. print('Executing')
  45. def Exit(self):
  46. print('Exiting')
  47. class WalkCam(State):
  48. def __init__(self,FSM):
  49. super(WalkCam, self).__init__(FSM)
  50. self.min = 4
  51. self.max = 5
  52. self.target = 'player'
  53. def Enter(self):
  54. print('Preparing to enter walk cam state.')
  55. self.FSM.stateLife = 1
  56. super(WalkCam, self).Enter()
  57. def Execute(self):
  58. #print('walk cam state', self.FSM.stateLife)
  59. self.FSM.stateLife += 1
  60. duration = 50
  61. #activate_camera('camCube', 2, 3, 1)
  62. new_state = get_request_state(self.__class__.__name__)
  63. if new_state:
  64. self.FSM.ToTransition(new_state)
  65. print('call new state')
  66. def Exit(self):
  67. print('walk cam state')
  68. class RollCam(State):
  69. def __init__(self,FSM):
  70. super(RollCam, self).__init__(FSM)
  71. def Enter(self):
  72. print('Preparing to enter roll cam state')
  73. self.FSM.stateLife = 1
  74. super(RollCam, self).Enter()
  75. def Execute(self):
  76. #print('Eating Breakfast.', self.FSM.stateLife)
  77. self.FSM.stateLife += 1
  78. duration = 500
  79. #activate_camera('camCube', 1.5, 3, 2)
  80. new_state = get_request_state(self.__class__.__name__)
  81. if new_state:
  82. self.FSM.ToTransition(new_state)
  83. print('call new state')
  84. def Exit(self):
  85. print('Finished RollCaming')
  86. class RagdollCam(State):
  87. def __init__(self,FSM):
  88. super(RagdollCam, self).__init__(FSM)
  89. def Enter(self):
  90. print('Preparing to RagdollCam state.')
  91. self.FSM.stateLife = 1
  92. super(RagdollCam, self).Enter()
  93. def Execute(self):
  94. #print('Vacumming.', self.FSM.stateLife)
  95. self.FSM.stateLife += 1
  96. duration = 6
  97. new_state = get_request_state(self.__class__.__name__)
  98. if new_state:
  99. self.FSM.ToTransition(new_state)
  100. print('call new state')
  101. def Exit(self):
  102. print('Finished RagdollCaming')
  103. class PauseCam(State):
  104. def __init__(self,FSM):
  105. super(PauseCam, self).__init__(FSM)
  106. def Enter(self):
  107. print('Starting to PauseCam.')
  108. super(PauseCam, self).Enter()
  109. self.FSM.stateLife = 1
  110. def Execute(self):
  111. print('PauseCaming.', self.FSM.stateLife)
  112. self.FSM.stateLife += 1
  113. new_state = get_request_state(self.__class__.__name__)
  114. if new_state != None:
  115. self.FSM.ToTransition(new_state)
  116. print('call new state')
  117. def Exit(self):
  118. print('Finished PauseCaming.')
  119. class PauseIdleCam(State):
  120. def __init__(self,FSM):
  121. super(PauseIdleCam, self).__init__(FSM)
  122. def Enter(self):
  123. print('Starting to PauseIdleCam.')
  124. super(PauseCam, self).Enter()
  125. self.FSM.stateLife = 1
  126. def Execute(self):
  127. print('PauseIdleCaming.', self.FSM.stateLife)
  128. self.FSM.stateLife += 1
  129. new_state = get_request_state(self.__class__.__name__)
  130. if new_state != None:
  131. self.FSM.ToTransition(new_state)
  132. print('call new state')
  133. def Exit(self):
  134. print('Finished PauseIdleCaming.')
  135. #===================================
  136. class FSM(object):
  137. def __init__ (self, character):
  138. self.char = character
  139. self.states = {}
  140. self.transitions = {}
  141. self.curState = None
  142. self.prevState = None
  143. self.trans = None
  144. self.stateLife = 0
  145. def AddTransition(self, transName, transition):
  146. self.transitions[transName] = transition
  147. def AddState(self, stateName, state):
  148. self.states[stateName] = state
  149. def SetState(self, stateName):
  150. self.prevState = self.curState
  151. self.curState = self.states[stateName]
  152. def ToTransition(self, toTrans):
  153. self.trans = self.transitions[toTrans]
  154. def Execute(self):
  155. if (self.trans):
  156. self.curState.Exit()
  157. self.trans.Execute()
  158. self.SetState(self.trans.toState)
  159. self.curState.Enter()
  160. self.trans = None
  161. self.curState.Execute()
  162. #====================================
  163. Char = type("Char",(object,),{})
  164. class CameraFSM(Char):
  165. def __init__(self):
  166. self.FSM = FSM(self)
  167. ##STATES
  168. self.FSM.AddState('WalkCam', WalkCam(self.FSM))
  169. self.FSM.AddState('RollCam', RollCam(self.FSM))
  170. self.FSM.AddState('RagdollCam', RagdollCam(self.FSM))
  171. self.FSM.AddState("PauseCam", PauseCam(self.FSM))
  172. self.FSM.AddState("PauseIdleCam", PauseIdleCam(self.FSM))
  173. #TRANSITIONS
  174. self.FSM.AddTransition('toPauseCam', Transition('PauseCam'))
  175. self.FSM.AddTransition('toPauseIdleCam', Transition('PauseIdleCam'))
  176. self.FSM.AddTransition('toWalkCam', Transition('WalkCam'))
  177. self.FSM.AddTransition('toRollCam', Transition('RollCam'))
  178. self.FSM.AddTransition('toRagdollCam', Transition('RagdollCam'))
  179. if self.FSM.curState == None:
  180. self.FSM.SetState('WalkCam')
  181. print('setting none')
  182. def Execute(self):
  183. self.FSM.Execute()
  184. #====================================
  185. machine = CameraFSM()
  186. def main(cont):
  187. own = cont.owner
  188. if 'FSMinited' not in own:
  189. own['FSMinited'] = True
  190. #own['frame'] = 0
  191. #own['state'] = 'On'
  192. print('FSMiniting')
  193. machine.Execute()
  194. #own['frame'] += 1