Skip to content

Commit

Permalink
Merge pull request #38 from cs3217-2324/entity-component-factory
Browse files Browse the repository at this point in the history
Minor Refactoring and Add EntityFactory
  • Loading branch information
HoJunHao2000 committed Mar 28, 2024
2 parents 72d7363 + fe97f10 commit 4d838ad
Show file tree
Hide file tree
Showing 48 changed files with 480 additions and 371 deletions.
12 changes: 12 additions & 0 deletions star-dash/star-dash.xcodeproj/project.pbxproj
Expand Up @@ -41,6 +41,9 @@
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 */; };
46C3B1C02BB45C2700F10574 /* EntityFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B1BF2BB45C2700F10574 /* EntityFactory.swift */; };
46C3B1C22BB467BB00F10574 /* EntityManagerInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B1C12BB467BB00F10574 /* EntityManagerInterface.swift */; };
46C3B1C42BB5055F00F10574 /* EntityBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B1C32BB5055F00F10574 /* EntityBuilder.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 */; };
Expand Down Expand Up @@ -188,6 +191,9 @@
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>"; };
46C3B1BF2BB45C2700F10574 /* EntityFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityFactory.swift; sourceTree = "<group>"; };
46C3B1C12BB467BB00F10574 /* EntityManagerInterface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManagerInterface.swift; sourceTree = "<group>"; };
46C3B1C32BB5055F00F10574 /* EntityBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityBuilder.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>"; };
Expand Down Expand Up @@ -548,6 +554,9 @@
4E8660602BA0961F0035530D /* GameEntities */,
4E630F262B9F7E770008F887 /* Entity.swift */,
4E630F382B9F943B0008F887 /* EntityManager.swift */,
46C3B1BF2BB45C2700F10574 /* EntityFactory.swift */,
46C3B1C12BB467BB00F10574 /* EntityManagerInterface.swift */,
46C3B1C32BB5055F00F10574 /* EntityBuilder.swift */,
);
path = Entities;
sourceTree = "<group>";
Expand Down Expand Up @@ -840,6 +849,7 @@
1471B0A62BA6AC2D00878B14 /* PlayerAttackMonsterEvent.swift in Sources */,
4E8660642BA096600035530D /* Tool.swift in Sources */,
4E630F392B9F943B0008F887 /* EntityManager.swift in Sources */,
46C3B1C02BB45C2700F10574 /* EntityFactory.swift in Sources */,
4E630F302B9F83DE0008F887 /* SpriteComponent.swift in Sources */,
46D4181A2BA5CDBD0091A38B /* CollisionHandler.swift in Sources */,
4E86605F2BA095E30035530D /* Collectible.swift in Sources */,
Expand Down Expand Up @@ -874,6 +884,7 @@
14E247952BA2CB480071FFC0 /* EventManager.swift in Sources */,
4EC561E32BB1E98400166DDC /* PlayerObstacleContactEvent.swift in Sources */,
4E630EF82B9F7E070008F887 /* SceneDelegate.swift in Sources */,
46C3B1C22BB467BB00F10574 /* EntityManagerInterface.swift in Sources */,
E69FDDE02BAD3DAD0089D5F3 /* PointsComponent.swift in Sources */,
14970F542BA8163300CC1E8A /* ScoreSystem.swift in Sources */,
E64361112BA4C2CD003850FD /* SyncModule.swift in Sources */,
Expand All @@ -885,6 +896,7 @@
143AA3972BA4E0D9009C28E7 /* PickupCollectibleEvent.swift in Sources */,
46D418242BA5D5280091A38B /* Tool+Collidable.swift in Sources */,
E64361142BA4C2CD003850FD /* CreationModule.swift in Sources */,
46C3B1C42BB5055F00F10574 /* EntityBuilder.swift in Sources */,
4E59E2612BAB42FD007B3FA7 /* LevelPersistable.swift in Sources */,
460A200B2BB1D04F002597B8 /* AttackSystem.swift in Sources */,
E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */,
Expand Down
4 changes: 1 addition & 3 deletions star-dash/star-dash/Events/CommonEvents/JumpEvent.swift
Expand Up @@ -10,13 +10,11 @@ import Foundation
class JumpEvent: Event {
let timestamp: Date
let entityId: EntityId

let jumpImpulse: CGVector

init(on entityId: EntityId, by jumpImpulse: CGVector) {
timestamp = Date.now
self.timestamp = Date.now
self.entityId = entityId
self.jumpImpulse = jumpImpulse
}

}
4 changes: 1 addition & 3 deletions star-dash/star-dash/Events/CommonEvents/MoveEvent.swift
Expand Up @@ -10,13 +10,11 @@ import Foundation
class MoveEvent: Event {
let timestamp: Date
let entityId: EntityId

let toLeft: Bool

init(on entityId: EntityId, toLeft: Bool) {
timestamp = Date.now
self.timestamp = Date.now
self.entityId = entityId
self.toLeft = toLeft
}

}
2 changes: 1 addition & 1 deletion star-dash/star-dash/Events/CommonEvents/RemoveEvent.swift
Expand Up @@ -12,7 +12,7 @@ class RemoveEvent: Event {
let entityId: EntityId

init(on entityId: EntityId) {
timestamp = Date.now
self.timestamp = Date.now
self.entityId = entityId
}
}
Expand Up @@ -5,8 +5,7 @@ class StopMovingEvent: Event {
let entityId: EntityId

init(on entityId: EntityId) {
timestamp = Date.now
self.timestamp = Date.now
self.entityId = entityId
}

}
3 changes: 1 addition & 2 deletions star-dash/star-dash/Events/CommonEvents/TeleportEvent.swift
Expand Up @@ -3,11 +3,10 @@ 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.timestamp = Date.now
self.entityId = entityId
self.destination = destination
}
Expand Down
Expand Up @@ -3,12 +3,12 @@ import CoreGraphics

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

