-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameViewController.swift
130 lines (102 loc) · 3.5 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// GameViewController.swift
// ExplodingMonkeys
//
// Created by Julian Moorhouse on 03/09/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
var currentGame: GameScene?
@IBOutlet var angleSlider: UISlider!
@IBOutlet var angleLabel: UILabel!
@IBOutlet var velocitySlider: UISlider!
@IBOutlet var velocityLabel: UILabel!
@IBOutlet var launchButton: UIButton!
@IBOutlet var playerNumber: UILabel!
@IBOutlet var scoreLabel: UILabel!
@IBOutlet var windLabel: UILabel!
struct WindItem {
var speed: CGFloat
var direction: Int
}
var wind: WindItem = WindItem(speed: 0, direction: 0) {
didSet {
let directionForce: Int = abs(wind.direction) * 10
let direction: String = wind.direction > 0 ? "East" : "West"
windLabel.text = "Wind: Speed=\(wind.speed.rounded()) Direction=\(direction) \(directionForce)"
}
}
struct ScoreItem {
var p1: Int
var p2: Int
}
var score: ScoreItem = ScoreItem(p1: 0, p2: 0) {
didSet {
scoreLabel.text = "Player 1 : \(score.p1) / Player 2 : \(score.p2)"
}
}
var gameRound: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
score.p1 = 0
score.p2 = 0
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
currentGame = scene as? GameScene
currentGame?.viewController = self
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
angleChanged(self)
velocityChanged(self)
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return true
}
@IBAction func angleChanged(_ sender: Any) {
angleLabel.text = "Angle: \(Int(angleSlider.value))°"
}
@IBAction func velocityChanged(_ sender: Any) {
velocityLabel.text = "Velocity: \(Int(velocitySlider.value))"
}
@IBAction func launch(_ sender: Any) {
angleSlider.isHidden = true
angleLabel.isHidden = true
velocitySlider.isHidden = true
velocityLabel.isHidden = true
launchButton.isHidden = true
currentGame?.launch(angle: Int(angleSlider.value), velocity: Int(velocitySlider.value))
}
func activatePlayer(number: Int) {
if number == 1 {
playerNumber.text = "<<< PLAYER ONE"
} else {
playerNumber.text = "PLAYER TWO >>>"
}
angleSlider.isHidden = false
angleLabel.isHidden = false
velocitySlider.isHidden = false
velocityLabel.isHidden = false
launchButton.isHidden = false
}
}