123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- import bge
- from random import randint
- from time import clock
- dict = bge.logic.globalDict
-
-
- def get_request_state(cur_state):
- if dict['cam_state'] != cur_state:
- print(cur_state)
- print('change cam')
- return 'to' + str(dict['cam_state'])
- else:
- return None
-
- #camera - (Target Object, Min Distance, Max Distance, Height)
- def activate_camera(cobj, cmin, cmax, cheight):
- pass
- # cont = bge.logic.getCurrentController()
- # scene = bge.logic.getCurrentScene()
- # camActu = cont.actuators['Camera']
- # camActu.object = scene.objects[cobj]
- # camActu.min = cmin
- # camActu.max = cmax
- # camActu.height = cheight
- # cont.activate(camActu)
-
-
-
- #====================================
- class Transition(object):
- def __init__(self, toState):
- self.toState = toState
- def Execute(self):
- print('Transitioning ...', self.toState)
-
-
-
- #====================================
- State = type("State", (object,), {})
-
- class State(object):
- def __init__(self, FSM):
- self.FSM = FSM
- self.timer = 0
- self.startTime = 0
- #cam settings
- self.target = None
- self.min = 0
- self.max = 0
-
- def Enter(self):
- self.timer = randint(0,5)
- self.startTime = int(clock())
- def Execute(self):
- print('Executing')
- def Exit(self):
- print('Exiting')
-
- class WalkCam(State):
- def __init__(self,FSM):
- super(WalkCam, self).__init__(FSM)
- self.min = 4
- self.max = 5
- self.target = 'player'
-
- def Enter(self):
- print('Preparing to enter walk cam state.')
- self.FSM.stateLife = 1
- super(WalkCam, self).Enter()
-
- def Execute(self):
- #print('walk cam state', self.FSM.stateLife)
- self.FSM.stateLife += 1
- duration = 50
- #activate_camera('camCube', 2, 3, 1)
- new_state = get_request_state(self.__class__.__name__)
- if new_state:
- self.FSM.ToTransition(new_state)
- print('call new state')
-
- def Exit(self):
- print('walk cam state')
-
- class RollCam(State):
- def __init__(self,FSM):
- super(RollCam, self).__init__(FSM)
-
- def Enter(self):
- print('Preparing to enter roll cam state')
- self.FSM.stateLife = 1
- super(RollCam, self).Enter()
-
- def Execute(self):
- #print('Eating Breakfast.', self.FSM.stateLife)
- self.FSM.stateLife += 1
- duration = 500
- #activate_camera('camCube', 1.5, 3, 2)
- new_state = get_request_state(self.__class__.__name__)
- if new_state:
- self.FSM.ToTransition(new_state)
- print('call new state')
-
- def Exit(self):
- print('Finished RollCaming')
-
- class RagdollCam(State):
- def __init__(self,FSM):
- super(RagdollCam, self).__init__(FSM)
-
- def Enter(self):
- print('Preparing to RagdollCam state.')
- self.FSM.stateLife = 1
- super(RagdollCam, self).Enter()
-
- def Execute(self):
- #print('Vacumming.', self.FSM.stateLife)
- self.FSM.stateLife += 1
- duration = 6
- new_state = get_request_state(self.__class__.__name__)
- if new_state:
- self.FSM.ToTransition(new_state)
- print('call new state')
-
- def Exit(self):
- print('Finished RagdollCaming')
-
-
- class PauseCam(State):
-
- def __init__(self,FSM):
- super(PauseCam, self).__init__(FSM)
-
- def Enter(self):
- print('Starting to PauseCam.')
- super(PauseCam, self).Enter()
- self.FSM.stateLife = 1
-
- def Execute(self):
- print('PauseCaming.', self.FSM.stateLife)
- self.FSM.stateLife += 1
- new_state = get_request_state(self.__class__.__name__)
- if new_state != None:
- self.FSM.ToTransition(new_state)
- print('call new state')
-
- def Exit(self):
- print('Finished PauseCaming.')
-
- class PauseIdleCam(State):
-
- def __init__(self,FSM):
- super(PauseIdleCam, self).__init__(FSM)
-
- def Enter(self):
- print('Starting to PauseIdleCam.')
- super(PauseCam, self).Enter()
- self.FSM.stateLife = 1
-
- def Execute(self):
- print('PauseIdleCaming.', self.FSM.stateLife)
- self.FSM.stateLife += 1
- new_state = get_request_state(self.__class__.__name__)
- if new_state != None:
- self.FSM.ToTransition(new_state)
- print('call new state')
-
- def Exit(self):
- print('Finished PauseIdleCaming.')
-
- #===================================
-
-
- class FSM(object):
- def __init__ (self, character):
- self.char = character
- self.states = {}
- self.transitions = {}
- self.curState = None
- self.prevState = None
- self.trans = None
- self.stateLife = 0
-
- def AddTransition(self, transName, transition):
- self.transitions[transName] = transition
-
- def AddState(self, stateName, state):
- self.states[stateName] = state
-
- def SetState(self, stateName):
- self.prevState = self.curState
- self.curState = self.states[stateName]
-
- def ToTransition(self, toTrans):
- self.trans = self.transitions[toTrans]
-
- def Execute(self):
- if (self.trans):
- self.curState.Exit()
- self.trans.Execute()
- self.SetState(self.trans.toState)
- self.curState.Enter()
- self.trans = None
- self.curState.Execute()
-
- #====================================
- Char = type("Char",(object,),{})
-
- class CameraFSM(Char):
- def __init__(self):
- self.FSM = FSM(self)
-
- ##STATES
- self.FSM.AddState('WalkCam', WalkCam(self.FSM))
- self.FSM.AddState('RollCam', RollCam(self.FSM))
- self.FSM.AddState('RagdollCam', RagdollCam(self.FSM))
- self.FSM.AddState("PauseCam", PauseCam(self.FSM))
- self.FSM.AddState("PauseIdleCam", PauseIdleCam(self.FSM))
-
- #TRANSITIONS
- self.FSM.AddTransition('toPauseCam', Transition('PauseCam'))
- self.FSM.AddTransition('toPauseIdleCam', Transition('PauseIdleCam'))
- self.FSM.AddTransition('toWalkCam', Transition('WalkCam'))
- self.FSM.AddTransition('toRollCam', Transition('RollCam'))
- self.FSM.AddTransition('toRagdollCam', Transition('RagdollCam'))
-
- if self.FSM.curState == None:
- self.FSM.SetState('WalkCam')
- print('setting none')
-
- def Execute(self):
- self.FSM.Execute()
- #====================================
- machine = CameraFSM()
- def main(cont):
- own = cont.owner
-
- if 'FSMinited' not in own:
- own['FSMinited'] = True
- #own['frame'] = 0
- #own['state'] = 'On'
- print('FSMiniting')
-
- machine.Execute()
-
- #own['frame'] += 1
|