init(from playerEntityId: EntityId, at contactPoint: CGPoint) {
init(from playerId: EntityId, at contactPoint: CGPoint) {
self.timestamp = Date.now
self.entityId = playerEntityId
self.playerId = playerId
self.contactPoint = contactPoint
}
}
Expand Up @@ -8,14 +8,13 @@
import Foundation

class PlayerMonsterContactEvent: Event {
var entityId: EntityId

let timestamp: Date
let playerId: EntityId
let monsterId: EntityId

init(from playerId: EntityId, on monsterId: EntityId) {
self.timestamp = Date.now
self.entityId = playerId
self.playerId = playerId
self.monsterId = monsterId
}
}
Expand Up @@ -8,12 +8,12 @@
import Foundation
class PlayerObstacleContactEvent: Event {
let timestamp: Date
let entityId: EntityId
let playerId: EntityId
let obstacleId: EntityId

init(from playerEntityId: EntityId, on obstacleEntityId: EntityId) {
init(from playerId: EntityId, on obstacleEntityId: EntityId) {
self.timestamp = Date.now
self.entityId = playerEntityId
self.playerId = playerId
self.obstacleId = obstacleEntityId
}
}
Expand Up @@ -8,12 +8,12 @@
import Foundation
class PlayerToolContactEvent: Event {
let timestamp: Date
let entityId: EntityId
let playerId: EntityId
let toolId: EntityId

init(from playerEntityId: EntityId, on toolEntityId: EntityId) {
init(from playerId: EntityId, on toolEntityId: EntityId) {
self.timestamp = Date.now
self.entityId = playerEntityId
self.playerId = playerId
self.toolId = toolEntityId
}
}
1 change: 0 additions & 1 deletion star-dash/star-dash/Events/Event.swift
Expand Up @@ -9,5 +9,4 @@ import Foundation

protocol Event {
var timestamp: Date { get }
var entityId: EntityId { get }
}
2 changes: 1 addition & 1 deletion star-dash/star-dash/Events/EventManager.swift
Expand Up @@ -37,7 +37,7 @@ class EventManager {
listeners[key]?.append(listener)
}

func emit(event: Event) {
private func emit(event: Event) {
let key = ObjectIdentifier(type(of: event))
if let eventListeners = listeners[key] {
for listener in eventListeners {
Expand Down
5 changes: 1 addition & 4 deletions star-dash/star-dash/Events/EventModifiable.swift
Expand Up @@ -9,11 +9,8 @@ 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)
func remove(entity: Entity)
func add(event: Event)
func add(entity: Entity)
func registerListener<T: Event>(for eventType: T.Type, listener: EventListener)
}
Expand Up @@ -9,12 +9,12 @@ import Foundation

class MonsterAttackPlayerEvent: Event {
let timestamp: Date
let entityId: EntityId
let playerId: EntityId
let monsterId: EntityId

init(from monsterId: EntityId, on entityId: EntityId) {
timestamp = Date.now
self.entityId = entityId
init(from monsterId: EntityId, on playerId: EntityId) {
self.timestamp = Date.now
self.playerId = playerId
self.monsterId = monsterId
}
}
Expand Up @@ -9,10 +9,10 @@ import Foundation

class MonsterDeathEvent: Event {
let timestamp: Date
let entityId: EntityId
let monsterId: EntityId

init(on entityId: EntityId) {
init(on monsterId: EntityId) {
self.timestamp = Date.now
self.entityId = entityId
self.monsterId = monsterId
}
}
Expand Up @@ -9,10 +9,12 @@ import Foundation

class PlayerAttackMonsterEvent: Event {
let timestamp: Date
let entityId: EntityId
let playerId: EntityId
let monsterId: EntityId

init(on entityId: EntityId) {
timestamp = Date.now
self.entityId = entityId
init(from playerId: EntityId, on monsterId: EntityId) {
self.timestamp = Date.now
self.playerId = playerId
self.monsterId = monsterId
}
}
Expand Up @@ -9,13 +9,12 @@ import Foundation

class PickupCollectibleEvent: Event {
let timestamp: Date
let entityId: EntityId

let playerId: EntityId
let collectibleEntityId: EntityId

init(by entityId: EntityId, collectibleEntityId: EntityId) {
timestamp = Date.now
self.entityId = entityId
init(by playerId: EntityId, collectibleEntityId: EntityId) {
self.timestamp = Date.now
self.playerId = playerId
self.collectibleEntityId = collectibleEntityId
}
}
Expand Up @@ -9,10 +9,10 @@ import Foundation

class PlayerDeathEvent: Event {
let timestamp: Date
let entityId: EntityId
let playerId: EntityId

init(on entityId: EntityId) {
timestamp = Date.now
self.entityId = entityId
init(on playerId: EntityId) {
self.timestamp = Date.now
self.playerId = playerId
}
}
3 changes: 1 addition & 2 deletions star-dash/star-dash/Events/PlayerEvents/RespawnEvent.swift
Expand Up @@ -10,11 +10,10 @@ import Foundation
class RespawnEvent: Event {
let timestamp: Date
let entityId: EntityId

let newPosition: CGPoint

init(on entityId: EntityId, to newPosition: CGPoint) {
timestamp = Date.now
self.timestamp = Date.now
self.entityId = entityId
self.newPosition = newPosition
}
Expand Down
Expand Up @@ -9,10 +9,10 @@ import Foundation

class UseGrappleHookEvent: Event {
let timestamp: Date
let entityId: EntityId
let playerId: EntityId

init(from playerEntityId: EntityId) {
init(from playerId: EntityId) {
self.timestamp = Date.now
entityId = playerEntityId
self.playerId = playerId
}
}
@@ -1,7 +1,6 @@
import Foundation

class PlayerComponent: Component {

let playerIndex: Int
var canJump = true
var canMove = true
Expand Down
2 changes: 0 additions & 2 deletions star-dash/star-dash/GameEngine/Entities/Entity.swift
Expand Up @@ -11,6 +11,4 @@ typealias EntityId = UUID

protocol Entity {
var id: EntityId { get }

func setUpAndAdd(to: EntityManager)
}

0 comments on commit 4d838ad

Please sign in to comment.