4

ive been following the Apple Docs on GKNoiseMaps and i managed to get an image to spawn and it looks really good

using this code here

class GameScene: SKScene {


class Noise: GKNoise {
    var NoiseSource = GKPerlinNoiseSource(frequency: 0.05, octaveCount: 3, persistence: 1, lacunarity: 1, seed: Int32(arc4random_uniform(UInt32(500 - 1))))
    override init(_ noiseSource: GKNoiseSource, gradientColors: [NSNumber : UIColor]) {
        super.init(NoiseSource, gradientColors: [ (+1.0 as NSNumber): UIColor.red, (-1.0 as NSNumber) : UIColor.black])
    }
}
let noise = Noise()
let Vector1 = vector_double2(1.0, 1.0)

override func didMove(to view: SKView) {
    let NoiseMap = GKNoiseMap(noise, size: vector_double2(300.0, 300.0),
                              origin: vector_double2(0.0, 0.0),
                              sampleCount: vector_int2(100),
                              seamless: true)
    let texture = SKTexture(noiseMap: NoiseMap)
    let Node = SKSpriteNode(texture: texture)

    Node.size = CGSize(width: 1000,height: 1000)
    Node.position = CGPoint(x: 0,y: 0)
    self.addChild(Node)
}

    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}

now how do i create a SKTileMap with this updated code here

class GameScene: SKScene {


class Noise: GKNoise {
    var NoiseSource = GKPerlinNoiseSource(frequency: 0.05, octaveCount: 3, persistence: 1, lacunarity: 1, seed: Int32(arc4random_uniform(UInt32(500 - 1))))
    override init(_ noiseSource: GKNoiseSource, gradientColors: [NSNumber : UIColor]) {
        super.init(NoiseSource, gradientColors: [(+1.0 as NSNumber): UIColor.red, (-1.0 as NSNumber) : UIColor.black])
    }
}
let noise = Noise()
let Vector1 = vector_double2(1.0, 1.0)

override func didMove(to view: SKView) {
    let NoiseMap = GKNoiseMap(noise, size: vector_double2(300.0, 300.0),
                              origin: vector_double2(0.0, 0.0),
                              sampleCount: vector_int2(100),
                              seamless: true)
    let tileGroup = [SKTileGroup]()
    let tileSet = SKTileSet(tileGroups: tileGroup)
    let map = SKTileMapNode(tileSet: tileSet, columns: 10, rows: 10, tileSize: CGSize(width: 20,height: 20), tileGroupLayout: tileGroup)
}

    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}

and generate the SKTileMap with values from the GKNoiseMap as stated in the Apple Docs?

any help would be appreciated as i dont really know about SKTileMaps and how they work

Dennis Vennink
  • 1,083
  • 1
  • 7
  • 23
E. Huckabee
  • 1,788
  • 1
  • 13
  • 29

1 Answers1

5

You can create an SKTileDefinition using the texture created from the NoiseMap. Then it's possible to paint this tile into the SKTileMapNode at any location. This example iterates through all columns and rows and sets the tile. I made the size of the NoiseMap 64 x 64 since that is a typical size for a tile.

override func sceneDidLoad() {
    let noise = Noise()
    let noiseMap = GKNoiseMap(noise, size: vector_double2(64.0, 64.0), origin: vector_double2(0.0, 0.0), sampleCount: vector_int2(100), seamless: true)
    let bgTexture = SKTexture(noiseMap: noiseMap)

    let bgDefinition = SKTileDefinition(texture: bgTexture, size: bgTexture.size())
    let bgGroup = SKTileGroup(tileDefinition: bgDefinition)
    bgGroup.name = "noiseTest"

    let tileSet = SKTileSet(tileGroups: [bgGroup])

    let bgNode = SKTileMapNode(tileSet: tileSet, columns: 10, rows: 10, tileSize: bgTexture.size())
    let tile = bgNode.tileSet.tileGroups.first(where: { $0.name == "noiseTest" })

    for column in 0 ..< bgNode.numberOfColumns {
        for row in 0 ..< bgNode.numberOfRows {
            bgNode.setTileGroup(tile, forColumn: column, row: row)
        }
    }

    bgNode.position = CGPoint(x: 0, y: 0)
    bgNode.setScale(1)

    self.addChild(bgNode)
}
Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23
  • Ok thanks i will test it. BTW my app launched today (it has nothing to do with this) and its called "Crappy Duck" and i would appreciate it if you could check it out – E. Huckabee Oct 11 '17 at 13:54
  • dude! it works! but i need to set the tiles based on the level of noise i am attempting to create a map based on the level of noise so the map has a bit of randomness to it – E. Huckabee Oct 11 '17 at 13:57
  • If you initialise the `SKTileGroup` using the init method `public init(rules: [SKTileGroupRule])` then you can specify an array of `SKTileDefinitions`. That should handle the randomness! – Mark Brownsword Oct 11 '17 at 19:41
  • could you update your answer with an example so we dont get in trouble for not being the correct answer – E. Huckabee Oct 12 '17 at 09:16
  • I think randomising an SKTileMapNode is a separate thing. You could try yourself, then ask another question. – Mark Brownsword Oct 12 '17 at 10:41
  • oh. ok thanks for your help though! i will ask another question – E. Huckabee Oct 12 '17 at 10:43