Exemple #1
0
    def setSomeSpawnCoordinates(self, renderable, direction):
        # X
        if direction is Direction.right:
            myx = self.viewport.getx() + Config.columns + 1
        else:
            myx = self.viewport.getx() - 1 - renderable.texture.width

        minY = Config.areaMoveable['miny']
        maxY = Config.areaMoveable['maxy']
        myy = random.randint(minY, maxY)

        renderable.setLocation(Coordinates(myx, myy))

        # if enemies overlap, move them sideway away from player
        spotFree = False
        while not spotFree:
            if EntityFinder.isDestinationEmpty(
                world=self.world, renderable=renderable
            ):
                spotFree = True
            else:
                if direction is Direction.right:
                    renderable.coordinates.x += 3
                else:
                    renderable.coordinates.x -= 3
Exemple #2
0
    def movePlayer(self):
        playerEntity = EntityFinder.findPlayer(self.world)
        if playerEntity is None:
            return

        for msg in directMessaging.getByType(DirectMessageType.movePlayer):
            # usually just one msg...
            playerGroupId = self.world.component_for_entity(
                playerEntity, system.groupid.GroupId)
            playerRenderable = self.world.component_for_entity(
                playerEntity, system.graphics.renderable.Renderable)

            x = msg.data['x']
            y = msg.data['y']
            dontChangeDirection = msg.data['dontChangeDirection']
            whenMoved = msg.data['whenMoved']

            # only turn, dont walk
            if not dontChangeDirection and Config.turnOnSpot:
                if playerRenderable.direction is Direction.left and x > 0:
                    self.updateRenderableDirection(playerRenderable,
                                                   Direction.right,
                                                   playerGroupId.getId())
                    continue
                if playerRenderable.direction is Direction.right and x < 0:
                    self.updateRenderableDirection(playerRenderable,
                                                   Direction.left,
                                                   playerGroupId.getId())
                    continue

            playerRenderable.storeCoords()
            playerRenderable.changeLocationFromStored(x, y)

            canMove = EntityFinder.isDestinationEmpty(self.world,
                                                      playerRenderable)
            if not canMove:
                # try with one step, instead of the original two
                if x > 0:
                    x -= 1
                    playerRenderable.changeLocationFromStored(x, y)
                elif x < 0:
                    x += 1
                    playerRenderable.changeLocationFromStored(x, y)

                canMove = EntityFinder.isDestinationEmpty(
                    self.world, playerRenderable)
                if not canMove:
                    # we are stuck
                    playerRenderable.restoreCoords()
                    continue
                else:
                    # can move with new coordinates
                    pass

            playerRenderable.restoreCoords()

            didMove = self.moveRenderable(playerRenderable,
                                          playerGroupId.getId(), x, y,
                                          dontChangeDirection)

            if didMove:
                if whenMoved == "showAppearEffect":
                    locCenter = playerRenderable.getLocationCenter()
                    messaging.add(type=MessageType.EmitMirageParticleEffect,
                                  data={
                                      'location': locCenter,
                                      'effectType': ParticleEffectType.appear,
                                      'damage': 0,
                                      'byPlayer': True,
                                      'direction': Direction.none,
                                  })
                if whenMoved == "showOnKnockback":
                    pass  # for now

                extcords = ExtCoordinates(playerRenderable.coordinates.x,
                                          playerRenderable.coordinates.y,
                                          playerRenderable.texture.width,
                                          playerRenderable.texture.height)
                messaging.add(type=MessageType.PlayerLocation, data=extcords)
