Browse Source

cars_walkers_observer

shuvit 4 years ago
parent
commit
651fb46616

+ 2
- 2
assets/ledges.blend View File

@@ -1,3 +1,3 @@
1 1
 version https://git-lfs.github.com/spec/v1
2
-oid sha256:1257f85158652bcb9e3538f71844c6d3e9aa76ff424f2b03bb97b08a07b88b26
3
-size 5655216
2
+oid sha256:f421d34a40921e1a91067a4d8eb7530d8bfe173b45cfe7a14452775011c9e4fe
3
+size 5657192

+ 1
- 0
assets/nav_points
File diff suppressed because it is too large
View File


+ 2
- 2
assets/roads.blend View File

@@ -1,3 +1,3 @@
1 1
 version https://git-lfs.github.com/spec/v1
2
-oid sha256:a6d14e8cd3b6eceba87234defa958a750191cb8f5dc8efe3b9389df7b146fb2b
3
-size 19630604
2
+oid sha256:de4ce42645721fec7b24aa432a1a5577514381c5247208a78f77a5cb001a2540
3
+size 19634124

+ 2
- 2
assets/user2.blend View File

@@ -1,3 +1,3 @@
1 1
 version https://git-lfs.github.com/spec/v1
2
-oid sha256:360113ba4041dd9ff4dd58cc407875bcd4497814a32b0aa6f220f1f357bd13e4
3
-size 5501496
2
+oid sha256:1f24f97dbd1be9394811c0c5ca1ef2f42a07da4fae255bad599ab261f214e50d
3
+size 6860928

+ 2
- 2
assets/user2_working.blend View File

@@ -1,3 +1,3 @@
1 1
 version https://git-lfs.github.com/spec/v1
2
-oid sha256:9624f238b3ac3dcf002347b568c66605adcdf8b88d952255e786787674b0d2f6
3
-size 4859740
2
+oid sha256:bf72e339505d14e43eaf460f09fc7e73886c311cf4be7f24c158dd17a45e5e90
3
+size 4888408

+ 3
- 0
assets/user3_working.blend View File

@@ -0,0 +1,3 @@
1
+version https://git-lfs.github.com/spec/v1
2
+oid sha256:53fad62065c766976d70ec955045e76943f9f0c40f2d600464a75616a82a0f22
3
+size 4970584

+ 1
- 0
assets/walker_nav_points
File diff suppressed because it is too large
View File


+ 3
- 3
config.ini View File

@@ -91,12 +91,12 @@ wheel4_b = 0.21
91 91
 ###################
92 92
 
93 93
 #brightness / contrast
94
-bc = 1
94
+bc = 0
95 95
 BC_BRIGHTNESS = 1.12
96 96
 BC_CONTRAST = 1.06
97 97
 
98 98
 #HDR
99
-hdr = 1
99
+hdr = 0
100 100
 avgL = 0.72
101 101
 HDRamount = 0.3
102 102
 
@@ -107,7 +107,7 @@ aowidth = 0.2
107 107
 aoradius = 2.4
108 108
 
109 109
 #Depth of Field
110
-dof_on = 1
110
+dof_on = 0
111 111
 
112 112
 #Bloom
113 113
 bloom_on = 0

+ 1
- 1
npc_walkers/larry.blend View File

@@ -1,3 +1,3 @@
1 1
 version https://git-lfs.github.com/spec/v1
2
-oid sha256:0df1002d71bf91ba07c9afbac1aa41360803340218100d6a91a32eff732b0c4b
2
+oid sha256:3f20d8d1e3b7b809089f2483347932b2d5d40e37ed3ae11df37f0981236b034e
3 3
 size 3687104

+ 22
- 11
scripts/FSM.py View File

@@ -1,6 +1,6 @@
1 1
 import bge
2 2
 
3
-import StatesNpcPed
3
+import StatesWalker
4 4
 import StatesCamera
5 5
 import StatesCar
6 6
    
@@ -55,8 +55,11 @@ class FSM(object):
55 55
 Char = type("Char",(object,),{})
56 56
 
57 57
 #===================================
58
+          
58 59
         
59
-class NpcPed(Char):
60
+#=================================== 
61
+
62
+class CameraFSM(Char):
60 63
     def __init__(self, owner):
61 64
         self.FSM = FSM(self, owner)
62 65
         self.owner = owner
@@ -65,7 +68,7 @@ class NpcPed(Char):
65 68
         'Example']
66 69
         
67 70
         for s in state_list:
68
-            self.FSM.AddState(s, getattr(StatesNpcPed, s)(self.FSM))
71
+            self.FSM.AddState(s, getattr(StatesCamera, s)(self.FSM))
69 72
             t = 'to' + s
70 73
             self.FSM.AddTransition(t, Transition(s))
71 74
         
@@ -74,19 +77,26 @@ class NpcPed(Char):
74 77
     
75 78
     def Execute(self):
76 79
         self.FSM.Execute(self.owner)    
77
-        
80
+
78 81
 #=================================== 
79 82
 
80
-class CameraFSM(Char):
83
+class CarFSM(Char):
81 84
     def __init__(self, owner):
82 85
         self.FSM = FSM(self, owner)
83 86
         self.owner = owner
87
+
84 88
         
85 89
         state_list = [
86
-        'Example']
90
+        'Example',
91
+        'Activate',
92
+        'ExitParallelPark',
93
+        'EnterParallelPark',
94
+        'NavigateToTarget', 
95
+        'RequestPath']
96
+
87 97
         
88 98
         for s in state_list:
89
-            self.FSM.AddState(s, getattr(StatesCamera, s)(self.FSM))
99
+            self.FSM.AddState(s, getattr(StatesCar, s)(self.FSM))
90 100
             t = 'to' + s
91 101
             self.FSM.AddTransition(t, Transition(s))
92 102
         
@@ -98,7 +108,7 @@ class CameraFSM(Char):
98 108
 
99 109
 #=================================== 
100 110
 
101
-class CarFSM(Char):
111
+class WalkerFSM(Char):
102 112
     def __init__(self, owner):
103 113
         self.FSM = FSM(self, owner)
104 114
         self.owner = owner
@@ -109,11 +119,12 @@ class CarFSM(Char):
109 119
         'Activate',
110 120
         'ExitParallelPark',
111 121
         'EnterParallelPark',
112
-        'NavigateToTarget']
122
+        'NavigateToTarget', 
123
+        'RequestPath']
113 124
 
114 125
         
115 126
         for s in state_list:
116
-            self.FSM.AddState(s, getattr(StatesCar, s)(self.FSM))
127
+            self.FSM.AddState(s, getattr(StatesWalker, s)(self.FSM))
117 128
             t = 'to' + s
118 129
             self.FSM.AddTransition(t, Transition(s))
119 130
         
@@ -121,4 +132,4 @@ class CarFSM(Char):
121 132
             self.FSM.SetState('Example')
122 133
     
123 134
     def Execute(self):
124
-        self.FSM.Execute(self.owner)    
135
+        self.FSM.Execute(self.owner)   

