Skip to content

Commit 2011055

Browse files
author
Madeline O'Connell
committed
- Added the falling (floating) and landing animations. Tapping will randomly generate either a walking or a falling lemming. Falling lemmings walk after landing.
- A long press will clear the screen.
1 parent e44ae52 commit 2011055

File tree

7 files changed

+379
-170
lines changed

7 files changed

+379
-170
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ open build/Release/TouchLemmings.app
1515

1616
# Spawn lemmings
1717

18-
Just hit the touch bar while the application is in the foreground. Tapping a lemming will transform it into a blocker.
18+
Just hit the touch bar while the application is in the foreground. Tapping a walking lemming will transform it into a blocker. Tapping a floating lemming will cause it to land and start walking. Long press to clear the screen and start over.

TouchLemmings.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@
276276
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
277277
PRODUCT_BUNDLE_IDENTIFIER = com.example.TouchLemmings;
278278
PRODUCT_NAME = "$(TARGET_NAME)";
279-
SWIFT_VERSION = 3.0;
279+
SWIFT_VERSION = 4.0;
280280
};
281281
name = Debug;
282282
};
@@ -291,7 +291,7 @@
291291
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
292292
PRODUCT_BUNDLE_IDENTIFIER = com.example.TouchLemmings;
293293
PRODUCT_NAME = "$(TARGET_NAME)";
294-
SWIFT_VERSION = 3.0;
294+
SWIFT_VERSION = 4.0;
295295
};
296296
name = Release;
297297
};

TouchLemmings/Atlas.plist

+106
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,111 @@
318318
<real>0.767</real>
319319
</dict>
320320
</array>
321+
<key>fall</key>
322+
<array>
323+
<dict>
324+
<key>Height</key>
325+
<real>0.2</real>
326+
<key>Width</key>
327+
<real>0.033</real>
328+
<key>X</key>
329+
<real>0.003</real>
330+
<key>Y</key>
331+
<real>0.43</real>
332+
</dict>
333+
<dict>
334+
<key>Height</key>
335+
<real>0.2</real>
336+
<key>Width</key>
337+
<real>0.033</real>
338+
<key>X</key>
339+
<real>0.042</real>
340+
<key>Y</key>
341+
<real>0.43</real>
342+
</dict>
343+
<dict>
344+
<key>Height</key>
345+
<real>0.2</real>
346+
<key>Width</key>
347+
<real>0.033</real>
348+
<key>X</key>
349+
<real>0.081</real>
350+
<key>Y</key>
351+
<real>0.43</real>
352+
</dict>
353+
<dict>
354+
<key>Height</key>
355+
<real>0.2</real>
356+
<key>Width</key>
357+
<real>0.033</real>
358+
<key>X</key>
359+
<real>0.042</real>
360+
<key>Y</key>
361+
<real>0.43</real>
362+
</dict>
363+
</array>
364+
<key>jump</key>
365+
<array>
366+
<dict>
367+
<key>Height</key>
368+
<real>0.17</real>
369+
<key>Width</key>
370+
<real>0.0361</real>
371+
<key>X</key>
372+
<real>0.252</real>
373+
<key>Y</key>
374+
<real>0.45</real>
375+
</dict>
376+
<dict>
377+
<key>Height</key>
378+
<real>0.17</real>
379+
<key>Width</key>
380+
<real>0.0361</real>
381+
<key>X</key>
382+
<real>0.291</real>
383+
<key>Y</key>
384+
<real>0.46</real>
385+
</dict>
386+
<dict>
387+
<key>Height</key>
388+
<real>0.17</real>
389+
<key>Width</key>
390+
<real>0.0361</real>
391+
<key>X</key>
392+
<real>0.33</real>
393+
<key>Y</key>
394+
<real>0.46</real>
395+
</dict>
396+
<dict>
397+
<key>Height</key>
398+
<real>0.17</real>
399+
<key>Width</key>
400+
<real>0.0361</real>
401+
<key>X</key>
402+
<real>0.369</real>
403+
<key>Y</key>
404+
<real>0.46</real>
405+
</dict>
406+
<dict>
407+
<key>Height</key>
408+
<real>0.17</real>
409+
<key>Width</key>
410+
<real>0.0361</real>
411+
<key>X</key>
412+
<real>0.408</real>
413+
<key>Y</key>
414+
<real>0.46</real>
415+
</dict>
416+
<dict>
417+
<key>Height</key>
418+
<real>0.17</real>
419+
<key>Width</key>
420+
<real>0.0361</real>
421+
<key>X</key>
422+
<real>0.447</real>
423+
<key>Y</key>
424+
<real>0.46</real>
425+
</dict>
426+
</array>
321427
</dict>
322428
</plist>

TouchLemmings/Lemming.swift

+139-89
Original file line numberDiff line numberDiff line change
@@ -9,104 +9,154 @@
99
import SpriteKit
1010

1111
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+
}
2122
}
22-
}
2323
}
2424

2525
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+
}
3744
}
38-
}
39-
4045
}
4146

4247

4348
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+
}
5870
}
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+
}
6783
}
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+
112162
}

0 commit comments

Comments
 (0)