def initiateActionEvent(self,event):
        pos = event.pos

        if self.minimap.rect.collidepoint(pos):
            mapClickPoint = self.minimap.clickToGridPos(pos)
            if mapClickPoint is not None:
                return 
            else:            
                cartPos = specialMath.isoToCart(pos)
                destCart = self.cartScrollLoc[0] + cartPos[0], \
                            self.cartScrollLoc[1] + cartPos[1]
        else:
            cartPos = specialMath.isoToCart(pos)
            destCart = self.cartScrollLoc[0] + cartPos[0], \
                        self.cartScrollLoc[1] + cartPos[1]
        
        clicked = specialMath.closestEntity(self.viewportEntities,pos)
        
        if clicked:
            drawRect = clicked.rect.move(clicked.drawOffset)
            drawRect.center = specialMath.cartToIso(drawRect.center)
        
            if not drawRect.collidepoint(pos):
                clicked = None
        
        if clicked is not None:
            self.currentMenu = self.contextualMenu.getMenu(self.selectedEntities,clicked)
        else:
            self.currentMenu = self.contextualMenu.getMenu(self.selectedEntities,WayPoint(*destCart))
            
        if self.currentMenu is not None:
            self.currentMenu.open(event.pos)
예제 #2
0
 def _gridPosToDrawPos(self,point):
     """
     Takes a cartesian position on the internal state grid and
     converts it to a drawing position on the minimap.
     """
     gridPos = float(point[0])/self.tileSize[1],float(point[1])/self.tileSize[1]
     rawPos = specialMath.cartToIso(gridPos)
     return self.offsetToDraw( \
             ( int(self.scale*rawPos[0]),int(self.scale*rawPos[1]) ) \
             )
 def completeActionEvent(self,event):
     attacking = False
     pos = event.pos
     
     # Performs action indicated by menu if menu is visible
     # and exists.  Otherwise, the menu reference is destroyed.
     if self.currentMenu is not None and self.currentMenu.visible:
         self.selectMenu(pos)
         return # Do not do anything else if the menu is selected
     else:
         self.currentMenu = None
     
     # Sets destination in cartesian coordinates
     # Handles minimap clicks
     if self.minimap.rect.collidepoint(pos):
         mapClickPoint = self.minimap.clickToGridPos(pos)
         if mapClickPoint is not None:
             destCart = mapClickPoint
         else:
             cartPos = specialMath.isoToCart(pos)
             destCart = (self.cartScrollLoc[0] + cartPos[0])%self.worldSize[0], \
                         (self.cartScrollLoc[1] + cartPos[1])%self.worldSize[1]
     else:
         cartPos = specialMath.isoToCart(pos)
         destCart = (self.cartScrollLoc[0] + cartPos[0])%self.worldSize[0], \
                     (self.cartScrollLoc[1] + cartPos[1])%self.worldSize[1]
     
     # Determines closest entity to a click
     clicked = specialMath.closestEntity(self.viewportEntities,pos)
     
     if clicked:
         drawRect = clicked.rect.move(clicked.drawOffset)
         drawRect.center = specialMath.cartToIso(drawRect.center)
     
         if not drawRect.collidepoint(pos):
             clicked = None
     
     # clicked is now either None or the closest Entity to the click
     if clicked:
         for selected in self.selectedEntities:
             attacking=True
             if isinstance(selected,Unit):
                 selected.initAction(clicked)
    
     if not attacking:
         eCenter = specialMath.centerOfEntityList(self.selectedEntities, self.worldSize)
         for entity in self.selectedEntities:
             if not entity.status==Locals.MOVING:
                 entity.dest=entity.realCenter
             if entity.movable: entity.status=Locals.MOVING
             dx = entity.rect.center[0] - eCenter[0]
             dy = entity.rect.center[1] - eCenter[1]
             newLoc = (dx+destCart[0],dy+destCart[1])
             entity.addToPath(newLoc)
    def clickEvent(self,event):
        """
        What works:
        single - clicking on units
        click on ground to deselect all (without a modifier)
        click a unit while holding a modifier to add to the selection
        click a selected unit while holding a modifier to remove from the selection
        """
        
        pos = event.pos
        
        if self.minimap.rect.collidepoint(pos):
            mapClickPoint = self.minimap.clickToGridPos(pos)
            if mapClickPoint is not None:
                self._setCartScrollLocation(mapClickPoint)
                return
        cartPos = specialMath.isoToCart(pos)
        destCart = (self.cartScrollLoc[0] + cartPos[0])/self.worldSize[0], \
                       (self.cartScrollLoc[1] + cartPos[1])/self.worldSize[1]
        
        # MAY BREAK THINGS - CHECK
        #clicked = specialMath.closestEntity(self.viewportEntities,pos)
        clicked = specialMath.closestEntity(self.myViewportEntities,pos)
        
        if clicked:
            drawRect = clicked.rect.move(clicked.drawOffset)
            drawRect.center = specialMath.cartToIso(drawRect.center)
            
            selectRect=clicked.getSelectionRect(drawRect)
            
            if not selectRect.collidepoint(pos):
                clicked = None
        
        if isinstance(event,Event.SelectionEvent):
            for e in self.selectedEntities:
                e.deselect()

            self.selectedEntities = []
            self._selectedEntitiesChanged = True

        if clicked and self.ownsEntity(clicked):
            # Determines if the closest entity is already selected.
            # If it is, it makes it no longer selected.
            if clicked.selected:
                clicked.deselect()
                self.selectedEntities.remove(clicked)
            else:
                clicked.select()
                self.selectedEntities.append(clicked)
                #print clicked.healthStr()
                #if isinstance(clicked, Unit): print '\n' + str(clicked.inventory)
            self._selectedEntitiesChanged = True
 def setFocusedEntities(self):
     rawMouseLoc = pygame.mouse.get_pos()
     viewportMouseLoc = rawMouseLoc[0]-self.loc[0],rawMouseLoc[1]-self.loc[1]
     
     for e in self.viewportEntities:
         
         drawRect = e.rect.move(e.drawOffset)
         drawRect.center = specialMath.cartToIso(drawRect.center)
         
         selectRect = e.getSelectionRect(drawRect)
         
         if selectRect.collidepoint(viewportMouseLoc):
             e.focus()
 def scrollBasedOnElapsedTime(self,elapsedTime):
     
     if not self.world == None and self.currentMenu == None:# FIXME and self.dragRect == None:
         newScrollLoc = list(self.scrollLoc)
         scrollAddX = self.scrollSpeed[0]*elapsedTime
         scrollAddY = self.scrollSpeed[1]*elapsedTime
         if not self.dragRect == None:
             self.dragRect.scroll((scrollAddX,scrollAddY)) 
         newScrollLoc[0] = (newScrollLoc[0]+scrollAddX)
         newScrollLoc[1] = (newScrollLoc[1]+scrollAddY)
         
         # used to calculate corner of scroll location in cartesian grid
         self.cartScrollLoc = self.isoToWrappedCart(newScrollLoc)
         
         newScrollLoc = specialMath.cartToIso(self.cartScrollLoc)
         self.scrollLoc = tuple(newScrollLoc)
