-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJoystick.swift
110 lines (89 loc) · 3.53 KB
/
Joystick.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
//
// Joystick.swift
// HungryKnight
//
// Created by Jared Warren on 12/20/19.
// Copyright © 2019 Warren. All rights reserved.
//
import UIKit
/// Receives directional updates from the Joystick.
protocol JoystickDelegate: AnyObject {
func joystickDirectionChanged(_ direction: Direction?)
}
/// Converts the user's touches to a Direction.
class Joystick: UIImageView {
// MARK: - Properties
weak var delegate: JoystickDelegate?
private var currentDirection: Direction? = .none {
didSet {
updateViewForNewDirection()
}
}
// MARK: - Method Overrides
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touched(touches)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
touched(touches)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// Reset currentDirection once the player lets go.
currentDirection = .none
delegate?.joystickDirectionChanged(.none)
}
// MARK: - Private Methods
private func touched(_ touches: Set<UITouch>) {
// Get location from the touch.
// rawLocation is to the topLeft of the actual JoystickView
guard let rawLocation = touches.first?.location(in: self) else { return }
// takes the rawLocation and centers it over the middle of the Joystick
let centeredLocation = CGPoint(x: rawLocation.x - bounds.width/2, y: -rawLocation.y + bounds.height/2)
// Find out the Direction
let touchDirection = checkForDirection(centeredLocation)
// Notify delegate if the direction has changed.
if currentDirection != touchDirection {
self.currentDirection = touchDirection
delegate?.joystickDirectionChanged(touchDirection)
}
}
private func updateViewForNewDirection() {
let imageName = "Joystick-" + (currentDirection?.rawValue ?? "idle")
image = UIImage(named: imageName)
}
/// Converts a location on the Joystick into a Direction or returns nil if it's in the middle.
private func checkForDirection(_ location: CGPoint) -> Direction? {
var direction: Direction? = .none
// Creates a deadzone in the center of the joystick.
let diameter = bounds.width
let deadZone = diameter * 0.2
if abs(location.x) + abs(location.y) > deadZone {
// Divides the 8 directions into even slices
let sliceSize = diameter * 0.125
// Right third of the Joystick
if location.x >= sliceSize {
if location.y >= sliceSize {
direction = .upRight
} else if location.y <= -sliceSize {
direction = .downRight
} else {
direction = .right
}
// Left third of the Joystick
} else if location.x <= -sliceSize {
if location.y >= sliceSize {
direction = .upLeft
} else if location.y <= -sliceSize {
direction = .downLeft
} else {
direction = .left
}
// Middle third of the Joystick
} else if location.y >= 0 {
direction = .up
} else {
direction = .down
}
}
return direction
}
}