+ 48
- 22
scripts/StatesCar.py View File

@@ -59,21 +59,23 @@ def find_new_parking(self):
59 59
         if x.status == 'available':
60 60
             potentials.append(x)
61 61
     for x in potentials:
62
-        min_dist = 100
62
+        min_dist = 45
63 63
         dist = self.obj.getDistanceTo(x.obj)
64
+        #print('dist to potential', dist)
64 65
         if dist < min_dist:
65
-            #potentials.remove(x)
66
-            print('----removing potential')
66
+            potentials.remove(x)
67
+            #print('----removing potential')
67 68
     if len(potentials) > 0:
68 69
 
69 70
         new_parking = random.choice(potentials)
70
-        path = self.manager.navmesh.findPath(self.start_empty.obj.worldPosition, new_parking.obj.worldPosition)
71
-        print('parking added at', new_parking.obj.worldPosition)
72
-
73
-        mark_path(path, self)
74
-        return new_parking, path
71
+        #path = self.manager.navmesh.findPath(self.start_empty.obj.worldPosition, new_parking.obj.worldPosition)
72
+        path2 = self.manager.navmesh2.queue_path(self.start_empty.obj.worldPosition, new_parking.obj.worldPosition, self)
73
+        #print('parking added at', new_parking.obj.worldPosition)
74
+        #path.remove(path[0])
75
+        #mark_path(path, self)
76
+        return new_parking, path2
75 77
     else:
76
-        print('cant find parking for', self)
78
+        #print('cant find parking for', self)
77 79
         self.FSM.FSM.ToTransition('toEnterParallelPark')
78 80
 
79 81
 def get_lane_point(self):
@@ -85,10 +87,11 @@ def get_lane_point(self):
85 87
         self.last_lane_point = self.lane_point
86 88
         self.lane_point = self.point + self.manager.lane_position * nv
87 89
     else:
88
-        print('skipping lane point')
90
+        pass
91
+        #print('skipping lane point')
89 92
 
90 93
 def update_point(self):
91
-    print(self.path_index, 'path index', len(self.path))
94
+    #print(self.path_index, 'path index', len(self.path))
92 95
     if self.path_index >= (len(self.path) ):
93 96
         self.FSM.FSM.ToTransition('toEnterParallelPark')
94 97
     else:
@@ -102,6 +105,7 @@ def update_point(self):
102 105
                 self.path_index += 1
103 106
          
104 107
 def align_to_point(self):
108
+    #print('lane_point', self.lane_point)
105 109
     v = self.obj.getVectTo(self.lane_point)[1]
106 110
     v.z = 0
107 111
     self.obj.alignAxisToVect(v, 0, .1)
@@ -171,12 +175,12 @@ class ExitParallelPark(State):
171 175
         self.FSM.owner.obj.restorePhysics()
172 176
         self.FSM.owner.obj.restoreDynamics()
173 177
         self.FSM.owner.obj.linearVelocity = [0,0,0]
174
-        self.FSM.owner.target, self.FSM.owner.path = find_new_parking(self.FSM.owner)
178
+        # self.FSM.owner.target, self.FSM.owner.path = find_new_parking(self.FSM.owner)
175 179
         self.FSM.owner.path_index = 0
176 180
         self.FSM.owner.point = self.FSM.owner.path[self.FSM.owner.path_index]
177 181
         self.FSM.owner.target.status = 'targetted'
178 182
         self.FSM.owner.start_empty.status = 'available'
179
-        
183
+        #mark_path(self.FSM.owner.path, self.FSM.owner)
180 184
         print('physics resumed')
181 185
         super(ExitParallelPark, self).Enter()        
182 186
         
@@ -199,7 +203,7 @@ class EnterParallelPark(State):
199 203
         
200 204
     def Enter(self):
201 205
         self.FSM.stateLife = 1
202
-        print('entering parallel park')
206
+        #print('entering parallel park')
203 207
         self.FSM.owner.obj.worldPosition = self.FSM.owner.target.obj.worldPosition.copy()
204 208
         self.FSM.owner.obj.worldOrientation = self.FSM.owner.target.obj.worldOrientation.copy()
205 209
         self.FSM.owner.obj.applyMovement([0, -6, 0], True)
@@ -208,23 +212,25 @@ class EnterParallelPark(State):
208 212
         self.FSM.owner.active = False
209 213
         self.FSM.owner.start_empty = self.FSM.owner.target
210 214
         self.FSM.owner.last_point = self.FSM.owner.target.obj.worldPosition.copy()
211
-        self.FSM.owner.last_lane_point = self.FSM.owner.obj.worldPosition.copy()
215
+        self.FSM.owner.lane_point = self.FSM.owner.target.obj.worldPosition.copy()
216
+        self.FSM.owner.last_lane_point = self.FSM.owner.target.obj.worldPosition.copy()
212 217
         self.FSM.owner.point = self.FSM.owner.target.obj.worldPosition.copy()
213 218
         self.FSM.owner.path_index = 0
214 219
         self.FSM.owner.path = None
215
-        print('clearning last path')
220
+        #print('clearning last path')
216 221
         
217 222
         clear_markers(self)
218 223
         
219
-        self.FSM.owner.obj.suspendDynamics()
220
-        self.FSM.owner.obj.suspendPhysics()
224
+        #self.FSM.owner.obj.suspendDynamics()
225
+        #self.FSM.owner.obj.suspendPhysics()
221 226
         super(EnterParallelPark, self).Enter()        
222 227
         
223 228
     def Execute(self):
224 229
         self.FSM.stateLife += 1
225 230
         if self.FSM.stateLife == 2:
226 231
             self.FSM.owner.manager.cars_active.remove(self.FSM.owner)
227
-
232
+            self.FSM.owner.obj.suspendDynamics()
233
+            self.FSM.owner.obj.suspendPhysics()
228 234
     def Exit(self):
229 235
         pass
230 236
 
@@ -246,11 +252,11 @@ class NavigateToTarget(State):
246 252
         align_to_point(self.FSM.owner)
247 253
         align_to_road(self.FSM.owner)
248 254
         set_height(self.FSM.owner)
249
-        #delta_to_vect(self.FSM.owner)
255
+        delta_to_vect(self.FSM.owner)
250 256
         apply_gas(self.FSM.owner)
251 257
 
252 258
         #emergency exit
253
-        if self.FSM.stateLife > 30 * 100:
259
+        if self.FSM.stateLife > 30 * 60:
254 260
             self.FSM.ToTransition('toEnterParallelPark')
255 261
 
256 262
     def Exit(self):
@@ -366,4 +372,24 @@ class Activate(State):
366 372
     def Exit(self):
367 373
         pass
368 374
 
