示例#1
0
    def mousePressed(self, evt):
        if evt.getButton(
        ) == fife.MouseEvent.RIGHT and not evt.isConsumedByWidgets():
            target_mapcoord = self.session.view.cam.toMapCoordinates(
                fife.ScreenPoint(evt.getX(), evt.getY()), False)

            target = self._get_attackable_instance(evt)

            if target:
                if not self.session.world.diplomacy.are_enemies(
                        self.session.world.player, target.owner):
                    AddEnemyPair(self.session.world.player,
                                 target.owner).execute(self.session)
                for i in self.session.selected_instances:
                    if hasattr(i, 'attack'):
                        Attack(i, target).execute(self.session)
            else:
                for i in self.session.selected_instances:
                    if i.movable:
                        Act(i, target_mapcoord.x,
                            target_mapcoord.y).execute(self.session)
            evt.consume()
        else:
            self.deselect_at_end = False
            super().mousePressed(evt)
示例#2
0
	def mousePressed(self, evt):
		if evt.isConsumedByWidgets():
			super(SelectionTool, self).mousePressed(evt)
			return
		elif evt.getButton() == fife.MouseEvent.LEFT:
			if self.session.selected_instances is None:
				# this is a very odd corner case, it should only happen after the session has been ended
				# we can't allow to just let it crash however
				print 'WARNING: selected_instance is None. Please report this!'
				import traceback
				traceback.print_stack()
				print 'WARNING: selected_instance is None. Please report this!'
				return
			instances = self.get_hover_instances(evt)
			self.select_old = frozenset(self.session.selected_instances) if evt.isControlPressed() else frozenset()

			instances = filter(self.is_selectable, instances)
			#on single click only one building should be selected from the hover_instances
			#the if is for [] and [single_item] cases (they crashed)
			#it acts as user would expect (instances[0] selects buildings in front first)
			instances = instances if len(instances) <= 1 else [instances[0]]

			self._update_selection(instances)

			self.select_begin = (evt.getX(), evt.getY())
			self.session.ingame_gui.hide_menu()
		elif evt.getButton() == fife.MouseEvent.RIGHT:
			target_mapcoord = self.get_exact_world_location(evt)
			for i in self.session.selected_instances:
				if i.movable:
					Act(i, target_mapcoord.x, target_mapcoord.y).execute(self.session)
		else:
			super(SelectionTool, self).mousePressed(evt)
			return
		evt.consume()
	def on_click(self, event, drag):
		"""Handler for clicks (pressed and dragged)
		Scrolls screen to the point, where the cursor points to on the minimap.
		Overwrite this method to your convenience.
		"""
		if self.preview:
			return # we don't do anything in this mode
		map_coord = event.map_coord
		moveable_selecteds = [ i for i in self.session.selected_instances if i.movable ]
		if moveable_selecteds and event.getButton() == fife.MouseEvent.RIGHT:
			if drag:
				return
			for i in moveable_selecteds:
				Act(i, *map_coord).execute(self.session)
		elif event.getButton() == fife.MouseEvent.LEFT:
			if self.view is None:
				print "Warning: Can't handle minimap clicks since we have no view object"
			else:
				self.view.center(*map_coord)
示例#4
0
 def mousePressed(self, evt):
     if evt.isConsumedByWidgets():
         super(SelectionTool, self).mousePressed(evt)
         return
     elif evt.getButton() == fife.MouseEvent.LEFT:
         selectable = []
         instances = self.session.view.cam.getMatchingInstances(\
          fife.ScreenPoint(evt.getX(), evt.getY()), self.session.view.layers[LAYERS.OBJECTS])
         for i in instances:
             # Check id, can be '' if instance is created and clicked on before
             # actual game representation class is created (network play)
             id = i.getId()
             if id == '':
                 continue
             instance = WorldObject.get_object_by_id(int(id))
             if instance.is_selectable:
                 selectable.append(instance)
         if len(selectable) > 1:
             selectable = selectable[0:0]
         self.select_old = frozenset(
             self.session.selected_instances) if evt.isControlPressed(
             ) else frozenset()
         selectable = set(self.select_old ^ frozenset(selectable))
         for instance in self.session.selected_instances - selectable:
             instance.deselect()
         for instance in selectable - self.session.selected_instances:
             instance.select()
         self.session.selected_instances = selectable
         self.select_begin = (evt.getX(), evt.getY())
         self.session.ingame_gui.hide_menu()
     elif evt.getButton() == fife.MouseEvent.RIGHT:
         target_mapcoord = self.session.view.cam.toMapCoordinates(\
          fife.ScreenPoint(evt.getX(), evt.getY()), False)
         for i in self.session.selected_instances:
             if i.movable:
                 Act(i, target_mapcoord.x,
                     target_mapcoord.y).execute(self.session)
     else:
         super(SelectionTool, self).mousePressed(evt)
         return
     evt.consume()
示例#5
0
from horizons.util.shapes import Point

from tests.gui import cooperative


def get_player_ship(session):
    """Returns the first ship of a player."""
    for ship in session.world.ships:
        if ship.owner == session.world.player:
            return ship
    raise Exception('Player ship not found')


def move_ship(ship, (x, y)):
    """Move ship to coordinates and wait until it arrives."""
    Act(ship, x, y)(ship.owner)

    while (ship.position.x, ship.position.y) != (x, y):
        cooperative.schedule()


def found_settlement(gui, ship_pos, (x, y)):
    """Move ship to coordinates and build a warehouse."""
    ship = get_player_ship(gui.session)
    gui.select([ship])

    move_ship(ship, ship_pos)

    # Found a settlement
    gui.trigger('overview_trade_ship', 'found_settlement')
    assert isinstance(gui.cursor, BuildingTool)