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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import bge
  2. import random
  3. import FSM
  4. import astar
  5. import observer
  6. car_colors = [[.1, .1, .1, 1], [0.0, 0.0, 0.25, 1], [0.1, 0.03, 0.02, 1], [.4, .3, .08, 1], [.1, .1, .1, 1], [.05, .12, .05, 1], [.3, .05, 0, 1], [.2, .03, .3, 1], [.3, 0, 0, 1], [.3, .3, .3, 1], [.3, .25, .15, 1], [1,1,1,1], [1, 1, 0, 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. if 'perp' in obj:
  22. ps.type = 'perp'
  23. else:
  24. ps.type = 'parallel'
  25. op.append(ps)
  26. return op
  27. def get_intersections():
  28. op = []
  29. for obj in bge.logic.getCurrentScene().objects:
  30. if 'intersection' in obj:
  31. op.append(obj)
  32. return op
  33. def add_car(self, x):
  34. print('put car here', x.obj.worldPosition)
  35. car = bge.logic.getCurrentScene().addObject('car_collider', x.obj, 0)
  36. car.worldOrientation = x.obj.worldOrientation
  37. car.worldPosition.z += .8
  38. if x.type == 'perp':
  39. car.applyMovement([7, 0, 0], True)
  40. else:
  41. car.applyMovement([0, -6, 0], True)
  42. car.suspendDynamics()
  43. #car.suspendPhysics()
  44. car.name = 'lcar' + str(len(self.manager.cars))
  45. color = random.choice(car_colors)
  46. car.color = color
  47. for y in car.children:
  48. y.color = color
  49. print('setting car color to ', car.color)
  50. x.status = 'in_use'
  51. return car
  52. #====================================
  53. class ParkingSpot:
  54. def __init__(self, obj, status):
  55. self.obj = obj
  56. self.type = None
  57. self.status = status
  58. class Car:
  59. def __init__(self, own, start_empty):
  60. self.manager = own
  61. self.life = 0
  62. self.start_empty = start_empty
  63. self.obj = add_car(self, self.start_empty)
  64. self.speed_targ = self.manager.default_speed
  65. self.speed_inc = 800
  66. self.FSM = FSM.CarFSM(self)
  67. self.active = False
  68. self.target = None
  69. self.lane_point = self.obj.worldPosition
  70. self.point = self.obj.worldPosition
  71. self.last_lane_point = self.obj.worldPosition
  72. self.path = None
  73. self.path_index = 0
  74. self.path_display = []
  75. self.manager.pub.register("path found", self)
  76. def Execute(self):
  77. self.FSM.Execute()
  78. self.life += 1
  79. def ReceiveMessage(self, message):
  80. if self == message[1]:
  81. self.path = message[2]
  82. if self.path != []:
  83. #print(self.start_empty.type, 'target type')
  84. if self.start_empty.type == 'perp':
  85. self.FSM.FSM.ToTransition('toExitPerpPark')
  86. else:
  87. self.FSM.FSM.ToTransition('toExitParallelPark')
  88. #print('setting goal available after failure', self.FSM.owner.target.obj.worldPosition)
  89. self.FSM.owner.target.status = 'available'
  90. else:
  91. #print('path failed for', self)
  92. self.FSM.FSM.ToTransition('toEnterParallelPark')
  93. class CarManager:
  94. def __init__(self, own):
  95. self.parent = own
  96. self.navmesh =None
  97. self.pub = observer.Publisher(['path found', 'working'])
  98. self.navmesh2 = astar.Astar('nav_points', self.pub)
  99. self.cars = []
  100. self.max_cars = 10
  101. self.max_active = 6
  102. self.targets = []
  103. self.target_loc = None
  104. self.parking_spots = get_parking_spots()
  105. self.intersections = get_intersections()
  106. self.active = True
  107. self.cars_active = []
  108. self.cars_loaded = False
  109. self.life = 0
  110. self.default_speed = 8.0#5.0
  111. self.lane_position = 2.5#1.75
  112. def load_cars(self):
  113. iter_ = 0
  114. if 'car_collider' in bge.logic.getCurrentScene().objectsInactive:
  115. start_choices = self.parking_spots.copy()
  116. while len(self.cars) < self.max_cars:
  117. start_choice = random.choice(start_choices)
  118. start_choices.remove(start_choice)
  119. car_ = Car(self, start_choice)
  120. self.cars.append(car_)
  121. self.cars_loaded = True
  122. else:
  123. mainDir = bge.logic.expandPath("//")
  124. car = 'car1'
  125. fileName = mainDir + "assets/" + str(car) + '.blend'
  126. path = bge.logic.expandPath(fileName)
  127. print('loading car')
  128. try:
  129. bge.logic.LibLoad(path, 'Scene')
  130. except:
  131. print('loading', fileName, 'failed')
  132. def activator_check(self):
  133. if len(self.cars_active) < self.max_active:
  134. l = []
  135. for c in self.cars:
  136. if not c.active:
  137. l.append(c)
  138. car = random.choice(l)
  139. self.cars_active.append(car)
  140. car.active = True
  141. car.FSM.FSM.ToTransition('toRequestPath')
  142. #print('activate car', car)
  143. def update(self):
  144. self.life += 1
  145. if self.cars_loaded:
  146. if self.life % 120 == 0:
  147. self.activator_check()
  148. for car in self.cars_active:
  149. car.Execute()
  150. self.navmesh2.update()
  151. else:
  152. self.load_cars()
  153. def Execute(cont):
  154. own = cont.owner
  155. if 'car_manager' not in own:
  156. own['car_manager'] = CarManager(own)
  157. car_man = own['car_manager']
  158. if car_man.active:
  159. car_man.update()