369
-#====================================  
375
+#====================================  
376
+
377
+class RequestPath(State):
378
+    def __init__(self,FSM):
379
+        super(RequestPath, self).__init__(FSM)    
380
+        
381
+    def Enter(self):
382
+        self.FSM.stateLife = 1
383
+
384
+        self.FSM.owner.target, self.FSM.owner.path = find_new_parking(self.FSM.owner)
385
+        self.FSM.owner.path_index = 0
386
+        super(RequestPath, self).Enter()        
387
+        
388
+    def Execute(self):
389
+        self.FSM.stateLife += 1
390
+        #own['ped_astar'] = Astar('nav_points')
391
+        #own['ped_astar'].get_path([5,-12,1] , [16,16,1])
392
+        #print('doing example')
393
+        
394
+    def Exit(self):
395
+        pass

+ 395
- 0
scripts/StatesWalker.py View File

@@ -0,0 +1,395 @@
1
+import utils
2
+import bge
3
+import random
4
+from mathutils import Vector
5
+
6
+#====================================     
7
+
8
+def mark_path(path, y):
9
+    iter_ = 0
10
+    for x in path:
11
+        pm = bge.logic.getCurrentScene().addObject('path_marker', y.obj, 0)
12
+        pm.worldPosition = path[iter_]    
13
+        iter_ += 1
14
+        if iter_ == 1:
15
+            pm.color = [0,1,0,.4]
16
+        if iter_ == (len(path) ):
17
+            pm.color = [1,0,0,.4]
18
+        if iter_ == (len(path) +1):
19
+            pm.color = [1,0,1,.4]    
20
+        y.path_display.append(pm)
21
+
22
+def clear_markers(self):
23
+    for x in self.FSM.owner.path_display:
24
+        try:
25
+            x.endObject()
26
+        except:
27
+            pass
28
+def get_ground_ray(self):
29
+    Axis = 2
30
+    Distance = -10
31
+    end = self.obj.worldPosition + (self.obj.worldOrientation.col[Axis]*Distance)
32
+    start = self.obj.worldPosition.copy()
33
+    ground_ray = self.obj.rayCast(end, start, 6,'', 1, 0)
34
+    return ground_ray
35
+
36
+def set_height(self):
37
+    ground_ray = get_ground_ray(self)
38
+    target_height = 0.9
39
+    hitpoint = ground_ray[1]
40
+    try:
41
+        dist = self.obj.getDistanceTo(hitpoint)
42
+        if dist < target_height:
43
+            self.obj.worldPosition.z += target_height - dist
44
+            self.obj.linearVelocity.z = 0
45
+        self.obj.linearVelocity.y *= .1
46
+    except:
47
+        pass
48
+
49
+def align_to_road(self):
50
+    ground_ray = get_ground_ray(self)
51
+    try:
52
+        self.obj.alignAxisToVect(ground_ray[2], 2, .15)
53
+    except:
54
+        pass
55
+    
56
+def find_new_parking(self):
57
+    potentials = []
58
+    for x in self.manager.idle_spots:
59
+        if x.status == 'available':
60
+            potentials.append(x)
61
+    for x in potentials:
62
+        min_dist = 45
63
+        dist = self.obj.getDistanceTo(x.obj)
64
+        #print('dist to potential', dist)
65
+        if dist < min_dist:
66
+            potentials.remove(x)
67
+            #print('----removing potential')
68
+    if len(potentials) > 0:
69
+
70
+        new_parking = random.choice(potentials)
71
+        #path = self.manager.navmesh.findPath(self.start_empty.obj.worldPosition, new_parking.obj.worldPosition)
72
+        path2 = self.manager.navmesh2.queue_path(self.start_empty.obj.worldPosition, new_parking.obj.worldPosition, self)
73
+        #print('parking added at', new_parking.obj.worldPosition)
74
+        #path.remove(path[0])
75
+        #mark_path(path, self)
76
+        return new_parking, path2
77
+    else:
78
+        #print('cant find parking for', self)
79
+        self.FSM.FSM.ToTransition('toEnterParallelPark')
80
+
81
+def get_lane_point(self):
82
+    self.point = self.path[self.path_index]
83
+    if self.point != self.last_lane_point:
84
+        v = Vector([self.last_lane_point.x - self.point.x, self.last_lane_point.y - self.point.y, 0])
85
+        tv = v.normalized()
86
+        nv = Vector([-tv.y, tv.x, 0]) #rotate 90 degrees
87
+        self.last_lane_point = self.lane_point
88
+        self.lane_point = self.point + self.manager.lane_position * nv
89
+    else:
90
+        pass
91
+        #print('skipping lane point')
92
+
93
+def update_point(self):
94
+    #print(self.path_index, 'path index', len(self.path))
95
+    if self.path_index >= (len(self.path) ):
96
+        self.FSM.FSM.ToTransition('toEnterParallelPark')
97
+    else:
98
+        dist = self.obj.getDistanceTo(self.lane_point)
99
+        self.point = self.path[self.path_index]
100
+        if dist < 2.5:
101
+            get_lane_point(self)   
102
+            if self.path_index > (len(self.path)):
103
+                pass
104
+            else:
105
+                self.path_index += 1
106
+         
107
+def align_to_point(self):
108
+    #print('lane_point', self.lane_point)
109
+    v = self.obj.getVectTo(self.lane_point)[1]
110
+    v.z = 0
111
+    self.obj.alignAxisToVect(v, 0, .1)
112
+
113
+def delta_to_vect(self):
114
+    v = self.obj.getVectTo(self.lane_point)[1]
115
+    delta = self.last_lane_point - self.lane_point
116
+    delta = delta.cross(v)
117
+    delta_mult = -.1
118
+    mult = 1.0
119
+    deltamove = delta[2] * delta_mult
120
+
121
+    #check if in front
122
+    local = self.obj.worldOrientation.inverted() * (self.lane_point - self.obj.worldPosition) 
123
+    f = deltamove * 50
124
+    if local < 0:
125
+        f *= -1
126
+    self.obj.applyForce([0, f, 0], True)
127
+            
128
+              
129
+def apply_gas(self):
130
+    if self.obj.linearVelocity.x < self.speed_targ:
131
+        self.obj.applyForce([self.speed_inc, 0, 0], True)
132
+
133
+#====================================     
134
+
135
+State = type("State", (object,), {})
136
+#====================================     
137
+class State(object):
138
+    def __init__(self, FSM):
139
+        self.FSM = FSM
140
+        self.timer = 0
141
+        self.startTime = 0
142
+    def Enter(self):
143
+        self.timer = 0
144
+        self.startTime = 0
145
+    def Execute(self):
146
+        print('Executing')
147
+    def Exit(self):
148
+        print('Exiting')
149
+#==================================== 
150
+            
151
+class Example(State):
152
+    def __init__(self,FSM):
153
+        super(Example, self).__init__(FSM)    
154
+        
155
+    def Enter(self):
156
+        self.FSM.stateLife = 1
157
+        self.FSM.owner.resumePhysics()
158
+        self.FSM.owner.resumeDynamics()
159
+        print('physics resumed')
160
+        super(Example, self).Enter()        
161
+        
162
+    def Execute(self):
163
+        self.FSM.stateLife += 1
164
+        print('doing example')
165
+        
166
+    def Exit(self):
167
+        pass
168
+
169
+class ExitParallelPark(State):
170
+    def __init__(self,FSM):
171
+        super(ExitParallelPark, self).__init__(FSM)    
172
+        
173
+    def Enter(self):
174
+        self.FSM.stateLife = 1
175
+        self.FSM.owner.obj.restorePhysics()
176
+        self.FSM.owner.obj.restoreDynamics()
177
+        self.FSM.owner.obj.linearVelocity = [0,0,0]
178
+        # self.FSM.owner.target, self.FSM.owner.path = find_new_parking(self.FSM.owner)
179
+        self.FSM.owner.path_index = 0
180
+        self.FSM.owner.point = self.FSM.owner.path[self.FSM.owner.path_index]
181
+        self.FSM.owner.target.status = 'targetted'
182
+        self.FSM.owner.start_empty.status = 'available'
183
+        #mark_path(self.FSM.owner.path, self.FSM.owner)
184
+        print('physics resumed')
185
+        super(ExitParallelPark, self).Enter()        
186
+        
187
+    def Execute(self):
188
+        self.FSM.stateLife += 1
189
+        v = self.FSM.owner.obj.getVectTo(self.FSM.owner.path[0])
190
+        self.FSM.owner.obj.alignAxisToVect(v[1], 0, .01)
191
+        self.FSM.owner.obj.alignAxisToVect([0,0,1], 2, 1)
192
+        if self.FSM.stateLife > 220:
193
+            self.FSM.ToTransition('toNavigateToTarget')
194
+
195
+    def Exit(self):
196
+        pass        
197
+
198
+#====================================  
199
+            
200
+class EnterParallelPark(State):
201
+    def __init__(self,FSM):        
202
+        super(EnterParallelPark, self).__init__(FSM)    
203
+        
204
+    def Enter(self):
205
+        self.FSM.stateLife = 1
206
+        #print('entering parallel park')
207
+        self.FSM.owner.obj.worldPosition = self.FSM.owner.target.obj.worldPosition.copy()
208
+        self.FSM.owner.obj.worldOrientation = self.FSM.owner.target.obj.worldOrientation.copy()
209
+        self.FSM.owner.obj.applyMovement([0, 0, 0], True)
210
+        self.FSM.owner.target.status = 'in_use'
211
+        self.FSM.owner.obj.worldPosition.z += .9
212
+        self.FSM.owner.active = False
213
+        self.FSM.owner.start_empty = self.FSM.owner.target
214
+        self.FSM.owner.last_point = self.FSM.owner.target.obj.worldPosition.copy()
215
+        self.FSM.owner.lane_point = self.FSM.owner.target.obj.worldPosition.copy()
216
+        self.FSM.owner.last_lane_point = self.FSM.owner.target.obj.worldPosition.copy()
217
+        self.FSM.owner.point = self.FSM.owner.target.obj.worldPosition.copy()
218
+        self.FSM.owner.path_index = 0
219
+        self.FSM.owner.path = None
220
+        #print('clearning last path')
221
+        
222
+        clear_markers(self)
223
+        
224
+        #self.FSM.owner.obj.suspendDynamics()
225
+        #self.FSM.owner.obj.suspendPhysics()
226
+        super(EnterParallelPark, self).Enter()        
227
+        
228
+    def Execute(self):
229
+        self.FSM.stateLife += 1
230
+        if self.FSM.stateLife == 2:
231
+            self.FSM.owner.manager.walkers_active.remove(self.FSM.owner)
232
+            self.FSM.owner.obj.suspendDynamics()
233
+            self.FSM.owner.obj.suspendPhysics()
234
+    def Exit(self):
235
+        pass
236
+
237
+#==================================== 
238
+
239
+class NavigateToTarget(State):
240
+    def __init__(self,FSM):
241
+        super(NavigateToTarget, self).__init__(FSM)    
242
+        
243
+    def Enter(self):
244
+        self.FSM.stateLife = 1
245
+        
246
+        super(NavigateToTarget, self).Enter()        
247
+        
248
+    def Execute(self):
249
+        self.FSM.stateLife += 1
250
+        update_point(self.FSM.owner)
251
+
252
+        align_to_point(self.FSM.owner)
253
+        align_to_road(self.FSM.owner)
254
+        set_height(self.FSM.owner)
255
+        delta_to_vect(self.FSM.owner)
256
+        apply_gas(self.FSM.owner)
257
+
258
+        #emergency exit
259
+        if self.FSM.stateLife > 30 * 180 * 3:
260
+            self.FSM.ToTransition('toEnterParallelPark')
261
+
262
+    def Exit(self):
263
+        pass        
264
+
265
+#==================================== 
266
+
267
+class Activate(State):
268
+    def __init__(self,FSM):
269
+        super(Activate, self).__init__(FSM)    
270
+        
271
+    def Enter(self):
272
+        self.FSM.stateLife = 1
273
+        super(Activate, self).Enter()        
274
+        
275
+    def find_target(self):
276
+        pass
277
+
278
+    def drive_to_point(self):
279
+        pass
280
+        # if self.FSM.path:
281
+        #     ground_ray = get_ground_ray(self)
282
+        #     if ground_ray[0]:
283
+        #         set_height(self, ground_ray)
284
+        #         align_to_road(self, ground_ray) 
285
+
286
+        #     v = self.FSM.owner.getVectTo(self.lane_point)               
287
+        #     speed_force = 800.0
288
+        #     max_speed = 5.0
289
+        #     behind = False
290
+        #     local = self.FSM.owner.worldOrientation.inverted() * (self.lane_point - self.FSM.owner.worldPosition) 
291
+        #     v2 = v[1].copy()
292
+        #     v2.z = 0
293
+        #     delta = self.last_lane_point - self.lane_point
294
+        #     delta = delta.cross(v[1])
295
+        #     delta_mult = -.1
296
+        #     mult = 1.0
297
+        #     backup_time = 20
298
+
299
+        #     #change max speed
300
+        #     if self.FSM.stateLife % 180 == 0:
301
+        #         print('change speed force')
302
+        #         self.speed_force = random.choice([max_speed * 1.3, max_speed * 1.6, max_speed * .8, max_speed * .6, max_speed])
303
+
304
+        #     if local.x > 0 and self.backup == 0:
305
+
306
+        #         if self.FSM.owner.linearVelocity.x < self.max_speed:
307
+        #             self.FSM.owner.applyForce([speed_force, 0, 0], True)
308
+
309
+        #         deltamove = delta[2] * delta_mult
310
+        #         #self.FSM.owner.applyMovement([0, deltamove, 0], True)
311
+
312
+        #         f = deltamove * 5000
313
+        #         self.FSM.owner.applyForce([0, f, 0], True)
314
+        #         v = self.FSM.owner.getVectTo(self.lane_point)
315
+        #         v2 = v[1].copy()
316
+        #         v2.z = 0
317
+        #         self.FSM.owner.alignAxisToVect(v2, 0, .05)
318
+
319
+
320
+        #     else:
321
+        #         if local.x < 0:
322
+        #             self.backup = backup_time   
323
+
324
+        #         if self.backup > 0:
325
+        #             print('backing up')
326
+        #             v = self.FSM.owner.getVectTo(self.FSM.path[0])
327
+        #             v2 = v[1].copy()
328
+        #             v2.z = 0
329
+        #             self.FSM.owner.alignAxisToVect(v2, 0, .02)
330
+        #             if self.FSM.owner.linearVelocity.x > -max_speed / 2:
331
+        #                 self.FSM.owner.applyForce([-speed_force * .8, 0, 0], True)
332
+        #             self.backup -= 1
333
+
334
+        #     dist = self.FSM.owner.getDistanceTo(self.lane_point)
335
+
336
+        #     if dist < 2.5 and (len(self.FSM.path) > 0):    
337
+        #         #print(self.FSM.path,'this is the path')
338
+        #         #print('navmesh point removed')
339
+        #         self.last_point = self.point
340
+        #         self.last_lane_point = self.lane_point
341
+        #         self.FSM.path.remove(self.FSM.path[0])
342
+
343
+        #         if len(self.FSM.path) > 0:
344
+        #             self.point = self.FSM.path[0]
345
+        #             v = Vector([self.last_point.x - self.point.x, self.last_point.y - self.point.y, 0])
346
+        #             tv = v.normalized()
347
+        #             nv = Vector([-tv.y, tv.x, 0]) #rotate 90 degrees
348
+        #             self.lane_point = self.point + self.lane_position * nv
349
+
350
+        #         else:
351
+        #             self.point = None
352
+
353
+        #     if self.FSM.path == []:
354
+        #         self.FSM.curTarget = None          
355
+
356
+        #     #progress
357
+        #     self.pos_his.append(self.FSM.owner.worldPosition.copy())
358
+        #     pos_his_len = len(self.pos_his)
359
+        #     if pos_his_len > 200:
360
+                
361
+        #         sum_ = abs(self.FSM.owner.worldPosition.x - self.pos_his[0][0]) + abs(self.FSM.owner.worldPosition.y - self.pos_his[0][1])
362
+        #         #print(sum_, 'sum')
363
+        #         if sum_ < .05:
364
+        #             self.backup = backup_time
365
+        #             print('progress stopped')
366
+        #         del self.pos_his[0]
367
+            
368
+
369
+    def Execute(self):
370
+        self.FSM.stateLife += 1
371
+
372
+    def Exit(self):
373
+        pass
374
+
375
+#====================================  
376
+
377
+class RequestPath(State):
378
+    def __init__(self,FSM):
379
+        super(RequestPath, self).__init__(FSM)    
380
+        
381
+    def Enter(self):
382
+        self.FSM.stateLife = 1
383
+
384
+        self.FSM.owner.target, self.FSM.owner.path = find_new_parking(self.FSM.owner)
385
+        self.FSM.owner.path_index = 0
386
+        super(RequestPath, self).Enter()        
387
+        
388
+    def Execute(self):
389
+        self.FSM.stateLife += 1
390
+        #own['ped_astar'] = Astar('nav_points')
391
+        #own['ped_astar'].get_path([5,-12,1] , [16,16,1])
392
+        #print('doing example')
393
+        
394
+    def Exit(self):
395
+        pass

