This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputProxy.swift
168 lines (139 loc) · 6.16 KB
/
InputProxy.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// InputProxy.swift
// Bomberman
//
// Created by Wolfgang Schreurs on 08/08/16.
//
//
/*
* Used to route all controller input to the current visible scene. We keep a weak reference to the
* current scene, which needs to be updated each time the scene changes. Controller input is
* forwarded to the referenced scene.
*
* Some properties of input handling can be modified here. In particular we can configure the proxy
* class to automatically release a direction button after each press. By setting this property to
* true way we can navigate we can navigate through the menus of the game by a single directional
* press at a time.
*/
// NOTE: If we encounter performance issues, perhaps we can use the @inline(__always) attribute on
// some functions. These functions will likely be called many times per second. Reducing function
// calls might improve performance.@inline(__always)
import SpriteKit
import GameController
class InputProxy {
static let sharedInstance = InputProxy()
weak var scene: BaseScene?
fileprivate var directionPressed = false
var autoDirectionPressRelease = false
// MARK: - GameController suppport
#if os(tvOS)
func configureController(controller: GCController, forPlayer player: GCControllerPlayerIndex) {
controller.playerIndex = player
controller.controllerPausedHandler = { [unowned self] _ in
self.handlePausePress()
}
let playerIndex: PlayerIndex = player == .Index1 ? .Player1 : .Player2
if let profile = controller.microGamepad {
profile.allowsRotation = true
profile.reportsAbsoluteDpadValues = true
profile.buttonX.pressedChangedHandler = { _, value, pressed in
if let scene = self.scene {
self.handleActionPressReleaseForPlayer(playerIndex, scene: scene, pressed: pressed)
}
}
profile.dpad.valueChangedHandler = { _, xValue, yValue in
if let scene = self.scene {
self.handleDpadValueChangedForPlayer(.Player1, scene: scene, xValue: xValue, yValue: yValue, centerOffset: 0.25)
}
}
} else if let profile = controller.gamepad {
profile.dpad.valueChangedHandler = { _, xValue, yValue in
if let scene = self.scene {
self.handleDpadValueChangedForPlayer(.Player1, scene: scene, xValue: xValue, yValue: yValue, centerOffset: 0.10)
}
}
profile.buttonA.pressedChangedHandler = { _, value, pressed in
if let scene = self.scene {
self.handleActionPressReleaseForPlayer(playerIndex, scene: scene, pressed: pressed)
}
}
}
}
#else
func configureController(_ controller: GCController, forPlayer player: GCControllerPlayerIndex) {
controller.playerIndex = player
controller.controllerPausedHandler = { [unowned self] _ in
self.handlePausePress()
}
if let dpad = controller.gamepad?.dpad {
dpad.valueChangedHandler = { _, xValue, yValue in
if let scene = self.scene {
self.handleDpadValueChangedForPlayer(.player1, scene: scene, xValue: xValue, yValue: yValue, centerOffset: 0.10)
}
}
}
if let buttonA = controller.gamepad?.buttonA {
buttonA.pressedChangedHandler = { _, value, pressed in
if let scene = self.scene {
self.handleActionPressReleaseForPlayer(.player1, scene: scene, pressed: pressed)
}
}
}
if let buttonX = controller.gamepad?.buttonX {
buttonX.pressedChangedHandler = { _, value, pressed in
if let scene = self.scene {
self.handleActionPressReleaseForPlayer(.player1, scene: scene, pressed: pressed)
}
}
}
}
#endif
// MARK: - Private
fileprivate init() {
}
fileprivate func handleDpadValueChangedForPlayer(_ player: PlayerIndex, scene: BaseScene, xValue: Float, yValue: Float, centerOffset: Float) {
if self.autoDirectionPressRelease {
if fabs(xValue) < centerOffset && fabs(yValue) < centerOffset {
directionPressed = false
} else if !directionPressed {
directionPressed = true
handleDirectionalButtonPressForPlayer(player, scene: scene, xValue: xValue, yValue: yValue)
}
} else {
if fabs(xValue) < centerOffset && fabs(yValue) < centerOffset {
releaseDirectionalButtonsForPlayer(player, scene: scene)
} else if !self.directionPressed {
handleDirectionalButtonPressForPlayer(player, scene: scene, xValue: xValue, yValue: yValue)
}
}
}
fileprivate func handleDirectionalButtonPressForPlayer(_ player: PlayerIndex, scene: BaseScene, xValue: Float, yValue: Float) {
if fabs(xValue) > fabs(yValue) {
if xValue > 0 {
scene.handleRightPress(forPlayer: player)
} else {
scene.handleLeftPress(forPlayer: player)
}
} else {
if yValue > 0 {
scene.handleUpPress(forPlayer: player)
} else {
scene.handleDownPress(forPlayer: player)
}
}
}
fileprivate func handleActionPressReleaseForPlayer(_ player: PlayerIndex, scene: BaseScene, pressed: Bool) {
if pressed {
scene.handleActionPress(forPlayer: .player1)
}
}
fileprivate func releaseDirectionalButtonsForPlayer(_ player: PlayerIndex, scene: BaseScene) {
scene.handleRightRelease(forPlayer: player)
scene.handleLeftRelease(forPlayer: player)
scene.handleUpRelease(forPlayer: player)
scene.handleDownRelease(forPlayer: player)
}
fileprivate func handlePausePress() {
scene?.handlePausePress(forPlayer: .player1)
}
}