Giter Club home page Giter Club logo

Comments (3)

technicallyerik avatar technicallyerik commented on June 13, 2024 1

This was my logic for an older version of SKTiled when cameraPanned was able to be overridden. But perhaps some of the math can help out:

    open func cameraPanned(_ recognizer: UIPanGestureRecognizer) {
        guard let scene = self.scene as? SKTiledScene else { return }
         
        let minPanX = (scene.size.halfWidth * zoom) - (scene.tilemap.sizeInPoints.halfWidth * zoom - ((scene.size.width * xScale) / 2)) - (panInsets.left * xScale)
        let maxPanX = (scene.size.halfWidth * zoom) + (scene.tilemap.sizeInPoints.halfWidth * zoom  - ((scene.size.width * xScale) / 2)) + (panInsets.right * xScale)
        let minPanY = (scene.size.halfHeight * zoom) - (scene.tilemap.sizeInPoints.halfHeight * zoom  - ((scene.size.height * yScale) / 2)) - (panInsets.bottom * yScale)
        let maxPanY = (scene.size.halfHeight * zoom) + (scene.tilemap.sizeInPoints.halfHeight * zoom - ((scene.size.height * yScale) / 2)) + (panInsets.top * yScale)
         
        if (recognizer.state == .began) {
            let location = recognizer.location(in: recognizer.view)
            lastLocation = location
        }
         
        if (recognizer.state == .changed) && (allowMovement == true) {
            if lastLocation == nil { return }
             
            let location = recognizer.location(in: recognizer.view)
            let difference = CGPoint(x: location.x - lastLocation.x, y: location.y - lastLocation.y)
             
            var newPositionX = position.x - (difference.x * self.xScale)
            if((scene.tilemap.sizeInPoints.width / xScale) * zoom < scene.size.width - (panInsets.left + panInsets.right)) {
                if(newPositionX < maxPanX) {
                    newPositionX = maxPanX
                }
                if(newPositionX > minPanX) {
                    newPositionX = minPanX
                }
            } else {
                if(newPositionX > maxPanX) {
                    newPositionX = maxPanX
                }
                if(newPositionX < minPanX) {
                    newPositionX = minPanX
                }
            }
             
            var newPositionY = position.y - -(difference.y * self.yScale)
            if((scene.tilemap.sizeInPoints.height / yScale) * zoom < scene.size.height - (panInsets.top + panInsets.bottom)) {
                if(newPositionY < maxPanY) {
                    newPositionY = maxPanY
                }
                if(newPositionY > minPanY) {
                    newPositionY = minPanY
                }
            } else {
                if(newPositionY > maxPanY) {
                    newPositionY = maxPanY
                }
                if(newPositionY < minPanY) {
                    newPositionY = minPanY
                }
            }
             
            centerOn(scenePoint: CGPoint(x: newPositionX, y: newPositionY))
             
            lastLocation = location
        }
    }

It handles zooming, and if the map is bigger than the screen, the map locks to the edges of the screen as you pan. It also handles if the map is smaller than the screen, the edges act as bumpers.

from sktiled.

mfessenden avatar mfessenden commented on June 13, 2024

I haven't implemented anything to automatically limit panning because that will be very specific to your game, but check out the included SKTiledSceneCameraDelegate protocol - it will update delegates when the camera position, zoom or bounds have changed. It should be easy to constrain the camera with the tilemap node with a SKContraint node.

from sktiled.

leandersantosm avatar leandersantosm commented on June 13, 2024

I will try this! Thanks!!

from sktiled.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.