Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/collision #8

Merged
merged 5 commits into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions star-dash/star-dash.xcodeproj/project.pbxproj
Expand Up @@ -18,6 +18,18 @@
461148962BA1D53D0073E7E1 /* PositionSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461148952BA1D53D0073E7E1 /* PositionSystem.swift */; };
461148982BA1E41F0073E7E1 /* PhysicsSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 461148972BA1E41F0073E7E1 /* PhysicsSystem.swift */; };
46B8C09A2BA328D900498705 /* GameEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B8C0992BA328D900498705 /* GameEngine.swift */; };
46D418112BA5C88B0091A38B /* Wall.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418102BA5C88B0091A38B /* Wall.swift */; };
46D418132BA5C9930091A38B /* Floor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418122BA5C9930091A38B /* Floor.swift */; };
46D418162BA5CBD60091A38B /* Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418152BA5CBD60091A38B /* Collidable.swift */; };
46D418182BA5CD840091A38B /* Player+Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418172BA5CD840091A38B /* Player+Collidable.swift */; };
46D4181A2BA5CDBD0091A38B /* CollisionHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418192BA5CDBD0091A38B /* CollisionHandler.swift */; };
46D4181C2BA5CDC70091A38B /* SeparationHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D4181B2BA5CDC70091A38B /* SeparationHandler.swift */; };
46D4181E2BA5D2620091A38B /* Monster+Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D4181D2BA5D2620091A38B /* Monster+Collidable.swift */; };
46D418202BA5D3420091A38B /* Collectible+Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D4181F2BA5D3420091A38B /* Collectible+Collidable.swift */; };
46D418222BA5D4E60091A38B /* Obstacle+Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418212BA5D4E60091A38B /* Obstacle+Collidable.swift */; };
46D418242BA5D5280091A38B /* Tool+Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418232BA5D5280091A38B /* Tool+Collidable.swift */; };
46D418262BA5D6500091A38B /* Wall+Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418252BA5D6500091A38B /* Wall+Collidable.swift */; };
46D418282BA5D6800091A38B /* Floor+Collidable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46D418272BA5D6800091A38B /* Floor+Collidable.swift */; };
4E630EF62B9F7E070008F887 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630EF52B9F7E070008F887 /* AppDelegate.swift */; };
4E630EF82B9F7E070008F887 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630EF72B9F7E070008F887 /* SceneDelegate.swift */; };
4E630EFA2B9F7E070008F887 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630EF92B9F7E070008F887 /* ViewController.swift */; };
Expand Down Expand Up @@ -99,6 +111,18 @@
461148952BA1D53D0073E7E1 /* PositionSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionSystem.swift; sourceTree = "<group>"; };
461148972BA1E41F0073E7E1 /* PhysicsSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhysicsSystem.swift; sourceTree = "<group>"; };
46B8C0992BA328D900498705 /* GameEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameEngine.swift; sourceTree = "<group>"; };
46D418102BA5C88B0091A38B /* Wall.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Wall.swift; sourceTree = "<group>"; };
46D418122BA5C9930091A38B /* Floor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Floor.swift; sourceTree = "<group>"; };
46D418152BA5CBD60091A38B /* Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Collidable.swift; sourceTree = "<group>"; };
46D418172BA5CD840091A38B /* Player+Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Player+Collidable.swift"; sourceTree = "<group>"; };
46D418192BA5CDBD0091A38B /* CollisionHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollisionHandler.swift; sourceTree = "<group>"; };
46D4181B2BA5CDC70091A38B /* SeparationHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeparationHandler.swift; sourceTree = "<group>"; };
46D4181D2BA5D2620091A38B /* Monster+Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Monster+Collidable.swift"; sourceTree = "<group>"; };
46D4181F2BA5D3420091A38B /* Collectible+Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Collectible+Collidable.swift"; sourceTree = "<group>"; };
46D418212BA5D4E60091A38B /* Obstacle+Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Obstacle+Collidable.swift"; sourceTree = "<group>"; };
46D418232BA5D5280091A38B /* Tool+Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Tool+Collidable.swift"; sourceTree = "<group>"; };
46D418252BA5D6500091A38B /* Wall+Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Wall+Collidable.swift"; sourceTree = "<group>"; };
46D418272BA5D6800091A38B /* Floor+Collidable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Floor+Collidable.swift"; sourceTree = "<group>"; };
4E630EF22B9F7E070008F887 /* star-dash.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "star-dash.app"; sourceTree = BUILT_PRODUCTS_DIR; };
4E630EF52B9F7E070008F887 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4E630EF72B9F7E070008F887 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -194,6 +218,7 @@
46B8C0982BA328BF00498705 /* GameEngine */ = {
isa = PBXGroup;
children = (
46D418142BA5CB760091A38B /* Collision */,
4611488F2BA1CD9E0073E7E1 /* Systems */,
4E630F282B9F7EC20008F887 /* Components */,
4E630F252B9F7E500008F887 /* Entities */,
Expand All @@ -202,6 +227,23 @@
path = GameEngine;
sourceTree = "<group>";
};
46D418142BA5CB760091A38B /* Collision */ = {
isa = PBXGroup;
children = (
46D418152BA5CBD60091A38B /* Collidable.swift */,
46D418172BA5CD840091A38B /* Player+Collidable.swift */,
46D418192BA5CDBD0091A38B /* CollisionHandler.swift */,
46D4181B2BA5CDC70091A38B /* SeparationHandler.swift */,
46D4181D2BA5D2620091A38B /* Monster+Collidable.swift */,
46D4181F2BA5D3420091A38B /* Collectible+Collidable.swift */,
46D418212BA5D4E60091A38B /* Obstacle+Collidable.swift */,
46D418232BA5D5280091A38B /* Tool+Collidable.swift */,
46D418252BA5D6500091A38B /* Wall+Collidable.swift */,
46D418272BA5D6800091A38B /* Floor+Collidable.swift */,
);
path = Collision;
sourceTree = "<group>";
};
4E630EE92B9F7E070008F887 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -308,6 +350,8 @@
4E86605E2BA095E30035530D /* Collectible.swift */,
4E8660612BA0964A0035530D /* Obstacle.swift */,
4E8660632BA096600035530D /* Tool.swift */,
46D418102BA5C88B0091A38B /* Wall.swift */,
46D418122BA5C9930091A38B /* Floor.swift */,
);
path = GameEntities;
sourceTree = "<group>";
Expand Down Expand Up @@ -525,6 +569,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
46D418182BA5CD840091A38B /* Player+Collidable.swift in Sources */,
46D4181E2BA5D2620091A38B /* Monster+Collidable.swift in Sources */,
E6B1DC902BA34A4800473563 /* SDPhysicsEngine in Sources */,
4E630F272B9F7E770008F887 /* Entity.swift in Sources */,
4E630F2C2B9F7F460008F887 /* Component.swift in Sources */,
Expand All @@ -534,9 +580,11 @@
4E8660642BA096600035530D /* Tool.swift in Sources */,
4E630F392B9F943B0008F887 /* EntityManager.swift in Sources */,
4E630F302B9F83DE0008F887 /* SpriteComponent.swift in Sources */,
46D4181A2BA5CDBD0091A38B /* CollisionHandler.swift in Sources */,
4E86605F2BA095E30035530D /* Collectible.swift in Sources */,
E64361122BA4C2CD003850FD /* ObjectModule.swift in Sources */,
4E8660662BA097D40035530D /* PhysicsConstants.swift in Sources */,
46D418162BA5CBD60091A38B /* Collidable.swift in Sources */,
461148912BA1CDBF0073E7E1 /* SystemManager.swift in Sources */,
145F2C842BA22CA300457549 /* EventModifiable.swift in Sources */,
4E630EFA2B9F7E070008F887 /* ViewController.swift in Sources */,
Expand All @@ -546,25 +594,36 @@
4E630EF62B9F7E070008F887 /* AppDelegate.swift in Sources */,
461148962BA1D53D0073E7E1 /* PositionSystem.swift in Sources */,
4E630F2A2B9F7EF60008F887 /* PositionComponent.swift in Sources */,
46D418132BA5C9930091A38B /* Floor.swift in Sources */,
E64361152BA4C2CD003850FD /* GameBridge.swift in Sources */,
14E247952BA2CB480071FFC0 /* EventManager.swift in Sources */,
4E630EF82B9F7E070008F887 /* SceneDelegate.swift in Sources */,
E64361112BA4C2CD003850FD /* SyncModule.swift in Sources */,
E6A7451B2BA0C1890080C1BE /* PlayerView.swift in Sources */,
46D418282BA5D6800091A38B /* Floor+Collidable.swift in Sources */,
46D418222BA5D4E60091A38B /* Obstacle+Collidable.swift in Sources */,
E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */,
E6A745182BA057040080C1BE /* Renderer.swift in Sources */,
E6A745192BA057040080C1BE /* GameScene.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 */,
46D418202BA5D3420091A38B /* Collectible+Collidable.swift in Sources */,
E6A7451D2BA0CAD90080C1BE /* JoystickView.swift in Sources */,
E6B550A12BA15E9C00DC7396 /* OverlayView.swift in Sources */,
46D418262BA5D6500091A38B /* Wall+Collidable.swift in Sources */,
4E630F342B9F8FC00008F887 /* Player.swift in Sources */,
4E630F372B9F91DE0008F887 /* PlayerSprite.swift in Sources */,
E64361102BA4C2CD003850FD /* SpriteModule.swift in Sources */,
46B8C09A2BA328D900498705 /* GameEngine.swift in Sources */,
461148982BA1E41F0073E7E1 /* PhysicsSystem.swift in Sources */,
46D4181C2BA5CDC70091A38B /* SeparationHandler.swift in Sources */,
4E630F2E2B9F81850008F887 /* HealthComponent.swift in Sources */,
4E86605C2BA095460035530D /* Monster.swift in Sources */,
145F2C802BA203B400457549 /* Event.swift in Sources */,
46D418112BA5C88B0091A38B /* Wall.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
40 changes: 26 additions & 14 deletions star-dash/star-dash/Constants/PhysicsConstants.swift
Expand Up @@ -9,23 +9,35 @@ import Foundation

struct PhysicsConstants {
private struct CollisionCategory {
static let player: UInt32 = 0b00001
static let monster: UInt32 = 0b00010
static let collectible: UInt32 = 0b00100
static let obstacle: UInt32 = 0b01000
static let tool: UInt32 = 0b10000
static let none: UInt32 = 0
static let max: UInt32 = 0xFFFFFFFF
static let player: UInt32 = 0b1 << 0
static let monster: UInt32 = 0b1 << 1
static let collectible: UInt32 = 0b1 << 2
static let obstacle: UInt32 = 0b1 << 3
static let tool: UInt32 = 0b1 << 4
static let wall: UInt32 = 0b1 << 5
static let floor: UInt32 = 0b1 << 6
}

struct CollisionMask {
static let playerCollisionMask = CollisionCategory.monster
| CollisionCategory.collectible | CollisionCategory.obstacle | CollisionCategory.tool

static let monsterCollisionMask = CollisionCategory.player | CollisionCategory.tool

static let collectibleCollisionMask = CollisionCategory.player

static let obstacleCollisionMask = CollisionCategory.player
static let player = CollisionCategory.max ^ CollisionCategory.player
static let monster = CollisionCategory.player | CollisionCategory.tool
static let collectible = CollisionCategory.player
static let obstacle = CollisionCategory.player
static let tool = CollisionCategory.player | CollisionCategory.monster
static let wall = CollisionCategory.player | CollisionCategory.monster
static let floor = CollisionCategory.player | CollisionCategory.monster
}

static let toolCollisionMask = CollisionCategory.player | CollisionCategory.monster
struct Dimensions {
// TODO: determine appropriate size for each
static let player = CGSize(width: 60, height: 60)
static let monster = CGSize(width: 60, height: 60)
static let collectible = CGSize(width: 60, height: 60)
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)
}
}
@@ -0,0 +1,72 @@
//
// Collectible+Collidable.swift
// star-dash
//
// Created by Ho Jun Hao on 16/3/24.
//

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

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

func collideWithMonster(_ monster: Monster) -> Event? {
nil
}

func collideWithCollectible(_ collectible: Collectible) -> Event? {
nil
}

func collideWithObstacle(_ obstacle: Obstacle) -> Event? {
nil
}

func collideWithTool(_ tool: Tool) -> Event? {
nil
}

func collideWithWall(_ wall: Wall) -> Event? {
nil
}

func collideWithFloor(_ floor: Floor) -> Event? {
nil
}

func separates(from collidable: Collidable) -> Event? {
collidable.separatesFromCollectible(self)
}

func separatesFromPlayer(_ player: Player) -> Event? {
SeparationHandler.between(player: player, collectible: self)
}

func separatesFromMonster(_ monster: Monster) -> Event? {
nil
}

func separatesFromCollectible(_ collectible: Collectible) -> Event? {
nil
}

func separatesFromObstacle(_ obstacle: Obstacle) -> Event? {
nil
}

func separatesFromTool(_ tool: Tool) -> Event? {
nil
}

func separatesFromWall(_ wall: Wall) -> Event? {
nil
}

func separatesFromFloor(_ floor: Floor) -> Event? {
nil
}
}
26 changes: 26 additions & 0 deletions star-dash/star-dash/GameEngine/Collision/Collidable.swift
@@ -0,0 +1,26 @@
//
// Collidable.swift
// star-dash
//
// Created by Ho Jun Hao on 16/3/24.
//

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 separates(from collidable: Collidable) -> Event?
func separatesFromPlayer(_ player: Player) -> Event?
func separatesFromMonster(_ monster: Monster) -> Event?
func separatesFromCollectible(_ collectible: Collectible) -> Event?
func separatesFromObstacle(_ obstacle: Obstacle) -> Event?
func separatesFromTool(_ tool: Tool) -> Event?
func separatesFromWall(_ wall: Wall) -> Event?
func separatesFromFloor(_ floor: Floor) -> Event?
}
46 changes: 46 additions & 0 deletions star-dash/star-dash/GameEngine/Collision/CollisionHandler.swift
@@ -0,0 +1,46 @@
//
// CollisionHandler.swift
// star-dash
//
// Created by Ho Jun Hao on 16/3/24.
//

import Foundation

struct CollisionHandler {
static func between(player: Player, monster: Monster) -> Event? {
nil
}

static func between(player: Player, collectible: Collectible) -> Event? {
nil
}

static func between(player: Player, obstacle: Obstacle) -> Event? {
nil
}

static func between(player: Player, floor: Floor) -> Event? {
nil
}

static func between(player: Player, wall: Wall) -> Event? {
nil
}

static func between(player: Player, tool: Tool) -> Event? {
nil
}

static func between(monster: Monster, tool: Tool) -> Event? {
nil
}

static func between(monster: Monster, wall: Wall) -> Event? {
nil
}

static func between(monster: Monster, floor: Floor) -> Event? {
nil
}
}