0
\$\begingroup\$

I tried play with spritekit in isometric grid. But i can't understand how to fix level.

import SpriteKit
import XCPlayground

let view:SKView = SKView(frame: CGRectMake(0, 0, 1024, 768))
XCPlaygroundPage.currentPage.liveView = view



func + (left: CGPoint, right: CGPoint) -> CGPoint {
  return CGPoint(x: left.x + right.x, y: left.y + right.y)
}

func - (left: CGPoint, right: CGPoint) -> CGPoint {
  return CGPoint(x: left.x - right.x, y: left.y - right.y)
}

func * (point: CGPoint, scalar: CGPoint) -> CGPoint {
  return CGPoint(x: point.x * scalar.x, y: point.y * scalar.y)
}

func / (point: CGPoint, scalar: CGPoint) -> CGPoint {
  return CGPoint(x: point.x / scalar.x, y: point.y / scalar.y)
}

enum Tile: Int {

  case Ground
  case Wall

  var description:String {
    switch self {
    case Ground:
      return "Ground"
    case Wall:
      return "Wall"
    }
  }

  var image:String {
    switch self {
    case Ground:
      return "ground"
    case Wall:
      return "wall"

    }
  }
}

class GameScene: SKScene {

  //1
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  //2
  let view2D:SKSpriteNode
  let viewIso:SKSpriteNode

  //3
  let tiles = [
    [6, 1, 0, 0, 0],
    [1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
  ]
  let tileSize = (width:32, height:32)

  //2
  override init(size: CGSize) {

    view2D = SKSpriteNode()
    viewIso = SKSpriteNode()

    super.init(size: size)
    self.anchorPoint = CGPoint(x:0.5, y:0.5)
  }

  //5
  override func didMoveToView(view: SKView) {

    let deviceScale = self.size.width/667

    view2D.position = CGPoint(x:-self.size.width*0.25, y:self.size.height*0.17)
    view2D.xScale = deviceScale
    view2D.yScale = deviceScale
    addChild(view2D)

    viewIso.position = CGPoint(x: 50, y: 50)
    viewIso.xScale = deviceScale
    viewIso.yScale = deviceScale
    addChild(viewIso)

    placeAllTiles2D()
    placeAllTilesIso()
  }

  func placeTile2D(image:String, withPosition:CGPoint) {

    let tileSprite = SKSpriteNode(imageNamed: image)

    tileSprite.position = withPosition

    tileSprite.anchorPoint = CGPoint(x:0, y:0)

    view2D.addChild(tileSprite)

  }

  func placeAllTiles2D() {

    for i in 0..<tiles.count {

      let row = tiles[i];

      for j in 0..<row.count {
        var tileInt = row[j]

        if (tileInt > 1) {
          tileInt = 1
        }

        //1
        let tile = Tile(rawValue: tileInt)!

        //2
        let point = CGPoint(x: (j*tileSize.width), y: -(i*tileSize.height))

        placeTile2D(tile.image, withPosition:point)
      }

    }

  }

  func point2DToIso(p:CGPoint) -> CGPoint {

    //invert y pre conversion
    var point = p * CGPoint(x:1, y:-1)

    //convert using algorithm
    point = CGPoint(x:(point.x - point.y), y: ((point.x + point.y) / 2))

    //invert y post conversion
    point = point * CGPoint(x:1, y:-1)

    return point

  }

  func pointIsoTo2D(p:CGPoint) -> CGPoint {

    //invert y pre conversion
    var point = p * CGPoint(x:1, y:-1)

    //convert using algorithm
    point = CGPoint(x:((2 * point.y + point.x) / 2), y: ((2 * point.y - point.x) / 2))

    //invert y post conversion
    point = point * CGPoint(x:1, y:-1)

    return point

  }

  func placeTileIso(image:String, withPosition:CGPoint) {

    let tileSprite = SKSpriteNode(imageNamed: image)

    tileSprite.position = withPosition

    tileSprite.anchorPoint = CGPoint(x:0, y:0)

    viewIso.addChild(tileSprite)
  }

  func placeAllTilesIso() {

    for i in 0..<tiles.count {

      let row = tiles[i];

      for j in 0..<row.count {
        var tileInt = row[j]

        if (tileInt > 1) {
          tileInt = 1
        }

        let tile = Tile(rawValue: tileInt)!

        //        if (tile.rawValue == Tile.Wall.rawValue) {

        let index = tileSize.height

        for int in (0..<row[j]).reverse() {

          let x = (j*tileSize.width) + index * int

          let y = -(i*tileSize.height + index  * int)

          print("index = \(int) height: \(row[j]) x: \(x) y: \(y)")

          let pointx = point2DToIso(CGPoint(x: x, y: y))
          placeTileIso(("iso_"+tile.image), withPosition:pointx)

        }

//        let pointx = point2DToIso(CGPoint(x: (j*tileSize.width) + tileSize.height, y: -(i*tileSize.height + tileSize.height)))
//                  
//
//        placeTileIso(("iso_ground"), withPosition:pointx)


      }
    }
  }
}

let scene:SKScene = GameScene(size: CGSizeMake(2048, 1024))
scene.scaleMode = SKSceneScaleMode.AspectFit
view.presentScene(scene)

In result i have:

enter image description here

But need draw block as level in matrix

enter image description here

As matrix levels:

