-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from Ark-Kit/tests/physics-kit
[ark-physics-kit] tests and abstracting classes
- Loading branch information
Showing
23 changed files
with
773 additions
and
86 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
ArkKit/ark-physics-kit/sprite-kit-physics-facade/ArkSKPhysicsBodyFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
|
||
class ArkSKPhysicsBodyFactory: AbstractArkSKPhysicsBodyFactory { | ||
func createCirclePhysicsBody(for entity: Entity, radius: CGFloat, | ||
at position: CGPoint) -> any AbstractArkSKPhysicsBody { | ||
ArkSKPhysicsBody(circleOf: radius, at: position) | ||
} | ||
|
||
func createRectanglePhysicsBody(for entity: Entity, size: CGSize, | ||
at position: CGPoint) -> any AbstractArkSKPhysicsBody { | ||
ArkSKPhysicsBody(rectangleOf: size, at: position) | ||
} | ||
|
||
func createPolygonPhysicsBody(for entity: Entity, vertices: [CGPoint], | ||
at position: CGPoint) -> any AbstractArkSKPhysicsBody { | ||
ArkSKPhysicsBody(polygonOf: vertices, at: position) | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
ArkKit/ark-physics-kit/sprite-kit-physics-facade/BaseSKPhysicsScene.swift
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
ArkKit/ark-physics-kit/sprite-kit-physics-facade/BaseSKScene.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import SpriteKit | ||
|
||
class BaseSKScene: AbstractSKScene { | ||
override func sceneDidLoad() { | ||
super.sceneDidLoad() | ||
physicsWorld.gravity = CGVector(dx: 0, dy: -9.81) | ||
physicsWorld.contactDelegate = self | ||
} | ||
} | ||
|
||
extension BaseSKScene: SKPhysicsContactDelegate { | ||
func didBegin(_ contact: SKPhysicsContact) { | ||
guard let entityA = gameScene?.getEntity(for: contact.bodyA), | ||
let entityB = gameScene?.getEntity(for: contact.bodyB) else { | ||
return | ||
} | ||
sceneContactUpdateDelegate?.didContactBegin(between: entityA, and: entityB) | ||
} | ||
|
||
func didEnd(_ contact: SKPhysicsContact) { | ||
guard let entityA = gameScene?.getEntity(for: contact.bodyA), | ||
let entityB = gameScene?.getEntity(for: contact.bodyB) else { | ||
return | ||
} | ||
sceneContactUpdateDelegate?.didContactEnd(between: entityA, and: entityB) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
ArkKit/ark-physics-kit/sprite-kit-physics-facade/abstracts/AbstractArkSKPhysicsBody.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import SpriteKit | ||
|
||
protocol AbstractArkSKPhysicsBody: AbstractArkPhysicsBody { | ||
var node: SKNode { get } | ||
} |
10 changes: 10 additions & 0 deletions
10
...ark-physics-kit/sprite-kit-physics-facade/abstracts/AbstractArkSKPhysicsBodyFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
protocol AbstractArkSKPhysicsBodyFactory { | ||
func createCirclePhysicsBody(for entity: Entity, radius: CGFloat, | ||
at position: CGPoint) -> any AbstractArkSKPhysicsBody | ||
func createRectanglePhysicsBody(for entity: Entity, size: CGSize, | ||
at position: CGPoint) -> any AbstractArkSKPhysicsBody | ||
func createPolygonPhysicsBody(for entity: Entity, vertices: [CGPoint], | ||
at position: CGPoint) -> any AbstractArkSKPhysicsBody | ||
} |
35 changes: 35 additions & 0 deletions
35
ArkKit/ark-physics-kit/sprite-kit-physics-facade/abstracts/AbstractSKScene.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import SpriteKit | ||
|
||
protocol AbstractSKSceneProtocol: AnyObject { | ||
var gameScene: SKPhysicsScene? { get set } | ||
var sceneContactUpdateDelegate: ArkPhysicsContactUpdateDelegate? { get set } | ||
var sceneUpdateLoopDelegate: ArkPhysicsSceneUpdateLoopDelegate? { get set } | ||
var currentTime: TimeInterval { get set } | ||
var deltaTime: TimeInterval { get } | ||
|
||
func update(_ currentTime: TimeInterval) | ||
} | ||
|
||
class AbstractSKScene: SKScene, AbstractSKSceneProtocol { | ||
weak var gameScene: SKPhysicsScene? | ||
weak var sceneContactUpdateDelegate: ArkPhysicsContactUpdateDelegate? | ||
weak var sceneUpdateLoopDelegate: ArkPhysicsSceneUpdateLoopDelegate? | ||
|
||
var currentTime: TimeInterval = 0 | ||
private var lastUpdateTime: TimeInterval = 0 | ||
var deltaTime: TimeInterval = 0 | ||
|
||
func calculateDeltaTime(from currentTime: TimeInterval) -> TimeInterval { | ||
if lastUpdateTime.isZero { | ||
lastUpdateTime = currentTime | ||
} | ||
let deltaTime = currentTime - lastUpdateTime | ||
lastUpdateTime = currentTime | ||
return deltaTime | ||
} | ||
|
||
override func update(_ currentTime: TimeInterval) { | ||
deltaTime = calculateDeltaTime(from: currentTime) | ||
self.currentTime = currentTime | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
ArkKitTests/ark-physics-kit-tests/AbstractArkPhysicsBodyTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import XCTest | ||
@testable import ArkKit | ||
|
||
class AbstractArkPhysicsBodyTests: XCTestCase { | ||
var physicsBody: MockArkSKPhysicsBody! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
physicsBody = MockArkSKPhysicsBody() | ||
} | ||
|
||
func testPropertySettings() { | ||
physicsBody.mass = 10.0 | ||
XCTAssertEqual(physicsBody.mass, 10.0, "Mass should be settable and match the expected value.") | ||
} | ||
|
||
func testApplyLinearImpulse() { | ||
let impulse = CGVector(dx: 15, dy: 20) | ||
physicsBody.applyImpulse(impulse) | ||
XCTAssertEqual(physicsBody.velocity, impulse, "Applied linear impulse should affect the velocity correctly.") | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
ArkKitTests/ark-physics-kit-tests/AbstractArkPhysicsSceneTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import XCTest | ||
@testable import ArkKit | ||
|
||
class AbstractArkPhysicsSceneTests: XCTestCase { | ||
var scene: MockArkPhysicsScene! | ||
var entity: Entity! | ||
var physicsBody: AbstractArkPhysicsBody! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
scene = MockArkPhysicsScene() | ||
entity = Entity() | ||
physicsBody = scene.createCirclePhysicsBody(for: entity, withRadius: 20.0, at: .zero) | ||
} | ||
|
||
func testCreateCirclePhysicsBody() { | ||
let radius: CGFloat = 5.0 | ||
let position = CGPoint(x: 100, y: 100) | ||
_ = scene.createCirclePhysicsBody(for: entity, withRadius: radius, at: position) | ||
XCTAssertNotNil(scene.getPhysicsBody(for: entity), "Physics body should be created and retrievable.") | ||
} | ||
|
||
func testApplyImpulse() { | ||
let impulse = CGVector(dx: 10, dy: 10) | ||
scene.apply(impulse: impulse, to: entity) | ||
XCTAssertEqual(physicsBody.velocity, impulse, "Impulse should be applied correctly to the body's velocity.") | ||
} | ||
|
||
func testSetGravity() { | ||
let gravity = CGVector(dx: 0, dy: -9.8) | ||
scene.setGravity(gravity) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.