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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 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.manager.pub.register("path found", self)
  67. def Execute(self):
  68. self.FSM.Execute()
  69. self.life += 1
  70. def ReceiveMessage(self, message):
  71. if self == message[1]:
  72. self.path = message[2]
  73. self.FSM.FSM.ToTransition('toExitParallelPark')
  74. class CarManager:
  75. def __init__(self, own):
  76. self.parent = own
  77. self.navmesh =None
  78. self.pub = observer.Publisher(['path found', 'working'])
  79. self.navmesh2 = astar.Astar('nav_points', self.pub)
  80. self.cars = []
  81. self.max_cars = 14
  82. self.max_active = 12
  83. self.targets = []
  84. self.target_loc = None
  85. self.parking_spots = get_parking_spots()
  86. self.intersections = get_intersections()
  87. self.active = True
  88. self.cars_active = []
  89. self.cars_loaded = False
  90. self.life = 0
  91. self.default_speed = 8.0#5.0
  92. self.lane_position = 2.5#1.75
  93. def load_cars(self):
  94. iter_ = 0
  95. if 'car_collider' in bge.logic.getCurrentScene().objectsInactive:
  96. start_choices = self.parking_spots.copy()
  97. while len(self.cars) < self.max_cars:
  98. start_choice = random.choice(start_choices)
  99. start_choices.remove(start_choice)
  100. car_ = Car(self, start_choice)
  101. self.cars.append(car_)
  102. self.cars_loaded = True
  103. else:
  104. mainDir = bge.logic.expandPath("//")
  105. car = 'car1'
  106. fileName = mainDir + "assets/" + str(car) + '.blend'
  107. path = bge.logic.expandPath(fileName)
  108. print('loading car')
  109. try:
  110. bge.logic.LibLoad(path, 'Scene')
  111. except:
  112. print('loading', fileName, 'failed')
  113. def activator_check(self):
  114. if len(self.cars_active) < self.max_active:
  115. l = []
  116. for c in self.cars:
  117. if not c.active:
  118. l.append(c)
  119. car = random.choice(l)
  120. self.cars_active.append(car)
  121. car.active = True
  122. car.FSM.FSM.ToTransition('toRequestPath')
  123. #print('activate car', car)
  124. def update(self):
  125. self.life += 1
  126. if self.cars_loaded:
  127. if self.life % 120 == 0:
  128. self.activator_check()
  129. for car in self.cars_active:
  130. car.Execute()
  131. self.navmesh2.update()
  132. else:
  133. self.load_cars()
  134. def Execute(cont):
  135. own = cont.owner
  136. if 'car_manager' not in own:
  137. own['car_manager'] = CarManager(own)
  138. car_man = own['car_manager']
  139. if car_man.active:
  140. car_man.update()