  let tiles = [
    [6, 1, 0, 0, 0],
    [1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
  ]

All sources in github [email protected]:rmuhamedgaliev/IsoWorld.git

\$\endgroup\$
3
  • 1
    \$\begingroup\$ what is the difference besides the arrows added to the image? \$\endgroup\$ Commented Apr 13, 2016 at 12:36
  • \$\begingroup\$ @BenediktS.Vogler need draw 4 tile from down, not from up \$\endgroup\$ Commented Apr 13, 2016 at 13:00
  • \$\begingroup\$ you have to change the drawing order, and then just change the y position in your code. maybe just remove the "-" in "-(i*tileSize.height + index * int)"? \$\endgroup\$ Commented Apr 13, 2016 at 14:44

1 Answer 1

-1
\$\begingroup\$

I fixed it:

import SpriteKit

func + (left: CGPoint, right: CGPoint) -> CGPoint {
  return CGPoint(x: left.x + right.x, y: left.y + right.y)
}

func - (left: CGPoint, right: CGPoint) -> CGPoint {
  return CGPoint(x: left.x - right.x, y: left.y - right.y)
}

func * (point: CGPoint, scalar: CGPoint) -> CGPoint {
  return CGPoint(x: point.x * scalar.x, y: point.y * scalar.y)
}

func / (point: CGPoint, scalar: CGPoint) -> CGPoint {
  return CGPoint(x: point.x / scalar.x, y: point.y / scalar.y)
}

enum Tile: Int {

  case Ground
  case Wall

  var description:String {
    switch self {
    case Ground:
      return "Ground"
    case Wall:
      return "Wall"
    }
  }

  var image:String {
    switch self {
    case Ground:
      return "ground"
    case Wall:
      return "wall"

    }
  }
}

class GameScene: SKScene {

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  let viewIso:SKSpriteNode

  let tiles = [
    [9, 8, 1, 1, 9],
    [8, 8, 1, 1, 1],
    [1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1],
    [2, 1, 8, 1, 1]
  ]

  let tileSize = (width:32, height:32)

  override init(size: CGSize) {
    viewIso = SKSpriteNode()
    super.init(size: size)
  }


  override func didMoveToView(view: SKView) {

    let deviceScale = self.size.width/667

    viewIso.position = CGPoint(x: 200, y: 200)
    viewIso.xScale = deviceScale
    viewIso.yScale = deviceScale
    addChild(viewIso)

    placeAllTilesIso()
  }


  func point2DToIso(p:CGPoint, inverse: Bool) -> CGPoint {

    //invert y pre conversion
    var point = p * CGPoint(x:1, y:-1)

    //convert using algorithm
    point = CGPoint(x:(point.x - point.y), y: ((point.x + point.y) / 2))

    //invert y post conversion
    if (!inverse) {
      point = point * CGPoint(x:1, y:-1)
    } else {
      point = point * CGPoint(x:1, y:-1)
    }

    return point

  }

  func placeTileIso(image:String, withPosition:CGPoint) {
    let tileSprite = SKSpriteNode(imageNamed: image)
    tileSprite.position = withPosition
    tileSprite.anchorPoint = CGPoint(x:0, y:0)
    viewIso.addChild(tileSprite)
  }

  func placeAllTilesIso() {
    for i in 0..<tiles.count {
      let row = tiles[i];
      for j in 0..<row.count {
        var tileInt = row[j]
        if (tileInt > 1) {
          tileInt = 1
        }

//        let tile = Tile(rawValue: tileInt)!

        if (tileInt > 0) {
          let index = tileSize.height


            let xxx = ((j*tileSize.width) + index * 0)
            let yyy = -(i*tileSize.height + index  * 0)

            print("index = \(0) height: \(row[j]) xxx: \(xxx) yyy: \(yyy)")

            let pointxxx = point2DToIso(CGPoint(x: xxx, y: yyy), inverse: false)
            placeTileIso(("iso_wall"), withPosition:pointxxx)


          if (row[j] > 1) {

            if (j > 0 || i > 0) {
                for indexs in (0..<row[j]){
                    let xx = ((j*tileSize.width) + index * (-indexs))
                    let yy = -(i*tileSize.height + index  * (-indexs ))

                    print("index = \(0) height: \(row[j]) xx: \(xx) yy: \(yy)")

                    let pointxx = point2DToIso(CGPoint(x: xx, y: yy), inverse: false)
                    placeTileIso(("iso_wall"), withPosition:pointxx)
                }
            } else {
                for indexs in (1..<row[j]) {
                    let xx = -((j*tileSize.width) + index * indexs)
                    let yy = (i*tileSize.height + index  * indexs)

                    print("index = \(0) height: \(row[j]) xx: \(xx) yy: \(yy)")

                    let pointxx = point2DToIso(CGPoint(x: xx, y: yy), inverse: false)
                    placeTileIso(("iso_wall"), withPosition:pointxx)
                }
            }
          }

        }


        }
    }
  }
}

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Could you explain what is the difference between the original code and the updated code? What did you change? What was the error? Readers want to see it right away and not have to open their favourite diff tool to find the difference! \$\endgroup\$ Commented Apr 14, 2016 at 12:30
  • \$\begingroup\$ What did you actually change? \$\endgroup\$ Commented Mar 29, 2017 at 22:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.