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)
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 cartPointTupleOfScreen(self): """ Returns a tuple of the Cartesian points of the current view into the world. Order: topleft,topright,bottomright,bottomleft """ l,t = self.cartScrollLoc w,h = self.size cartWidthVector=specialMath.isoToCart((w,0)) cartHeightVector=specialMath.isoToCart((0,h)) cartTopLeft=l,t cartTopRight=l+cartWidthVector[0],t+cartWidthVector[1] cartBottomRight=l+cartWidthVector[0]+cartHeightVector[0],t+cartWidthVector[1]+cartHeightVector[1] cartBottomLeft=l+cartHeightVector[0],t+cartHeightVector[1] return cartTopLeft,cartTopRight,cartBottomRight,cartBottomLeft
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 _drawPosToGridPos(self,point): """ Takes a click on the screen, and returns the point on the Cartesian internal space which has been clicked, or None if the minimap was not clicked. """ point = (point[0]-self.loc[0],point[1]-self.loc[1]) #Added this to correct the clicking code with a new map loc - Julian unOffsetPoint = (point[0] - self.xOffset, point[1] - self.yOffset) unscaledPoint = unOffsetPoint[0]/self.scale, unOffsetPoint[1]/self.scale cartPoint = specialMath.isoToCart(unscaledPoint) gridPos = int(cartPoint[0]*self.tileSize[1]),int(cartPoint[1]*self.tileSize[1]) if 0 <= gridPos[0] <= self.gridDim[0] and 0 <= gridPos[1] <= self.gridDim[1]: return gridPos return None
def __init__(self,world,manager,scrollLoc,screenPos,size,clientID): self.world = world self.manager = manager self.scrollLoc = scrollLoc self.cartScrollLoc = specialMath.isoToCart(scrollLoc) self.loc = screenPos self.size = size self.rect = pygame.Rect(screenPos,size) self.hud=None self.clientID = clientID if world is not None: self.minimap = MiniMap(self.world,clientID=clientID) self.worldSize=self.world.grid.getCartGridDimensions() else: self.minimap = None #FIXME - SHOULD COME FROM A CONFIG FILE self.scrollSensitivity=.001 self.initDeadZoneBasedOnSize() self.surface = pygame.Surface(size) self.surface.set_clip(((0,0),size)) self.scrollSpeed = [0,0] self.selectedEntities = [] self.viewportEntities = [] self.myViewportEntities = [] self.quickSelect = {} for i in xrange(pygame.K_0,pygame.K_9+1): self.quickSelect[i] = pygame.sprite.Group() self.dragRect = None self.currentMenu = None from ContextualMenu import getCGDcontextualMenu self.contextualMenu = getCGDcontextualMenu() self._selectedEntitiesChanged = False self.gameOver = False
def isoToWrappedCart(self,isoCoord): """ Returns a wrapped cartesian coordinate from an isometric coordinate. """ return self.cartWrap(specialMath.isoToCart(isoCoord))