+ 315
- 0
scripts/astar.py View File

@@ -0,0 +1,315 @@
1
+import bge, json, mathutils, math
2
+from timeit import default_timer as timer
3
+
4
+def chrono(function):
5
+    def wrapper(*args, **kargs):
6
+        before = timer()
7
+        value = function(*args, **kargs)
8
+        print(function.__qualname__, 'took', (timer() - before) * 100, 'to return', value)
9
+        return value
10
+    return wrapper
11
+
12
+def draw(locs, self):
13
+    for x in locs:
14
+        b = bge.logic.getCurrentScene().addObject('ball')
15
+        b.worldPosition = self.points[x].loc
16
+        b.color = [1,0,0,1]
17
+        
18
+def draw_mesh(self):
19
+    self.searched = set(self.searched)
20
+    for x in self.searched:
21
+        b = bge.logic.getCurrentScene().addObject('ball')
22
+        b.worldPosition = x.loc
23
+        b.color = [0,0,1,1]
24
+        b.worldScale = [.5, .5, .5] 
25
+    
26
+def draw_searched(self):
27
+    pass
28
+
29
+def draw_se(self):        
30
+    b = bge.logic.getCurrentScene().addObject('ball')
31
+    b.worldPosition = self.start.loc
32
+    b.color = [0,1,0,1]
33
+    b.worldScale = [.75, .75, .75] 
34
+    #----    
35
+    b = bge.logic.getCurrentScene().addObject('ball')
36
+    b.worldPosition = self.end.loc
37
+    b.color = [1,1,0,1]
38
+    b.worldScale = [2, 2, 2]  
39
+
40
+def draw_all(self):
41
+    for x in self.points:
42
+
43
+        pm = bge.logic.getCurrentScene().addObject('path_marker')
44
+        pm.worldPosition = x.loc
45
+        pm.color = [1,1,0,1]
46
+        pm.worldScale = [.25, .26, .25]  
47
+            
48
+class IterRegistry(type):
49
+    def __iter__(cls):
50
+        return iter(cls._registry)
51
+
52
+def load_points(points_file):        
53
+    #mainDir = bge.logic.expandPath("//")
54
+    mainDir = bge.logic.expandPath("//assets/")
55
+    fileName = mainDir + points_file    
56
+    with open(fileName, 'r') as filehandle:        
57
+        return json.load(filehandle)
58
+
59
+def build_tree(points):
60
+    kd = mathutils.kdtree.KDTree(len(points))
61
+    id = 0
62
+    for p in points:
63
+        p = mathutils.Vector(p[0])
64
+        kd.insert(p, id)
65
+        id += 1
66
+    kd.balance()
67
+    return kd
68
+
69
+def get_distance(start, end):
70
+    length = (end - start).magnitude
71
+    return length
72
+
73
+
74
+
75
+class Point(object):
76
+    __metaclass__ = IterRegistry
77
+    _registry = []
78
+
79
+    def __init__(self, list_point, parent, iter):
80
+        self.l = list_point[0]
81
+        self.id = iter
82
+        self.parent = parent
83
+        self.loc = mathutils.Vector(list_point[0])
84
+        self.lneighbors = list_point[1]
85
+        self.neighbors = []
86
+        self._registry.append(self)  
87
+        self.g = 100000
88
+        self.h = 100000
89
+        self.f = 100000 
90
+        self.prev = None  
91
+        
92
+    def get_neighbors(self):
93
+        for p in self.lneighbors:
94
+            for p_ in self.parent.points:
95
+                if p_.loc == mathutils.Vector(p):
96
+                    self.neighbors.append(p_)
97
+        self.neighbors = set(self.neighbors)            
98
+                 
99
+def init_points(self):
100
+    points = []
101
+    iter = 0
102
+    for x in self.points_list:
103
+        name = 'p' + str(iter)
104
+        y = Point(x, self, iter)
105
+        points.append(y)
106
+        iter += 1
107
+    return points    
108
+    
109
+class Astar:
110
+    def __init__(self, points_file, messenger):
111
+        self.state = 'idle'
112
+        self.points_list = load_points(points_file)
113
+        self.points = init_points(self) 
114
+        self.start = []
115
+        self.end = []
116
+        self.kdtree = build_tree(self.points_list)
117
+        self.life = 0
118
+        self.open = []
119
+        self.closed = []
120
+        self.searched = []
121
+        self.current = []
122
+        self.fscores = []
123
+        self.gscores = []
124
+        self.lps = []
125
+        self.messenger = messenger
126
+        self.queue = []
127
+        self.current_searcher = None
128
+        for x in self.points: x.get_neighbors()
129
+        
130
+    def update(self):
131
+        self.life += 1
132
+        if self.state == 'searching':
133
+            self.search()
134
+            #print('life', self.life)
135
+            if self.life > 5000:
136
+                #print('path failed')
137
+                self.state = 'idle'
138
+        else:
139
+            #print('updating pathfinder queue')
140
+            self.update_queue()        
141
+             
142
+    def get_results(self):
143
+        result_locs = []
144
+        results = []
145
+        #print('self.end', self.end, self.end.id)
146
+        cp = self.end   
147
+        breaker = 0
148
+        while cp != self.start:
149
+            result_locs.append(cp.id)
150
+            results.append(cp.loc)
151
+            if cp.prev == None:
152
+                cp = self.current
153
+            else:
154
+                cp = cp.prev
155
+            breaker += 1
156
+            if breaker > 2000:
157
+                #print('breaking results')
158
+                break
159
+        #if cp == self.start:
160
+            #print('start found!!')
161
+        #print('sp', self.start.id, 'ep', self.end.id)
162
+        #print('final path', result_locs, len(result_locs)) 
163
+        #draw_mesh(self)
164
+        #draw(result_locs, self)  
165
+        #publish
166
+        results.reverse()
167
+        self.messenger.dispatch('path found', ['found path for object', self.current_searcher, results])
168
+        #draw_all(self)
169
+
170
+    def smallest_f(self):
171
+        if len(self.open) > 1:
172
+            l = []
173
+            lfs = []
174
+            for x in self.open:
175
+                l.append(x)
176
+                lfs.append(x.f)
177
+            ind = lfs.index(min(lfs))  
178
+            #print(ind, lfs, l[ind]) 
179
+            return l[ind]
180
+        else:
181
+            return self.start
182
+            
183
+    #@chrono    
184
+    def search(self):
185
+        #Loop until you find the end
186
+        tries = 0
187
+        if self.open == []:
188
+            draw_mesh(self)
189
+            self.state = 'inactive'
190
+
191
+        while self.open != []:
192
+            #Get the current node from open list with smallest fval
193
+            try:
194
+                self.open.remove(self.current)
195
+            except:
196
+                pass
197
+                #print('current is not in open, cant remove')
198
+            current_node = self.smallest_f()
199
+            self.current = current_node
200
+            self.searched.append(current_node)
201
+             #remove from open
202
+            self.closed.append(current_node) #add to closed
203
+            if current_node == self.end:
204
+                #print('path found')
205
+                path = self.get_results()
206
+                #print('p', path)
207
+                self.state = 'inactive'
208
+                self.open = []
209
+                break                
210
+            else:
211
+                children = current_node.neighbors
212
+                for child in children:
213
+                    self.searched.append(child)
214
+                    p = current_node
215
+                    g = get_distance(current_node.loc, child.loc) + current_node.g
216
+                    h = get_distance(child.loc, self.end.loc)
217
+                    f = g + h   
218
+
219
+                    if child not in self.open and child not in self.closed:
220
+                        if g >= child.g:
221
+                            pass
222
+                        else:
223
+                            child.g = g
224
+                            child.h = h
225
+                            child.f = f
226
+                            child.prev = p
227
+                            
228
+                        self.open[:] = [x for x in self.open if x != child]  
229
+                        self.closed[:] = [x for x in self.closed if x != child]    
230
+                        self.open.append(child)
231
+
232
+#            print('trying', current_node.id, current_node.f, len(self.open))
233
+#            nei = []
234
+#            for z in current_node.neighbors:
235
+#                nei.append(z.id)
236
+            tries += 1
237
+            if tries >= 5:
238
+                break                      
239
+                
240
+    def get_neighbor_id(self, id):
241
+        iter = 0
242
+        for p in self.points:
243
+            if p[0] == id:
244
+                num = iter
245
+            else:
246
+                pass
247
+                #print('cant get navmesh neighbor id')    
248
+            iter += 1    
249
+        #print(num, 'id')
250
+        
251
+    def set_gscore(self):
252
+        self.gscores = []
253
+        self.fscores = []
254
+        self.lps = []
255
+        for x in self.points:
256
+            self.gscores.append(100000)    
257
+            self.fscores.append(100000)  
258
+            self.lps.append(None)   
259
+            x.f = 100000
260
+            x.h = 100000
261
+            x.g = 100000  
262
+                   
263
+    def set_fscore(self):
264
+        self.fscores = []
265
+        for x in self.points:
266
+            self.fscores.append(100000) 
267
+
268
+    def queue_path(self, start_in, end_in, obj):
269
+        self.queue.append([start_in, end_in, obj])
270
+        #obj.manager.pub.register("path found", obj)
271
+        #print('queueing path', len(self.queue))
272
+
273
+    def update_queue(self):
274
+        if self.queue != []:
275
+            #print('--getting next path in queue')
276
+            self.get_path(self.queue[0][0], self.queue[0][1], self.queue[0][2])
277
+            self.queue.remove(self.queue[0])           
278
+                         
279
+    def get_path(self, start_in, end_in, obj):
280
+        stree = self.kdtree.find_n(start_in, 1)
281
+        start_out = self.points[stree[0][1]]
282
+        otree = self.kdtree.find_n(end_in, 1)
283
+        end_out = self.points[otree[0][1]]
284
+        self.life = 0
285
+        self.current_searcher = obj
286
+        self.start = start_out
287
+        self.end = end_out
288
+        self.state = 'searching'
289
+        self.open.append(self.start)
290
+        self.set_gscore()
291
+        self.current = self.start
292
+        #beginning search
293
+        #Initialize both open and closed list
294
+        self.open = []
295
+        self.closed = []
296
+        self.searched = []
297
+
298
+        #Add the start node
299
+        self.open.append(self.start)
300
+        self.current = self.start
301
+        self.current.g = 0
302
+        self.current.f = 0
303
+        self.current.h = get_distance(self.current.loc, self.end.loc)
304
+        #draw_se(self)
305
+        print('start', self.start.id, 'end', self.end.id)
306
+        
307
+# def main(cont):
308
+#     own = cont.owner
309
+#     if 'inited' not in own:
310
+#         own['inited'] = True
311
+#         own['ped_astar'] = Astar('nav_points')
312
+#         own['ped_astar'].get_path([5,-12,1] , [16,16,1])
313
+        
314
+#     own['ped_astar'].update()
315
+

