Browse Source

camera fsm

shuvit 5 years ago
parent
commit
fb95a581e5
1 changed files with 218 additions and 0 deletions
  1. 218
    0
      scripts/camFSM.py

+ 218
- 0
scripts/camFSM.py View File

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

Loading…
Cancel
Save