def __init__(self, size=GRID_SIZE, cell_size=CELL_SIZE): """Construct a new tower defence game""" super().__init__() self.grid = GridCoordinateTranslator(cells=size, cell_size=cell_size) self.towers = {} # assign the start and end point of the enemies self._start, self._end = (-1, 1), (self.grid.cells[0], 1) # recalculate the path for enemies to travel avoiding current towers self.path = self.generate_path() self.obstacles = [] self.enemies = [] self._unspawned_enemies = [] # Game data to be passed to units when stepped # It's poor form to pass entire game model, so distinct object is # used without special methods (i.e. step methods) self._data = GameData() self._data.enemies = UnitManager(self.grid.pixels) self._data.obstacles = UnitManager(self.grid.pixels) self._data.towers = self.towers self._data.path = self.path self._data.grid = self.grid
def get_units_in_range(self, enemies: UnitManager, limit=0): """(AbstractEnemy) Yields enemies that are in-range of this tower Parameters: enemies (UnitManager): All enemies in the game Note: For efficiency, method could be extended to first convert to potentially valid buckets, and only search those rather than iterating through every enemy. """ count = 0 for enemy in enemies.get_closish(self.position): if self.is_position_in_range(enemy.position): yield enemy count += 1 if limit == count: break