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

Add remaining events #7

Merged
merged 25 commits into from Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b1bfb01
feat: add system getter
jasonqiu212 Mar 15, 2024
50ea3ea
feat: finish MoveEvent
jasonqiu212 Mar 15, 2024
1610e51
style: lint
jasonqiu212 Mar 15, 2024
8e89ee3
feat: add operator extensions for CGVector for convenience
jasonqiu212 Mar 15, 2024
ff87945
feat: add impulse
jasonqiu212 Mar 15, 2024
7ea238e
feat: add JumpEvent
jasonqiu212 Mar 15, 2024
395be59
refactor: rename parameters for readability
jasonqiu212 Mar 15, 2024
cbb9aa1
feat: add mock DeathEvent
jasonqiu212 Mar 15, 2024
bacc2a6
refactor: set timestamp of events
jasonqiu212 Mar 15, 2024
d79d586
feat: add mock PlayerMonsterCollisionEvent
jasonqiu212 Mar 15, 2024
7529246
feat: add AttackEvent
jasonqiu212 Mar 15, 2024
e4b4811
feat: add CollectiblePickupEvent
jasonqiu212 Mar 15, 2024
d62a8ba
style: lint
jasonqiu212 Mar 15, 2024
1ac00d4
feat: add RemoveEvent
jasonqiu212 Mar 16, 2024
ad9d349
feat: add more functionalities for EventModifiable
jasonqiu212 Mar 16, 2024
c9c898f
feat: remove collectible after pickup
jasonqiu212 Mar 16, 2024
0cfe812
refactor: restructure events for better organization
jasonqiu212 Mar 16, 2024
4923ec2
refactor: restructure code
jasonqiu212 Mar 17, 2024
c7897e4
feat: finish MonsterDeathEvent
jasonqiu212 Mar 17, 2024
7b684f6
feat: add health system
jasonqiu212 Mar 17, 2024
855e229
feat: add attack events
jasonqiu212 Mar 17, 2024
56c54fa
style: lint
jasonqiu212 Mar 17, 2024
cfe665c
fix: fix todo for PlayerMonsterContactEvent
jasonqiu212 Mar 17, 2024
f309752
feat: add draft UseGrappleHookEvent
jasonqiu212 Mar 17, 2024
6a3c7e6
Merge branch 'main' into add-remaining-events
jasonqiu212 Mar 17, 2024
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
98 changes: 97 additions & 1 deletion star-dash/star-dash.xcodeproj/project.pbxproj
Expand Up @@ -8,8 +8,20 @@

/* Begin PBXBuildFile section */
142D9F8F2BA15FBB005FE9E0 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 142D9F8E2BA15FBB005FE9E0 /* .swiftlint.yml */; };
143AA38C2BA4D3E3009C28E7 /* JumpEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 143AA38B2BA4D3E3009C28E7 /* JumpEvent.swift */; };
143AA38F2BA4D61A009C28E7 /* CGVector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 143AA38E2BA4D61A009C28E7 /* CGVector.swift */; };
143AA3912BA4D7AC009C28E7 /* MonsterDeathEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 143AA3902BA4D7AC009C28E7 /* MonsterDeathEvent.swift */; };
143AA3932BA4DBE7009C28E7 /* PlayerMonsterContactEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 143AA3922BA4DBE7009C28E7 /* PlayerMonsterContactEvent.swift */; };
143AA3952BA4DF1C009C28E7 /* MonsterAttackPlayerEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 143AA3942BA4DF1C009C28E7 /* MonsterAttackPlayerEvent.swift */; };
143AA3972BA4E0D9009C28E7 /* PickupCollectibleEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 143AA3962BA4E0D9009C28E7 /* PickupCollectibleEvent.swift */; };
145F2C802BA203B400457549 /* Event.swift in Sources */ = {isa = PBXBuildFile; fileRef = 145F2C7F2BA203B400457549 /* Event.swift */; };
145F2C842BA22CA300457549 /* EventModifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 145F2C832BA22CA300457549 /* EventModifiable.swift */; };
1471B0A02BA6A70600878B14 /* HealthSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1471B09F2BA6A70600878B14 /* HealthSystem.swift */; };
1471B0A22BA6AA2200878B14 /* RespawnEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1471B0A12BA6AA2200878B14 /* RespawnEvent.swift */; };
1471B0A42BA6AAF200878B14 /* PlayerDeathEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1471B0A32BA6AAF200878B14 /* PlayerDeathEvent.swift */; };
1471B0A62BA6AC2D00878B14 /* PlayerAttackMonsterEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1471B0A52BA6AC2D00878B14 /* PlayerAttackMonsterEvent.swift */; };
1471B0AD2BA6AE4E00878B14 /* UseGrappleHookEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1471B0AC2BA6AE4E00878B14 /* UseGrappleHookEvent.swift */; };
14D14B732BA5A3CD00386C3B /* RemoveEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14D14B722BA5A3CD00386C3B /* RemoveEvent.swift */; };
14E2478E2BA22FCE0071FFC0 /* MoveEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14E2478D2BA22FCE0071FFC0 /* MoveEvent.swift */; };
14E247932BA2CA920071FFC0 /* DequeModule in Frameworks */ = {isa = PBXBuildFile; productRef = 14E247922BA2CA920071FFC0 /* DequeModule */; };
14E247952BA2CB480071FFC0 /* EventManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14E247942BA2CB480071FFC0 /* EventManager.swift */; };
Expand Down Expand Up @@ -102,8 +114,20 @@

