-
Notifications
You must be signed in to change notification settings - Fork 104
/
ActorTests.swift
736 lines (557 loc) · 23.5 KB
/
ActorTests.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/**
* Imagine Engine
* Copyright (c) John Sundell 2017
* See LICENSE file for license
*/
import Foundation
import XCTest
@testable import ImagineEngine
final class ActorTests: XCTestCase {
private var actor: Actor!
private var game: GameMock!
// MARK: - XCTestCase
override func setUp() {
super.setUp()
actor = Actor()
game = GameMock()
game.scene.add(actor)
}
// MARK: - Tests
func testRect() {
XCTAssertEqual(actor.rect, .zero)
actor.position = Point(x: 150, y: 200)
XCTAssertEqual(actor.rect, Rect(x: 150, y: 200, width: 0, height: 0))
actor.size = Size(width: 100, height: 300)
XCTAssertEqual(actor.rect, Rect(x: 100, y: 50, width: 100, height: 300))
// The actor's rect should be adapted if its scale is changed
actor.scale = 3
XCTAssertEqual(actor.rect, Rect(x: 0, y: -250, width: 300, height: 900))
}
func testAnimationAutoResizingActor() {
let imageSizes = [
Size(width: 200, height: 150),
Size(width: 300, height: 50),
Size(width: 100, height: 90)
]
let images = imageSizes.map(ImageMockFactory.makeImage)
actor.animation = Animation(images: images, frameDuration: 1.5)
// The actor should directly render the first frame
XCTAssertEqual(actor.size, imageSizes[0].scaled)
// Perform a game update to start the animation
game.update()
// After 1.5 seconds the second frame should be rendered, and the actor resized
game.timeTraveler.travel(by: 1.5)
game.update()
XCTAssertEqual(actor.size, imageSizes[1].scaled)
// Same thing for the third frame
game.timeTraveler.travel(by: 1.5)
game.update()
XCTAssertEqual(actor.size, imageSizes[2].scaled)
// After an additional 1.5 seconds, the initial frame should again be rendered
game.timeTraveler.travel(by: 1.5)
game.update()
XCTAssertEqual(actor.size, imageSizes[0].scaled)
}
func testSettingActorInitialSizeFromAnimation() {
let imageSize = Size(width: 200, height: 150)
let image = ImageMockFactory.makeImage(withSize: imageSize)
let actor = Actor()
actor.animation = Animation(image: image)
// Before being added to the scene, the actor's size should remain zero
XCTAssertEqual(actor.size, .zero)
// As soon as the actor is added, it should be resized (even without an update)
game.scene.add(actor)
XCTAssertEqual(actor.size, imageSize.scaled)
}
func testAnimatingWithSpriteSheet() {
let imageSize = Size(width: 300, height: 100)
let image = ImageMockFactory.makeImage(withSize: imageSize)
game.textureImageLoader.images["sheet.png"] = image.cgImage
var animation = Animation(
spriteSheetNamed: "sheet",
frameCount: 6,
rowCount: 2,
frameDuration: 1
)
animation.textureScale = 1
let actor = Actor()
actor.animation = animation
game.scene.add(actor)
XCTAssertEqual(actor.size, Size(width: 100, height: 50))
game.timeTraveler.travel(by: 1)
game.update()
XCTAssertEqual(actor.size, Size(width: 100, height: 50))
game.textureImageLoader.images["sheet2.png"] = image.cgImage
// Assigning new sprite sheet mid-animation should update the animation
var newAnimation = Animation(
spriteSheetNamed: "sheet2",
frameCount: 6,
rowCount: 2,
frameDuration: 1
)
newAnimation.textureScale = 1
actor.animation = newAnimation
game.update()
XCTAssertEqual(game.textureImageLoader.imageNames, ["sheet.png", "sheet2.png"])
}
func testDefaultAnimationTextureFormatIsPNG() {
let imageSize = Size(width: 200, height: 150)
let image = ImageMockFactory.makeImage(withSize: imageSize)
game.textureImageLoader.images["Animation/0.png"] = image.cgImage
game.textureImageLoader.images["Animation/1.png"] = image.cgImage
var animation = Animation(
name: "Animation",
frameCount: 2,
frameDuration: 1
)
animation.textureScale = 1
actor.animation = animation
game.update()
game.timeTraveler.travel(by: 1)
game.update()
XCTAssertEqual(game.textureImageLoader.imageNames, ["Animation/0.png", "Animation/1.png"])
}
func testAnimatingWithJPGTextures() {
let imageSize = Size(width: 200, height: 150)
let image = ImageMockFactory.makeImage(withSize: imageSize)
game.textureImageLoader.images["Animation/0.jpg"] = image.cgImage
game.textureImageLoader.images["Animation/1.jpg"] = image.cgImage
var animation = Animation(
name: "Animation",
frameCount: 2,
frameDuration: 1,
textureFormat: .jpg
)
animation.textureScale = 1
actor.animation = animation
game.update()
game.timeTraveler.travel(by: 1)
game.update()
XCTAssertEqual(game.textureImageLoader.imageNames, ["Animation/0.jpg", "Animation/1.jpg"])
}
func testInitializingWithTextureName() {
let imageSize = Size(width: 200, height: 150)
let image = ImageMockFactory.makeImage(withSize: imageSize)
game.textureImageLoader.images["SomeTexture.png"] = image.cgImage
let actor = Actor(textureNamed: "SomeTexture", scale: 1)
game.scene.add(actor)
XCTAssertEqual(game.textureImageLoader.imageNames, ["SomeTexture.png"])
}
func testTextureNamePrefix() {
let imageSize = Size(width: 200, height: 150)
let image = ImageMockFactory.makeImage(withSize: imageSize)
game.textureImageLoader.images["PrefixTexture.png"] = image.cgImage
actor.textureNamePrefix = "Prefix"
actor.animation = Animation(textureNamed: "Texture", scale: 1)
game.update()
XCTAssertEqual(game.textureImageLoader.imageNames, ["PrefixTexture.png"])
}
func testAddingAndRemovingPlugin() {
let plugin = PluginMock<Actor>()
actor.add(plugin)
XCTAssertTrue(plugin.isActive)
assertSameInstance(plugin.object, actor)
assertSameInstance(plugin.game, game)
actor.remove(plugin)
XCTAssertFalse(plugin.isActive)
}
func testRetrievingPlugin() {
let pluginsBeforeAdded = actor.plugins(ofType: PluginMock<Actor>.self)
XCTAssertEqual(pluginsBeforeAdded.count, 0)
let plugin = PluginMock<Actor>()
actor.add(plugin)
XCTAssertTrue(plugin.isActive)
let plugins = actor.plugins(ofType: PluginMock<Actor>.self)
XCTAssertEqual(plugins.count, 1)
assertSameInstance(plugins.first, plugin)
actor.removePlugins(ofType: PluginMock<Actor>.self)
let pluginsAfterRemoved = actor.plugins(ofType: PluginMock<Actor>.self)
XCTAssertEqual(pluginsAfterRemoved.count, 0)
}
func testConstrainingToScene() {
game.scene.size = Size(width: 500, height: 500)
actor.constraints = [.scene]
actor.size = Size(width: 100, height: 100)
actor.position.x = 500
XCTAssertEqual(actor.position.x, 450)
actor.position.y = 600
XCTAssertEqual(actor.position.y, 450)
// Set actor scale
actor.scale = 0.5
actor.position.x = 500
XCTAssertEqual(actor.position.x, 475)
actor.position.y = 600
XCTAssertEqual(actor.position.y, 475)
// Removing the constraint should free the actor to move outside of the scene
actor.constraints = []
actor.position.x = 700
XCTAssertEqual(actor.position.x, 700)
}
func testConstrainingToNotOverlapBlock() {
let blockSize = Size(width: 300, height: 300)
let blockGroup = Group.name("Block")
let block = Block(size: blockSize, textureCollectionName: "Block")
block.group = blockGroup
game.scene.add(block)
actor.size = Size(width: 100, height: 100)
actor.position = Point(x: 300, y: 0)
actor.constraints = [.neverOverlapBlockInGroup(blockGroup)]
// Approaching from the right should stop the actor at the block's right edge
actor.position = Point(x: 180, y: 0)
XCTAssertEqual(actor.position.x, 200)
// Approaching from the right should stop the actor at the block's right edge
actor.position = Point(x: -130, y: 0)
XCTAssertEqual(actor.position.x, -200)
// Approaching from the top should stop the actor at the block's top edge
actor.position = Point(x: 0, y: -130)
XCTAssertEqual(actor.position.y, -200)
// Approaching from the bottom should stop the actor at the block's bottom edge
actor.position = Point(x: 0, y: 180)
XCTAssertEqual(actor.position.y, 200)
// When moving the actor away, the least possible distance should be picked
actor.position = Point(x: 180, y: -190)
XCTAssertEqual(actor.position, Point(x: 180, y: -200))
actor.position = Point(x: 190, y: -180)
XCTAssertEqual(actor.position, Point(x: 200, y: -180))
}
func testMovedEventsNotCalledMultipleTimesWhenConstraining() {
let blockSize = Size(width: 400, height: 400)
let blockGroup = Group.name("Block")
let block = Block(size: blockSize, textureCollectionName: "Block")
block.group = blockGroup
game.scene.add(block)
actor.size = Size(width: 100, height: 100)
actor.position = Point(x: 250, y: 0)
actor.constraints = [.neverOverlapBlockInGroup(blockGroup)]
var movedCallCount = 0
actor.events.moved.observe { movedCallCount += 1 }
// Moving the actor to overlap the block should not trigger any events
// since, from the user's perspective, it simply remains at the same poisition
actor.position.x = 200
XCTAssertEqual(actor.position.x, 250)
XCTAssertEqual(movedCallCount, 0)
// Moving the actor anywhere else should trigger events
actor.position.x = 300
XCTAssertEqual(actor.position.x, 300)
XCTAssertEqual(movedCallCount, 1)
}
func testObservingMove() {
var noValueTriggerCount = 0
actor.events.moved.observe { noValueTriggerCount += 1 }
var actorPositions = [Point]()
var oldPositions = [Point]()
var newPositions = [Point]()
actor.events.moved.observe { actor, positions in
actorPositions.append(actor.position)
oldPositions.append(positions.old)
newPositions.append(positions.new)
}
actor.position.x += 100
actor.position.y += 50
XCTAssertEqual(noValueTriggerCount, 2)
XCTAssertEqual(actorPositions, [Point(x: 100, y: 0), Point(x: 100, y: 50)])
XCTAssertEqual(oldPositions, [.zero, Point(x: 100, y: 0)])
XCTAssertEqual(newPositions, [Point(x: 100, y: 0), Point(x: 100, y: 50)])
}
func testEnteredScene() {
// Create actor and place in center of scene
let actor = Actor()
actor.position = game.scene.center
game.scene.add(actor)
var enterSceneCount = 0
actor.events.enteredScene.observe { enterSceneCount += 1 }
// Move actor outside scene
actor.position.x = -100
XCTAssertFalse(actor.isWithinScene)
XCTAssertEqual(enterSceneCount, 0)
// Now move actor back into scene
actor.position.x = 0
XCTAssertTrue(actor.isWithinScene)
XCTAssertEqual(enterSceneCount, 1)
}
func testObservingResize() {
var noValueTriggerCount = 0
actor.events.resized.observe { noValueTriggerCount += 1 }
var sizes = [Size]()
actor.events.resized.observe { sizes.append($0.size) }
actor.size.width += 100
actor.size.height += 50
XCTAssertEqual(noValueTriggerCount, 2)
XCTAssertEqual(sizes, [Size(width: 100, height: 0),
Size(width: 100, height: 50)])
}
func testObservingVelocityChange() {
var noValueTriggerCount = 0
actor.events.velocityChanged.observe { noValueTriggerCount += 1 }
var velocities = [Vector]()
actor.events.velocityChanged.observe { velocities.append($0.velocity) }
actor.velocity.dx += 100
actor.velocity.dy += 50
XCTAssertEqual(noValueTriggerCount, 2)
XCTAssertEqual(velocities, [Vector(dx: 100, dy: 0), Vector(dx: 100, dy: 50)])
}
func testObservingRotationChange() {
var triggerCount = 0
actor.events.rotated.observe { triggerCount += 1 }
actor.rotation = 1
actor.rotation = 2
// Setting rotation to same value should not generate event
actor.rotation = 2
XCTAssertEqual(triggerCount, 2)
XCTAssertEqual(actor.layer.rotation, 2, accuracy: 0.001)
}
func testObservingCollisionsWithOtherActor() {
let otherActor = Actor(size: Size(width: 100, height: 100))
game.scene.add(otherActor)
actor.size = otherActor.size
actor.position = Point(x: 300, y: 300)
var numberOfCollisions = 0
actor.events.collided(with: otherActor).observe {
numberOfCollisions += 1
}
XCTAssertEqual(numberOfCollisions, 0)
// Actors start intersecting = first collision
actor.position = Point(x: 50, y: 50)
XCTAssertEqual(numberOfCollisions, 1)
// Moving any of the actors while overlapped should
// not trigger additional collisions
actor.position = Point(x: 60, y: 30)
otherActor.position = Point(x: 10, y: 10)
XCTAssertEqual(numberOfCollisions, 1)
// Moving the actors away from each other, and then back again
// should trigger another collision. Also, this time we're moving
// the other actor to ensure event symmetry.
actor.position = Point(x: 300, y: 300)
XCTAssertEqual(numberOfCollisions, 1)
otherActor.position = actor.position
XCTAssertEqual(numberOfCollisions, 2)
// Removing one of the actors should prevent further collisions
otherActor.position = .zero
otherActor.remove()
actor.position = .zero
XCTAssertEqual(numberOfCollisions, 2)
// Adding it back again should re-enable collisions
actor.position = Point(x: 300, y: 300)
game.scene.add(otherActor)
actor.position = .zero
XCTAssertEqual(numberOfCollisions, 3)
actor.position = Point(x: 300, y: 300)
// A scaled down actor should use its scale and avoid a collision
actor.scale = 0.5
actor.position = Point(x: 90, y: 90)
XCTAssertEqual(numberOfCollisions, 3)
// Scaling back up should trigger a collision
actor.scale = 1
XCTAssertEqual(numberOfCollisions, 4)
// A scaled up actor should trigger a collision from farther away.
actor.position = Point(x: 300, y: 300)
actor.scale = 1.5
actor.position = Point(x: 120, y: 120)
XCTAssertEqual(numberOfCollisions, 5)
// Setting a hitbox should override the scaling effect.
actor.hitboxSize = Size(width: 25, height: 25)
actor.position = Point(x: 90, y: 90)
XCTAssertEqual(numberOfCollisions, 5)
}
func testObservingCollisionWithActorInGroup() {
actor.size = Size(width: 100, height: 100)
actor.position = Point(x: 300, y: 300)
let group = Group.name("ActorGroup")
let otherActor = Actor(size: Size(width: 100, height: 100))
otherActor.group = group
game.scene.add(otherActor)
var numberOfCollisions = 0
actor.events.collided(withActorInGroup: group).observe {
numberOfCollisions += 1
}
XCTAssertEqual(numberOfCollisions, 0)
// Move the actor to collide with the other actor
actor.position = otherActor.position
XCTAssertEqual(numberOfCollisions, 1)
// Then move the other actor away, then back, which should also trigger a collision
otherActor.position = Point(x: 300, y: 300)
otherActor.position = actor.position
XCTAssertEqual(numberOfCollisions, 2)
}
func testShouldNotCollideWithItself() {
let group = Group.name("ActorGroup")
actor.size = Size(width: 100, height: 100)
actor.position = Point(x: 300, y: 300)
actor.group = group
var numberOfCollisions = 0
actor.events.collided(withActorInGroup: group).observe {
numberOfCollisions += 1
}
XCTAssertEqual(numberOfCollisions, 0)
// Move the actor to trigger collision detection
actor.position = Point(x: 400, y: 400)
// Actor should not collide with itself
XCTAssertEqual(numberOfCollisions, 0)
}
func testObservingCollisionWhenCollidingActorIsRemoved() {
actor.size = Size(width: 100, height: 100)
actor.position = Point(x: 300, y: 300)
let otherActor = Actor(size: Size(width: 100, height: 100))
game.scene.add(otherActor)
var numberOfCollisions = 0
actor.events.collided(with: otherActor).observe {
otherActor.remove()
numberOfCollisions += 1
}
XCTAssertEqual(numberOfCollisions, 0)
// Move the actor to trigger collision detection but not so far that it
// collides with the other actor.
actor.position = Point(x: 200, y: 0)
// Move the other actor so that it collides with the actor
otherActor.position = actor.position
XCTAssertEqual(numberOfCollisions, 1)
}
func testDisablingCollisionDetection() {
actor.size = Size(width: 100, height: 100)
actor.position = Point(x: 300, y: 300)
let otherActor = Actor(size: Size(width: 100, height: 100))
game.scene.add(otherActor)
var numberOfCollisions = 0
actor.events.collided(with: otherActor).observe {
numberOfCollisions += 1
}
otherActor.isCollisionDetectionEnabled = false
otherActor.position = actor.position
XCTAssertEqual(numberOfCollisions, 0)
// Regardless of which actor has collision detection disabled
// no collision should occur
otherActor.position = .zero
otherActor.isCollisionDetectionEnabled = true
actor.isCollisionDetectionEnabled = false
otherActor.position = actor.position
XCTAssertEqual(numberOfCollisions, 0)
// Make sure collision detection setting is still respected even
// an actor is assigned to a group
otherActor.position = .zero
actor.group = Group.number(1)
otherActor.position = actor.position
XCTAssertEqual(numberOfCollisions, 0)
}
func testObservingCollisionWithBlockInGroup() {
actor.size = Size(width: 100, height: 100)
actor.position = Point(x: 300, y: 300)
let group = Group.name("BlockGroup")
let block = Block(size: Size(width: 100, height: 100), spriteSheetName: "Block")
block.group = group
game.scene.add(block)
var numberOfCollisions = 0
actor.events.collided(withBlockInGroup: group).observe {
numberOfCollisions += 1
}
XCTAssertEqual(numberOfCollisions, 0)
// Move the actor to collide with the block
actor.position = block.position
XCTAssertEqual(numberOfCollisions, 1)
// Then move the other actor away, then back, which should also trigger a collision
block.position = Point(x: 300, y: 300)
block.position = actor.position
XCTAssertEqual(numberOfCollisions, 2)
}
func testAssigningZIndex() {
XCTAssertEqual(actor.zIndex, 0)
let secondActor = Actor()
game.scene.add(secondActor)
XCTAssertEqual(secondActor.zIndex, 1)
let thirdActor = Actor()
game.scene.add(thirdActor)
XCTAssertEqual(thirdActor.zIndex, 2)
}
func testExplicitZIndexNotOverriden() {
let actor = Actor()
actor.zIndex = 500
game.scene.add(actor)
XCTAssertEqual(actor.zIndex, 500)
}
func testLayerAndSceneReferenceRemovedWhenActorIsRemoved() {
XCTAssertNotNil(actor.layer.superlayer)
XCTAssertNotNil(actor.scene)
actor.remove()
XCTAssertNil(actor.layer.superlayer)
XCTAssertNil(actor.scene)
}
func testShadowPropertyMapping() {
let pathRect = Rect(x: 10, y: 9, width: 20, height: 17)
let path = Path(rect: pathRect, transform: nil)
actor.shadow = Shadow(
radius: 10,
opacity: 0.5,
color: .blue,
offset: Point(x: 10, y: 7),
path: path
)
XCTAssertEqual(actor.layer.shadowRadius, 10)
XCTAssertEqual(actor.layer.shadowOpacity, 0.5)
XCTAssertEqual(actor.layer.shadowColor, Color.blue.cgColor)
XCTAssertEqual(actor.layer.shadowOffset.width, 10)
XCTAssertEqual(actor.layer.shadowOffset.height, 7)
XCTAssertEqual(actor.layer.shadowPath, path)
}
func testObservingCollisionsWhileInContactAndMovingBetweenGridTiles() {
let otherActor = Actor(size: Size(width: 100, height: 100))
game.scene.add(otherActor)
actor.size = otherActor.size
actor.position = Point(x: 200, y: 0)
var numberOfCollisions = 0
actor.events.collided(with: otherActor).observe {
numberOfCollisions += 1
}
XCTAssertEqual(numberOfCollisions, 0)
// Actors start intersecting = first collision
actor.position = Point(x: 0, y: 0)
XCTAssertEqual(numberOfCollisions, 1)
// Actor moving out of grid tile and then back into it while
// in contact the whole time
actor.position = Point(x: 60, y: 0)
actor.position = Point(x: 40, y: 0)
XCTAssertEqual(numberOfCollisions, 1)
}
func testIsInContactWithOtherActor() {
let otherActor = Actor(size: Size(width: 100, height: 100))
game.scene.add(otherActor)
actor.size = otherActor.size
var collided = false
actor.events.collided(with: otherActor).observe {
collided = true
}
actor.position = Point(x: 200, y: 0)
XCTAssertFalse(actor.isInContact(with: otherActor))
XCTAssertFalse(collided)
actor.position = Point(x: 40, y: 0)
XCTAssertTrue(collided)
XCTAssertTrue(actor.isInContact(with: otherActor))
}
func testIsInContactWithBlock() {
let blockSize = Size(width: 100, height: 100)
let blockGroup = Group.name("BlockInContact")
let block = Block(size: blockSize, textureCollectionName: "BlockInContact")
block.group = blockGroup
game.scene.add(block)
actor.size = Size(width: 200, height: 200)
var collided = false
actor.events.collided(withBlockInGroup: blockGroup).observe {
collided = true
}
actor.position = Point(x: 200, y: 0)
XCTAssertFalse(actor.isInContact(with: block))
XCTAssertFalse(collided)
actor.position = Point(x: 40, y: 0)
XCTAssertTrue(collided)
XCTAssertTrue(actor.isInContact(with: block))
}
}
private extension Size {
// TODO: Needed until we find a better way of figuring out the image scale factor on macOS
var scaled: Size {
#if (os(iOS) || os(tvOS))
return self
#else
let scale = Screen.mainScreenScale
return Size(width: self.width / scale, height: self.height / scale)
#endif
}
}