 import SpriteKit
 
 class GameScene: SKScene, LogicEngineDelegate {
-    private let grid = SKSpriteNode()
     private let tileSize = (width: 32, height: 32)
     
+    private let grid = SKSpriteNode()
     private let logicEngine = LogicEngine(playerNumber: 0)
+    
     private var gameState: GameState!
     private var myPlayer: SKNode!
     private var obstacles = [String: SKNode]()

        logicEngine.setDelegate(self)
        gameState = logicEngine.state

        let obstacleNodes = gameState.obstacles.map { createObstacle($0) }
        for node in obstacleNodes {
            obstacles[node.name!] = node
        }

        myPlayer = createPlayer(gameState.myPlayer)
        for i in 1...3 {
            createPlayer(Player(playerNumber: i))
        }

        physicsWorld.gravity = CGVector(dx: 0.0, dy: -4.0)
        
        grid.position = CGPoint(x: 0, y: size.height/2)
        addChild(grid)
        
        setupGestureRecognizers(view)
        renderIsoGrid()
    }
    
    private func renderIsoGrid() {

    }

    private func calculateRenderPositionFor(object: GameObject) -> CGPoint {
        // multiple is to convert object's coordinates to coordinates in the actual grid
        let multiple = Double(tileSize.width / unitsPerGameGridCell) / 2
        let x = CGFloat(Double(object.xCoordinate) * multiple)
        let y = CGFloat(Double(object.yCoordinate) * multiple)
        return pointToIso(CGPointMake(x, y))
    }

    private func createGameObject(object: GameObject, imageName: String) -> SKSpriteNode {
        let sprite = SKSpriteNode(imageNamed: imageName)
        sprite.position = calculateRenderPositionFor(object)
        sprite.anchorPoint = CGPointMake(0, 0)
        grid.addChild(sprite)
        return sprite
    }
    
    private func createPlayer(player: Player) -> SKNode {
        let playerSprite = createGameObject(player, imageName: "iso_player")
        playerSprite.zPosition = 2
        playerSprite.position.y -= CGFloat(playerSprite.size.height/CGFloat(tileSize.height) - 1) * 8
        return playerSprite
    }
    
    private func createObstacle(obstacle: Obstacle) -> SKNode {
        let obstacleSprite = createGameObject(obstacle, imageName: "iso_non_floating_obstacle")
        obstacleSprite.zPosition = 1
        obstacleSprite.name = obstacle.identifier
        return obstacleSprite
    }