+ 6
- 1
scripts/camera.py View File

@@ -8,6 +8,7 @@ import birds
8 8
 import sound_man
9 9
 import FSM
10 10
 import cars
11
+import walkers
11 12
 
12 13
 def main(cont):
13 14
     #camFSM.main(cont)
@@ -65,7 +66,7 @@ def main(cont):
65 66
         dict['cam_state'] = 'walking'
66 67
         controlcube['ragdoll_active'] = False
67 68
         dict['camera'] = cam1
68
-        own['NpcPedFSM'] = FSM.NpcPed(own)
69
+        #own['NpcPedFSM'] = FSM.NpcPed(own)
69 70
         own['CamFSM'] = FSM.CameraFSM(own)
70 71
         own['life'] = 0
71 72
     acam = scene.active_camera
@@ -376,6 +377,10 @@ def main(cont):
376 377
     if own['life'] % 2 == 1:
377 378
         cars.Execute(cont)
378 379
         own['CamFSM'].Execute()
380
+        #print('doing')
381
+        walkers.Execute(cont)
379 382
 
383
+    else:
384
+        pass
380 385
 
381 386
     own['life'] += 1

+ 32
- 8
scripts/cars.py View File

@@ -1,6 +1,8 @@
1 1
 import bge
2 2
 import random
