Exemplo n.º 1
0
	def renderBoundingBox(self, obj):
		"""
		Just a hack to render an objects bounding box.
		"""
		
		bbox = copy.copy(obj.boundingbox)
		
		#apply the object layer scale
		bbox.x /= 0.25
		bbox.y /= 0.25
		bbox.w /= 0.25
		bbox.h /= 0.25
	
		obj_topleft = fife.ExactModelCoordinate(bbox.x, bbox.y)
		obj_topright = fife.ExactModelCoordinate(bbox.x + bbox.w, bbox.y)
		obj_bottomright = fife.ExactModelCoordinate(bbox.x + bbox.w, bbox.y + bbox.h)
		obj_bottomleft = fife.ExactModelCoordinate(bbox.x, bbox.y + bbox.h)
		
		loc_topleft = fife.Location()
		loc_topleft.setLayer(self._map.getLayer('boundingbox'))
		loc_topleft.setExactLayerCoordinates(obj_topleft)
		
		loc_topright = fife.Location()
		loc_topright.setLayer(self._map.getLayer('boundingbox'))
		loc_topright.setExactLayerCoordinates(obj_topright)
		
		loc_bottomright = fife.Location()
		loc_bottomright.setLayer(self._map.getLayer('boundingbox'))
		loc_bottomright.setExactLayerCoordinates(obj_bottomright)
		
		loc_bottomleft = fife.Location()
		loc_bottomleft.setLayer(self._map.getLayer('boundingbox'))
		loc_bottomleft.setExactLayerCoordinates(obj_bottomleft)
		
		node_topleft = fife.GenericRendererNode(loc_topleft)
		node_topright = fife.GenericRendererNode(loc_topright)
		node_bottomright = fife.GenericRendererNode(loc_bottomright)
		node_bottomleft = fife.GenericRendererNode(loc_bottomleft)


		self._genericrenderer.addLine("quads", node_topleft, node_topright, 255, 255, 255)
		self._genericrenderer.addLine("quads", node_topright, node_bottomright, 255, 255, 255)
		self._genericrenderer.addLine("quads", node_bottomright, node_bottomleft, 255, 255, 255)
		self._genericrenderer.addLine("quads", node_bottomleft, node_topleft, 255, 255, 255)
		
		#had to do this or it would segfault
		obj_topleft.thisown = 0
		obj_topright.thisown = 0
		obj_bottomright.thisown = 0
		obj_bottomleft.thisown = 0
		loc_topleft.thisown = 0
		loc_topright.thisown = 0
		loc_bottomright.thisown = 0
		loc_bottomleft.thisown = 0				
		node_topleft.thisown = 0
		node_topright.thisown = 0
		node_bottomright.thisown = 0
		node_bottomleft.thisown = 0
Exemplo n.º 2
0
 def update_cam(self):
     """Redraw camera border."""
     if self.world is None or not self.world.inited:
         return  # don't draw while loading
     self.renderer.removeAll("minimap_cam_border")
     # draw rect for current screen
     displayed_area = self.session.view.get_displayed_area()
     minimap_corners_as_renderer_node = []
     for corner in displayed_area.get_corners():
         # check if the corners are outside of the screen
         corner = list(corner)
         if corner[0] > self.world.max_x:
             corner[0] = self.world.max_x
         if corner[0] < self.world.min_x:
             corner[0] = self.world.min_x
         if corner[1] > self.world.max_y:
             corner[1] = self.world.max_y
         if corner[1] < self.world.min_y:
             corner[1] = self.world.min_y
         corner = tuple(corner)
         minimap_coords = self._get_rotated_coords(
             self._world_coord_to_minimap_coord(corner))
         minimap_corners_as_renderer_node.append( fife.GenericRendererNode( \
           fife.Point(*minimap_coords) ) )
     for i in xrange(0, 3):
         self.renderer.addLine("minimap_cam_border", minimap_corners_as_renderer_node[i], \
                          minimap_corners_as_renderer_node[i+1], *self.colors[self.cam_border])
     # close the rect
     self.renderer.addLine("minimap_cam_border", minimap_corners_as_renderer_node[3], \
                      minimap_corners_as_renderer_node[0], *self.colors[self.cam_border])