예제 #7
0
    def _updateBaseSurface(self):
        """
        Initializes the self.baseSurface attribute.  This stores
        the image of the map.
        """

        #fill base surface with alphaColor -> transparent
        pygame.draw.rect(self.baseSurface,self.alphaColor,self.rect,0)

        for y in xrange(self.gridSize[1]):
            for x in xrange(self.gridSize[0]):
                
                # Grab average color value of the grid at loc x,y
                curColor = (self.gridDict[(x,y)]).getMiniMapColor()
                
                # Calculate corners of polygons
                topleft = specialMath.cartToIso((x*self.scale,y*self.scale))
                topright = specialMath.cartToIso(((x+1)*self.scale,y*self.scale))
                bottomright = specialMath.cartToIso(((x+1)*self.scale,(y+1)*self.scale))
                bottomleft = specialMath.cartToIso((x*self.scale,(y+1)*self.scale))
                
                # Apply offsets
                topleft = self.offsetToDraw(topleft)
                topright = self.offsetToDraw(topright)
                bottomright = self.offsetToDraw(bottomright)
                bottomleft = self.offsetToDraw(bottomleft)
                
                # Draws minimap to baseSurface
                pygame.draw.polygon(self.baseSurface, curColor, \
                    [topleft,topright,bottomright,bottomleft])
        
        tl = specialMath.cartToIso((0,0))
        tr = specialMath.cartToIso((self.gridSize[0]*self.scale,0))
        br = specialMath.cartToIso((self.gridSize[0]*self.scale,self.gridSize[1]*self.scale))
        bl = specialMath.cartToIso((0,self.gridSize[1]*self.scale))
        
        tl = (tl[0]+self.xOffset-self.borderWidth,tl[1]+self.yOffset)
        tr = (tr[0]+self.xOffset,tr[1]+self.yOffset)#-self.borderWidth)
        br = (br[0]+self.xOffset+self.borderWidth,br[1]+self.yOffset)
        bl = (bl[0]+self.xOffset,bl[1]+self.yOffset)#+self.borderWidth)
        
        if self.borderWidth:
            pygame.draw.polygon(self.baseSurface,self.borderColor, \
                [tl,tr,br,bl],self.borderWidth)
