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.

walkers.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 idle_run_check(self):
  8. output = False
  9. for x in bge.logic.getCurrentScene().objects:
  10. if 'walker_idle_spot' in x.name:
  11. self.idle_spots.append(x)
  12. x['in_use'] = False
  13. output = True
  14. print(output, 'walker idle spots')
  15. return output
  16. def get_idle_spots():
  17. op = []
  18. for obj in bge.logic.getCurrentScene().objects:
  19. if 'idle_spot' in obj.name:
  20. ps = IdleSpot(obj, 'available')
  21. op.append(ps)
  22. print('&&&&&& idle spots')
  23. print(op)
  24. return op
  25. def get_intersections():
  26. op = []
  27. for obj in bge.logic.getCurrentScene().objects:
  28. if 'intersection' in obj:
  29. op.append(obj)
  30. return op
  31. def add_walker(self, x):
  32. print(x.obj.worldPosition, '--walker here')
  33. walker = bge.logic.getCurrentScene().addObject('larryCube', x.obj, 0)
  34. print(walker.worldPosition)
  35. #walker.worldPosition = x.obj.worldPosition
  36. walker.worldOrientation = x.obj.worldOrientation
  37. walker.worldPosition.z += 2.8
  38. walker.name = 'lwalker' + str(len(self.manager.walkers))
  39. x.status = 'in_use'
  40. return walker
  41. #====================================
  42. class IdleSpot:
  43. def __init__(self, obj, status):
  44. self.obj = obj
  45. self.status = status
  46. class Walker:
  47. def __init__(self, own, start_empty):
  48. self.manager = own
  49. self.life = 0
  50. self.start_empty = start_empty
  51. self.obj = add_walker(self, self.start_empty)
  52. self.speed_targ = self.manager.default_speed
  53. self.speed_inc = 100
  54. self.FSM = FSM.WalkerFSM(self)
  55. self.active = False
  56. self.target = None
  57. self.lane_point = self.obj.worldPosition
  58. self.last_lane_point = self.obj.worldPosition
  59. self.path = None
  60. self.path_index = 0
  61. self.path_display = []
  62. self.manager.pub.register("path found", self)
  63. #self.obj.worldPosition = self.start_empty.obj.worldPosition
  64. def Execute(self):
  65. self.FSM.Execute()
  66. self.life += 1
  67. def ReceiveMessage(self, message):
  68. if self == message[1]:
  69. self.path = message[2]
  70. self.FSM.FSM.ToTransition('toExitParallelPark')
  71. class WalkerManager:
  72. def __init__(self, own):
  73. self.parent = own
  74. self.navmesh =None
  75. self.pub = observer.Publisher(['path found', 'working'])
  76. self.navmesh2 = astar.Astar('walker_nav_points', self.pub)
  77. self.walkers = []
  78. self.max_walkers = 12
  79. self.max_active = 10
  80. self.targets = []
  81. self.target_loc = None
  82. self.idle_spots = get_idle_spots()
  83. self.active = True
  84. self.walkers_active = []
  85. self.walkers_loaded = False
  86. self.life = 0
  87. self.default_speed = 1.0#5.0
  88. self.lane_position = 0.5#1.75
  89. def load_walkers(self):
  90. iter_ = 0
  91. if 'larryCube' in bge.logic.getCurrentScene().objectsInactive:
  92. start_choices = self.idle_spots.copy()
  93. while len(self.walkers) < self.max_walkers:
  94. #get starting position
  95. start_choice = random.choice(start_choices)
  96. start_choices.remove(start_choice)
  97. walker_ = Walker(self, start_choice)
  98. self.walkers.append(walker_)
  99. for x in self.idle_spots:
  100. iter_ += 1
  101. self.walkers_loaded = True
  102. else:
  103. mainDir = bge.logic.expandPath("//npc_walkers/")
  104. npc = 'larry'
  105. fileName = mainDir + str(npc) + '.blend'
  106. path = bge.logic.expandPath(fileName)
  107. print('loading npc')
  108. try:
  109. bge.logic.LibLoad(fileName, 'Scene', load_actions=True)
  110. print('larry loaded')
  111. except Exception as e:
  112. print('loading', fileName, 'failed', e)
  113. def activator_check(self):
  114. if len(self.walkers_active) < self.max_active:
  115. l = []
  116. for c in self.walkers:
  117. if not c.active:
  118. l.append(c)
  119. walker = random.choice(l)
  120. self.walkers_active.append(walker)
  121. walker.active = True
  122. walker.obj.worldPosition = walker.start_empty.obj.worldPosition
  123. walker.FSM.FSM.ToTransition('toRequestPath')
  124. def update(self):
  125. self.life += 1
  126. if self.walkers_loaded:
  127. if self.life % 180 == 0:
  128. self.activator_check()
  129. for walker in self.walkers_active:
  130. walker.Execute()
  131. self.navmesh2.update()
  132. else:
  133. self.load_walkers()
  134. def Execute(cont):
  135. own = cont.owner
  136. if 'walker_manager' not in own:
  137. own['walker_manager'] = WalkerManager(own)
  138. walker_man = own['walker_manager']
  139. if walker_man.active:
  140. walker_man.update()