Skip to content

Commit

Permalink
Merge pull request #16 from cs3217-2324/feat/player-move
Browse files Browse the repository at this point in the history
Feat/player move
  • Loading branch information
jasonqiu212 committed Mar 21, 2024
2 parents 483ea81 + a00b9ef commit fb52cc4
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 24 deletions.
12 changes: 8 additions & 4 deletions star-dash/star-dash.xcodeproj/project.pbxproj
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -200,6 +201,7 @@
4E8660612BA0964A0035530D /* Obstacle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Obstacle.swift; sourceTree = "<group>"; };
4E8660632BA096600035530D /* Tool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tool.swift; sourceTree = "<group>"; };
4E8660652BA097D40035530D /* PhysicsConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhysicsConstants.swift; sourceTree = "<group>"; };
E638B9CE2BAB3C5D00931CC2 /* TeleportEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeleportEvent.swift; sourceTree = "<group>"; };
E643610A2BA4C2CC003850FD /* SpriteModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpriteModule.swift; sourceTree = "<group>"; };
E643610B2BA4C2CC003850FD /* SyncModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SyncModule.swift; sourceTree = "<group>"; };
E643610C2BA4C2CC003850FD /* ObjectModule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectModule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -313,6 +315,7 @@
14E2478D2BA22FCE0071FFC0 /* MoveEvent.swift */,
143AA38B2BA4D3E3009C28E7 /* JumpEvent.swift */,
14D14B722BA5A3CD00386C3B /* RemoveEvent.swift */,
E638B9CE2BAB3C5D00931CC2 /* TeleportEvent.swift */,
);
path = CommonEvents;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion star-dash/star-dash/Constants/PhysicsConstants.swift
Expand Up @@ -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)
}
5 changes: 3 additions & 2 deletions star-dash/star-dash/Events/CommonEvents/JumpEvent.swift
Expand Up @@ -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)
}
Expand Down
11 changes: 6 additions & 5 deletions star-dash/star-dash/Events/CommonEvents/MoveEvent.swift
Expand Up @@ -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
}
}
22 changes: 22 additions & 0 deletions 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)
}
}
Expand Up @@ -19,6 +19,7 @@ class PlayerFloorContactEvent: Event {
return
}

playerComponent.isJumping = false
playerComponent.canJump = true
playerComponent.canMove = true
}
}
2 changes: 1 addition & 1 deletion star-dash/star-dash/Events/PlayerEvents/RespawnEvent.swift
Expand Up @@ -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))
}
}
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion star-dash/star-dash/GameEngine/GameEngine.swift
Expand Up @@ -43,8 +43,15 @@ class GameEngine {
eventManager.add(event: JumpEvent(on: playerEntityId, by: PhysicsConstants.jumpImpulse))
}

func handlePlayerMove() {
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),
playerComponent.canMove else {
return
}

eventManager.add(event: MoveEvent(on: playerEntityId, toLeft: toLeft))
}

private func setUpSystems() {
Expand Down
2 changes: 1 addition & 1 deletion star-dash/star-dash/Rendering/ControlViewDelegate.swift
@@ -1,5 +1,5 @@
protocol ControlViewDelegate: AnyObject {

func joystickMoved(isLeft: Bool)
func joystickMoved(toLeft: Bool)
func jumpButtonPressed()
}
4 changes: 2 additions & 2 deletions star-dash/star-dash/Rendering/MTKRenderer/ControlView.swift
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions star-dash/star-dash/Rendering/MTKRenderer/MTKRenderer.swift
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion star-dash/star-dash/Rendering/ViewDelegate.swift
@@ -1,5 +1,5 @@
protocol ViewDelegate: AnyObject {

func joystickMoved(isLeft: Bool)
func joystickMoved(toLeft: Bool)
func jumpButtonPressed()
}
4 changes: 2 additions & 2 deletions star-dash/star-dash/ViewController.swift
Expand Up @@ -80,8 +80,8 @@ extension ViewController: SDSceneDelegate {

extension ViewController: ViewDelegate {

func joystickMoved(isLeft: Bool) {

func joystickMoved(toLeft: Bool) {
gameEngine?.handlePlayerMove(toLeft: toLeft)
}

func jumpButtonPressed() {
Expand Down

0 comments on commit fb52cc4

Please sign in to comment.