Skip to content

Commit

Permalink
Merge pull request #15 from cs3217-2324/feat/player-interactions
Browse files Browse the repository at this point in the history
feat: Player Jump
  • Loading branch information
Junyi00 committed Mar 20, 2024
2 parents 9c94e10 + 6866eae commit 483ea81
Show file tree
Hide file tree
Showing 31 changed files with 278 additions and 121 deletions.
3 changes: 2 additions & 1 deletion SDPhysicsEngine/Sources/SDPhysicsEngine/GameScene.swift
Expand Up @@ -54,7 +54,8 @@ extension GameScene: SKPhysicsContactDelegate {

sceneDelegate?.contactOccured(
objectA: objectA,
objectB: objectB
objectB: objectB,
contactPoint: contact.contactPoint
)
}
}
Expand Up @@ -27,6 +27,11 @@ public class SDPhysicsBody {
CGVector(dx: 0, dy: 0)
}

public var restitution: CGFloat {
get { body.restitution }
set { body.restitution = newValue }
}

public var affectedByGravity: Bool {
get { body.affectedByGravity }
set { body.affectedByGravity = newValue }
Expand All @@ -36,17 +41,17 @@ public class SDPhysicsBody {
get { body.isDynamic }
set { body.isDynamic = newValue }
}

public var categoryBitMask: UInt32 {
get { body.categoryBitMask }
set { body.categoryBitMask = newValue }
}

public var contactTestMask: UInt32 {
get { body.contactTestBitMask }
set { body.contactTestBitMask = newValue }
}

public var collisionBitMask: UInt32 {
get { body.collisionBitMask }
set { body.collisionBitMask = newValue }
Expand Down
@@ -1,5 +1,7 @@
import CoreGraphics

public protocol SDSceneDelegate: AnyObject {

func update(_ scene: SDScene, deltaTime: Double)
func contactOccured(objectA: SDObject, objectB: SDObject)
func contactOccured(objectA: SDObject, objectB: SDObject, contactPoint: CGPoint)
}
16 changes: 16 additions & 0 deletions star-dash/star-dash.xcodeproj/project.pbxproj
Expand Up @@ -88,7 +88,11 @@
E6A745182BA057040080C1BE /* Renderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A745132BA057040080C1BE /* Renderer.swift */; };
E6A7451B2BA0C1890080C1BE /* PlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A7451A2BA0C1890080C1BE /* PlayerView.swift */; };
E6A7451D2BA0CAD90080C1BE /* JoystickView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6A7451C2BA0CAD90080C1BE /* JoystickView.swift */; };
E6B0AAD12BAAE3DC009CB939 /* ViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B0AAD02BAAE3DC009CB939 /* ViewDelegate.swift */; };
E6B0AAD32BAAE438009CB939 /* ControlViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B0AAD22BAAE438009CB939 /* ControlViewDelegate.swift */; };
E6B0AAD52BAAE4EC009CB939 /* PlayerFloorContactEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B0AAD42BAAE4EC009CB939 /* PlayerFloorContactEvent.swift */; };
E6B1DC902BA34A4800473563 /* SDPhysicsEngine in Sources */ = {isa = PBXBuildFile; fileRef = E6B1DC8D2BA32BC700473563 /* SDPhysicsEngine */; };
E6B34A472BAA03AF0009A60B /* PlayerComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B34A462BAA03AF0009A60B /* PlayerComponent.swift */; };
E6B550A12BA15E9C00DC7396 /* OverlayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B550A02BA15E9C00DC7396 /* OverlayView.swift */; };
E6E4B8302BA6C8D4005ABAB1 /* SDPhysicsEngine in Frameworks */ = {isa = PBXBuildFile; productRef = E6E4B82F2BA6C8D4005ABAB1 /* SDPhysicsEngine */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -208,7 +212,11 @@
E6A745132BA057040080C1BE /* Renderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Renderer.swift; sourceTree = "<group>"; };
E6A7451A2BA0C1890080C1BE /* PlayerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlayerView.swift; sourceTree = "<group>"; };
E6A7451C2BA0CAD90080C1BE /* JoystickView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoystickView.swift; sourceTree = "<group>"; };
E6B0AAD02BAAE3DC009CB939 /* ViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewDelegate.swift; sourceTree = "<group>"; };
E6B0AAD22BAAE438009CB939 /* ControlViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ControlViewDelegate.swift; sourceTree = "<group>"; };
E6B0AAD42BAAE4EC009CB939 /* PlayerFloorContactEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlayerFloorContactEvent.swift; sourceTree = "<group>"; };
E6B1DC8D2BA32BC700473563 /* SDPhysicsEngine */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = SDPhysicsEngine; path = ../SDPhysicsEngine; sourceTree = "<group>"; };
E6B34A462BAA03AF0009A60B /* PlayerComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlayerComponent.swift; sourceTree = "<group>"; };
E6B550A02BA15E9C00DC7396 /* OverlayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverlayView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -283,6 +291,7 @@
14D14B772BA5AB0700386C3B /* ContactEvents */ = {
isa = PBXGroup;
children = (
E6B0AAD42BAAE4EC009CB939 /* PlayerFloorContactEvent.swift */,
143AA3922BA4DBE7009C28E7 /* PlayerMonsterContactEvent.swift */,
);
path = ContactEvents;
Expand Down Expand Up @@ -447,6 +456,7 @@
4E630F282B9F7EC20008F887 /* Components */ = {
isa = PBXGroup;
children = (
E6B34A462BAA03AF0009A60B /* PlayerComponent.swift */,
4E630F292B9F7EF60008F887 /* PositionComponent.swift */,
14970F4F2BA814D500CC1E8A /* ScoreComponent.swift */,
4E630F2B2B9F7F460008F887 /* Component.swift */,
Expand Down Expand Up @@ -516,6 +526,8 @@
children = (
E6B5509F2BA15D2000DC7396 /* MTKRenderer */,
E6A745132BA057040080C1BE /* Renderer.swift */,
E6B0AAD02BAAE3DC009CB939 /* ViewDelegate.swift */,
E6B0AAD22BAAE438009CB939 /* ControlViewDelegate.swift */,
);
path = Rendering;
sourceTree = "<group>";
Expand Down Expand Up @@ -713,6 +725,7 @@
4E8660622BA0964A0035530D /* Obstacle.swift in Sources */,
1471B0A02BA6A70600878B14 /* HealthSystem.swift in Sources */,
461148932BA1D04B0073E7E1 /* System.swift in Sources */,
E6B0AAD12BAAE3DC009CB939 /* ViewDelegate.swift in Sources */,
1471B0A62BA6AC2D00878B14 /* PlayerAttackMonsterEvent.swift in Sources */,
4E8660642BA096600035530D /* Tool.swift in Sources */,
4E630F392B9F943B0008F887 /* EntityManager.swift in Sources */,
Expand All @@ -728,6 +741,7 @@
145F2C842BA22CA300457549 /* EventModifiable.swift in Sources */,
4E630EFA2B9F7E070008F887 /* ViewController.swift in Sources */,
E6A011172BA5F4AD006904D9 /* EntitySyncInterface.swift in Sources */,
E6B0AAD52BAAE4EC009CB939 /* PlayerFloorContactEvent.swift in Sources */,
E64361132BA4C2CD003850FD /* PhysicsModule.swift in Sources */,
4E630F322B9F887C0008F887 /* PhysicsComponent.swift in Sources */,
1471B0AD2BA6AE4E00878B14 /* UseGrappleHookEvent.swift in Sources */,
Expand All @@ -737,6 +751,7 @@
4E630F2A2B9F7EF60008F887 /* PositionComponent.swift in Sources */,
46D418132BA5C9930091A38B /* Floor.swift in Sources */,
E64361152BA4C2CD003850FD /* GameBridge.swift in Sources */,
E6B0AAD32BAAE438009CB939 /* ControlViewDelegate.swift in Sources */,
4604BBD92BA81C940078B84C /* InventorySystem.swift in Sources */,
14E247952BA2CB480071FFC0 /* EventManager.swift in Sources */,
4E630EF82B9F7E070008F887 /* SceneDelegate.swift in Sources */,
Expand Down Expand Up @@ -766,6 +781,7 @@
E64361102BA4C2CD003850FD /* SpriteModule.swift in Sources */,
4604BBD52BA819C70078B84C /* InventoryComponent.swift in Sources */,
46B8C09A2BA328D900498705 /* GameEngine.swift in Sources */,
E6B34A472BAA03AF0009A60B /* PlayerComponent.swift in Sources */,
461148982BA1E41F0073E7E1 /* PhysicsSystem.swift in Sources */,
4E630F2E2B9F81850008F887 /* HealthComponent.swift in Sources */,
4E86605C2BA095460035530D /* Monster.swift in Sources */,
Expand Down
4 changes: 3 additions & 1 deletion star-dash/star-dash/Constants/PhysicsConstants.swift
Expand Up @@ -8,7 +8,7 @@
import Foundation

struct PhysicsConstants {
private struct CollisionCategory {
struct CollisionCategory {
static let none: UInt32 = 0
static let max: UInt32 = 0xFFFFFFFF
static let player: UInt32 = 0b1 << 0
Expand Down Expand Up @@ -40,4 +40,6 @@ struct PhysicsConstants {
static let wall = CGSize(width: 60, height: 60)
static let floor = CGSize(width: 60, height: 60)
}

static let jumpImpulse = CGVector(dx: 0, dy: 80)
}
6 changes: 5 additions & 1 deletion star-dash/star-dash/Events/CommonEvents/JumpEvent.swift
Expand Up @@ -23,9 +23,13 @@ class JumpEvent: Event {
guard let physicsSystem = target.system(ofType: PhysicsSystem.self) else {
return
}
guard !physicsSystem.isJumping(entityId) else {

guard let playerComponent = target.component(ofType: PlayerComponent.self, ofEntity: entityId),
!playerComponent.isJumping else {
return
}
playerComponent.isJumping = true

physicsSystem.applyImpulse(to: entityId, impulse: jumpImpulse)
}
}
@@ -0,0 +1,24 @@
import Foundation
import CoreGraphics

class PlayerFloorContactEvent: Event {
let timestamp: Date
let entityId: EntityId
let contactPoint: CGPoint

init(from playerEntityId: EntityId, at contactPoint: CGPoint) {
self.timestamp = Date.now
self.entityId = playerEntityId
self.contactPoint = contactPoint
}

func execute(on target: EventModifiable) {
guard let positionComponent = target.component(ofType: PositionComponent.self, ofEntity: entityId),
let playerComponent = target.component(ofType: PlayerComponent.self, ofEntity: entityId),
positionComponent.position.y > contactPoint.y else {
return
}

playerComponent.isJumping = false
}
}
1 change: 1 addition & 0 deletions star-dash/star-dash/Events/EventModifiable.swift
Expand Up @@ -9,6 +9,7 @@ import Foundation

/// EventModifiable represents instances with entities that can be modified by events through systems.
protocol EventModifiable {
func component<T: Component>(ofType type: T.Type, ofEntity entityId: EntityId) -> T?
func entity(with entityId: EntityId) -> Entity?
func system<T: System>(ofType type: T.Type) -> T?
func add(entity: Entity)
Expand Down
13 changes: 5 additions & 8 deletions star-dash/star-dash/GameBridge/SyncModule/PhysicsModule.swift
Expand Up @@ -17,10 +17,6 @@ class PhysicsModule: SyncModule {
physicsComponent.mass = body.mass
physicsComponent.velocity = body.velocity
// physicsComponent.force = body.force
physicsComponent.affectedByGravity = body.affectedByGravity
physicsComponent.categoryBitMask = body.categoryBitMask
physicsComponent.contactTestMask = body.contactTestMask
physicsComponent.collisionBitMask = body.collisionBitMask
}

func sync(object: SDObject, from entity: Entity) {
Expand All @@ -32,10 +28,6 @@ class PhysicsModule: SyncModule {
body.mass = physicsComponent.mass
body.velocity = physicsComponent.velocity
// body.force = physicsComponent.force
body.affectedByGravity = physicsComponent.affectedByGravity
body.categoryBitMask = body.categoryBitMask
body.contactTestMask = body.contactTestMask
body.collisionBitMask = body.collisionBitMask
}

func create(for object: SDObject, from entity: Entity) {
Expand All @@ -44,6 +36,11 @@ class PhysicsModule: SyncModule {
}

object.physicsBody = createRectanglePhysicsBody(physicsComponent: physicsComponent)
object.physicsBody?.restitution = physicsComponent.restitution
object.physicsBody?.isDynamic = physicsComponent.isDynamic
object.physicsBody?.categoryBitMask = physicsComponent.categoryBitMask
object.physicsBody?.contactTestMask = physicsComponent.contactTestMask
object.physicsBody?.collisionBitMask = physicsComponent.collisionBitMask
}

private func createRectanglePhysicsBody(physicsComponent: PhysicsComponent) -> SDPhysicsBody {
Expand Down
Expand Up @@ -5,36 +5,38 @@
// Created by Ho Jun Hao on 16/3/24.
//

import CoreGraphics

extension Collectible: Collidable {
func collides(with collidable: Collidable) -> Event? {
collidable.collideWithCollectible(self)
func collides(with collidable: Collidable, at contactPoint: CGPoint) -> Event? {
collidable.collideWithCollectible(self, at: contactPoint)
}

func collideWithPlayer(_ player: Player) -> Event? {
func collideWithPlayer(_ player: Player, at contactPoint: CGPoint) -> Event? {
CollisionHandler.between(player: player, collectible: self)
}

func collideWithMonster(_ monster: Monster) -> Event? {
func collideWithMonster(_ monster: Monster, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithCollectible(_ collectible: Collectible) -> Event? {
func collideWithCollectible(_ collectible: Collectible, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithObstacle(_ obstacle: Obstacle) -> Event? {
func collideWithObstacle(_ obstacle: Obstacle, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithTool(_ tool: Tool) -> Event? {
func collideWithTool(_ tool: Tool, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithWall(_ wall: Wall) -> Event? {
func collideWithWall(_ wall: Wall, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithFloor(_ floor: Floor) -> Event? {
func collideWithFloor(_ floor: Floor, at contactPoint: CGPoint) -> Event? {
nil
}
}
18 changes: 10 additions & 8 deletions star-dash/star-dash/GameEngine/Collision/Collidable.swift
Expand Up @@ -5,13 +5,15 @@
// Created by Ho Jun Hao on 16/3/24.
//

import CoreGraphics

protocol Collidable {
func collides(with collidable: Collidable) -> Event?
func collideWithPlayer(_ player: Player) -> Event?
func collideWithMonster(_ monster: Monster) -> Event?
func collideWithCollectible(_ collectible: Collectible) -> Event?
func collideWithObstacle(_ obstacle: Obstacle) -> Event?
func collideWithTool(_ tool: Tool) -> Event?
func collideWithWall(_ wall: Wall) -> Event?
func collideWithFloor(_ floor: Floor) -> Event?
func collides(with collidable: Collidable, at contactPoint: CGPoint) -> Event?
func collideWithPlayer(_ player: Player, at contactPoint: CGPoint) -> Event?
func collideWithMonster(_ monster: Monster, at contactPoint: CGPoint) -> Event?
func collideWithCollectible(_ collectible: Collectible, at contactPoint: CGPoint) -> Event?
func collideWithObstacle(_ obstacle: Obstacle, at contactPoint: CGPoint) -> Event?
func collideWithTool(_ tool: Tool, at contactPoint: CGPoint) -> Event?
func collideWithWall(_ wall: Wall, at contactPoint: CGPoint) -> Event?
func collideWithFloor(_ floor: Floor, at contactPoint: CGPoint) -> Event?
}
Expand Up @@ -20,8 +20,8 @@ struct CollisionHandler {
nil
}

static func between(player: Player, floor: Floor) -> Event? {
nil
static func between(player: Player, floor: Floor, at contactPoint: CGPoint) -> Event? {
PlayerFloorContactEvent(from: player.id, at: contactPoint)
}

static func between(player: Player, wall: Wall) -> Event? {
Expand Down
22 changes: 12 additions & 10 deletions star-dash/star-dash/GameEngine/Collision/Floor+Collidable.swift
Expand Up @@ -5,36 +5,38 @@
// Created by Ho Jun Hao on 16/3/24.
//

import CoreGraphics

extension Floor: Collidable {
func collides(with collidable: Collidable) -> Event? {
collidable.collideWithFloor(self)
func collides(with collidable: Collidable, at contactPoint: CGPoint) -> Event? {
collidable.collideWithFloor(self, at: contactPoint)
}

func collideWithPlayer(_ player: Player) -> Event? {
CollisionHandler.between(player: player, floor: self)
func collideWithPlayer(_ player: Player, at contactPoint: CGPoint) -> Event? {
CollisionHandler.between(player: player, floor: self, at: contactPoint)
}

func collideWithMonster(_ monster: Monster) -> Event? {
func collideWithMonster(_ monster: Monster, at contactPoint: CGPoint) -> Event? {
CollisionHandler.between(monster: monster, floor: self)
}

func collideWithCollectible(_ collectible: Collectible) -> Event? {
func collideWithCollectible(_ collectible: Collectible, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithObstacle(_ obstacle: Obstacle) -> Event? {
func collideWithObstacle(_ obstacle: Obstacle, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithTool(_ tool: Tool) -> Event? {
func collideWithTool(_ tool: Tool, at contactPoint: CGPoint) -> Event? {
CollisionHandler.between(tool: tool, floor: self)
}

func collideWithWall(_ wall: Wall) -> Event? {
func collideWithWall(_ wall: Wall, at contactPoint: CGPoint) -> Event? {
nil
}

func collideWithFloor(_ floor: Floor) -> Event? {
func collideWithFloor(_ floor: Floor, at contactPoint: CGPoint) -> Event? {
nil
}
}

0 comments on commit 483ea81

Please sign in to comment.