3 3
 import FSM
4
+import astar
5
+import observer
4 6
 
5 7
 car_colors = [[.2,.01,.01,1], [.5,.5,.4,1], [.005,.01,.015,1], [.005, .1, 0.003, 1], [.1, .1, .1, 1]]
6 8
 
@@ -74,18 +76,33 @@ class Car:
74 76
 		self.path = None
75 77
 		self.path_index = 0
76 78
 		self.path_display = []
79
+		#self.messages = observer.Subscriber(self)
80
+		self.manager.pub.register("path found", self)
77 81
 
78 82
 	def Execute(self):
79 83
 		self.FSM.Execute()
80 84
 		self.life += 1
81 85
 
86
+	def ReceiveMessage(self, message):
87
+		#print('{} got message "{}"'.format(self, message))	
88
+		if self == message[1]:
89
+			#print('unregistering observer')
90
+			#print(message[2])
91
+			self.path = message[2]
92
+			self.FSM.FSM.ToTransition('toExitParallelPark')
93
+			
94
+			#self.manager.pub.unregister("path found", self)
95
+		#self.manager.pub.unregister("path found", self)	
96
+
82 97
 class CarManager:
83 98
 	def __init__(self, own):
84 99
 		self.parent = own
85 100
 		self.navmesh =None
