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

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