Skip to content

Commit

Permalink
[ark-physics-kit] test: skphysicsbodymanager
Browse files Browse the repository at this point in the history
  • Loading branch information
markusyeo committed Apr 20, 2024
1 parent 0c89ff5 commit 7e6beba
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ArkKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
28A032E52BAD781900851BFF /* AbstractPhysicsArkSimulator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A032E42BAD781900851BFF /* AbstractPhysicsArkSimulator.swift */; };
28A032E72BAD783D00851BFF /* SKSimulator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A032E62BAD783D00851BFF /* SKSimulator.swift */; };
28A34CF92BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A34CF82BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift */; };
28A34CFB2BD3A31900BD3F4C /* SKPhysicsBodyManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A34CFA2BD3A31900BD3F4C /* SKPhysicsBodyManagerTests.swift */; };
28D006572BAF116A001B4BD4 /* TankGameCollisionStrategyManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28D006562BAF116A001B4BD4 /* TankGameCollisionStrategyManager.swift */; };
28D0065F2BAFD7E1001B4BD4 /* EntityManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28D0065E2BAFD7E1001B4BD4 /* EntityManagerTests.swift */; };
28D006612BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28D006602BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift */; };
Expand Down Expand Up @@ -537,6 +538,7 @@
28A032E42BAD781900851BFF /* AbstractPhysicsArkSimulator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbstractPhysicsArkSimulator.swift; sourceTree = "<group>"; };
28A032E62BAD783D00851BFF /* SKSimulator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKSimulator.swift; sourceTree = "<group>"; };
28A34CF82BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkSKPhysicsBodyTests.swift; sourceTree = "<group>"; };
28A34CFA2BD3A31900BD3F4C /* SKPhysicsBodyManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKPhysicsBodyManagerTests.swift; sourceTree = "<group>"; };
28D006562BAF116A001B4BD4 /* TankGameCollisionStrategyManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TankGameCollisionStrategyManager.swift; sourceTree = "<group>"; };
28D0065E2BAFD7E1001B4BD4 /* EntityManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManagerTests.swift; sourceTree = "<group>"; };
28D006602BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkPhysicsKitTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1378,6 +1380,7 @@
children = (
28D006602BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift */,
28A34CF82BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift */,
28A34CFA2BD3A31900BD3F4C /* SKPhysicsBodyManagerTests.swift */,
);
path = "ark-physics-kit-tests";
sourceTree = "<group>";
Expand Down Expand Up @@ -2438,6 +2441,7 @@
02A4382B2BB00D4D000135BC /* MockECSContext.swift in Sources */,
281AE12C2BD3928700B79B91 /* PriorityQueueTests.swift in Sources */,
28D0065F2BAFD7E1001B4BD4 /* EntityManagerTests.swift in Sources */,
28A34CFB2BD3A31900BD3F4C /* SKPhysicsBodyManagerTests.swift in Sources */,
28D006612BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ protocol AbstractArkPhysicsBody {
var categoryBitMask: UInt32 { get set }
var collisionBitMask: UInt32 { get set }
var contactTestBitMask: UInt32 { get set }

func applyImpulse(_ impulse: CGVector)
func applyAngularImpulse(_ impulse: CGFloat)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ class SKPhysicsBodyManager {
nodeToEntityMap[node]
}

func applyImpulse(_ impulse: CGVector, to entity: Entity) {
@discardableResult
func applyImpulse(_ impulse: CGVector, to entity: Entity) -> Bool {
guard let body = entityToPhysicsBodyMap[entity] else {
assertionFailure("Entity does not exist.")
return
return false
}
body.applyImpulse(impulse)
return true
}

func applyAngularImpulse(_ angularImpulse: CGFloat, to entity: Entity) {
@discardableResult
func applyAngularImpulse(_ angularImpulse: CGFloat, to entity: Entity) -> Bool {
guard let body = entityToPhysicsBodyMap[entity] else {
assertionFailure("Entity does not exist.")
return
return false
}
body.applyAngularImpulse(angularImpulse)
return true
}
}
56 changes: 56 additions & 0 deletions ArkKitTests/ark-physics-kit-tests/SKPhysicsBodyManagerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import XCTest
@testable import ArkKit

class SKPhysicsBodyManagerTests: XCTestCase {
var manager: SKPhysicsBodyManager!
var dummyEntity: Entity!
var dummyBody: ArkSKPhysicsBody!

override func setUp() {
super.setUp()
manager = SKPhysicsBodyManager()
dummyEntity = Entity()
dummyBody = ArkSKPhysicsBody(circleOf: 10)
}

func testAddBody() {
let added = manager.addBody(for: dummyEntity, body: dummyBody)
XCTAssertTrue(added, "Should be able to add a new body.")
XCTAssertEqual(manager.getBody(for: dummyEntity), dummyBody)
XCTAssertEqual(manager.getEntity(for: dummyBody.node), dummyEntity)
}

func testRemoveBody() {
_ = manager.addBody(for: dummyEntity, body: dummyBody)
manager.removeBody(for: dummyEntity)

XCTAssertNil(manager.getBody(for: dummyEntity), "Body should be removed.")
XCTAssertNil(manager.getEntity(for: dummyBody.node), "Entity mapping should be cleared.")
}

func testApplyImpulseSuccessfully() {
_ = manager.addBody(for: dummyEntity, body: dummyBody)
let result = manager.applyImpulse(CGVector(dx: 10, dy: 20), to: dummyEntity)

XCTAssertTrue(result, "Apply impulse should succeed.")
}

func testApplyImpulseFailsForNonexistentEntity() {
let result = manager.applyImpulse(CGVector(dx: 10, dy: 20), to: dummyEntity)

XCTAssertFalse(result, "Apply impulse should fail for nonexistent entity.")
}

func testApplyAngularImpulseSuccessfully() {
_ = manager.addBody(for: dummyEntity, body: dummyBody)
let result = manager.applyAngularImpulse(30, to: dummyEntity)

XCTAssertTrue(result, "Apply angular impulse should succeed.")
}

func testApplyAngularImpulseFailsForNonexistentEntity() {
let result = manager.applyAngularImpulse(30, to: dummyEntity)

XCTAssertFalse(result, "Apply angular impulse should fail for nonexistent entity.")
}
}

0 comments on commit 7e6beba

Please sign in to comment.