import bge import random import FSM car_colors = [[.2,.01,.01,1], [.5,.5,.4,1], [.005,.01,.015,1], [.005, .1, 0.003, 1], [.1, .1, .1, 1]] def car_run_check(self): output = False for x in bge.logic.getCurrentScene().objects: if 'car_park_empty' in x.name: self.parking_spots.append(x) x['in_use'] = False print('this is a parking spot') output = True return output def get_parking_spots(): op = [] for obj in bge.logic.getCurrentScene().objects: if 'parking' in obj: ps = ParkingSpot(obj, 'available') op.append(ps) return op def get_intersections(): op = [] for obj in bge.logic.getCurrentScene().objects: if 'intersection' in obj: op.append(obj) return op def add_car(self, x): print('put car here', x.obj.worldPosition) car = bge.logic.getCurrentScene().addObject('car_collider', x.obj, 0) car.worldOrientation = x.obj.worldOrientation car.worldPosition.z += .8 car.applyMovement([0, -6, 0], True) car.suspendDynamics() car.suspendPhysics() car.name = 'lcar' + str(len(self.manager.cars)) color = random.choice(car_colors) car.color = color for y in car.children: y.color = color print('setting car color to ', car.color) x.status = 'in_use' return car #==================================== class ParkingSpot: def __init__(self, obj, status): self.obj = obj self.status = status class Car: def __init__(self, own, start_empty): self.manager = own self.life = 0 self.start_empty = start_empty self.obj = add_car(self, self.start_empty) self.speed_targ = self.manager.default_speed self.speed_inc = 800 self.FSM = FSM.CarFSM(self) self.active = False self.target = None self.lane_point = self.obj.worldPosition self.last_lane_point = self.obj.worldPosition self.path = None self.path_index = 0 self.path_display = [] def Execute(self): self.FSM.Execute() self.life += 1 class CarManager: def __init__(self, own): self.parent = own self.navmesh =None self.cars = [] self.max_cars = 15 self.max_active = 9 self.targets = [] self.target_loc = None self.parking_spots = get_parking_spots() self.intersections = get_intersections() #self.active = car_run_check(self) self.active = True self.cars_active = [] self.cars_loaded = False #self.max_active = 5 self.life = 0 self.default_speed = 5.0 self.lane_position = 2.5#1.75 def load_cars(self): iter_ = 0 #look for car in scene if 'car_collider' in bge.logic.getCurrentScene().objectsInactive: start_choices = self.parking_spots.copy() while len(self.cars) < self.max_cars: #get starting position start_choice = random.choice(start_choices) start_choices.remove(start_choice) #add_car(self, start_choice.worldPosition) car_ = Car(self, start_choice) self.cars.append(car_) for x in self.parking_spots: # print('put car here', x.worldPosition) # car = bge.logic.getCurrentScene().addObject('car_collider', x, 0) # car.worldOrientation = x.worldOrientation # car.worldPosition.z += 1 # car.suspendDynamics() # car.suspendPhysics() # car['FSM'] = FSM.CarFSM(car) # car['manager'] = self # self.cars.append(car) # car.name = 'lcar' + str(iter_) # color = random.choice(car_colors) # car.color = color # for x in car.children: # x.color = color # print('setting car color to ', car.color) iter_ += 1 for x in bge.logic.getCurrentScene().objects: if 'carNavmesh' in x: self.navmesh = x # if 'car_target' in x: # self.targets.append(x) # print('adding target', x) # if 'target_loc' in x: # self.target_loc = x self.cars_loaded = True else: mainDir = bge.logic.expandPath("//") car = 'car1' fileName = mainDir + "assets/" + str(car) + '.blend' path = bge.logic.expandPath(fileName) print('loading car') try: bge.logic.LibLoad(path, 'Scene') except: print('loading', fileName, 'failed') def activator_check(self): if len(self.cars_active) < self.max_active: l = [] for c in self.cars: if not c.active: l.append(c) car = random.choice(l) self.cars_active.append(car) car.active = True car.FSM.FSM.ToTransition('toExitParallelPark') #state print('activate car', car) def update(self): #print('car_manager_updating') self.life += 1 if self.cars_loaded: #check i f new car should be active if self.life % 180 == 0: self.activator_check() for car in self.cars_active: car.Execute() #print(car, 'is an acitve car') else: self.load_cars() print('------------calling load cars') # l = [] # for c in self.parking_spots: # if c.status == 'available': # l.append(1) # else: # l.append(0) #print(l, 'available spots') def Execute(cont): own = cont.owner if 'car_manager' not in own: own['car_manager'] = CarManager(own) car_man = own['car_manager'] if car_man.active: car_man.update()