101
+		self.pub = observer.Publisher(['path found', 'working'])
102
+		self.navmesh2 =  astar.Astar('nav_points', self.pub)
86 103
 		self.cars = []
87
-		self.max_cars = 2
88
-		self.max_active = 1
104
+		self.max_cars = 14
105
+		self.max_active = 12
89 106
 		self.targets = []
90 107
 		self.target_loc = None
91 108
 		self.parking_spots = get_parking_spots()
@@ -96,8 +113,10 @@ class CarManager:
96 113
 		self.cars_loaded = False
97 114
 		#self.max_active = 5
98 115
 		self.life = 0
99
-		self.default_speed = 5.0
116
+		self.default_speed = 8.0#5.0
100 117
 		self.lane_position = 2.5#1.75
118
+		
119
+		    
101 120
 
102 121
 
103 122
 	def load_cars(self):
@@ -136,9 +155,9 @@ class CarManager:
136 155
 				# print('setting car color to ', car.color)
137 156
 				iter_ += 1
138 157
 
139
-			for x in bge.logic.getCurrentScene().objects:
140
-				if 'carNavmesh' in x:
141
-					self.navmesh = x
158
+			# for x in bge.logic.getCurrentScene().objects:
159
+			# 	if 'carNavmesh' in x:
160
+			# 		self.navmesh = x
142 161
 				# if 'car_target' in x:
143 162
 				# 	self.targets.append(x)	
144 163
 				# 	print('adding target', x)
@@ -168,7 +187,7 @@ class CarManager:
168 187
 			car = random.choice(l)
169 188
 			self.cars_active.append(car)
170 189
 			car.active = True
171
-			car.FSM.FSM.ToTransition('toExitParallelPark')
190
+			car.FSM.FSM.ToTransition('toRequestPath')
172 191
 			#state
173 192
 			print('activate car', car)
174 193
 
@@ -177,18 +196,23 @@ class CarManager:
177 196
 		self.life += 1
178 197
 		if self.cars_loaded:
179 198
 			#check i f new car should be active
180
-			if self.life % 180 == 0:
199
+			if self.life % 120 == 0:
181 200
 
182 201
 				self.activator_check()
183 202
 
184 203
 			for car in self.cars_active:
185 204
 				car.Execute()
186 205
 				#print(car, 'is an acitve car')
206
+			self.navmesh2.update()
207
+			#print('updating navmesh from cars')	
187 208
 			
188 209
 
189 210
 		else:
190 211
 			self.load_cars()
191 212
 			print('------------calling load cars')
213
+
214
+		# if self.life == 400:
215
+		# 	self.pub.dispatch('path found', 'found path for object')
192 216
 		# l = []
193 217
 		# for c in self.parking_spots:		
194 218
 		# 	if c.status == 'available':

+ 0
- 5
scripts/controller2.py View File

@@ -3088,7 +3088,6 @@ def main():
3088 3088
     #nollie
3089 3089
     if sr.q5 > 0 and sr.q1 > 0 and sr.q5 > sr.q1:
3090 3090
         nollie()
3091
-        print('nollie')
3092 3091
         sr.reset()
3093 3092
 
3094 3093
     #kickflip
@@ -4426,7 +4425,3 @@ def main():
4426 4425
         own['requestAction'] = 'fak_land'   
4427 4426
 
4428 4427
 
4429
-
4430
- 
4431
-
4432
-    print(dict['rUD'])

+ 22
- 0
scripts/observer.py View File

@@ -0,0 +1,22 @@
1
+class Subscriber:
2
+    def __init__(self, name):
3
+        self.name = name
4
+    def update(self, message):
5
+        print('{} got message "{}"'.format(self.name, message))
6
+        
7
+class Publisher:
8
+    def __init__(self, events):
9
+        self.subscribers = { event : dict()
10
+                             for event in events }
11
+                             
12
+    def get_subscribers(self, event):
13
+        return self.subscribers[event]                         
14
+    def register(self, event, who, callback=None):
15
+        if callback is None:
16
+            callback = getattr(who, 'ReceiveMessage')
17
+        self.get_subscribers(event)[who] = callback
18
+    def unregister(self, event, who):
19
+        del self.get_subscribers(event)[who]
20
+    def dispatch(self, event, message):
21
+        for subscriber, callback in self.get_subscribers(event).items():
22
+            callback(message)        

+ 197
- 0
scripts/walkers.py View File

