-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathAdventureSceneiOSEvents.swift
83 lines (66 loc) · 2.79 KB
/
AdventureSceneiOSEvents.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
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
Defines iOS-specific extensions for the layered character scene.
*/
import SpriteKit
extension AdventureScene {
// MARK: Touch Handling
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// If we have no hero, we don't need to update the user interface at all.
if heroes.isEmpty || touches.count <= 0 {
return
}
// If a touch has already been processed on the default player (i.e. the only player),
// don't process another one until the next event loop.
if defaultPlayer.movementTouch != nil {
return
}
let touch = touches.first as! UITouch
defaultPlayer.targetLocation = touch.locationInNode(defaultPlayer.hero!.parent)
let nodes = nodesAtPoint(touch.locationInNode(self)) as! [SKNode]
let enemyBitmask = ColliderType.GoblinOrBoss.rawValue | ColliderType.Cave.rawValue
var heroWantsToAttack = false
for node in nodes {
// There are multiple values for `ColliderType`. We need to check if we should attack.
if let body = node.physicsBody {
if body.categoryBitMask & enemyBitmask > 0 {
heroWantsToAttack = true
}
}
}
defaultPlayer.fireAction = heroWantsToAttack
defaultPlayer.moveRequested = !heroWantsToAttack
defaultPlayer.movementTouch = touch
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
// If we have no hero, we don't need to update the user interface at all.
if heroes.isEmpty || touches.count <= 0 {
return
}
// If a touch has been previously recorded, move the player in the direction of the previous
// touch.
if let touch = defaultPlayer.movementTouch {
if touches.contains(touch) {
defaultPlayer.targetLocation = touch.locationInNode(defaultPlayer.hero!.parent)
if !defaultPlayer.fireAction {
defaultPlayer.moveRequested = true
}
}
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
// If we have no hero, we don't need to update the user interface at all.
if heroes.isEmpty || touches.count <= 0 {
return
}
// If there was a touch being tracked, stop tracking it. Don't move the player anywhere.
if let touch = defaultPlayer.movementTouch {
if touches.contains(touch) {
defaultPlayer.movementTouch = nil
defaultPlayer.fireAction = false
}
}
}
}