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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import bge
  2. import math
  3. from random import randint
  4. from time import clock
  5. dict = bge.logic.globalDict
  6. def get_request_state(cur_state):
  7. if dict['cam_state'] != cur_state:
  8. print(cur_state)
  9. print('change cam')
  10. return 'to' + str(dict['cam_state'])
  11. else:
  12. return None
  13. #camera - (Target Object, Min Distance, Max Distance, Height)
  14. def activate_camera(cobj, cmin, cmax, cheight):
  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. if self.FSM.stateLife > 60:
  80. dict = bge.logic.globalDict
  81. own = dict['p1c']
  82. scene = bge.logic.getCurrentScene()
  83. #dict['camera'].object = scene.objects['camobj']
  84. if own['grinding'] == True:
  85. ce = scene.objects['cam_helper2']
  86. ce.worldPosition = own.worldPosition
  87. ce.worldOrientation = own.worldOrientation
  88. #ce.applyMovement([-1,0,0], True)
  89. #activate_camera('camCube', 1.5, 3, 2)
  90. fliplist = ['reg_tailslide', 'reg_noseslide', 'reg_bsboard', 'fak_tailslide', 'fak_noseslide', 'fak_bsboard']
  91. #fliplist = []
  92. stre = .02
  93. local = own.worldOrientation.inverted() * (dict['camera'].worldPosition - own.worldPosition)
  94. if local.y > 0:
  95. rot = 'right'
  96. else:
  97. rot = 'left'
  98. print(own['requestAction'])
  99. yval = 0
  100. if own['stance'] == True:
  101. xval = -1
  102. else:
  103. xval = 1
  104. if rot == 'right':
  105. yval = 1
  106. if own['requestAction'] in fliplist:
  107. xval *= -1
  108. #print('flipping')
  109. dict['camera'].worldPosition = dict['camera'].worldPosition.lerp(ce.worldPosition, stre)
  110. else:
  111. yval = -1
  112. if own['requestAction'] in fliplist:
  113. xval *= -1
  114. print('flipping')
  115. ce.applyMovement([xval,yval,0], True)
  116. dict['camera'].worldPosition = dict['camera'].worldPosition.lerp(ce.worldPosition, stre)
  117. new_state = get_request_state(self.__class__.__name__)
  118. if new_state:
  119. self.FSM.ToTransition(new_state)
  120. print('call new state')
  121. def Exit(self):
  122. print('Finished RollCaming')
  123. class RagdollCam(State):
  124. def __init__(self,FSM):
  125. super(RagdollCam, self).__init__(FSM)
  126. def Enter(self):
  127. print('Preparing to RagdollCam state.')
  128. self.FSM.stateLife = 1
  129. super(RagdollCam, self).Enter()
  130. def Execute(self):
  131. #print('Vacumming.', self.FSM.stateLife)
  132. self.FSM.stateLife += 1
  133. duration = 6
  134. new_state = get_request_state(self.__class__.__name__)
  135. if new_state:
  136. self.FSM.ToTransition(new_state)
  137. print('call new state')
  138. def Exit(self):
  139. print('Finished RagdollCaming')
  140. class PauseCam(State):
  141. def __init__(self,FSM):
  142. super(PauseCam, self).__init__(FSM)
  143. def Enter(self):
  144. print('Starting to PauseCam.')
  145. super(PauseCam, self).Enter()
  146. self.FSM.stateLife = 1
  147. def Execute(self):
  148. #print('PauseCaming.', self.FSM.stateLife)
  149. self.FSM.stateLife += 1
  150. new_state = get_request_state(self.__class__.__name__)
  151. if new_state != None:
  152. self.FSM.ToTransition(new_state)
  153. print('call new state')
  154. def Exit(self):
  155. print('Finished PauseCaming.')
  156. class PauseIdleCam(State):
  157. def __init__(self,FSM):
  158. super(PauseIdleCam, self).__init__(FSM)
  159. def Enter(self):
  160. print('Starting to PauseIdleCam.')
  161. super(PauseIdleCam, self).Enter()
  162. self.FSM.stateLife = 1
  163. def Execute(self):
  164. #print('PauseIdleCaming.', self.FSM.stateLife)
  165. self.FSM.stateLife += 1
  166. new_state = get_request_state(self.__class__.__name__)
  167. if new_state != None:
  168. self.FSM.ToTransition(new_state)
  169. print('call new state')
  170. def Exit(self):
  171. print('Finished PauseIdleCaming.')
  172. #===================================
  173. class FSM(object):
  174. def __init__ (self, character):
  175. self.char = character
  176. self.states = {}
  177. self.transitions = {}
  178. self.curState = None
  179. self.prevState = None
  180. self.trans = None
  181. self.stateLife = 0
  182. def AddTransition(self, transName, transition):
  183. self.transitions[transName] = transition
  184. def AddState(self, stateName, state):
  185. self.states[stateName] = state
  186. def SetState(self, stateName):
  187. self.prevState = self.curState
  188. self.curState = self.states[stateName]
  189. def ToTransition(self, toTrans):
  190. self.trans = self.transitions[toTrans]
  191. def Execute(self):
  192. if (self.trans):
  193. self.curState.Exit()
  194. self.trans.Execute()
  195. self.SetState(self.trans.toState)
  196. self.curState.Enter()
  197. self.trans = None
  198. self.curState.Execute()
  199. #====================================
  200. Char = type("Char",(object,),{})
  201. class CameraFSM(Char):
  202. def __init__(self):
  203. self.FSM = FSM(self)
  204. ##STATES
  205. self.FSM.AddState('WalkCam', WalkCam(self.FSM))
  206. self.FSM.AddState('RollCam', RollCam(self.FSM))
  207. self.FSM.AddState('RagdollCam', RagdollCam(self.FSM))
  208. self.FSM.AddState("PauseCam", PauseCam(self.FSM))
  209. self.FSM.AddState("PauseIdleCam", PauseIdleCam(self.FSM))
  210. #TRANSITIONS
  211. self.FSM.AddTransition('toPauseCam', Transition('PauseCam'))
  212. self.FSM.AddTransition('toPauseIdleCam', Transition('PauseIdleCam'))
  213. self.FSM.AddTransition('toWalkCam', Transition('WalkCam'))
  214. self.FSM.AddTransition('toRollCam', Transition('RollCam'))
  215. self.FSM.AddTransition('toRagdollCam', Transition('RagdollCam'))
  216. if self.FSM.curState == None:
  217. self.FSM.SetState('WalkCam')
  218. print('setting none')
  219. def Execute(self):
  220. self.FSM.Execute()
  221. #====================================
  222. machine = CameraFSM()
  223. def main(cont):
  224. own = cont.owner
  225. if 'FSMinited' not in own:
  226. own['FSMinited'] = True
  227. #own['frame'] = 0
  228. #own['state'] = 'On'
  229. print('FSMiniting')
  230. machine.Execute()
  231. #own['frame'] += 1