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

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