/* Begin PBXFileReference section */
142D9F8E2BA15FBB005FE9E0 /* .swiftlint.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = "<group>"; };
143AA38B2BA4D3E3009C28E7 /* JumpEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JumpEvent.swift; sourceTree = "<group>"; };
143AA38E2BA4D61A009C28E7 /* CGVector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CGVector.swift; sourceTree = "<group>"; };
143AA3902BA4D7AC009C28E7 /* MonsterDeathEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonsterDeathEvent.swift; sourceTree = "<group>"; };
143AA3922BA4DBE7009C28E7 /* PlayerMonsterContactEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMonsterContactEvent.swift; sourceTree = "<group>"; };
143AA3942BA4DF1C009C28E7 /* MonsterAttackPlayerEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonsterAttackPlayerEvent.swift; sourceTree = "<group>"; };
143AA3962BA4E0D9009C28E7 /* PickupCollectibleEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PickupCollectibleEvent.swift; sourceTree = "<group>"; };
145F2C7F2BA203B400457549 /* Event.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Event.swift; sourceTree = "<group>"; };
145F2C832BA22CA300457549 /* EventModifiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventModifiable.swift; sourceTree = "<group>"; };
1471B09F2BA6A70600878B14 /* HealthSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthSystem.swift; sourceTree = "<group>"; };
1471B0A12BA6AA2200878B14 /* RespawnEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RespawnEvent.swift; sourceTree = "<group>"; };
1471B0A32BA6AAF200878B14 /* PlayerDeathEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerDeathEvent.swift; sourceTree = "<group>"; };
1471B0A52BA6AC2D00878B14 /* PlayerAttackMonsterEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerAttackMonsterEvent.swift; sourceTree = "<group>"; };
1471B0AC2BA6AE4E00878B14 /* UseGrappleHookEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UseGrappleHookEvent.swift; sourceTree = "<group>"; };
14D14B722BA5A3CD00386C3B /* RemoveEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoveEvent.swift; sourceTree = "<group>"; };
14E2478D2BA22FCE0071FFC0 /* MoveEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveEvent.swift; sourceTree = "<group>"; };
14E247942BA2CB480071FFC0 /* EventManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventManager.swift; sourceTree = "<group>"; };
461148902BA1CDBF0073E7E1 /* SystemManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -193,22 +217,81 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
143AA38D2BA4D600009C28E7 /* Extensions */ = {
isa = PBXGroup;
children = (
143AA38E2BA4D61A009C28E7 /* CGVector.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
145F2C7E2BA2039700457549 /* Events */ = {
isa = PBXGroup;
children = (
145F2C7F2BA203B400457549 /* Event.swift */,
14E247942BA2CB480071FFC0 /* EventManager.swift */,
145F2C832BA22CA300457549 /* EventModifiable.swift */,
14E2478D2BA22FCE0071FFC0 /* MoveEvent.swift */,
1471B0AB2BA6AE4400878B14 /* ToolEvents */,
14D14B742BA5A72800386C3B /* PlayerEvents */,
14D14B782BA5ABAC00386C3B /* MonsterEvents */,
14D14B772BA5AB0700386C3B /* ContactEvents */,
14D14B792BA5AC1900386C3B /* CommonEvents */,
);
path = Events;
sourceTree = "<group>";
};
1471B0AB2BA6AE4400878B14 /* ToolEvents */ = {
isa = PBXGroup;
children = (
1471B0AC2BA6AE4E00878B14 /* UseGrappleHookEvent.swift */,
);
path = ToolEvents;
sourceTree = "<group>";
};
14D14B742BA5A72800386C3B /* PlayerEvents */ = {
isa = PBXGroup;
children = (
143AA3962BA4E0D9009C28E7 /* PickupCollectibleEvent.swift */,
1471B0A12BA6AA2200878B14 /* RespawnEvent.swift */,
1471B0A32BA6AAF200878B14 /* PlayerDeathEvent.swift */,
);
path = PlayerEvents;
sourceTree = "<group>";
};
14D14B772BA5AB0700386C3B /* ContactEvents */ = {
isa = PBXGroup;
children = (
143AA3922BA4DBE7009C28E7 /* PlayerMonsterContactEvent.swift */,
);
path = ContactEvents;
sourceTree = "<group>";
};
14D14B782BA5ABAC00386C3B /* MonsterEvents */ = {
isa = PBXGroup;
children = (
143AA3942BA4DF1C009C28E7 /* MonsterAttackPlayerEvent.swift */,
1471B0A52BA6AC2D00878B14 /* PlayerAttackMonsterEvent.swift */,
143AA3902BA4D7AC009C28E7 /* MonsterDeathEvent.swift */,
);
path = MonsterEvents;
sourceTree = "<group>";
};
14D14B792BA5AC1900386C3B /* CommonEvents */ = {
isa = PBXGroup;
children = (
14E2478D2BA22FCE0071FFC0 /* MoveEvent.swift */,
143AA38B2BA4D3E3009C28E7 /* JumpEvent.swift */,
14D14B722BA5A3CD00386C3B /* RemoveEvent.swift */,
);
path = CommonEvents;
sourceTree = "<group>";
};
4611488F2BA1CD9E0073E7E1 /* Systems */ = {
isa = PBXGroup;
children = (
461148902BA1CDBF0073E7E1 /* SystemManager.swift */,
461148922BA1D04B0073E7E1 /* System.swift */,
1471B09F2BA6A70600878B14 /* HealthSystem.swift */,
461148952BA1D53D0073E7E1 /* PositionSystem.swift */,
461148972BA1E41F0073E7E1 /* PhysicsSystem.swift */,
);
Expand Down Expand Up @@ -276,6 +359,7 @@
4E86605D2BA095CC0035530D /* Constants */,
4E630F352B9F91C20008F887 /* Enums */,
145F2C7E2BA2039700457549 /* Events */,
143AA38D2BA4D600009C28E7 /* Extensions */,
4E630EF52B9F7E070008F887 /* AppDelegate.swift */,
4E630EF72B9F7E070008F887 /* SceneDelegate.swift */,
4E630EF92B9F7E070008F887 /* ViewController.swift */,
Expand Down Expand Up @@ -574,25 +658,33 @@
E6B1DC902BA34A4800473563 /* SDPhysicsEngine in Sources */,
4E630F272B9F7E770008F887 /* Entity.swift in Sources */,
4E630F2C2B9F7F460008F887 /* Component.swift in Sources */,
143AA38F2BA4D61A009C28E7 /* CGVector.swift in Sources */,
14E2478E2BA22FCE0071FFC0 /* MoveEvent.swift in Sources */,
14D14B732BA5A3CD00386C3B /* RemoveEvent.swift in Sources */,
4E8660622BA0964A0035530D /* Obstacle.swift in Sources */,
1471B0A02BA6A70600878B14 /* HealthSystem.swift in Sources */,
461148932BA1D04B0073E7E1 /* System.swift in Sources */,
1471B0A62BA6AC2D00878B14 /* PlayerAttackMonsterEvent.swift in Sources */,
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 */,
143AA3952BA4DF1C009C28E7 /* MonsterAttackPlayerEvent.swift in Sources */,
143AA3932BA4DBE7009C28E7 /* PlayerMonsterContactEvent.swift in Sources */,
46D418162BA5CBD60091A38B /* Collidable.swift in Sources */,
461148912BA1CDBF0073E7E1 /* SystemManager.swift in Sources */,
145F2C842BA22CA300457549 /* EventModifiable.swift in Sources */,
4E630EFA2B9F7E070008F887 /* ViewController.swift in Sources */,
E6A011172BA5F4AD006904D9 /* EntitySyncInterface.swift in Sources */,
E64361132BA4C2CD003850FD /* PhysicsModule.swift in Sources */,
4E630F322B9F887C0008F887 /* PhysicsComponent.swift in Sources */,
1471B0AD2BA6AE4E00878B14 /* UseGrappleHookEvent.swift in Sources */,
4E630EF62B9F7E070008F887 /* AppDelegate.swift in Sources */,
461148962BA1D53D0073E7E1 /* PositionSystem.swift in Sources */,
143AA38C2BA4D3E3009C28E7 /* JumpEvent.swift in Sources */,
4E630F2A2B9F7EF60008F887 /* PositionComponent.swift in Sources */,
46D418132BA5C9930091A38B /* Floor.swift in Sources */,
E64361152BA4C2CD003850FD /* GameBridge.swift in Sources */,
Expand All @@ -604,25 +696,29 @@
46D418222BA5D4E60091A38B /* Obstacle+Collidable.swift in Sources */,
E6A745162BA057040080C1BE /* MTKRenderer.swift in Sources */,
E6A745182BA057040080C1BE /* Renderer.swift in Sources */,
143AA3972BA4E0D9009C28E7 /* PickupCollectibleEvent.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 */,
1471B0A42BA6AAF200878B14 /* PlayerDeathEvent.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 */,
143AA3912BA4D7AC009C28E7 /* MonsterDeathEvent.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 */,
1471B0A22BA6AA2200878B14 /* RespawnEvent.swift in Sources */,
46D418112BA5C88B0091A38B /* Wall.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
31 changes: 31 additions & 0 deletions star-dash/star-dash/Events/CommonEvents/JumpEvent.swift
@@ -0,0 +1,31 @@
//
// JumpEvent.swift
// star-dash
//
// Created by Jason Qiu on 16/3/24.
//

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.entityId = entityId
self.jumpImpulse = jumpImpulse
}

func execute(on target: EventModifiable) {
guard let physicsSystem = target.system(ofType: PhysicsSystem.self) else {
return
}
guard !physicsSystem.isJumping(entityId) else {
return
}
physicsSystem.applyImpulse(to: entityId, impulse: jumpImpulse)
}
}
28 changes: 28 additions & 0 deletions star-dash/star-dash/Events/CommonEvents/MoveEvent.swift
@@ -0,0 +1,28 @@
//
// MoveEvent.swift
// star-dash
//
// Created by Jason Qiu on 14/3/24.
//

import Foundation

class MoveEvent: 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)
}
}
25 changes: 25 additions & 0 deletions star-dash/star-dash/Events/CommonEvents/RemoveEvent.swift
@@ -0,0 +1,25 @@
//
// RemoveEvent.swift
// star-dash
//
// Created by Jason Qiu on 16/3/24.
//

import Foundation

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

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

func execute(on target: EventModifiable) {
guard let entity = target.entity(with: entityId) else {
return
}
target.remove(entity: entity)
}
}
@@ -0,0 +1,26 @@
//
// PlayerMonsterContactEvent.swift
// star-dash
//
// Created by Jason Qiu on 16/3/24.
//

import Foundation

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

let monsterEntityId: EntityId

init(from playerEntityId: EntityId, on monsterEntityId: EntityId) {
self.timestamp = Date.now
entityId = playerEntityId
self.monsterEntityId = monsterEntityId
}

func execute(on target: EventModifiable) {
// TODO: Determine if player is directly on top of monster.
// Trigger PlayerAttackMonsterEvent and MonsterAttackPlayerEvent accordingly
}
}
10 changes: 8 additions & 2 deletions star-dash/star-dash/Events/EventModifiable.swift
Expand Up @@ -7,5 +7,11 @@

import Foundation

/// EventModifiable represents objects that can be modified by events.
protocol EventModifiable { }
/// EventModifiable represents instances with entities that can be modified by events through systems.
protocol EventModifiable {
func entity(with entityId: EntityId) -> Entity?
func system<T: System>(ofType type: T.Type) -> T?
func add(entity: Entity)
func add(event: Event)
func remove(entity: Entity)
}
@@ -0,0 +1,31 @@
//
// MonsterAttackPlayerEvent.swift
// star-dash
//
// Created by Jason Qiu on 16/3/24.
//

import Foundation

class MonsterAttackPlayerEvent: Event {
private static let playerHealthDecrement = 20

let timestamp: Date
let entityId: EntityId

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

func execute(on target: EventModifiable) {
guard let healthSystem = target.system(ofType: HealthSystem.self) else {
return
}
healthSystem.decreaseHealth(of: entityId, by: MonsterAttackPlayerEvent.playerHealthDecrement)

if !healthSystem.hasHealth(for: entityId) {
target.add(event: PlayerDeathEvent(on: entityId))
}
}
}
22 changes: 22 additions & 0 deletions star-dash/star-dash/Events/MonsterEvents/MonsterDeathEvent.swift
@@ -0,0 +1,22 @@
//
// MonsterDeathEvent.swift
// star-dash
//
// Created by Jason Qiu on 16/3/24.
//

import Foundation

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

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

func execute(on target: EventModifiable) {
target.add(event: RemoveEvent(on: entityId))
}
}