From bde976e28cf56bd09ca5478928d197607f0e743c Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Wed, 20 Mar 2024 23:09:03 +0800 Subject: [PATCH 1/2] wip: Player movement --- star-dash/star-dash/Events/CommonEvents/JumpEvent.swift | 5 +++-- .../Events/ContactEvents/PlayerFloorContactEvent.swift | 3 ++- .../GameEngine/Components/PlayerComponent.swift | 3 ++- star-dash/star-dash/GameEngine/GameEngine.swift | 9 ++++++++- star-dash/star-dash/ViewController.swift | 2 +- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/star-dash/star-dash/Events/CommonEvents/JumpEvent.swift b/star-dash/star-dash/Events/CommonEvents/JumpEvent.swift index 939ac08e..49a35192 100644 --- a/star-dash/star-dash/Events/CommonEvents/JumpEvent.swift +++ b/star-dash/star-dash/Events/CommonEvents/JumpEvent.swift @@ -25,10 +25,11 @@ class JumpEvent: Event { } guard let playerComponent = target.component(ofType: PlayerComponent.self, ofEntity: entityId), - !playerComponent.isJumping else { + playerComponent.canJump else { return } - playerComponent.isJumping = true + playerComponent.canJump = false + playerComponent.canMove = false physicsSystem.applyImpulse(to: entityId, impulse: jumpImpulse) } diff --git a/star-dash/star-dash/Events/ContactEvents/PlayerFloorContactEvent.swift b/star-dash/star-dash/Events/ContactEvents/PlayerFloorContactEvent.swift index 13c2c74d..2557b83d 100644 --- a/star-dash/star-dash/Events/ContactEvents/PlayerFloorContactEvent.swift +++ b/star-dash/star-dash/Events/ContactEvents/PlayerFloorContactEvent.swift @@ -19,6 +19,7 @@ class PlayerFloorContactEvent: Event { return } - playerComponent.isJumping = false + playerComponent.canJump = true + playerComponent.canMove = true } } diff --git a/star-dash/star-dash/GameEngine/Components/PlayerComponent.swift b/star-dash/star-dash/GameEngine/Components/PlayerComponent.swift index f9931697..7dd6fef4 100644 --- a/star-dash/star-dash/GameEngine/Components/PlayerComponent.swift +++ b/star-dash/star-dash/GameEngine/Components/PlayerComponent.swift @@ -3,7 +3,8 @@ import Foundation class PlayerComponent: Component { let playerIndex: Int - var isJumping = false + var canJump = true + var canMove = true init(id: ComponentId, entityId: EntityId, playerIndex: Int) { self.playerIndex = playerIndex diff --git a/star-dash/star-dash/GameEngine/GameEngine.swift b/star-dash/star-dash/GameEngine/GameEngine.swift index abff4362..31873960 100644 --- a/star-dash/star-dash/GameEngine/GameEngine.swift +++ b/star-dash/star-dash/GameEngine/GameEngine.swift @@ -43,8 +43,15 @@ class GameEngine { eventManager.add(event: JumpEvent(on: playerEntityId, by: PhysicsConstants.jumpImpulse)) } - func handlePlayerMove() { + func handlePlayerMove(isLeft: Bool) { + guard let playerEntityId = entityManager.playerEntityId(), + let physicsComponent = entityManager.component(ofType: PhysicsComponent.self, of: playerEntityId), + let playerComponent = entityManager.component(ofType: PlayerComponent.self, of: playerEntityId), + playerComponent.canMove else { + return + } + physicsComponent.velocity = CGVector(dx: 50, dy: 0) } private func setUpSystems() { diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index f9627bf5..5b36aa23 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -81,7 +81,7 @@ extension ViewController: SDSceneDelegate { extension ViewController: ViewDelegate { func joystickMoved(isLeft: Bool) { - + gameEngine?.handlePlayerMove(isLeft: isLeft) } func jumpButtonPressed() { From a00b9ef9d8e1aabc56b0c365801b7170ea89d2f4 Mon Sep 17 00:00:00 2001 From: Goh Jun Yi <54541329+Junyi00@users.noreply.github.com> Date: Thu, 21 Mar 2024 00:08:11 +0800 Subject: [PATCH 2/2] feat: Simple movement logic --- star-dash/star-dash.xcodeproj/project.pbxproj | 12 ++++++---- .../Constants/PhysicsConstants.swift | 3 ++- .../Events/CommonEvents/MoveEvent.swift | 11 +++++----- .../Events/CommonEvents/TeleportEvent.swift | 22 +++++++++++++++++++ .../Events/PlayerEvents/RespawnEvent.swift | 2 +- .../star-dash/GameEngine/GameEngine.swift | 4 ++-- .../Rendering/ControlViewDelegate.swift | 2 +- .../Rendering/MTKRenderer/ControlView.swift | 4 ++-- .../Rendering/MTKRenderer/MTKRenderer.swift | 4 ++-- .../star-dash/Rendering/ViewDelegate.swift | 2 +- star-dash/star-dash/ViewController.swift | 4 ++-- 11 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 star-dash/star-dash/Events/CommonEvents/TeleportEvent.swift diff --git a/star-dash/star-dash.xcodeproj/project.pbxproj b/star-dash/star-dash.xcodeproj/project.pbxproj index 55db5cb2..6090ff75 100644 --- a/star-dash/star-dash.xcodeproj/project.pbxproj +++ b/star-dash/star-dash.xcodeproj/project.pbxproj @@ -76,6 +76,7 @@ 4E8660622BA0964A0035530D /* Obstacle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E8660612BA0964A0035530D /* Obstacle.swift */; }; 4E8660642BA096600035530D /* Tool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E8660632BA096600035530D /* Tool.swift */; }; 4E8660662BA097D40035530D /* PhysicsConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E8660652BA097D40035530D /* PhysicsConstants.swift */; }; + E638B9CF2BAB3C5D00931CC2 /* TeleportEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E638B9CE2BAB3C5D00931CC2 /* TeleportEvent.swift */; }; E64361102BA4C2CD003850FD /* SpriteModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E643610A2BA4C2CC003850FD /* SpriteModule.swift */; }; E64361112BA4C2CD003850FD /* SyncModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E643610B2BA4C2CC003850FD /* SyncModule.swift */; }; E64361122BA4C2CD003850FD /* ObjectModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E643610C2BA4C2CC003850FD /* ObjectModule.swift */; }; @@ -200,6 +201,7 @@ 4E8660612BA0964A0035530D /* Obstacle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Obstacle.swift; sourceTree = ""; }; 4E8660632BA096600035530D /* Tool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tool.swift; sourceTree = ""; }; 4E8660652BA097D40035530D /* PhysicsConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhysicsConstants.swift; sourceTree = ""; }; + E638B9CE2BAB3C5D00931CC2 /* TeleportEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeleportEvent.swift; sourceTree = ""; }; E643610A2BA4C2CC003850FD /* SpriteModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpriteModule.swift; sourceTree = ""; }; E643610B2BA4C2CC003850FD /* SyncModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncModule.swift; sourceTree = ""; }; E643610C2BA4C2CC003850FD /* ObjectModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectModule.swift; sourceTree = ""; }; @@ -313,6 +315,7 @@ 14E2478D2BA22FCE0071FFC0 /* MoveEvent.swift */, 143AA38B2BA4D3E3009C28E7 /* JumpEvent.swift */, 14D14B722BA5A3CD00386C3B /* RemoveEvent.swift */, + E638B9CE2BAB3C5D00931CC2 /* TeleportEvent.swift */, ); path = CommonEvents; sourceTree = ""; @@ -760,6 +763,7 @@ E6A7451B2BA0C1890080C1BE /* PlayerView.swift in Sources */, 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 */, @@ -980,7 +984,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = VFS4882B2L; + DEVELOPMENT_TEAM = EBN5NJJVSU; ENABLE_USER_SCRIPT_SANDBOXING = NO; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "star-dash/Info.plist"; @@ -995,7 +999,7 @@ "@executable_path/Frameworks", ); MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = "junyi-star-dash"; + PRODUCT_BUNDLE_IDENTIFIER = "junyi-sd"; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; @@ -1014,7 +1018,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = VFS4882B2L; + DEVELOPMENT_TEAM = EBN5NJJVSU; ENABLE_USER_SCRIPT_SANDBOXING = NO; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "star-dash/Info.plist"; @@ -1029,7 +1033,7 @@ "@executable_path/Frameworks", ); MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = "junyi-star-dash"; + PRODUCT_BUNDLE_IDENTIFIER = "junyi-sd"; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; diff --git a/star-dash/star-dash/Constants/PhysicsConstants.swift b/star-dash/star-dash/Constants/PhysicsConstants.swift index 143f7f0a..7a7e0dc4 100644 --- a/star-dash/star-dash/Constants/PhysicsConstants.swift +++ b/star-dash/star-dash/Constants/PhysicsConstants.swift @@ -38,8 +38,9 @@ struct PhysicsConstants { static let obstacle = CGSize(width: 60, height: 60) static let tool = CGSize(width: 60, height: 60) static let wall = CGSize(width: 60, height: 60) - static let floor = CGSize(width: 60, height: 60) + static let floor = CGSize(width: 300, height: 60) } static let jumpImpulse = CGVector(dx: 0, dy: 80) + static let runVelocity = CGVector(dx: 50, dy: 0) } diff --git a/star-dash/star-dash/Events/CommonEvents/MoveEvent.swift b/star-dash/star-dash/Events/CommonEvents/MoveEvent.swift index 5396a804..433a13af 100644 --- a/star-dash/star-dash/Events/CommonEvents/MoveEvent.swift +++ b/star-dash/star-dash/Events/CommonEvents/MoveEvent.swift @@ -11,18 +11,19 @@ class MoveEvent: Event { let timestamp: Date let entityId: EntityId - let destination: CGPoint + let toLeft: Bool - init(on entityId: EntityId, to destination: CGPoint) { + init(on entityId: EntityId, toLeft: Bool) { timestamp = Date.now self.entityId = entityId - self.destination = destination + self.toLeft = toLeft } func execute(on target: EventModifiable) { - guard let positionSystem = target.system(ofType: PositionSystem.self) else { + guard let physicsComponent = target.component(ofType: PhysicsComponent.self, ofEntity: entityId) else { return } - positionSystem.move(entityId: entityId, to: destination) + + physicsComponent.velocity = (toLeft ? -1 : 1) * PhysicsConstants.runVelocity } } diff --git a/star-dash/star-dash/Events/CommonEvents/TeleportEvent.swift b/star-dash/star-dash/Events/CommonEvents/TeleportEvent.swift new file mode 100644 index 00000000..622e21e3 --- /dev/null +++ b/star-dash/star-dash/Events/CommonEvents/TeleportEvent.swift @@ -0,0 +1,22 @@ +import Foundation + +class TeleportEvent: Event { + let timestamp: Date + let entityId: EntityId + + let destination: CGPoint + + init(on entityId: EntityId, to destination: CGPoint) { + timestamp = Date.now + self.entityId = entityId + self.destination = destination + } + + func execute(on target: EventModifiable) { + guard let positionSystem = target.system(ofType: PositionSystem.self) else { + return + } + + positionSystem.move(entityId: entityId, to: destination) + } +} diff --git a/star-dash/star-dash/Events/PlayerEvents/RespawnEvent.swift b/star-dash/star-dash/Events/PlayerEvents/RespawnEvent.swift index 929f26d8..4d819487 100644 --- a/star-dash/star-dash/Events/PlayerEvents/RespawnEvent.swift +++ b/star-dash/star-dash/Events/PlayerEvents/RespawnEvent.swift @@ -20,6 +20,6 @@ class RespawnEvent: Event { } func execute(on target: EventModifiable) { - target.add(event: MoveEvent(on: entityId, to: newPosition)) + target.add(event: TeleportEvent(on: entityId, to: newPosition)) } } diff --git a/star-dash/star-dash/GameEngine/GameEngine.swift b/star-dash/star-dash/GameEngine/GameEngine.swift index 31873960..207872fc 100644 --- a/star-dash/star-dash/GameEngine/GameEngine.swift +++ b/star-dash/star-dash/GameEngine/GameEngine.swift @@ -43,7 +43,7 @@ class GameEngine { eventManager.add(event: JumpEvent(on: playerEntityId, by: PhysicsConstants.jumpImpulse)) } - func handlePlayerMove(isLeft: Bool) { + func handlePlayerMove(toLeft: Bool) { guard let playerEntityId = entityManager.playerEntityId(), let physicsComponent = entityManager.component(ofType: PhysicsComponent.self, of: playerEntityId), let playerComponent = entityManager.component(ofType: PlayerComponent.self, of: playerEntityId), @@ -51,7 +51,7 @@ class GameEngine { return } - physicsComponent.velocity = CGVector(dx: 50, dy: 0) + eventManager.add(event: MoveEvent(on: playerEntityId, toLeft: toLeft)) } private func setUpSystems() { diff --git a/star-dash/star-dash/Rendering/ControlViewDelegate.swift b/star-dash/star-dash/Rendering/ControlViewDelegate.swift index 97fcac29..a61d330f 100644 --- a/star-dash/star-dash/Rendering/ControlViewDelegate.swift +++ b/star-dash/star-dash/Rendering/ControlViewDelegate.swift @@ -1,5 +1,5 @@ protocol ControlViewDelegate: AnyObject { - func joystickMoved(isLeft: Bool) + func joystickMoved(toLeft: Bool) func jumpButtonPressed() } diff --git a/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift b/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift index b9753125..2850aa52 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift @@ -98,12 +98,12 @@ class ControlView: UIView { if shouldSendMoveEvent(location: location) { let isLeft = gesture.location(in: joystickView).x < joystickView.center.x - controlViewDelegate?.joystickMoved(isLeft: isLeft) + controlViewDelegate?.joystickMoved(toLeft: isLeft) } } } private func shouldSendMoveEvent(location: CGPoint) -> Bool { - false + true } } diff --git a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift index 70c0ce99..dbd7042b 100644 --- a/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift +++ b/star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift @@ -70,8 +70,8 @@ extension MTKRenderer: MTKViewDelegate { } extension MTKRenderer: ControlViewDelegate { - func joystickMoved(isLeft: Bool) { - viewDelegate?.joystickMoved(isLeft: isLeft) + func joystickMoved(toLeft: Bool) { + viewDelegate?.joystickMoved(toLeft: toLeft) } func jumpButtonPressed() { diff --git a/star-dash/star-dash/Rendering/ViewDelegate.swift b/star-dash/star-dash/Rendering/ViewDelegate.swift index 7b69b09d..ffe4d356 100644 --- a/star-dash/star-dash/Rendering/ViewDelegate.swift +++ b/star-dash/star-dash/Rendering/ViewDelegate.swift @@ -1,5 +1,5 @@ protocol ViewDelegate: AnyObject { - func joystickMoved(isLeft: Bool) + func joystickMoved(toLeft: Bool) func jumpButtonPressed() } diff --git a/star-dash/star-dash/ViewController.swift b/star-dash/star-dash/ViewController.swift index 5b36aa23..cb24472b 100644 --- a/star-dash/star-dash/ViewController.swift +++ b/star-dash/star-dash/ViewController.swift @@ -80,8 +80,8 @@ extension ViewController: SDSceneDelegate { extension ViewController: ViewDelegate { - func joystickMoved(isLeft: Bool) { - gameEngine?.handlePlayerMove(isLeft: isLeft) + func joystickMoved(toLeft: Bool) { + gameEngine?.handlePlayerMove(toLeft: toLeft) } func jumpButtonPressed() {