@@ -0,0 +1,197 @@
1
+import bge
2
+import random
3
+import FSM
4
+import astar
5
+import observer
6
+
7
+car_colors = [[.2,.01,.01,1], [.5,.5,.4,1], [.005,.01,.015,1], [.005, .1, 0.003, 1], [.1, .1, .1, 1]]
8
+
9
+def idle_run_check(self):
10
+	output = False
11
+	for x in bge.logic.getCurrentScene().objects:
12
+		if 'walker_idle_empty' in x.name:
13
+			self.idle_spots.append(x)
14
+			x['in_use'] = False
15
+			#print('this is a parking spot')
16
+			output = True
17
+	
18
+	return output
19
+			
20
+
21
+def get_idle_spots():
22
+	op = []
23
+	for obj in bge.logic.getCurrentScene().objects:
24
+		if 'idle_spot' in obj:
25
+			ps = IdleSpot(obj, 'available')
26
+			op.append(ps)
27
+	return op
28
+
29
+def get_intersections():
30
+	op = []
31
+	for obj in bge.logic.getCurrentScene().objects:
32
+		if 'intersection' in obj:
33
+			op.append(obj)
34
+	return op		
35
+
36
+def add_walker(self, x):
37
+	#print('put car here', x.obj.worldPosition)
38
+	walker = bge.logic.getCurrentScene().addObject('larryCube', x.obj, 0)
39
+	walker.worldOrientation = x.obj.worldOrientation
40
+	walker.worldPosition.z += .8
41
+	#car.applyMovement([0, -6, 0], True)
42
+	#car.suspendDynamics()
43
+	#car.suspendPhysics()	
44
+
45
+	walker.name = 'lwalker' + str(len(self.manager.walkers))
46
+	#color = random.choice(car_colors)
47
+	#walker.color = color
48
+	#for y in car.children:
49
+		#y.color = color
50
+	#print('setting car color to ', car.color)
51
+	x.status = 'in_use'
52
+
53
+
54
+	return walker
55
+
56
+#====================================  
57
+
58
+class IdleSpot:
59
+	def __init__(self, obj, status):
60
+		self.obj = obj
61
+		self.status = status
62
+
63
+class Walker:
64
+	def __init__(self, own, start_empty):
65
+		self.manager = own
66
+		self.life = 0
67
+		self.start_empty = start_empty
68
+		self.obj = add_walker(self, self.start_empty)
69
+		self.speed_targ = self.manager.default_speed
70
+		self.speed_inc = 100
71
+		self.FSM = FSM.WalkerFSM(self)
72
+		self.active = False
73
+		self.target = None
74
+		self.lane_point = self.obj.worldPosition
75
+		self.last_lane_point = self.obj.worldPosition
76
+		self.path = None
77
+		self.path_index = 0
78
+		self.path_display = []
79
+		#self.messages = observer.Subscriber(self)
80
+		self.manager.pub.register("path found", self)
81
+
82
+	def Execute(self):
83
+		self.FSM.Execute()
84
+		self.life += 1
85
+
86
+	def ReceiveMessage(self, message):
87
+		#print('{} got message "{}"'.format(self, message))	
88
+		if self == message[1]:
89
+			#print('unregistering observer')
90
+			#print(message[2])
91
+			self.path = message[2]
92
+			self.FSM.FSM.ToTransition('toExitParallelPark')
93
+			
94
+			#self.manager.pub.unregister("path found", self)
95
+		#self.manager.pub.unregister("path found", self)	
96
+
97
+class WalkerManager:
98
+	def __init__(self, own):
99
+		self.parent = own
100
+		self.navmesh =None
101
+		self.pub = observer.Publisher(['path found', 'working'])
102
+		self.navmesh2 =  astar.Astar('walker_nav_points', self.pub)
103
+		self.walkers = []
104
+		self.max_walkers = 12
105
+		self.max_active = 10
106
+		self.targets = []
107
+		self.target_loc = None
108
+		self.idle_spots = get_idle_spots()
109
+		#self.intersections = get_intersections()
110
+		#self.active = car_run_check(self)
111
+		self.active = True
112
+		self.walkers_active = []
113
+		self.walkers_loaded = False
114
+		#self.max_active = 5
115
+		self.life = 0
116
+		self.default_speed = 1.0#5.0
117
+		self.lane_position = 0.5#1.75
118
+		
119
+		    
120
+
121
+
122
+	def load_walkers(self):
123
+		iter_ = 0
124
+		#look for car in scene
125
+		if 'larryCube' in bge.logic.getCurrentScene().objectsInactive:
126
+
127
+			start_choices = self.idle_spots.copy()
128
+			while len(self.walkers) < self.max_walkers:
129
+				#get starting position
130
+				start_choice = random.choice(start_choices)
131
+				start_choices.remove(start_choice)
132
+
133
+				#add_car(self, start_choice.worldPosition)
134
+				walker_ = Walker(self, start_choice)
135
+				self.walkers.append(walker_)
136
+
137
+			for x in self.idle_spots:
138
+				iter_ += 1	
139
+
140
+			self.walkers_loaded = True	
141
+
142
+		else:
143
+			mainDir = bge.logic.expandPath("//npc_walkers/")
144
+			npc = 'larry'
145
+			fileName = mainDir + str(npc) + '.blend'    
146
+			
147
+			path = bge.logic.expandPath(fileName)
148
+			print('loading npc')
149
+			try:
150
+				#bge.logic.LibLoad(path, 'Scene') 
151
+				bge.logic.LibLoad(fileName, 'Scene') 
152
+				print('larry loaded')
153
+			except Exception as e:
154
+				print('loading', fileName, 'failed', e)   
155
+
156
+	def activator_check(self):
157
+		if len(self.walkers_active) < self.max_active:
158
+			l = []
159
+			for c in self.walkers:
160
+				if not c.active:
161
+					l.append(c)
162
+			walker = random.choice(l)
163
+			self.walkers_active.append(walker)
164
+			walker.active = True
165
+			walker.FSM.FSM.ToTransition('toRequestPath')
166
+			#state
167
+			print('activate walker', walker)
168
+
169
+	def update(self):
170
+		#print('car_manager_updating')
171
+		self.life += 1
172
+		if self.walkers_loaded:
173
+			#check i f new car should be active
174
+			if self.life % 180 == 0:
175
+
176
+				self.activator_check()
177
+
178
+			for walker in self.walkers_active:
179
+				walker.Execute()
180
+				#print(car, 'is an acitve car')
181
+			self.navmesh2.update()
182
+			#print('updating navmesh from cars')	
183
+			
184
+
185
+		else:
186
+			self.load_walkers()
187
+			print('------------calling walkers')
188
+
189
+def Execute(cont):
190
+	own = cont.owner
191
+	if 'walker_manager' not in own:
192
+		own['walker_manager'] = WalkerManager(own)
193
+	walker_man = own['walker_manager']	
194
+	#print('running walkers')
195
+	if walker_man.active:
196
+
197
+		walker_man.update()

Loading…
Cancel
Save