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.1KB

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