Exemplo n.º 3
0
 def draw_health(self):
     """Draws the units current health as a healthbar over the unit."""
     renderer = self.session.view.renderer['GenericRenderer']
     renderer.removeAll("health_" + str(self.worldid))
     zoom = self.session.view.get_zoom()
     height = int(5 * zoom)
     width = int(50 * zoom)
     y_pos = int(self.health_bar_y * zoom)
     mid_node_up = fife.GenericRendererNode(self._instance, \
            fife.Point(-width/2+int(((self.health/self.max_health)*width)),\
                                            y_pos-height)
                                 )
     mid_node_down = fife.GenericRendererNode(self._instance, \
                                              fife.Point(
                                                  -width/2+int(((self.health/self.max_health)*width))
                                                  ,y_pos)
                                              )
     if self.health != 0:
         renderer.addQuad("health_" + str(self.worldid), \
                         fife.GenericRendererNode(self._instance, \
                                                  fife.Point(-width/2, y_pos-height)), \
                         mid_node_up, \
                         mid_node_down, \
                         fife.GenericRendererNode(self._instance, fife.Point(-width/2, y_pos)), \
                         0, 255, 0)
     if self.health != self.max_health:
         renderer.addQuad("health_" + str(self.worldid), mid_node_up, \
                          fife.GenericRendererNode(self._instance, fife.Point(width/2, y_pos-height)), \
                          fife.GenericRendererNode(self._instance, fife.Point(width/2, y_pos)), \
                          mid_node_down, 255, 0, 0)
Exemplo n.º 4
0
    def go(self, x, y):
        """Moves the ship.
		This is called when a ship is selected and RMB is pressed outside the ship"""
        self.stop()

        #disable the trading route
        if hasattr(self, 'route'):
            self.route.disable()
        ship_id = self.worldid  # this has to happen here,

        # cause a reference to self in a temporary function is implemented
        # as a hard reference, which causes a memory leak
        def tmp():
            if self.session.world.player == self.owner:
                self.session.view.renderer['GenericRenderer'].removeAll(
                    "buoy_" + str(ship_id))

        tmp()
        move_target = Point(int(round(x)), int(round(y)))
        try:
            self.move(move_target, tmp)
        except MoveNotPossible:
            # find a near tile to move to
            target_found = False
            surrounding = Circle(move_target, radius=0)
            while not target_found and surrounding.radius < 4:
                surrounding.radius += 1
                for move_target in surrounding:
                    try:
                        self.move(move_target, tmp)
                    except MoveNotPossible:
                        continue
                    target_found = True
                    break
        if self.session.world.player == self.owner:
            if self.position.x != move_target.x or self.position.y != move_target.y:
                move_target = self.get_move_target()
            if move_target is not None:
                loc = fife.Location(self.session.view.layers[LAYERS.OBJECTS])
                loc.thisown = 0
                coords = fife.ModelCoordinate(move_target.x, move_target.y)
                coords.thisown = 0
                loc.setLayerCoordinates(coords)
                self.session.view.renderer['GenericRenderer'].addAnimation(
                    "buoy_" + str(self.worldid), fife.GenericRendererNode(loc),
                    horizons.main.fife.animationpool.addResourceFromFile(
                        "as_buoy0-idle-45"))
