示例#1
0
    def moveUnit(self, unit, deltaX, deltaY, ignoreObstacles=()):
        '''
        Attempts to move the unit by the specified amount, taking into
        account the positions of walls. Also checks if the unit
        changes zones or changes map blocks.

        If the unit hit an obstacle, returns the obstacle.
        This routine only checks for obstacle collisions if unit.isSolid() is
        True.
        '''

        if unit.isSolid():
            lastObstacle, deltaX, deltaY = self.trimPathToObstacle(
                unit, deltaX, deltaY, ignoreObstacles)
        else:
            lastObstacle = None

        newX, newY = unit.pos[0] + deltaX, unit.pos[1] + deltaY
        i, j = MapLayout.getMapBlockIndices(newX, newY)
        zoneBlocks = self.world.zoneBlocks
        if i < 0 or j < 0 or i >= len(zoneBlocks) or j >= len(zoneBlocks[0]):
            if not unit.continueOffMap():
                return
        else:
            # Check for change of zones.
            newBlock = zoneBlocks[i][j]
            newZone = newBlock.getZoneAtPoint(newX, newY)
            if not unit.canEnterZone(newZone):
                return lastObstacle

        # Move the unit.
        unit.pos = (newX, newY)

        return lastObstacle
示例#2
0
    def getZoneAtPoint(self, pt):
        x, y = screenToMapPos(pt, self._focus, self.sRect)

        i, j = MapLayout.getMapBlockIndices(x, y)
        try:
            return self.universe.map.zoneBlocks[i][j].getZoneAtPoint(x, y)
        except IndexError:
            return None
示例#3
0
 def getZoneAtPoint(self, pt):
     if self.getRect().collidepoint(pt):
         x, y = self.screenToMap(pt)
         i, j = MapLayout.getMapBlockIndices(x, y)
         try:
             return self.universe.map.zoneBlocks[i][j].getZoneAtPoint(x, y)
         except IndexError:
             return None
     return None
示例#4
0
    def loadMap(self, filename):
        '''
        Loads a map from the .trosnoth/maps directory.

        @param filename: The filename of the map (with extension)
        '''
        game = self.getGame()
        layout = MapLayout.fromFile(game.layoutDatabase, filename)
        game.world.loadedMap = layout

        return 'Map loaded; it will appear on the next game start.'
    def __init__(self, kind, x, y):
        self.pos = (x, y)  # Pos is the top-left corner of this block.
        assert kind in ('top', 'btm', 'fwd', 'bck')
        self.kind = kind
        self.layout = None

        self.indices = MapLayout.getMapBlockIndices(x, y)

        self.rect = Rect(x, y, 0, 0)
        self.rect.size = self.getSize(kind)

        self.graphics = None
        self.blocked = True  # There's a barrier somewhere depending on type

        # For body block.
        self.zone = None

        # For interface block.
        self.zone1 = None
        self.zone2 = None
示例#6
0
文件: utils.py 项目: iliescufm/pygame
def getBlocksInRect(world, rect):
    '''
    Returns an iterator of the blocks in the given rect.
    '''
    i, j0 = MapLayout.getMapBlockIndices(rect.left, rect.top)
    j0 = max(0, j0)
    i = max(0, i)

    while i < len(world.zoneBlocks):
        row = world.zoneBlocks[i]
        j = j0
        while j < len(row):
            block = row[j]
            blockLeft, blockTop = block.defn.pos
            if blockTop >= rect.bottom:
                return
            if blockLeft >= rect.right:
                break
            yield block
            j += 1
        i += 1
示例#7
0
    def __init__(self, kind, x, y):
        self.pos = (x, y)  # Pos is the top-left corner of this block.
        assert kind in ('top', 'btm', 'fwd', 'bck')
        self.kind = kind
        self.layout = None

        self.obstacles = []
        self.ledges = []
        self.indices = MapLayout.getMapBlockIndices(x, y)

        self.rect = pygame.Rect(x, y, 0, 0)
        self.rect.size = (self._getWidth(), MapLayout.halfZoneHeight)

        self.graphics = None
        self.blocked = True  # There's a barrier somewhere depending on type

        self.nextObstacleId = 0

        # For body block.
        self.zone = None

        # For interface block.
        self.zone1 = None
        self.zone2 = None
示例#8
0
    def drawZones(self, screen, sRect, forceColour=None):
        '''Draws the miniMap graphics onto the screen'''
        topCorner = [
            self._focus[i] - (sRect.size[i] / 2 * self.scale) for i in (0, 1)
        ]
        # Find which map blocks are on the screen.
        i, j = MapLayout.getMapBlockIndices(*topCorner)
        i = max(0, i)
        j = max(0, j)
        firstBlock = self.universe.zoneBlocks[i][j]

        # Set the initial position back to where it should be.
        firstPos = self.mapPosToMinimap(sRect, firstBlock.defn.rect.topleft)
        firstPos = [min(firstPos[a], sRect.topleft[a]) for a in (0, 1)]

        posToDraw = [firstPos[a] for a in (0, 1)]
        y, x = i, j
        while posToDraw[1] < sRect.bottom:
            while posToDraw[0] < sRect.right:
                try:
                    block = self.universe.zoneBlocks[y][x]
                except IndexError:
                    break
                zone = None
                if isinstance(block, mapblocks.InterfaceMapBlock):
                    currentRect = self.interfaceBlockRect
                elif isinstance(block, mapblocks.BottomBodyMapBlock):
                    currentRect = self.bodyBlockRect
                    zone = block.zone
                else:
                    currentRect = self.bodyBlockRect
                currentRect.topleft = posToDraw
                area = currentRect.clip(sRect)
                draw = True
                if area.size == currentRect.size:
                    # Nothing has changed.
                    self._drawBlockMiniBg(block,
                                          screen,
                                          sRect,
                                          currentRect,
                                          forceColour=forceColour)
                elif area.size == (0, 0):
                    # Outside the bounds of the minimap
                    draw = False
                else:
                    self._drawBlockMiniBg(block,
                                          screen,
                                          sRect,
                                          currentRect,
                                          area,
                                          forceColour=forceColour)

                if draw and zone:
                    self.drawZoneDecoration(zone, screen, sRect,
                                            currentRect.midtop)

                posToDraw[0] += currentRect.width
                x += 1
            x = j
            y += 1
            # Next Row
            posToDraw[0] = firstPos[0]
            posToDraw[1] += self.interfaceBlockRect.height
示例#9
0
 def __init__(self):
     self.layoutDb = LayoutDatabase()
     self.world = Universe(self.layoutDb)
     self.world.setLayout(MapLayout(3, 4, False))
     self.blockDefs = []
     self.blockCombos = []