From 8ec15a12b35df2cbde6aac31916a1980bb2ef20b Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Thu, 21 Mar 2024 16:54:37 +0800 Subject: [PATCH 01/11] fix: fix typo --- SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift | 2 +- SDPhysicsEngine/Sources/SDPhysicsEngine/SDSceneDelegate.swift | 2 +- star-dash/star-dash/ViewController.swift | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift index 77903c85..183c52c5 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift @@ -52,7 +52,7 @@ extension GameScene: SKPhysicsContactDelegate { fatalError("Unknown node in game scene") } - sceneDelegate?.contactOccured( + sceneDelegate?.contactOccurred( objectA: objectA, objectB: objectB, contactPoint: contact.contactPoint diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/SDSceneDelegate.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/SDSceneDelegate.swift index 7b43f92d..16b3f1eb 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/SDSceneDelegate.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/SDSceneDelegate.swift @@ -3,5 +3,5 @@ import CoreGraphics public protocol SDSceneDelegate: AnyObject { func update(_ scene: SDScene, deltaTime: Double) - func contactOccured(objectA: SDObject, objectB: SDObject, contactPoint: CGPoint) + func contactOccurred(objectA: SDObject, objectB: SDObject, contactPoint: CGPoint) } diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index cb24472b..62d6589e 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -68,7 +68,7 @@ extension ViewController: SDSceneDelegate { gameBridge?.syncFromEntities() } - func contactOccured(objectA: SDObject, objectB: SDObject, contactPoint: CGPoint) { + func contactOccurred(objectA: SDObject, objectB: SDObject, contactPoint: CGPoint) { guard let entityA = gameBridge?.entityId(of: objectA.id), let entityB = gameBridge?.entityId(of: objectB.id) else { return From 2d2d31014aa09913002f2a314afa9db5d46f586e Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Thu, 21 Mar 2024 17:41:09 +0800 Subject: [PATCH 02/11] style: lint --- .../Rendering/MTKRenderer/ControlView.swift | 6 ++++-- star-dash/star-dashUITests/star_dashUITests.swift | 14 +++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift b/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift index 2850aa52..47729a3c 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift @@ -58,7 +58,8 @@ class ControlView: UIView { // MARK: Gesture handler methods - @objc func jumpButtonTapped() { + @objc + func jumpButtonTapped() { controlViewDelegate?.jumpButtonPressed() } @@ -85,7 +86,8 @@ class ControlView: UIView { joystickView?.returnJoystick() } - @objc func handlePan(_ gesture: UIPanGestureRecognizer) { + @objc + func handlePan(_ gesture: UIPanGestureRecognizer) { guard let joystickView = self.joystickView else { return } diff --git a/star-dash/star-dashUITests/star_dashUITests.swift b/star-dash/star-dashUITests/star_dashUITests.swift index 48efc1a4..5379bb05 100644 --- a/star-dash/star-dashUITests/star_dashUITests.swift +++ b/star-dash/star-dashUITests/star_dashUITests.swift @@ -9,15 +9,15 @@ import XCTest final class star_dashUITests: XCTestCase { - override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. + // override func setUpWithError() throws { + // // Put setup code here. This method is called before the invocation of each test method in the class. - // In UI tests it is usually best to stop immediately when a failure occurs. - continueAfterFailure = false + // // In UI tests it is usually best to stop immediately when a failure occurs. + // continueAfterFailure = false - // In UI tests it’s important to set the initial state - such as interface orientation - - // required for your tests before they run. The setUp method is a good place to do this. - } + // // In UI tests it’s important to set the initial state - such as interface orientation - + // // required for your tests before they run. The setUp method is a good place to do this. + // } // override func tearDownWithError() throws { // // Put teardown code here. This method is called after the invocation of each test method in the class. From 6cdcc8ceb12332f39c998dc6e64b174b7e530ddb Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Thu, 21 Mar 2024 18:00:56 +0800 Subject: [PATCH 03/11] build: fix swiftlint warnings and deprecations --- .swiftlint.yml | 9 +++++---- star-dash/.swiftlint.yml | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 5310eee9..3d4ab71e 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -2,7 +2,6 @@ excluded: - Pods opt_in_rules: - - anyobject_protocol - array_init - attributes - closure_body_length @@ -20,7 +19,6 @@ opt_in_rules: - empty_xctest_method - expiring_todo - explicit_init - - explicit_self - fallthrough - fatal_error_message - file_name @@ -65,8 +63,6 @@ opt_in_rules: - unneeded_parentheses_in_closure_argument - unowned_variable_capture - untyped_error_in_catch - - unused_declaration - - unused_import - vertical_parameter_alignment_on_call - xct_specific_matcher - yoda_condition @@ -81,3 +77,8 @@ disabled_rules: line_length: warning: 120 error: 150 + +analyzer_rules: + - explicit_self + - unused_declaration + - unused_import diff --git a/star-dash/.swiftlint.yml b/star-dash/.swiftlint.yml index 5310eee9..3d4ab71e 100644 --- a/star-dash/.swiftlint.yml +++ b/star-dash/.swiftlint.yml @@ -2,7 +2,6 @@ excluded: - Pods opt_in_rules: - - anyobject_protocol - array_init - attributes - closure_body_length @@ -20,7 +19,6 @@ opt_in_rules: - empty_xctest_method - expiring_todo - explicit_init - - explicit_self - fallthrough - fatal_error_message - file_name @@ -65,8 +63,6 @@ opt_in_rules: - unneeded_parentheses_in_closure_argument - unowned_variable_capture - untyped_error_in_catch - - unused_declaration - - unused_import - vertical_parameter_alignment_on_call - xct_specific_matcher - yoda_condition @@ -81,3 +77,8 @@ disabled_rules: line_length: warning: 120 error: 150 + +analyzer_rules: + - explicit_self + - unused_declaration + - unused_import From 7bcd90b5c55ddd9d7c129efbc5032dd227b12195 Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Thu, 21 Mar 2024 18:01:14 +0800 Subject: [PATCH 04/11] build: remove duplicate build files --- star-dash/star-dash.xcodeproj/project.pbxproj | 2 -- 1 file changed, 2 deletions(-) diff --git a/star-dash/star-dash.xcodeproj/project.pbxproj b/star-dash/star-dash.xcodeproj/project.pbxproj index 6090ff75..f6f6ed4e 100644 --- a/star-dash/star-dash.xcodeproj/project.pbxproj +++ b/star-dash/star-dash.xcodeproj/project.pbxproj @@ -764,13 +764,11 @@ 46D418282BA5D6800091A38B /* Floor+Collidable.swift in Sources */, 46D418222BA5D4E60091A38B /* Obstacle+Collidable.swift in Sources */, E638B9CF2BAB3C5D00931CC2 /* TeleportEvent.swift in Sources */, - E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */, E6A745182BA057040080C1BE /* Renderer.swift in Sources */, 143AA3972BA4E0D9009C28E7 /* PickupCollectibleEvent.swift in Sources */, 46D418242BA5D5280091A38B /* Tool+Collidable.swift in Sources */, E64361142BA4C2CD003850FD /* CreationModule.swift in Sources */, E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */, - E6A745182BA057040080C1BE /* Renderer.swift in Sources */, E6A745172BA057040080C1BE /* ControlView.swift in Sources */, 14970F562BA8177B00CC1E8A /* GameConstants.swift in Sources */, 1471B0A42BA6AAF200878B14 /* PlayerDeathEvent.swift in Sources */, From 7f755e13d798f136ee4a03065f00287d246fa5f9 Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Thu, 21 Mar 2024 23:04:08 +0800 Subject: [PATCH 05/11] fix: fix typo --- star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift index dbd7042b..62a80d02 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift @@ -7,7 +7,7 @@ import MetalKit the game on to the iOS device. The `SKScene` is rendered through a MetalKit while the controls - and game information overlay is rendered throuhg UIKit. + and game information overlay is rendered through UIKit. */ class MTKRenderer: NSObject, Renderer { var scene: SKScene From a75f656e8480b6af0c96492a78f891f683672874 Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Fri, 22 Mar 2024 01:37:06 +0800 Subject: [PATCH 06/11] feat: add SDCameraObject --- .../SDPhysicsEngine/Object/SDCameraObject.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDCameraObject.swift diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDCameraObject.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDCameraObject.swift new file mode 100644 index 00000000..320069ef --- /dev/null +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDCameraObject.swift @@ -0,0 +1,10 @@ +import SpriteKit + +public class SDCameraObject: SDObject { + let cameraNode: SKCameraNode + + override public init() { + cameraNode = SKCameraNode() + super.init(node: cameraNode) + } +} From 5fd52ea3962b6024ddb28a856e1dc769d663540f Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Sat, 23 Mar 2024 00:44:58 +0800 Subject: [PATCH 07/11] feat: fix camera's x position to player --- SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift | 9 +++++++++ SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift | 2 ++ star-dash/star-dash/GameEngine/GameEngine.swift | 8 ++++++++ star-dash/star-dash/ViewController.swift | 9 +++++++++ 4 files changed, 28 insertions(+) diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift index 183c52c5..46b38194 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift @@ -38,6 +38,15 @@ extension GameScene: SDScene { objectMap[object.node] = object addChild(object.node) } + + public func addCameraObject(_ cameraObject: SDCameraObject) { + addObject(cameraObject) + camera = cameraObject.cameraNode + } + + public func setCameraObjectXPosition(to x: CGFloat) { + camera?.position.x = x + } } extension GameScene: SKPhysicsContactDelegate { diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift index 79f0fe83..9ababb73 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift @@ -5,4 +5,6 @@ public protocol SDScene { var size: CGSize { get } func addObject(_ object: SDObject) + func addCameraObject(_ cameraObject: SDCameraObject) + func setCameraObjectXPosition(to x: CGFloat) } diff --git a/star-dash/star-dash/GameEngine/GameEngine.swift b/star-dash/star-dash/GameEngine/GameEngine.swift index 207872fc..f72fc1f8 100644 --- a/star-dash/star-dash/GameEngine/GameEngine.swift +++ b/star-dash/star-dash/GameEngine/GameEngine.swift @@ -54,6 +54,14 @@ class GameEngine { eventManager.add(event: MoveEvent(on: playerEntityId, toLeft: toLeft)) } + func playerPosition() -> CGPoint? { + guard let playerEntityId = entityManager.playerEntityId(), + let positionComponent = entityManager.component(ofType: PositionComponent.self, of: playerEntityId) else { + return nil + } + return positionComponent.position + } + private func setUpSystems() { systemManager.add(PositionSystem(entityManager, dispatcher: self)) systemManager.add(PhysicsSystem(entityManager, dispatcher: self)) diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index 62d6589e..e7d2d7c5 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -43,6 +43,10 @@ class ViewController: UIViewController { return } + let camera = SDCameraObject() + camera.position = CGPoint(x: scene.size.width / 2, y: scene.size.height / 2) + scene.addCameraObject(camera) + let background = SDSpriteObject(imageNamed: "GameBackground") background.position = CGPoint(x: scene.size.width / 2, y: scene.size.height / 2) background.zPosition = -1 @@ -66,6 +70,11 @@ extension ViewController: SDSceneDelegate { gameBridge?.syncToEntities() gameEngine?.update(by: deltaTime) gameBridge?.syncFromEntities() + + guard let playerPosition = gameEngine?.playerPosition() else { + return + } + scene.setCameraObjectXPosition(to: playerPosition.x) } func contactOccurred(objectA: SDObject, objectB: SDObject, contactPoint: CGPoint) { From 1d97e25a543129ab16e2e1bff2610e82cd1b7f00 Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Sat, 23 Mar 2024 00:52:01 +0800 Subject: [PATCH 08/11] fix: fix update logic parameter --- star-dash/star-dash/ViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index 91d9cb79..59ec568a 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -84,11 +84,11 @@ extension ViewController: SDSceneDelegate { gameEngine?.update(by: deltaTime) gameBridge?.syncFromEntities() - updateCameraObjectPosition() + updateCameraObjectPosition(scene) updateOverlay() } - private func updateCameraObjectPosition() { + private func updateCameraObjectPosition(_ scene: SDScene) { guard let playerPosition = gameEngine?.playerPosition() else { return } From 325f6bc51b8d44e7a6a1b37d8273243dfefd5369 Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Sat, 23 Mar 2024 00:56:07 +0800 Subject: [PATCH 09/11] style: lint --- .../star-dash/Persistence/Database.swift | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/star-dash/star-dash/Persistence/Database.swift b/star-dash/star-dash/Persistence/Database.swift index b366a869..4949a6f9 100644 --- a/star-dash/star-dash/Persistence/Database.swift +++ b/star-dash/star-dash/Persistence/Database.swift @@ -233,36 +233,34 @@ struct Database { extension Database { func insertJsonData() { - do { - if let fileURL = Bundle.main.url(forResource: "data", withExtension: "json") { - // Read JSON data from the file - do { - let jsonData = try Data(contentsOf: fileURL) - // Decode JSON data into LevelData - let levelData = try JSONDecoder().decode(LevelData.self, from: jsonData) - let levelPersistable = LevelPersistable(id: levelData.id, name: levelData.name, size: levelData.size) - insert(persistable: levelPersistable) - for persistable in levelData.collectibles { - insert(persistable: persistable) - } - for persistable in levelData.tools { - insert(persistable: persistable) - } - for persistable in levelData.obstacles { - insert(persistable: persistable) - } - for persistable in levelData.monsters { - insert(persistable: persistable) - } - - } catch { - print("Error reading or decoding JSON: \(error)") + if let fileURL = Bundle.main.url(forResource: "data", withExtension: "json") { + // Read JSON data from the file + do { + let jsonData = try Data(contentsOf: fileURL) + // Decode JSON data into LevelData + let levelData = try JSONDecoder().decode(LevelData.self, from: jsonData) + let levelPersistable = LevelPersistable(id: levelData.id, + name: levelData.name, + size: levelData.size) + insert(persistable: levelPersistable) + for persistable in levelData.collectibles { + insert(persistable: persistable) + } + for persistable in levelData.tools { + insert(persistable: persistable) + } + for persistable in levelData.obstacles { + insert(persistable: persistable) + } + for persistable in levelData.monsters { + insert(persistable: persistable) } - } else { - print("JSON file not found.") + + } catch { + print("Error reading or decoding JSON: \(error)") } - } catch { - print(error) + } else { + print("JSON file not found.") } } From b170293a0c8d8319e80c855c1a8cedff1d725738 Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Sat, 23 Mar 2024 00:59:44 +0800 Subject: [PATCH 10/11] build: remove duplicate build file --- star-dash/star-dash.xcodeproj/project.pbxproj | 1 - 1 file changed, 1 deletion(-) diff --git a/star-dash/star-dash.xcodeproj/project.pbxproj b/star-dash/star-dash.xcodeproj/project.pbxproj index 09f536bb..34ef0811 100644 --- a/star-dash/star-dash.xcodeproj/project.pbxproj +++ b/star-dash/star-dash.xcodeproj/project.pbxproj @@ -852,7 +852,6 @@ E69EE9322BAC6CBB00033AB5 /* GameInfo.swift in Sources */, 46D418222BA5D4E60091A38B /* Obstacle+Collidable.swift in Sources */, E638B9CF2BAB3C5D00931CC2 /* TeleportEvent.swift in Sources */, - E6A745182BA057040080C1BE /* Renderer.swift in Sources */, 143AA3972BA4E0D9009C28E7 /* PickupCollectibleEvent.swift in Sources */, 46D418242BA5D5280091A38B /* Tool+Collidable.swift in Sources */, E64361142BA4C2CD003850FD /* CreationModule.swift in Sources */, From 1e9f122199027a862df960100b23bf3d5f7e41ce Mon Sep 17 00:00:00 2001 From: jasonqiu212 Date: Sat, 23 Mar 2024 01:02:13 +0800 Subject: [PATCH 11/11] style: lint --- SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift index e37a2460..c002f9e7 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift @@ -38,7 +38,7 @@ extension GameScene: SDScene { objectMap[object.node] = object addChild(object.node) } - + public func removeObject(_ object: SDObject) { objectMap[object.node] = nil object.removeFromParent()