Exemplo n.º 5
0
	def select(self, reset_cam=False):
		"""Runs necessary steps to select the unit."""
		self.session.view.renderer['InstanceRenderer'].addOutlined(self._instance, 255, 255, 255, 1)
		# add a buoy at the ship's target if the player owns the ship
		if self.is_moving() and self.session.world.player == self.owner:
			loc = fife.Location(self.session.view.layers[LAYERS.OBJECTS])
			loc.thisown = 0 # thisown = 0 because the genericrenderernode might delete it
			move_target = self.get_move_target()
			coords = fife.ModelCoordinate(move_target.x, move_target.y)
			coords.thisown = 1 # thisown = 1 because setLayerCoordinates will create a copy
			loc.setLayerCoordinates(coords)
			self.session.view.renderer['GenericRenderer'].addAnimation(
				"buoy_" + str(self.worldid), fife.GenericRendererNode(loc),
				horizons.main.fife.animationpool.addResourceFromFile("as_buoy0-idle-45")
			)
		self.draw_health()
		if reset_cam:
			self.session.view.set_location(self.position.to_tuple())
		self.session.view.add_change_listener(self.draw_health)
Exemplo n.º 6
0
    def mouseDragged(self, evt):
        if evt.getButton() == fife.MouseEvent.LEFT and hasattr(
                self, 'select_begin'):
            do_multi = ((self.select_begin[0] - evt.getX())**2 +
                        (self.select_begin[1] - evt.getY())**
                        2) >= 10  # ab 3px (3*3 + 1)
            self.session.view.renderer['GenericRenderer'].removeAll("select")
            if do_multi:
                a = fife.Point(min(self.select_begin[0], evt.getX()), \
                        min(self.select_begin[1], evt.getY()))
                b = fife.Point(max(self.select_begin[0], evt.getX()), \
                        min(self.select_begin[1], evt.getY()))
                c = fife.Point(max(self.select_begin[0], evt.getX()), \
                        max(self.select_begin[1], evt.getY()))
                d = fife.Point(min(self.select_begin[0], evt.getX()), \
                        max(self.select_begin[1], evt.getY()))
                self.session.view.renderer['GenericRenderer'].addLine("select", \
                                                                      fife.GenericRendererNode(a), fife.GenericRendererNode(b), 200, 200, 200)
                self.session.view.renderer['GenericRenderer'].addLine("select", \
                                                                      fife.GenericRendererNode(b), fife.GenericRendererNode(c), 200, 200, 200)
                self.session.view.renderer['GenericRenderer'].addLine("select", \
                                                                      fife.GenericRendererNode(d), fife.GenericRendererNode(c), 200, 200, 200)
                self.session.view.renderer['GenericRenderer'].addLine("select", \
                                                                      fife.GenericRendererNode(a), fife.GenericRendererNode(d), 200, 200, 200)
            selectable = []
            instances = self.session.view.cam.getMatchingInstances(\
             fife.Rect(min(self.select_begin[0], evt.getX()), \
                  min(self.select_begin[1], evt.getY()), \
                  abs(evt.getX() - self.select_begin[0]), \
                  abs(evt.getY() - self.select_begin[1])) if do_multi else fife.ScreenPoint(evt.getX(), evt.getY()), self.session.view.layers[LAYERS.OBJECTS])
            # Only one unit, select anyway
            if len(instances) == 1:
                instance = WorldObject.get_object_by_id(
                    int(instances[0].getId()))
                if instance.is_selectable:
                    selectable.append(instance)
            else:
                for i in instances:
                    instance = WorldObject.get_object_by_id(int(i.getId()))
                    if instance.is_selectable and instance.owner == self.session.world.player:
                        selectable.append(instance)

            if len(selectable) > 1:
                if do_multi:
                    for instance in selectable[:]:  # iterate through copy for safe removal
                        if instance.is_building:
                            selectable.remove(instance)
                else:
                    selectable = [selectable.pop(0)]

            if do_multi:
                selectable = set(self.select_old | frozenset(selectable))
            else:
                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
        elif (evt.getButton() == fife.MouseEvent.RIGHT):
            pass
        else:
            super(SelectionTool, self).mouseDragged(evt)
            return
        evt.consume()