예제 #8
0
    def getScreenEntities(self,viewRects):
        """
        Receives a list of lists of coordinate tuples which define the
        coordinates of one view rectangle (the screen) in cartesian
        coordinates.
        
        Returns a list of references to entities which are visible
        in the given rectangles (called by the viewport).
        """

        # List of tuples - y position of rectangle (bottom) and entity
        entitySortList = []

        for entity in self.allEntities.values():
            
            # check all view rectangles
            for view in viewRects:
                
                # does the entity rectangle (cartesian) collide with
                # the rectangle of the screen transformed from isometric
                # to cartesian?
                if self.collideRectDiamond(entity.rect,view):
                    
                    # if it does, append it to the list
                    entitySortList.append((specialMath.cartToIso(entity.rect.center),entity))
                    
                    # set the drawOffset attribute of the entity for
                    # ease of drawing in the correct spot by the viewport
                    # once the list of references is returned
                    entity.drawOffset=-view[0][0],-view[0][1]
                    
                    break # if it collides, go to next loop

        # sort the list by y-position, such that the returned list
        # is in the correct order for drawing
        entitySortList.sort()
        
        # return only the list of entity references
        if len(entitySortList) > 0:
            ypos, screenEntities = zip(*entitySortList)
        else:
            screenEntities = []
            
        return screenEntities
    def dragSelect(self,event):
        """
        Fill this in.
        """
        if self.dragRect is not None:
            start = event.start
            end = event.curr
            
            if isinstance(event,Event.DragCompletedEvent):
                for e in self.selectedEntities:
                    e.deselect()
                    self._selectedEntitiesChanged = True
                self.selectedEntities = []
            #else: pass # if it is an Event.AddDragCompletedEvent, do
            # # not deselect
            
            #if self.dragRect.isOffScreen(self.size):
            #    searchList = self.world.allEntities.values()
            #else:
            #    searchList = self.viewportEntities
            searchList = self.myViewportEntities
            
            for entity in searchList:
            
                drawRect = entity.rect.move(entity.drawOffset)
                drawRect.center = specialMath.cartToIso(drawRect.center)
                
                selectRect=entity.getSelectionRect(drawRect)
            
                if selectRect.colliderect(MakeBoundingBox(start,end)):

                    if isinstance(event,Event.DragCompletedEvent):
                        entity.select()
                        self.selectedEntities.append(entity)
                        self._selectedEntitiesChanged = True
                    else: # Add drag completed event
                        if entity not in self.selectedEntities:
                            entity.select()
                            self.selectedEntities.append(entity)
                            self._selectedEntitiesChanged = True
예제 #10
0
 def _setCartScrollLocation(self,newCartLoc):
     self.cartScrollLoc = tuple(newCartLoc)
     self.scrollLoc = specialMath.cartToIso(self.cartScrollLoc)