-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameViewController.swift
115 lines (83 loc) · 3.38 KB
/
GameViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// GameViewController.swift
// Flip
//
// Created by Thang Le Tan on 9/20/16.
// Copyright © 2016 Thang Le Tan. All rights reserved.
//
import UIKit
import SpriteKit
import GameplayKit
import Messages
class GameViewController: UIViewController {
weak var delegate: MessagesViewController!
var rowsString = ""
var player = 0
var row = 0
var col = 0
var caroGameScene: GameScene?
override func viewDidLoad() {
super.viewDidLoad()
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
if let scene = GKScene(fileNamed: "GameScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
caroGameScene = sceneNode
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFit //.resizeFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
sceneNode.loadRows(rowsString: rowsString)
sceneNode.loadLastMove(row: row, col: col)
sceneNode.loadPlayerTurn(index: player)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
}
func load(message: MSMessage?) {
guard let message = message else { return }
guard let messageURL = message.url else { return }
guard let components = URLComponents(url: messageURL, resolvingAgainstBaseURL: false) else { return }
guard let queryItems = components.queryItems else { return }
for item in queryItems {
if item.name == "rows" {
rowsString = item.value ?? ""
} else if item.name == "player" {
player = Int(item.value ?? "") ?? 0
} else if item.name == "row" {
row = Int(item.value ?? "") ?? 0
} else if item.name == "col" {
col = Int(item.value ?? "") ?? 0
}
}
}
@IBAction func sendTapped(_ sender: AnyObject) {
guard let caroGameScene = caroGameScene else { return }
let text = caroGameScene.getBoardRows()
let player: Int = Player.allPlayers.index(of: caroGameScene.board.currentPlayer)!
let move = caroGameScene.board.lastMove
delegate.createMessage(rowsString: text, player: player, row: move.row, col: move.col)
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override var prefersStatusBarHidden: Bool {
return true
}
}