From d884c293fea8886aacc75a251fb93e678b6324d3 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 18:33:49 +0800 Subject: [PATCH 01/13] wip: Add coin collectible --- .../GameEngine/Components/SpriteComponent.swift | 6 +++--- .../Entities/GameEntities/Collectible.swift | 17 ++++++++++++++--- star-dash/star-dash/ViewController.swift | 5 +++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/star-dash/star-dash/GameEngine/Components/SpriteComponent.swift b/star-dash/star-dash/GameEngine/Components/SpriteComponent.swift index 585fac6d..6059e767 100644 --- a/star-dash/star-dash/GameEngine/Components/SpriteComponent.swift +++ b/star-dash/star-dash/GameEngine/Components/SpriteComponent.swift @@ -12,16 +12,16 @@ class SpriteComponent: Component { // for sprite set will need to discuss how to rep animation var image: String var textureAtlas: String? - var size: CGSize + var size: CGSize? - init(id: ComponentId, entityId: EntityId, image: String, textureAtlas: String?, size: CGSize) { + init(id: ComponentId, entityId: EntityId, image: String, textureAtlas: String?, size: CGSize?) { self.image = image self.size = size self.textureAtlas = textureAtlas super.init(id: id, entityId: entityId) } - convenience init(entityId: EntityId, image: String, textureAtlas: String?, size: CGSize) { + convenience init(entityId: EntityId, image: String, textureAtlas: String?, size: CGSize?) { self.init(id: UUID(), entityId: entityId, image: image, textureAtlas: textureAtlas, size: size) } } diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift index bb0d7c39..5672c82e 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift @@ -10,23 +10,34 @@ import Foundation class Collectible: Entity { let id: EntityId private let position: CGPoint + private let sprite: String + private let points: Int - init(id: EntityId, position: CGPoint) { + init(id: EntityId, position: CGPoint, sprite: String, points: Int) { self.id = id self.position = position + self.sprite = sprite + self.points = points } - convenience init(position: CGPoint) { - self.init(id: UUID(), position: position) + convenience init(position: CGPoint, sprite: String, points: Int) { + self.init(id: UUID(), position: position, sprite: sprite, points: points) } func setUpAndAdd(to: EntityManager) { let positionComponent = PositionComponent(entityId: self.id, position: self.position, rotation: .zero) let physicsComponent = PhysicsComponent(entityId: self.id, size: PhysicsConstants.Dimensions.collectible) + physicsComponent.affectedByGravity = false physicsComponent.collisionBitMask = PhysicsConstants.CollisionMask.collectible + let spriteComponent = SpriteComponent(image: sprite, textureAtlas: nil, size: nil) to.add(entity: self) to.add(component: positionComponent) to.add(component: physicsComponent) + to.add(component: spriteComponent) + } + + public static func createCoinCollectible(position: CGPoint) -> Collectible { + return Collectible(position: position, sprite: "Coin", points: 10) } } diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index cb24472b..2db95c21 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -57,6 +57,11 @@ class ViewController: UIViewController { let floor = Floor(position: CGPoint(x: scene.size.width / 2, y: scene.size.height / 2 - 400)) floor.setUpAndAdd(to: entityManager) + + let collectible = Collectible.createCoinCollectible( + position: CGPoint(scene.size.width + 50, y: scene.size.height / 2 + 100) + ) + collectible.setUpAndAdd(to: entityManager) } } From 91b34717bce9d12bc1bee413813a0e45511e54ea Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 21:24:23 +0800 Subject: [PATCH 02/13] wip: Display score in overlay --- star-dash/star-dash/GameEngine/GameEngine.swift | 12 ++++++++++++ star-dash/star-dash/GameEngine/GameState.swift | 3 +++ .../Rendering/MTKRenderer/MTKRenderer.swift | 4 ++++ .../Rendering/MTKRenderer/OverlayView.swift | 4 ++++ .../star-dash/Rendering/MTKRenderer/PlayerView.swift | 4 ++++ star-dash/star-dash/Rendering/OverlayInfo.swift | 3 +++ star-dash/star-dash/Rendering/Renderer.swift | 1 + star-dash/star-dash/ViewController.swift | 8 ++++++++ 8 files changed, 39 insertions(+) create mode 100644 star-dash/star-dash/GameEngine/GameState.swift create mode 100644 star-dash/star-dash/Rendering/OverlayInfo.swift diff --git a/star-dash/star-dash/GameEngine/GameEngine.swift b/star-dash/star-dash/GameEngine/GameEngine.swift index 207872fc..496a79b1 100644 --- a/star-dash/star-dash/GameEngine/GameEngine.swift +++ b/star-dash/star-dash/GameEngine/GameEngine.swift @@ -20,6 +20,18 @@ class GameEngine { setUpSystems() } + func gameState() -> GameState? { + guard let scoreSystem = systemManager.system(ofType: ScoreSystem.self), + let playerEntityId = entityManager.playerEntityId(), + let scoreComponent = scoreSystem.getScoreComponent(of: playerEntityId) else { + return nil + } + + return GameState( + playerScore: scoreComponent.score + ) + } + func update(by deltaTime: TimeInterval) { systemManager.update(by: deltaTime) eventManager.executeAll(on: self) diff --git a/star-dash/star-dash/GameEngine/GameState.swift b/star-dash/star-dash/GameEngine/GameState.swift new file mode 100644 index 00000000..1f73dfea --- /dev/null +++ b/star-dash/star-dash/GameEngine/GameState.swift @@ -0,0 +1,3 @@ +struct GameState { + let playerScore: Float +} \ No newline at end of file diff --git a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift index dbd7042b..e0df2517 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift @@ -35,6 +35,10 @@ class MTKRenderer: NSObject, Renderer { super.init() } + func updateOverlay(gameState: GameState) { + playerView?.updateOverlay(score: gameState.playerScore) + } + /// Set ups the views for a single player game. func createSinglePlayerView(at superview: UIView) { let playerView = PlayerView(superview: superview, device: self.device, drawDelegate: self) diff --git a/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift b/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift index 10334cad..47ed588c 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift @@ -23,4 +23,8 @@ class OverlayView: UIView { scoreLabel.leadingAnchor.constraint(greaterThanOrEqualTo: self.leadingAnchor, constant: margin) ]) } + + func update(playerScore: Float) { + scoreLabel.text = "Score: \(playerScore)" + } } diff --git a/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift b/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift index aa65fff2..c67d0115 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift @@ -35,4 +35,8 @@ class PlayerView { func setControlViewDelegate(_ delegate: ControlViewDelegate) { controlView.controlViewDelegate = delegate } + + func updateOverlay(playerScore: Float) { + + } } diff --git a/star-dash/star-dash/Rendering/OverlayInfo.swift b/star-dash/star-dash/Rendering/OverlayInfo.swift new file mode 100644 index 00000000..1f044b6c --- /dev/null +++ b/star-dash/star-dash/Rendering/OverlayInfo.swift @@ -0,0 +1,3 @@ +struct OverlayInfo { + let score: Float +} \ No newline at end of file diff --git a/star-dash/star-dash/Rendering/Renderer.swift b/star-dash/star-dash/Rendering/Renderer.swift index 89278d8b..1cd7c11c 100644 --- a/star-dash/star-dash/Rendering/Renderer.swift +++ b/star-dash/star-dash/Rendering/Renderer.swift @@ -4,5 +4,6 @@ import UIKit The `Renderer` protocol defines the requirements for an object responsible for rendering game objects onto a view. */ protocol Renderer { + func updateOverlay(overlayInfo: OverlayInfo) func createSinglePlayerView(at rootView: UIView) } diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index 2db95c21..2eb126da 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -71,6 +71,14 @@ extension ViewController: SDSceneDelegate { gameBridge?.syncToEntities() gameEngine?.update(by: deltaTime) gameBridge?.syncFromEntities() + + guard let gameState = gameEngine?.gameState() else { + return + } + + renderer.updateOverlay(overlyInfo: OverlayInfo( + score: gameState.playerScore + )) } func contactOccured(objectA: SDObject, objectB: SDObject, contactPoint: CGPoint) { From 2da7ec52f3be78999c41a554612ed0773db09ae7 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 19:19:40 +0800 Subject: [PATCH 03/13] fix: Fix missing systems and contact/collision masks --- .../star-dash/Constants/PhysicsConstants.swift | 14 ++++++++++++-- .../GameBridge/SyncModule/PhysicsModule.swift | 1 + .../Entities/GameEntities/Collectible.swift | 9 ++++++--- .../GameEngine/Entities/GameEntities/Floor.swift | 2 +- .../GameEngine/Entities/GameEntities/Player.swift | 2 +- star-dash/star-dash/GameEngine/GameEngine.swift | 1 + star-dash/star-dash/ViewController.swift | 2 +- 7 files changed, 23 insertions(+), 8 deletions(-) diff --git a/star-dash/star-dash/Constants/PhysicsConstants.swift b/star-dash/star-dash/Constants/PhysicsConstants.swift index 7a7e0dc4..395345d1 100644 --- a/star-dash/star-dash/Constants/PhysicsConstants.swift +++ b/star-dash/star-dash/Constants/PhysicsConstants.swift @@ -21,14 +21,24 @@ struct PhysicsConstants { } struct CollisionMask { - static let player = CollisionCategory.max ^ CollisionCategory.player + static let player = CollisionCategory.max ^ CollisionCategory.player ^ CollisionCategory.collectible static let monster = CollisionCategory.player | CollisionCategory.tool - static let collectible = CollisionCategory.player + static let collectible = CollisionCategory.none static let obstacle = CollisionCategory.player | CollisionCategory.monster | CollisionMask.tool static let tool = CollisionCategory.max ^ CollisionCategory.collectible ^ CollisionCategory.tool static let wall = CollisionCategory.player | CollisionCategory.monster | CollisionCategory.tool static let floor = CollisionCategory.player | CollisionCategory.monster | CollisionCategory.tool } + + struct ContactMask { + static let player = CollisionCategory.floor | CollisionCategory.collectible + static let monster = CollisionCategory.player + static let collectible = CollisionCategory.player + static let obstacle = CollisionCategory.none + static let tool = CollisionCategory.obstacle + static let wall = CollisionCategory.tool | CollisionCategory.player + static let floor = CollisionCategory.player + } struct Dimensions { // TODO: determine appropriate size for each diff --git a/star-dash/star-dash/GameBridge/SyncModule/PhysicsModule.swift b/star-dash/star-dash/GameBridge/SyncModule/PhysicsModule.swift index e9e4e622..b20a5a82 100644 --- a/star-dash/star-dash/GameBridge/SyncModule/PhysicsModule.swift +++ b/star-dash/star-dash/GameBridge/SyncModule/PhysicsModule.swift @@ -38,6 +38,7 @@ class PhysicsModule: SyncModule { object.physicsBody = createRectanglePhysicsBody(physicsComponent: physicsComponent) object.physicsBody?.restitution = physicsComponent.restitution object.physicsBody?.isDynamic = physicsComponent.isDynamic + object.physicsBody?.affectedByGravity = physicsComponent.affectedByGravity object.physicsBody?.categoryBitMask = physicsComponent.categoryBitMask object.physicsBody?.contactTestMask = physicsComponent.contactTestMask object.physicsBody?.collisionBitMask = physicsComponent.collisionBitMask diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift index 5672c82e..7caec26a 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift @@ -28,8 +28,11 @@ class Collectible: Entity { let positionComponent = PositionComponent(entityId: self.id, position: self.position, rotation: .zero) let physicsComponent = PhysicsComponent(entityId: self.id, size: PhysicsConstants.Dimensions.collectible) physicsComponent.affectedByGravity = false + physicsComponent.isDynamic = false + physicsComponent.categoryBitMask = PhysicsConstants.CollisionCategory.collectible + physicsComponent.contactTestMask = PhysicsConstants.ContactMask.collectible physicsComponent.collisionBitMask = PhysicsConstants.CollisionMask.collectible - let spriteComponent = SpriteComponent(image: sprite, textureAtlas: nil, size: nil) + let spriteComponent = SpriteComponent(entityId: self.id, image: sprite, textureAtlas: nil, size: nil) to.add(entity: self) to.add(component: positionComponent) @@ -37,7 +40,7 @@ class Collectible: Entity { to.add(component: spriteComponent) } - public static func createCoinCollectible(position: CGPoint) -> Collectible { - return Collectible(position: position, sprite: "Coin", points: 10) + static func createCoinCollectible(position: CGPoint) -> Collectible { + Collectible(position: position, sprite: "Coin", points: 10) } } diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Floor.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Floor.swift index 51b3f76f..72627b0c 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Floor.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Floor.swift @@ -27,7 +27,7 @@ class Floor: Entity { physicsComponent.restitution = 0.0 physicsComponent.isDynamic = false physicsComponent.categoryBitMask = PhysicsConstants.CollisionCategory.floor - physicsComponent.contactTestMask = PhysicsConstants.CollisionCategory.player + physicsComponent.contactTestMask = PhysicsConstants.ContactMask.floor physicsComponent.collisionBitMask = PhysicsConstants.CollisionMask.floor to.add(entity: self) diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift index 0c0d1b90..ad51eafa 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift @@ -30,7 +30,7 @@ class Player: Entity { let healthComponent = HealthComponent(entityId: self.id, health: GameConstants.InitialHealth.player) let physicsComponent = PhysicsComponent(entityId: self.id, size: PhysicsConstants.Dimensions.player) physicsComponent.categoryBitMask = PhysicsConstants.CollisionCategory.player - physicsComponent.contactTestMask = PhysicsConstants.CollisionCategory.floor + physicsComponent.contactTestMask = PhysicsConstants.ContactMask.player physicsComponent.collisionBitMask = PhysicsConstants.CollisionMask.player physicsComponent.affectedByGravity = true physicsComponent.restitution = 0.0 diff --git a/star-dash/star-dash/GameEngine/GameEngine.swift b/star-dash/star-dash/GameEngine/GameEngine.swift index 496a79b1..17f78df9 100644 --- a/star-dash/star-dash/GameEngine/GameEngine.swift +++ b/star-dash/star-dash/GameEngine/GameEngine.swift @@ -69,6 +69,7 @@ class GameEngine { private func setUpSystems() { systemManager.add(PositionSystem(entityManager, dispatcher: self)) systemManager.add(PhysicsSystem(entityManager, dispatcher: self)) + systemManager.add(ScoreSystem(entityManager, dispatcher: self)) } } diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index 2eb126da..9703de11 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -59,7 +59,7 @@ class ViewController: UIViewController { floor.setUpAndAdd(to: entityManager) let collectible = Collectible.createCoinCollectible( - position: CGPoint(scene.size.width + 50, y: scene.size.height / 2 + 100) + position: CGPoint(x: scene.size.width / 2 + 30, y: scene.size.height / 2 - 100) ) collectible.setUpAndAdd(to: entityManager) } From 8906bdb1c15ce7a14b722e61efb29a35df7c0a26 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 21:38:43 +0800 Subject: [PATCH 04/13] fix: Fix syntax issues --- star-dash/star-dash.xcodeproj/project.pbxproj | 8 ++++++++ star-dash/star-dash/GameEngine/GameEngine.swift | 8 ++++---- star-dash/star-dash/GameEngine/GameInfo.swift | 3 +++ star-dash/star-dash/GameEngine/GameState.swift | 3 --- star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift | 8 ++++++++ .../star-dash/Rendering/MTKRenderer/MTKRenderer.swift | 4 ++-- .../star-dash/Rendering/MTKRenderer/OverlayView.swift | 6 +++--- .../star-dash/Rendering/MTKRenderer/PlayerView.swift | 4 ++-- star-dash/star-dash/Rendering/OverlayInfo.swift | 4 ++-- star-dash/star-dash/ViewController.swift | 6 +++--- 10 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 star-dash/star-dash/GameEngine/GameInfo.swift delete mode 100644 star-dash/star-dash/GameEngine/GameState.swift diff --git a/star-dash/star-dash.xcodeproj/project.pbxproj b/star-dash/star-dash.xcodeproj/project.pbxproj index 6090ff75..74c5b857 100644 --- a/star-dash/star-dash.xcodeproj/project.pbxproj +++ b/star-dash/star-dash.xcodeproj/project.pbxproj @@ -83,6 +83,8 @@ E64361132BA4C2CD003850FD /* PhysicsModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E643610D2BA4C2CC003850FD /* PhysicsModule.swift */; }; E64361142BA4C2CD003850FD /* CreationModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E643610E2BA4C2CC003850FD /* CreationModule.swift */; }; E64361152BA4C2CD003850FD /* GameBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = E643610F2BA4C2CC003850FD /* GameBridge.swift */; }; + E69EE9322BAC6CBB00033AB5 /* GameInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = E69EE9312BAC6CBB00033AB5 /* GameInfo.swift */; }; + E69EE9342BAC6CC300033AB5 /* OverlayInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = E69EE9332BAC6CC300033AB5 /* OverlayInfo.swift */; }; E6A011172BA5F4AD006904D9 /* EntitySyncInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A011162BA5F4AD006904D9 /* EntitySyncInterface.swift */; }; E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A745112BA057040080C1BE /* MTKRenderer.swift */; }; E6A745172BA057040080C1BE /* ControlView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A745122BA057040080C1BE /* ControlView.swift */; }; @@ -208,6 +210,8 @@ E643610D2BA4C2CC003850FD /* PhysicsModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhysicsModule.swift; sourceTree = ""; }; E643610E2BA4C2CC003850FD /* CreationModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CreationModule.swift; sourceTree = ""; }; E643610F2BA4C2CC003850FD /* GameBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameBridge.swift; sourceTree = ""; }; + E69EE9312BAC6CBB00033AB5 /* GameInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameInfo.swift; sourceTree = ""; }; + E69EE9332BAC6CC300033AB5 /* OverlayInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OverlayInfo.swift; sourceTree = ""; }; E6A011162BA5F4AD006904D9 /* EntitySyncInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EntitySyncInterface.swift; sourceTree = ""; }; E6A745112BA057040080C1BE /* MTKRenderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MTKRenderer.swift; sourceTree = ""; }; E6A745122BA057040080C1BE /* ControlView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlView.swift; sourceTree = ""; }; @@ -342,6 +346,7 @@ 4E630F282B9F7EC20008F887 /* Components */, 4E630F252B9F7E500008F887 /* Entities */, 46B8C0992BA328D900498705 /* GameEngine.swift */, + E69EE9312BAC6CBB00033AB5 /* GameInfo.swift */, ); path = GameEngine; sourceTree = ""; @@ -527,6 +532,7 @@ E6A745102BA057040080C1BE /* Rendering */ = { isa = PBXGroup; children = ( + E69EE9332BAC6CC300033AB5 /* OverlayInfo.swift */, E6B5509F2BA15D2000DC7396 /* MTKRenderer */, E6A745132BA057040080C1BE /* Renderer.swift */, E6B0AAD02BAAE3DC009CB939 /* ViewDelegate.swift */, @@ -719,6 +725,7 @@ files = ( 46D418182BA5CD840091A38B /* Player+Collidable.swift in Sources */, 46D4181E2BA5D2620091A38B /* Monster+Collidable.swift in Sources */, + E69EE9342BAC6CC300033AB5 /* OverlayInfo.swift in Sources */, E6B1DC902BA34A4800473563 /* SDPhysicsEngine in Sources */, 4E630F272B9F7E770008F887 /* Entity.swift in Sources */, 4E630F2C2B9F7F460008F887 /* Component.swift in Sources */, @@ -762,6 +769,7 @@ E64361112BA4C2CD003850FD /* SyncModule.swift in Sources */, E6A7451B2BA0C1890080C1BE /* PlayerView.swift in Sources */, 46D418282BA5D6800091A38B /* Floor+Collidable.swift in Sources */, + E69EE9322BAC6CBB00033AB5 /* GameInfo.swift in Sources */, 46D418222BA5D4E60091A38B /* Obstacle+Collidable.swift in Sources */, E638B9CF2BAB3C5D00931CC2 /* TeleportEvent.swift in Sources */, E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */, diff --git a/star-dash/star-dash/GameEngine/GameEngine.swift b/star-dash/star-dash/GameEngine/GameEngine.swift index 17f78df9..71adbf39 100644 --- a/star-dash/star-dash/GameEngine/GameEngine.swift +++ b/star-dash/star-dash/GameEngine/GameEngine.swift @@ -20,15 +20,15 @@ class GameEngine { setUpSystems() } - func gameState() -> GameState? { + func gameInfo() -> GameInfo? { guard let scoreSystem = systemManager.system(ofType: ScoreSystem.self), let playerEntityId = entityManager.playerEntityId(), - let scoreComponent = scoreSystem.getScoreComponent(of: playerEntityId) else { + let score = scoreSystem.score(of: playerEntityId) else { return nil } - return GameState( - playerScore: scoreComponent.score + return GameInfo( + playerScore: score ) } diff --git a/star-dash/star-dash/GameEngine/GameInfo.swift b/star-dash/star-dash/GameEngine/GameInfo.swift new file mode 100644 index 00000000..3ca81506 --- /dev/null +++ b/star-dash/star-dash/GameEngine/GameInfo.swift @@ -0,0 +1,3 @@ +struct GameInfo { + let playerScore: Int +} diff --git a/star-dash/star-dash/GameEngine/GameState.swift b/star-dash/star-dash/GameEngine/GameState.swift deleted file mode 100644 index 1f73dfea..00000000 --- a/star-dash/star-dash/GameEngine/GameState.swift +++ /dev/null @@ -1,3 +0,0 @@ -struct GameState { - let playerScore: Float -} \ No newline at end of file diff --git a/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift b/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift index cc058f12..65c4d94d 100644 --- a/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift +++ b/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift @@ -17,6 +17,14 @@ class ScoreSystem: System { self.entityManager = entityManager self.dispatcher = dispatcher } + + func score(of entityId: EntityId) -> Int? { + guard let scoreComponent = getScoreComponent(of: entityId) else { + return nil + } + + return scoreComponent.score + } func applyScoreChange(to entityId: EntityId, scoreChange: Int) { guard let scoreComponent = getScoreComponent(of: entityId) else { diff --git a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift index e0df2517..7289eafc 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift @@ -35,8 +35,8 @@ class MTKRenderer: NSObject, Renderer { super.init() } - func updateOverlay(gameState: GameState) { - playerView?.updateOverlay(score: gameState.playerScore) + func updateOverlay(overlayInfo: OverlayInfo) { + playerView?.updateOverlay(score: overlayInfo.score) } /// Set ups the views for a single player game. diff --git a/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift b/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift index 47ed588c..2d70eb35 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift @@ -14,7 +14,7 @@ class OverlayView: UIView { scoreLabel.text = "Score: 0" scoreLabel.numberOfLines = 1 scoreLabel.translatesAutoresizingMaskIntoConstraints = false - scoreLabel.textColor = .white + scoreLabel.textColor = .black addSubview(scoreLabel) NSLayoutConstraint.activate([ @@ -24,7 +24,7 @@ class OverlayView: UIView { ]) } - func update(playerScore: Float) { - scoreLabel.text = "Score: \(playerScore)" + func update(score: Int) { + scoreLabel.text = "Score: \(score)" } } diff --git a/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift b/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift index c67d0115..85cbc703 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/PlayerView.swift @@ -36,7 +36,7 @@ class PlayerView { controlView.controlViewDelegate = delegate } - func updateOverlay(playerScore: Float) { - + func updateOverlay(score: Int) { + overlayView.update(score: score) } } diff --git a/star-dash/star-dash/Rendering/OverlayInfo.swift b/star-dash/star-dash/Rendering/OverlayInfo.swift index 1f044b6c..39078791 100644 --- a/star-dash/star-dash/Rendering/OverlayInfo.swift +++ b/star-dash/star-dash/Rendering/OverlayInfo.swift @@ -1,3 +1,3 @@ struct OverlayInfo { - let score: Float -} \ No newline at end of file + let score: Int +} diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index 9703de11..e7bc563c 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -72,12 +72,12 @@ extension ViewController: SDSceneDelegate { gameEngine?.update(by: deltaTime) gameBridge?.syncFromEntities() - guard let gameState = gameEngine?.gameState() else { + guard let gameInfo = gameEngine?.gameInfo() else { return } - renderer.updateOverlay(overlyInfo: OverlayInfo( - score: gameState.playerScore + renderer?.updateOverlay(overlayInfo: OverlayInfo( + score: gameInfo.playerScore )) } From 0b775c35e5b7ce237d1fef24aa8689c84d33f9d1 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 21:39:18 +0800 Subject: [PATCH 05/13] chore: Resolve swiftlint issues --- star-dash/star-dash/Constants/PhysicsConstants.swift | 2 +- star-dash/star-dash/GameEngine/GameEngine.swift | 2 +- star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/star-dash/star-dash/Constants/PhysicsConstants.swift b/star-dash/star-dash/Constants/PhysicsConstants.swift index 395345d1..c8e40f38 100644 --- a/star-dash/star-dash/Constants/PhysicsConstants.swift +++ b/star-dash/star-dash/Constants/PhysicsConstants.swift @@ -29,7 +29,7 @@ struct PhysicsConstants { static let wall = CollisionCategory.player | CollisionCategory.monster | CollisionCategory.tool static let floor = CollisionCategory.player | CollisionCategory.monster | CollisionCategory.tool } - + struct ContactMask { static let player = CollisionCategory.floor | CollisionCategory.collectible static let monster = CollisionCategory.player diff --git a/star-dash/star-dash/GameEngine/GameEngine.swift b/star-dash/star-dash/GameEngine/GameEngine.swift index 71adbf39..54ae9b84 100644 --- a/star-dash/star-dash/GameEngine/GameEngine.swift +++ b/star-dash/star-dash/GameEngine/GameEngine.swift @@ -25,7 +25,7 @@ class GameEngine { let playerEntityId = entityManager.playerEntityId(), let score = scoreSystem.score(of: playerEntityId) else { return nil - } + } return GameInfo( playerScore: score diff --git a/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift b/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift index 65c4d94d..14ed8fc4 100644 --- a/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift +++ b/star-dash/star-dash/GameEngine/Systems/ScoreSystem.swift @@ -17,12 +17,12 @@ class ScoreSystem: System { self.entityManager = entityManager self.dispatcher = dispatcher } - + func score(of entityId: EntityId) -> Int? { guard let scoreComponent = getScoreComponent(of: entityId) else { return nil } - + return scoreComponent.score } From 8afdbe1c41c7c511c841e3604673bb52467c8570 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:10:58 +0800 Subject: [PATCH 06/13] chore: Update collectible size --- .../Entities/GameEntities/Collectible.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift index 7caec26a..1dc8f60c 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift @@ -12,27 +12,29 @@ class Collectible: Entity { private let position: CGPoint private let sprite: String private let points: Int + private let size: CGSize - init(id: EntityId, position: CGPoint, sprite: String, points: Int) { + init(id: EntityId, position: CGPoint, sprite: String, points: Int, size: CGSize) { self.id = id self.position = position self.sprite = sprite self.points = points + self.size = size } - convenience init(position: CGPoint, sprite: String, points: Int) { - self.init(id: UUID(), position: position, sprite: sprite, points: points) + convenience init(position: CGPoint, sprite: String, points: Int, size: CGSize) { + self.init(id: UUID(), position: position, sprite: sprite, points: points, size: size) } func setUpAndAdd(to: EntityManager) { let positionComponent = PositionComponent(entityId: self.id, position: self.position, rotation: .zero) - let physicsComponent = PhysicsComponent(entityId: self.id, size: PhysicsConstants.Dimensions.collectible) + let physicsComponent = PhysicsComponent(entityId: self.id, size: self.size) physicsComponent.affectedByGravity = false physicsComponent.isDynamic = false physicsComponent.categoryBitMask = PhysicsConstants.CollisionCategory.collectible physicsComponent.contactTestMask = PhysicsConstants.ContactMask.collectible physicsComponent.collisionBitMask = PhysicsConstants.CollisionMask.collectible - let spriteComponent = SpriteComponent(entityId: self.id, image: sprite, textureAtlas: nil, size: nil) + let spriteComponent = SpriteComponent(entityId: self.id, image: sprite, textureAtlas: nil, size: size) to.add(entity: self) to.add(component: positionComponent) @@ -41,6 +43,6 @@ class Collectible: Entity { } static func createCoinCollectible(position: CGPoint) -> Collectible { - Collectible(position: position, sprite: "Coin", points: 10) + Collectible(position: position, sprite: "Coin", points: 10, size: CGSize(width: 50, height: 50)) } } From 7535d2b3732d64249cc496af78853b57a7c00d38 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:37:47 +0800 Subject: [PATCH 07/13] chore: Update SDSpriteObject to apply size --- .../star-dash/GameBridge/SyncModule/SpriteModule.swift | 6 +++++- .../GameEngine/Entities/GameEntities/Player.swift | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/star-dash/star-dash/GameBridge/SyncModule/SpriteModule.swift b/star-dash/star-dash/GameBridge/SyncModule/SpriteModule.swift index a08c9705..aa15e7c9 100644 --- a/star-dash/star-dash/GameBridge/SyncModule/SpriteModule.swift +++ b/star-dash/star-dash/GameBridge/SyncModule/SpriteModule.swift @@ -31,7 +31,11 @@ extension SpriteModule: CreationModule { var newObject = SDObject() if let spriteComponent = entityManager.component(ofType: SpriteComponent.self, of: entity.id) { let spriteObject = SDSpriteObject(imageNamed: "PlayerRedNose") - spriteObject.size = CGSize(width: 100, height: 140) + + if let size = spriteComponent.size { + spriteObject.size = size + } + newObject = spriteObject } diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift index ad51eafa..de6c668c 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Player.swift @@ -34,7 +34,12 @@ class Player: Entity { physicsComponent.collisionBitMask = PhysicsConstants.CollisionMask.player physicsComponent.affectedByGravity = true physicsComponent.restitution = 0.0 - let spriteComponent = SpriteComponent(entityId: self.id, image: "", textureAtlas: "", size: .zero) + let spriteComponent = SpriteComponent( + entityId: self.id, + image: "PlayerRedNose", + textureAtlas: "", + size: CGSize(width: 100, height: 140) + ) let scoreComponent = ScoreComponent(entityId: self.id, score: 0) to.add(entity: self) From 6d7ac6ccf9e7c317dfa09ee6ef15aa83cfadf176 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:43:27 +0800 Subject: [PATCH 08/13] chore: Add functionality to remove object from scene --- SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift | 5 +++++ .../Sources/SDPhysicsEngine/Object/SDObject.swift | 4 ++++ SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift | 1 + star-dash/star-dash/GameBridge/GameBridge.swift | 8 ++++++++ 4 files changed, 18 insertions(+) diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift index 77903c85..99848c47 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift @@ -38,6 +38,11 @@ extension GameScene: SDScene { objectMap[object.node] = object addChild(object.node) } + + public func removeObject(_ object: SDObject) { + objectMap[object.node] = nil + object.removeFromParent() + } } extension GameScene: SKPhysicsContactDelegate { diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift index 3549ee9a..fa98e6c7 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift @@ -39,4 +39,8 @@ public class SDObject { public var physicsBody: SDPhysicsBody? { willSet { node.physicsBody = newValue?.body } } + + func removeFromParent() { + node.removeFromParent() + } } diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift index 79f0fe83..c35a3142 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/SDScene.swift @@ -5,4 +5,5 @@ public protocol SDScene { var size: CGSize { get } func addObject(_ object: SDObject) + func removeObject(_ object: SDObject) } diff --git a/star-dash/star-dash/GameBridge/GameBridge.swift b/star-dash/star-dash/GameBridge/GameBridge.swift index a71662ba..9a66db72 100644 --- a/star-dash/star-dash/GameBridge/GameBridge.swift +++ b/star-dash/star-dash/GameBridge/GameBridge.swift @@ -88,5 +88,13 @@ class GameBridge { } private func removeObject(from entityId: EntityId) { + guard let object = entitiesMap[entityId] else { + return + } + + entitiesMap[entityId] = nil + objectsMap[object.id] = nil + + self.scene.removeObject(object) } } From 1f17f26661e86bf40b3e20ef6f40f82015524ebf Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:55:10 +0800 Subject: [PATCH 09/13] chore: Resolve swiftlint issues --- SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift | 2 +- SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift index 99848c47..68a4ebcc 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() diff --git a/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift b/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift index fa98e6c7..f66db885 100644 --- a/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift +++ b/SDPhysicsEngine/Sources/SDPhysicsEngine/Object/SDObject.swift @@ -39,7 +39,7 @@ public class SDObject { public var physicsBody: SDPhysicsBody? { willSet { node.physicsBody = newValue?.body } } - + func removeFromParent() { node.removeFromParent() } From 1ffa45eb48771404e5285485248e0a7cc5c5c26f Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 23:12:05 +0800 Subject: [PATCH 10/13] chore: Refactor to reduce duplicate strings --- star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift b/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift index 2d70eb35..dd49363e 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/OverlayView.swift @@ -11,7 +11,6 @@ class OverlayView: UIView { let scoreLabel = UILabel() func setupSubviews() { - scoreLabel.text = "Score: 0" scoreLabel.numberOfLines = 1 scoreLabel.translatesAutoresizingMaskIntoConstraints = false scoreLabel.textColor = .black @@ -22,6 +21,8 @@ class OverlayView: UIView { scoreLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -1 * margin), scoreLabel.leadingAnchor.constraint(greaterThanOrEqualTo: self.leadingAnchor, constant: margin) ]) + + update(score: 0) } func update(score: Int) { From 61766a9081dee0c3f27d4e907c756f59d6f58408 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:08:46 +0800 Subject: [PATCH 11/13] fix: Fix hardcoded points --- .../PlayerEvents/PickupCollectibleEvent.swift | 5 +++-- .../GameEngine/Components/PointsComponent.swift | 14 ++++++++++++++ .../Entities/GameEntities/Collectible.swift | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 star-dash/star-dash/GameEngine/Components/PointsComponent.swift diff --git a/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift b/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift index a0ab9821..28ed89f4 100644 --- a/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift +++ b/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift @@ -20,10 +20,11 @@ class PickupCollectibleEvent: Event { } func execute(on target: EventModifiable) { - guard let scoreSystem = target.system(ofType: ScoreSystem.self) else { + guard let scoreSystem = target.system(ofType: ScoreSystem.self), + let pointsComponent = target.component(ofType: PointsComponent.self, ofEntity: entityId) else { return } - scoreSystem.applyScoreChange(to: entityId, scoreChange: GameConstants.ScoreChange.pickupCollectible) + scoreSystem.applyScoreChange(to: entityId, scoreChange: pointsComponent.points) target.add(event: RemoveEvent(on: collectibleEntityId)) } } diff --git a/star-dash/star-dash/GameEngine/Components/PointsComponent.swift b/star-dash/star-dash/GameEngine/Components/PointsComponent.swift new file mode 100644 index 00000000..752e9ebf --- /dev/null +++ b/star-dash/star-dash/GameEngine/Components/PointsComponent.swift @@ -0,0 +1,14 @@ +import Foundation + +class PointsComponent: Component { + let points: Int + + init(id: UUID, entityId: UUID, points: Int) { + self.points = points + super.init(id: id, entityId: entityId) + } + + convenience init(entityId: UUID, points: Int) { + self.init(id: UUID(), entityId: entityId, points: points) + } +} diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift index 1dc8f60c..1cd06109 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift @@ -35,11 +35,13 @@ class Collectible: Entity { physicsComponent.contactTestMask = PhysicsConstants.ContactMask.collectible physicsComponent.collisionBitMask = PhysicsConstants.CollisionMask.collectible let spriteComponent = SpriteComponent(entityId: self.id, image: sprite, textureAtlas: nil, size: size) + let pointsComponent = PointsComponent(entityId: self.id, points: points) to.add(entity: self) to.add(component: positionComponent) to.add(component: physicsComponent) to.add(component: spriteComponent) + to.add(component: pointsComponent) } static func createCoinCollectible(position: CGPoint) -> Collectible { From 5b6c73fb04361ad14f8c010f71b772929d54c03c Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:11:16 +0800 Subject: [PATCH 12/13] chore: Move collectible init values to a constants file --- star-dash/star-dash/Constants/EntityConstants.swift | 7 +++++++ .../GameEngine/Entities/GameEntities/Collectible.swift | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 star-dash/star-dash/Constants/EntityConstants.swift diff --git a/star-dash/star-dash/Constants/EntityConstants.swift b/star-dash/star-dash/Constants/EntityConstants.swift new file mode 100644 index 00000000..54e43455 --- /dev/null +++ b/star-dash/star-dash/Constants/EntityConstants.swift @@ -0,0 +1,7 @@ +struct EntityConstants { + struct CoinCollectible { + let points = 10 + let sprite = "Coin" + let size = CGSize(width: 50, height: 50) + } +} diff --git a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift index 1cd06109..f7d5d59c 100644 --- a/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift +++ b/star-dash/star-dash/GameEngine/Entities/GameEntities/Collectible.swift @@ -45,6 +45,11 @@ class Collectible: Entity { } static func createCoinCollectible(position: CGPoint) -> Collectible { - Collectible(position: position, sprite: "Coin", points: 10, size: CGSize(width: 50, height: 50)) + Collectible( + position: position, + sprite: EntityConstants.CoinCollectible.sprite, + points: EntityConstants.CoinCollectible.points, + size: EntityConstants.CoinCollectible.size + ) } } From 6c9ae2c684a4434c9532af0795921cf7374ca9fd Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:21:34 +0800 Subject: [PATCH 13/13] fix: Fix syntax issues --- star-dash/star-dash.xcodeproj/project.pbxproj | 8 ++++++++ star-dash/star-dash/Constants/EntityConstants.swift | 8 +++++--- .../Events/PlayerEvents/PickupCollectibleEvent.swift | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/star-dash/star-dash.xcodeproj/project.pbxproj b/star-dash/star-dash.xcodeproj/project.pbxproj index 74c5b857..d4e1f390 100644 --- a/star-dash/star-dash.xcodeproj/project.pbxproj +++ b/star-dash/star-dash.xcodeproj/project.pbxproj @@ -85,6 +85,8 @@ E64361152BA4C2CD003850FD /* GameBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = E643610F2BA4C2CC003850FD /* GameBridge.swift */; }; E69EE9322BAC6CBB00033AB5 /* GameInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = E69EE9312BAC6CBB00033AB5 /* GameInfo.swift */; }; E69EE9342BAC6CC300033AB5 /* OverlayInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = E69EE9332BAC6CC300033AB5 /* OverlayInfo.swift */; }; + E69FDDE02BAD3DAD0089D5F3 /* PointsComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E69FDDDF2BAD3DAD0089D5F3 /* PointsComponent.swift */; }; + E69FDDE22BAD3DC40089D5F3 /* EntityConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = E69FDDE12BAD3DC40089D5F3 /* EntityConstants.swift */; }; E6A011172BA5F4AD006904D9 /* EntitySyncInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A011162BA5F4AD006904D9 /* EntitySyncInterface.swift */; }; E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A745112BA057040080C1BE /* MTKRenderer.swift */; }; E6A745172BA057040080C1BE /* ControlView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A745122BA057040080C1BE /* ControlView.swift */; }; @@ -212,6 +214,8 @@ E643610F2BA4C2CC003850FD /* GameBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameBridge.swift; sourceTree = ""; }; E69EE9312BAC6CBB00033AB5 /* GameInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameInfo.swift; sourceTree = ""; }; E69EE9332BAC6CC300033AB5 /* OverlayInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OverlayInfo.swift; sourceTree = ""; }; + E69FDDDF2BAD3DAD0089D5F3 /* PointsComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PointsComponent.swift; sourceTree = ""; }; + E69FDDE12BAD3DC40089D5F3 /* EntityConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EntityConstants.swift; sourceTree = ""; }; E6A011162BA5F4AD006904D9 /* EntitySyncInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EntitySyncInterface.swift; sourceTree = ""; }; E6A745112BA057040080C1BE /* MTKRenderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MTKRenderer.swift; sourceTree = ""; }; E6A745122BA057040080C1BE /* ControlView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlView.swift; sourceTree = ""; }; @@ -464,6 +468,7 @@ 4E630F282B9F7EC20008F887 /* Components */ = { isa = PBXGroup; children = ( + E69FDDDF2BAD3DAD0089D5F3 /* PointsComponent.swift */, E6B34A462BAA03AF0009A60B /* PlayerComponent.swift */, 4E630F292B9F7EF60008F887 /* PositionComponent.swift */, 14970F4F2BA814D500CC1E8A /* ScoreComponent.swift */, @@ -487,6 +492,7 @@ 4E86605D2BA095CC0035530D /* Constants */ = { isa = PBXGroup; children = ( + E69FDDE12BAD3DC40089D5F3 /* EntityConstants.swift */, 4E8660652BA097D40035530D /* PhysicsConstants.swift */, 14970F552BA8177B00CC1E8A /* GameConstants.swift */, ); @@ -765,6 +771,7 @@ 4604BBD92BA81C940078B84C /* InventorySystem.swift in Sources */, 14E247952BA2CB480071FFC0 /* EventManager.swift in Sources */, 4E630EF82B9F7E070008F887 /* SceneDelegate.swift in Sources */, + E69FDDE02BAD3DAD0089D5F3 /* PointsComponent.swift in Sources */, 14970F542BA8163300CC1E8A /* ScoreSystem.swift in Sources */, E64361112BA4C2CD003850FD /* SyncModule.swift in Sources */, E6A7451B2BA0C1890080C1BE /* PlayerView.swift in Sources */, @@ -786,6 +793,7 @@ E6A7451D2BA0CAD90080C1BE /* JoystickView.swift in Sources */, E6B550A12BA15E9C00DC7396 /* OverlayView.swift in Sources */, 46D418262BA5D6500091A38B /* Wall+Collidable.swift in Sources */, + E69FDDE22BAD3DC40089D5F3 /* EntityConstants.swift in Sources */, 4E630F342B9F8FC00008F887 /* Player.swift in Sources */, 4E630F372B9F91DE0008F887 /* PlayerSprite.swift in Sources */, 14970F502BA814D500CC1E8A /* ScoreComponent.swift in Sources */, diff --git a/star-dash/star-dash/Constants/EntityConstants.swift b/star-dash/star-dash/Constants/EntityConstants.swift index 54e43455..46f593fd 100644 --- a/star-dash/star-dash/Constants/EntityConstants.swift +++ b/star-dash/star-dash/Constants/EntityConstants.swift @@ -1,7 +1,9 @@ +import CoreGraphics + struct EntityConstants { struct CoinCollectible { - let points = 10 - let sprite = "Coin" - let size = CGSize(width: 50, height: 50) + static let points = 10 + static let sprite = "Coin" + static let size = CGSize(width: 50, height: 50) } } diff --git a/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift b/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift index 28ed89f4..b4a94aad 100644 --- a/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift +++ b/star-dash/star-dash/Events/PlayerEvents/PickupCollectibleEvent.swift @@ -21,7 +21,7 @@ class PickupCollectibleEvent: Event { func execute(on target: EventModifiable) { guard let scoreSystem = target.system(ofType: ScoreSystem.self), - let pointsComponent = target.component(ofType: PointsComponent.self, ofEntity: entityId) else { + let pointsComponent = target.component(ofType: PointsComponent.self, ofEntity: collectibleEntityId) else { return } scoreSystem.applyScoreChange(to: entityId, scoreChange: pointsComponent.points)