def handle_command_event(self, event, game): clicked_unit = None clicked_unit_pos = None for unit in game.units: unit_pos = game.cell_to_point(unit.cell) screen_pos = self.to_screen_coords(unit_pos, self.map_panel.get_size()) surface = self.unit_images[type(unit)] surface_size = surface.get_size() rect = rect_from_center_and_size(screen_pos, surface_size) if (rect_contains_point(rect, event.pos) and (clicked_unit is None or unit_pos[1] < clicked_unit_pos[1])): clicked_unit = unit clicked_unit_pos = unit_pos point = self.to_world_coords(event.pos, self.map_panel.get_size()) cell = game.point_to_cell(point) for unit in self.selection: if (unit.speed is not None and (clicked_unit is None or unit.color == clicked_unit.color)): if not pygame.key.get_mods() & KMOD_SHIFT: game.stop_unit(unit) game.add_task(unit, Move(cell)) elif unit.damage is not None and unit.color != clicked_unit.color: if not pygame.key.get_mods() & KMOD_SHIFT: game.stop_unit(unit) game.add_task(unit, Attack(clicked_unit))
def handle_select_event(self, event, game): clicked_unit = None clicked_unit_pos = None for unit in game.units: unit_pos = game.cell_to_point(unit.cell) screen_pos = self.to_screen_coords(unit_pos, self.map_panel.get_size()) surface = self.unit_images[type(unit)] surface_size = surface.get_size() rect = rect_from_center_and_size(screen_pos, surface_size) if (rect_contains_point(rect, event.pos) and (clicked_unit is None or unit_pos[1] < clicked_unit_pos[1])): clicked_unit = unit clicked_unit_pos = unit_pos if clicked_unit is not None: if pygame.key.get_mods() & KMOD_SHIFT: if clicked_unit in self.selection: self.selection.remove(clicked_unit) else: self.selection.add(clicked_unit) else: self.selection.clear() self.selection.add(clicked_unit)
def handle_rect_event(self, old_pos, event, game): if not pygame.key.get_mods() & KMOD_SHIFT: self.selection.clear() selection_rect = normalize_rect((old_pos, event.pos)) for unit in game.units: unit_pos = game.cell_to_point(unit.cell) screen_pos = self.to_screen_coords(unit_pos, self.map_panel.get_size()) surface = self.unit_images[type(unit)] surface_size = surface.get_size() surface_rect = rect_from_center_and_size(screen_pos, surface_size) if rect_intersects_rect(selection_rect, surface_rect): self.selection.add(unit)