def __init__(self, resourcemgr, state=STATES.INGAME, team=None, level=None): self.resourcemgr = resourcemgr self.state = state self.team = team self.teams = [] self.units = {} self.dudes = {} self.structs = {} self.selected = GameGroup() self.active = None if level: self.load_units(level)
class UnitManager(object): def __init__(self, resourcemgr, state=STATES.INGAME, team=None, level=None): self.resourcemgr = resourcemgr self.state = state self.team = team self.teams = [] self.units = {} self.dudes = {} self.structs = {} self.selected = GameGroup() self.active = None if level: self.load_units(level) def load_units(self, filename): try: level = open(filename, 'r') line = level.readline().strip() while line != '[units]': line = level.readline().strip() line = level.readline().strip() while line and line != '[tiles]': l = line.split(' ') x = int(l[0]) * self.resourcemgr.scale y = int(l[1]) * self.resourcemgr.scale k = int(l[2]) t = int(l[3]) unit = Unit(x, y, k, self.resourcemgr, t, self) self.add(unit) line = level.readline().strip() self.teams = [t for t in self.units] level.close() except IOError: pass def add(self, unit): self.units.setdefault(unit.team, GameGroup()).add(unit) if unit.has_flag(FLAGS.DUDE): self.dudes.setdefault(unit.team, GameGroup()).add(unit) else: self.structs.setdefault(unit.team, GameGroup()).add(unit) def input(self, event, *args, **kwargs): if event.type == MOUSEBUTTONUP and event.button == MOUSE.LEFT: self.active = None self.selected.empty() for t in self.teams: self.units[t].input(event, *args, **kwargs) self.active = self.selected.sprites()[0] if len(self.selected) > 0 \ else None def logic(self, camera, *args, **kwargs): for t in self.teams: for u in self.units[t]: u.die(camera) for t in self.teams: self.units[t].logic(*args, **kwargs) self.num_enemies = sum([len(self.units[t]) for t in self.teams if t != self.team]) self.num_units = len(self.units[self.team]) def render(self, *args, **kwargs): for t in self.teams: self.units[t].render(*args, **kwargs) def play_sounds(self, *args, **kwargs): for t in self.teams: self.units[t].play_sounds(*args, **kwargs)