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: Add Systems #4
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
819fabc
Made the following changes
HoJunHao2000 7cb629c
merge main into systems
HoJunHao2000 297cd71
feat: add event protocols
jasonqiu212 99f9ab9
fix: make event not conform to comparable to support usage of queue
jasonqiu212 a0e1e4e
feat: add mock event
jasonqiu212 3999203
feat: add collections package dependency
jasonqiu212 33b9c26
feat: add EventManager
jasonqiu212 524c75f
style: lint
jasonqiu212 04715fe
merge add-events into systems
HoJunHao2000 06392dc
Add GameEngine skeleton
HoJunHao2000 b1e195b
fix syle issues
HoJunHao2000 fd9570d
Made comment changes
HoJunHao2000 06cb9dc
merge main in systems
HoJunHao2000 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
star-dash/star-dash.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-collections", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-collections.git", | ||
"state" : { | ||
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb", | ||
"version" : "1.1.0" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// Event.swift | ||
// star-dash | ||
// | ||
// Created by Jason Qiu on 13/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol Event { | ||
var timestamp: Date { get } | ||
var entityId: EntityId { get } | ||
|
||
func execute(on target: EventModifiable) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// EventManager.swift | ||
// star-dash | ||
// | ||
// Created by Jason Qiu on 14/3/24. | ||
// | ||
|
||
import DequeModule | ||
import Foundation | ||
|
||
typealias EventQueue = Deque<Event> | ||
|
||
class EventManager { | ||
private var events: EventQueue | ||
|
||
init() { | ||
events = EventQueue() | ||
} | ||
|
||
func add(event: Event) { | ||
events.append(event) | ||
} | ||
|
||
func executeAll(on target: EventModifiable) { | ||
while let event = events.popFirst() { | ||
event.execute(on: target) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// EventModifiable.swift | ||
// star-dash | ||
// | ||
// Created by Jason Qiu on 14/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// EventModifiable represents objects that can be modified by events. | ||
protocol EventModifiable { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// MoveEvent.swift | ||
// star-dash | ||
// | ||
// Created by Jason Qiu on 14/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class MoveEvent: Event { | ||
var entityId: EntityId | ||
var timestamp: Date | ||
|
||
var displacement: CGVector | ||
|
||
init(entityId: EntityId, displacement: CGVector, timestamp: Date = Date.now) { | ||
self.entityId = entityId | ||
self.timestamp = timestamp | ||
self.displacement = displacement | ||
} | ||
|
||
func execute(on target: EventModifiable) { | ||
// TODO: Use PositionSystem from target to modify entity of entityId | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// GameEngine.swift | ||
// star-dash | ||
// | ||
// Created by Ho Jun Hao on 14/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class GameEngine { | ||
private let systemManager: SystemManager | ||
private let entityManager: EntityManager | ||
private let eventManager: EventManager | ||
|
||
init(scene: GameScene) { | ||
self.systemManager = SystemManager() | ||
self.entityManager = EntityManager() | ||
self.eventManager = EventManager() | ||
// TODO: link game engine to renderer | ||
|
||
setUpSystems() | ||
} | ||
|
||
func update(by deltaTime: TimeInterval) { | ||
systemManager.update(by: deltaTime) | ||
eventManager.executeAll(on: self) | ||
} | ||
|
||
private func setUpSystems() { | ||
systemManager.add(PositionSystem(entityManager, dispatcher: self)) | ||
systemManager.add(PhysicsSystem(entityManager, dispatcher: self)) | ||
} | ||
} | ||
|
||
extension GameEngine: EventModifiable { | ||
// TODO: functions of event modifiable | ||
} |
69 changes: 69 additions & 0 deletions
69
star-dash/star-dash/GameEngine/Systems/PhysicsSystem.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// PhysicsSystem.swift | ||
// star-dash | ||
// | ||
// Created by Ho Jun Hao on 13/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class PhysicsSystem: System { | ||
var isActive: Bool | ||
var dispatcher: EventModifiable? | ||
var entityManager: EntityManager | ||
|
||
init(_ entityManager: EntityManager, dispatcher: EventModifiable? = nil) { | ||
self.isActive = true | ||
self.dispatcher = dispatcher | ||
self.entityManager = entityManager | ||
} | ||
|
||
func update(by deltaTime: TimeInterval) { | ||
let physicsComponents = entityManager.componentMap.values.compactMap({ $0 as? PhysicsComponent }) | ||
|
||
for physicsComponent in physicsComponents { | ||
physicsComponent.force = .zero | ||
} | ||
} | ||
|
||
func isMoving(_ entityId: EntityId) -> Bool { | ||
guard let physicsComponent = getPhysicsComponent(of: entityId) else { | ||
return false | ||
} | ||
|
||
return physicsComponent.velocity != .zero | ||
} | ||
|
||
func isJumping(_ entityId: EntityId) -> Bool { | ||
guard let physicsComponent = getPhysicsComponent(of: entityId) else { | ||
return false | ||
} | ||
|
||
return physicsComponent.velocity.dy != .zero | ||
} | ||
|
||
func applyForce(to entityId: EntityId, newForce: CGVector) { | ||
guard let physicsComponent = getPhysicsComponent(of: entityId) else { | ||
return | ||
} | ||
|
||
let newForce = CGVector(dx: physicsComponent.force.dx + newForce.dx, | ||
dy: physicsComponent.force.dy + newForce.dy) | ||
|
||
physicsComponent.force = newForce | ||
} | ||
|
||
func sync(entityVelocityMap: [EntityId: CGVector]) { | ||
for (entityId, newVelocity) in entityVelocityMap { | ||
guard let physicsComponent = getPhysicsComponent(of: entityId) else { | ||
continue | ||
} | ||
|
||
physicsComponent.velocity = newVelocity | ||
} | ||
} | ||
|
||
private func getPhysicsComponent(of entityId: EntityId) -> PhysicsComponent? { | ||
entityManager.component(ofType: PhysicsComponent.self, of: entityId) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
star-dash/star-dash/GameEngine/Systems/PositionSystem.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// PositionSystem.swift | ||
// star-dash | ||
// | ||
// Created by Ho Jun Hao on 13/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class PositionSystem: System { | ||
var isActive: Bool | ||
var dispatcher: EventModifiable? | ||
var entityManager: EntityManager | ||
|
||
init(_ entityManager: EntityManager, dispatcher: EventModifiable? = nil) { | ||
self.isActive = true | ||
self.entityManager = entityManager | ||
self.dispatcher = dispatcher | ||
} | ||
|
||
func move(entityId: EntityId, to newPosition: CGPoint) { | ||
guard let positionComponent = getPositionComponent(of: entityId) else { | ||
return | ||
} | ||
|
||
positionComponent.setPosition(position: newPosition) | ||
} | ||
|
||
func rotate(entityId: EntityId, to newRotation: Float) { | ||
guard let positionComponent = getPositionComponent(of: entityId) else { | ||
return | ||
} | ||
|
||
positionComponent.setRotation(rotation: newRotation) | ||
} | ||
|
||
func sync(entityPositionMap: [EntityId: CGPoint], entityRotationMap: [EntityId: Float]) { | ||
for (entityId, newPosition) in entityPositionMap { | ||
move(entityId: entityId, to: newPosition) | ||
} | ||
|
||
for (entityId, newRotation) in entityRotationMap { | ||
rotate(entityId: entityId, to: newRotation) | ||
} | ||
} | ||
|
||
private func getPositionComponent(of entityId: EntityId) -> PositionComponent? { | ||
entityManager.component(ofType: PositionComponent.self, of: entityId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// System.swift | ||
// star-dash | ||
// | ||
// Created by Ho Jun Hao on 13/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol System { | ||
var isActive: Bool { get set } | ||
var dispatcher: EventModifiable? { get set } | ||
var entityManager: EntityManager { get set } | ||
|
||
func update(by deltaTime: TimeInterval) | ||
} | ||
|
||
extension System { | ||
func update(by deltaTime: TimeInterval) {} | ||
} |
22 changes: 22 additions & 0 deletions
22
star-dash/star-dash/GameEngine/Systems/SystemManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// SystemManager.swift | ||
// star-dash | ||
// | ||
// Created by Ho Jun Hao on 13/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class SystemManager { | ||
private var systems: [System] = [] | ||
|
||
func add(_ system: System) { | ||
systems.append(system) | ||
} | ||
|
||
func update(by deltaTime: TimeInterval) { | ||
for system in systems where system.isActive { | ||
system.update(by: deltaTime) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I clarify what is the role of this method? I'm a bit confused because I noticed that not all systems (like
PositionSystem
) override this method. And inPhysicsSystem
, theupdate
method basically sets all the force vectors to 0. Under what circumstance is this method called?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It more like for timed components in the game, e.g. those that after a certain time will stop working like powerups