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.

cars.py 5.6KB

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