Exemple #3
0
    def moveEnemy(self):
        for msg in directMessaging.getByType(DirectMessageType.moveEnemy):
            entity = EntityFinder.findCharacterByGroupId(
                self.world, msg.groupId)
            if entity is None:
                # May be already deleted?
                continue

            meRenderable = self.world.component_for_entity(
                entity, system.graphics.renderable.Renderable)

            # these are the actual x/y position change we will use to move
            x = msg.data['x']
            y = msg.data['y']

            # turning on the spot
            if meRenderable.direction is Direction.left and x > 0:
                self.updateRenderableDirection(meRenderable, Direction.right,
                                               msg.groupId)
                continue
            if meRenderable.direction is Direction.right and x < 0:
                self.updateRenderableDirection(meRenderable, Direction.left,
                                               msg.groupId)
                continue

            meRenderable.storeCoords()
            meRenderable.changeLocationFromStored(x, y)

            if msg.data['force']:
                canMove = True

                # push player out of the way, if there
                isDestEmpty = EntityFinder.isDestinationEmpty(
                    self.world, meRenderable)
                if not isDestEmpty:
                    if EntityFinder.isDestinationWithPlayer(
                            self.world, meRenderable):
                        directMessaging.add(
                            groupId=0,
                            type=DirectMessageType.movePlayer,
                            data={
                                'x': x,
                                'y': 0,
                                'dontChangeDirection': True,
                                'whenMoved': None,
                            },
                        )

            else:
                canMove = EntityFinder.isDestinationEmpty(
                    self.world, meRenderable)

                if not canMove:
                    # seems we cannot move in the chose direction.
                    # if there are two components in the coordinates, just try one of
                    # them
                    if x != 0 and y != 0:
                        # try x. yes this is ugly, but fast
                        x = msg.data['x']
                        y = 0
                        meRenderable.changeLocationFromStored(x, y)
                        canMove = EntityFinder.isDestinationEmpty(
                            self.world, meRenderable)

                        if not canMove:
                            # try y... ugh
                            x = 0
                            y = msg.data['y']
                            meRenderable.changeLocationFromStored(x, y)
                            canMove = EntityFinder.isDestinationEmpty(
                                self.world, meRenderable)

                # check if we are stuck
                if not canMove:
                    meRenderable.restoreCoords()

                    isStuck = not EntityFinder.isDestinationEmpty(
                        self.world, meRenderable)
                    if isStuck:
                        logger.info(
                            "{}: Overlaps, force way out".format(meRenderable))
                        # force our way out of here. do intended path
                        x = msg.data['x']
                        y = msg.data['y']
                        canMove = True
                    else:
                        # not stuck, just wait until we are free,
                        # as another enemy most likely blocks our way
                        logger.info(
                            "{}: Does not overlap, wait until moving".format(
                                meRenderable))
                        pass

            meRenderable.restoreCoords()

            if canMove:
                self.moveRenderable(meRenderable, msg.groupId, x, y,
                                    msg.data['dontChangeDirection'],
                                    msg.data['updateTexture'])
Exemple #4
0
    def test_renderableCollisionDetection(self):
        game.isunittest.setIsUnitTest()
        fileTextureLoader.loadFromFiles()

        self.viewport = MockWin(20, 10)
        self.world = esper.World()
        self.textureEmiter = None

        particleEmiter = ParticleEmiter(viewport=self.viewport)

        renderableProcessor = RenderableProcessor(
            textureEmiter=self.textureEmiter, particleEmiter=particleEmiter)
        movementProcessor = MovementProcessor(mapManager=None)
        inputProcessor = InputProcessor()
        renderableMinimalProcessor = RenderableMinimalProcessor(
            viewport=self.viewport, textureEmiter=self.textureEmiter)
        self.world.add_processor(inputProcessor)
        self.world.add_processor(movementProcessor)
        self.world.add_processor(renderableMinimalProcessor)
        self.world.add_processor(renderableProcessor)

        # Player
        playerEntity = self.world.create_entity()
        texture = CharacterTexture(
            characterTextureType=CharacterTextureType.player,
            characterAnimationType=CharacterAnimationType.standing,
            name='Player')

        coordinates = Coordinates(10, 10)
        playerRenderable = Renderable(texture=texture,
                                      viewport=self.viewport,
                                      parent=None,
                                      coordinates=coordinates,
                                      direction=Direction.right,
                                      name='Player')
        physics = Physics()

        self.world.add_component(playerEntity, playerRenderable)
        self.world.add_component(playerEntity, physics)
        # /Player

        # Enemy
        enemyEntity = self.world.create_entity()
        texture = CharacterTexture(
            characterTextureType=CharacterTextureType.player,
            characterAnimationType=CharacterAnimationType.standing,
            name='Enemy')

        coordinates = Coordinates(10, 10)
        enemyRenderable = Renderable(texture=texture,
                                     viewport=self.viewport,
                                     parent=None,
                                     coordinates=coordinates,
                                     direction=Direction.right,
                                     name='Enemy')
        physics = Physics()

        self.world.add_component(enemyEntity, enemyRenderable)
        self.world.add_component(playerEntity, physics)
        # /Enemy

        # process it
        targetFrameTime = 1.0 / Config.fps

        # sprites overlap, but not with chars (foot is in shoulder)
        #               o
        #              /|\
        #             o/ \
        #            /|\
        #            / \
        enemyRenderable.coordinates.y -= 2
        enemyRenderable.coordinates.x += 2

        self.world.process(targetFrameTime)
        self.viewport.internalPrint()

        overlap = enemyRenderable.overlapsWith(playerRenderable)
        self.assertTrue(overlap)  # inprecise check, true

        overlap = playerRenderable.overlapsWithRenderablePixel(enemyRenderable)
        self.assertFalse(overlap)  # precise check, false

        overlap = enemyRenderable.overlapsWithRenderablePixel(playerRenderable)
        self.assertFalse(overlap)  # precise check, false

        empty = EntityFinder.isDestinationEmpty(self.world, enemyRenderable)
        self.assertTrue(empty)

        empty = EntityFinder.isDestinationEmpty(self.world, playerRenderable)
        self.assertTrue(empty)

        distance = enemyRenderable.distanceToBorder(playerRenderable)
        self.assertTrue(distance['x'] == -1)
        self.assertTrue(distance['y'] == -1)