|
9 | 9 | import SpriteKit
|
10 | 10 |
|
11 | 11 | enum Direction {
|
12 |
| - case left |
13 |
| - case right |
14 |
| - |
15 |
| - func theOtherDirection () -> Direction { |
16 |
| - switch self { |
17 |
| - case .left: |
18 |
| - return .right |
19 |
| - case .right: |
20 |
| - return .left |
| 12 | + case left |
| 13 | + case right |
| 14 | + |
| 15 | + func theOtherDirection () -> Direction { |
| 16 | + switch self { |
| 17 | + case .left: |
| 18 | + return .right |
| 19 | + case .right: |
| 20 | + return .left |
| 21 | + } |
21 | 22 | }
|
22 |
| - } |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | enum State {
|
26 |
| - case walking(direction: Direction) |
27 |
| - case blocking |
28 |
| - |
29 |
| - var size: CGSize { |
30 |
| - get { |
31 |
| - switch self { |
32 |
| - case .walking(_): |
33 |
| - return CGSize(width: 20, height: 30) |
34 |
| - case .blocking: |
35 |
| - return CGSize(width: 30, height: 30) |
36 |
| - } |
| 26 | + case walking(direction: Direction) |
| 27 | + case blocking |
| 28 | + case falling |
| 29 | + case jumping |
| 30 | + |
| 31 | + var size: CGSize { |
| 32 | + get { |
| 33 | + switch self { |
| 34 | + case .walking(_): |
| 35 | + return CGSize(width: 20, height: 30) |
| 36 | + case .blocking: |
| 37 | + return CGSize(width: 30, height: 30) |
| 38 | + case .falling: |
| 39 | + return CGSize(width: 35, height: 60) |
| 40 | + case .jumping: |
| 41 | + return CGSize(width: 36, height: 45) |
| 42 | + } |
| 43 | + } |
37 | 44 | }
|
38 |
| - } |
39 |
| - |
40 | 45 | }
|
41 | 46 |
|
42 | 47 |
|
43 | 48 | class Lemming: SKSpriteNode {
|
44 |
| - |
45 |
| - static let walkingCategory: UInt32 = 0x1 << 1; |
46 |
| - static let blockingCategory: UInt32 = 0x1 << 2; |
47 |
| - |
48 |
| - var state: State = .walking(direction: .left) { |
49 |
| - didSet { |
50 |
| - switch state { |
51 |
| - case .walking(let direction): |
52 |
| - walk(direction: direction) |
53 |
| - case .blocking: |
54 |
| - block() |
55 |
| - } |
56 |
| - |
57 |
| - size = state.size |
| 49 | + |
| 50 | + static let walkingCategory: UInt32 = 0x1 << 1; |
| 51 | + static let blockingCategory: UInt32 = 0x1 << 2; |
| 52 | + static let fallingCategory: UInt32 = 0x1 << 3; |
| 53 | + static let jumpingCategory: UInt32 = 0x1 << 4; |
| 54 | + |
| 55 | + var state: State = .walking(direction: .left) { |
| 56 | + didSet { |
| 57 | + switch state { |
| 58 | + case .walking(let direction): |
| 59 | + walk(direction: direction) |
| 60 | + case .blocking: |
| 61 | + block() |
| 62 | + case .falling: |
| 63 | + fall() |
| 64 | + case .jumping: |
| 65 | + jump() |
| 66 | + } |
| 67 | + |
| 68 | + size = state.size |
| 69 | + } |
58 | 70 | }
|
59 |
| - } |
60 |
| - |
61 |
| - func toggleState() { |
62 |
| - switch state { |
63 |
| - case .walking: |
64 |
| - state = .blocking |
65 |
| - case .blocking: |
66 |
| - state = .walking(direction: .right) |
| 71 | + |
| 72 | + func toggleState() { |
| 73 | + switch state { |
| 74 | + case .walking: |
| 75 | + state = .blocking |
| 76 | + case .blocking: |
| 77 | + state = .walking(direction: .right) |
| 78 | + case .falling: |
| 79 | + state = .jumping |
| 80 | + case .jumping: |
| 81 | + state = .walking(direction: .right) |
| 82 | + } |
67 | 83 | }
|
68 |
| - } |
69 |
| - |
70 |
| - func walk(direction: Direction) { |
71 |
| - removeAllActions() |
72 |
| - let textures = SpriteLoader.loadWalkingTextures() |
73 |
| - let animate = SKAction.animate(with: textures, timePerFrame: 0.1) |
74 |
| - let walkAction = SKAction.repeatForever(animate) |
75 |
| - |
76 |
| - let reverse = direction == .right |
77 |
| - let moveAction = SKAction.moveBy(x: reverse ? 1000 : -1000, y: 0, duration: 60) |
78 |
| - xScale = reverse ? -1 : 1 |
79 |
| - run(walkAction) |
80 |
| - run(moveAction) |
81 |
| - |
82 |
| - physicsBody?.categoryBitMask = Lemming.walkingCategory |
83 |
| - physicsBody?.contactTestBitMask = Lemming.blockingCategory |
84 |
| - physicsBody?.collisionBitMask = Lemming.blockingCategory |
85 |
| - } |
86 |
| - |
87 |
| - func block() { |
88 |
| - removeAllActions() |
89 |
| - |
90 |
| - let textures = SpriteLoader.loadBlockingTextures() |
91 |
| - let animate = SKAction.animate(with: textures, timePerFrame: 0.1) |
92 |
| - let blockAction = SKAction.repeatForever(SKAction.group([animate, animate.reversed()])) |
93 |
| - |
94 |
| - run(blockAction) |
95 |
| - |
96 |
| - physicsBody?.categoryBitMask = Lemming.blockingCategory |
97 |
| - physicsBody?.contactTestBitMask = Lemming.walkingCategory |
98 |
| - physicsBody?.collisionBitMask = Lemming.walkingCategory |
99 |
| - |
100 |
| - } |
101 |
| - |
102 |
| - init() { |
103 |
| - let texture = SpriteLoader.SpriteSheet |
104 |
| - super.init(texture: texture, color: NSColor.clear, size: State.blocking.size) |
105 |
| - physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 30)) |
106 |
| - } |
107 |
| - |
108 |
| - required init?(coder aDecoder: NSCoder) { |
109 |
| - fatalError() |
110 |
| - } |
111 |
| - |
| 84 | + |
| 85 | + func freezeFrame() { |
| 86 | + removeAllActions() |
| 87 | + } |
| 88 | + |
| 89 | + func walk(direction: Direction) { |
| 90 | + removeAllActions() |
| 91 | + let textures = SpriteLoader.loadWalkingTextures() |
| 92 | + let animate = SKAction.animate(with: textures, timePerFrame: 0.1) |
| 93 | + let walkAction = SKAction.repeatForever(animate) |
| 94 | + |
| 95 | + let reverse = direction == .right |
| 96 | + let moveAction = SKAction.moveBy(x: reverse ? 1000 : -1000, y: 0, duration: 60) |
| 97 | + xScale = reverse ? -1 : 1 |
| 98 | + run(walkAction) |
| 99 | + run(moveAction) |
| 100 | + |
| 101 | + physicsBody?.categoryBitMask = Lemming.walkingCategory |
| 102 | + physicsBody?.contactTestBitMask = Lemming.blockingCategory |
| 103 | + physicsBody?.collisionBitMask = Lemming.blockingCategory |
| 104 | + } |
| 105 | + |
| 106 | + func block() { |
| 107 | + removeAllActions() |
| 108 | + |
| 109 | + let textures = SpriteLoader.loadBlockingTextures() |
| 110 | + let animate = SKAction.animate(with: textures, timePerFrame: 0.1) |
| 111 | + let blockAction = SKAction.repeatForever(SKAction.group([animate, animate.reversed()])) |
| 112 | + |
| 113 | + run(blockAction) |
| 114 | + |
| 115 | + physicsBody?.categoryBitMask = Lemming.blockingCategory |
| 116 | + physicsBody?.contactTestBitMask = Lemming.walkingCategory |
| 117 | + physicsBody?.collisionBitMask = Lemming.walkingCategory |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + func fall() { |
| 122 | + removeAllActions() |
| 123 | + let textures = SpriteLoader.loadFallingTextures() |
| 124 | + let animate = SKAction.animate(with: textures, timePerFrame: 0.15) |
| 125 | + let fallAction = SKAction.repeatForever(animate) |
| 126 | + |
| 127 | + let moveAction = SKAction.moveBy(x: 0, y: -500, duration: 60) |
| 128 | + run(fallAction) |
| 129 | + run(moveAction) |
| 130 | + |
| 131 | + physicsBody?.categoryBitMask = Lemming.fallingCategory |
| 132 | + physicsBody?.contactTestBitMask = Lemming.walkingCategory |
| 133 | + physicsBody?.collisionBitMask = Lemming.walkingCategory |
| 134 | + } |
| 135 | + |
| 136 | + func jump() { |
| 137 | + self.position.y = 14 |
| 138 | + removeAllActions() |
| 139 | + let textures = SpriteLoader.loadJumpingTextures() |
| 140 | + let animate = SKAction.animate(with: textures, timePerFrame: 0.12) |
| 141 | + let jumpAction = SKAction.repeat(SKAction.group([animate, animate.reversed()]), count: 1) |
| 142 | + |
| 143 | + run(jumpAction) { |
| 144 | + self.toggleState() |
| 145 | + } |
| 146 | + |
| 147 | + physicsBody?.categoryBitMask = Lemming.jumpingCategory |
| 148 | + physicsBody?.contactTestBitMask = Lemming.walkingCategory |
| 149 | + physicsBody?.collisionBitMask = Lemming.walkingCategory |
| 150 | + } |
| 151 | + |
| 152 | + init() { |
| 153 | + let texture = SpriteLoader.SpriteSheet |
| 154 | + super.init(texture: texture, color: NSColor.clear, size: State.blocking.size) |
| 155 | + physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 30)) |
| 156 | + } |
| 157 | + |
| 158 | + required init?(coder aDecoder: NSCoder) { |
| 159 | + fatalError() |
| 160 | + } |
| 161 | + |
112 | 162 | }
|
0 commit comments