diff --git a/star-dash/star-dash.xcodeproj/project.pbxproj b/star-dash/star-dash.xcodeproj/project.pbxproj index 1d0d7ac0..1f62f8d4 100644 --- a/star-dash/star-dash.xcodeproj/project.pbxproj +++ b/star-dash/star-dash.xcodeproj/project.pbxproj @@ -16,6 +16,15 @@ 4E630F0D2B9F7E090008F887 /* star_dashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F0C2B9F7E090008F887 /* star_dashTests.swift */; }; 4E630F172B9F7E090008F887 /* star_dashUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F162B9F7E090008F887 /* star_dashUITests.swift */; }; 4E630F192B9F7E090008F887 /* star_dashUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F182B9F7E090008F887 /* star_dashUITestsLaunchTests.swift */; }; + 4E630F272B9F7E770008F887 /* Entity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F262B9F7E770008F887 /* Entity.swift */; }; + 4E630F2A2B9F7EF60008F887 /* PositionComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F292B9F7EF60008F887 /* PositionComponent.swift */; }; + 4E630F2C2B9F7F460008F887 /* Component.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F2B2B9F7F460008F887 /* Component.swift */; }; + 4E630F2E2B9F81850008F887 /* HealthComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F2D2B9F81850008F887 /* HealthComponent.swift */; }; + 4E630F302B9F83DE0008F887 /* SpriteComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F2F2B9F83DE0008F887 /* SpriteComponent.swift */; }; + 4E630F322B9F887C0008F887 /* PhysicsComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F312B9F887C0008F887 /* PhysicsComponent.swift */; }; + 4E630F342B9F8FC00008F887 /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F332B9F8FC00008F887 /* Player.swift */; }; + 4E630F372B9F91DE0008F887 /* PlayerSprite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F362B9F91DE0008F887 /* PlayerSprite.swift */; }; + 4E630F392B9F943B0008F887 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E630F382B9F943B0008F887 /* EntityManager.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -49,6 +58,15 @@ 4E630F122B9F7E090008F887 /* star-dashUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "star-dashUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 4E630F162B9F7E090008F887 /* star_dashUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = star_dashUITests.swift; sourceTree = ""; }; 4E630F182B9F7E090008F887 /* star_dashUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = star_dashUITestsLaunchTests.swift; sourceTree = ""; }; + 4E630F262B9F7E770008F887 /* Entity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Entity.swift; sourceTree = ""; }; + 4E630F292B9F7EF60008F887 /* PositionComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionComponent.swift; sourceTree = ""; }; + 4E630F2B2B9F7F460008F887 /* Component.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Component.swift; sourceTree = ""; }; + 4E630F2D2B9F81850008F887 /* HealthComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthComponent.swift; sourceTree = ""; }; + 4E630F2F2B9F83DE0008F887 /* SpriteComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpriteComponent.swift; sourceTree = ""; }; + 4E630F312B9F887C0008F887 /* PhysicsComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhysicsComponent.swift; sourceTree = ""; }; + 4E630F332B9F8FC00008F887 /* Player.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = ""; }; + 4E630F362B9F91DE0008F887 /* PlayerSprite.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerSprite.swift; sourceTree = ""; }; + 4E630F382B9F943B0008F887 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -99,6 +117,8 @@ 4E630EF42B9F7E070008F887 /* star-dash */ = { isa = PBXGroup; children = ( + 4E630F352B9F91C20008F887 /* Enums */, + 4E630F252B9F7E500008F887 /* Entities */, 4E630EF52B9F7E070008F887 /* AppDelegate.swift */, 4E630EF72B9F7E070008F887 /* SceneDelegate.swift */, 4E630EF92B9F7E070008F887 /* ViewController.swift */, @@ -127,6 +147,37 @@ path = "star-dashUITests"; sourceTree = ""; }; + 4E630F252B9F7E500008F887 /* Entities */ = { + isa = PBXGroup; + children = ( + 4E630F282B9F7EC20008F887 /* Components */, + 4E630F262B9F7E770008F887 /* Entity.swift */, + 4E630F332B9F8FC00008F887 /* Player.swift */, + 4E630F382B9F943B0008F887 /* EntityManager.swift */, + ); + path = Entities; + sourceTree = ""; + }; + 4E630F282B9F7EC20008F887 /* Components */ = { + isa = PBXGroup; + children = ( + 4E630F292B9F7EF60008F887 /* PositionComponent.swift */, + 4E630F2B2B9F7F460008F887 /* Component.swift */, + 4E630F2D2B9F81850008F887 /* HealthComponent.swift */, + 4E630F2F2B9F83DE0008F887 /* SpriteComponent.swift */, + 4E630F312B9F887C0008F887 /* PhysicsComponent.swift */, + ); + path = Components; + sourceTree = ""; + }; + 4E630F352B9F91C20008F887 /* Enums */ = { + isa = PBXGroup; + children = ( + 4E630F362B9F91DE0008F887 /* PlayerSprite.swift */, + ); + path = Enums; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -258,9 +309,18 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 4E630F272B9F7E770008F887 /* Entity.swift in Sources */, + 4E630F2C2B9F7F460008F887 /* Component.swift in Sources */, + 4E630F392B9F943B0008F887 /* EntityManager.swift in Sources */, + 4E630F302B9F83DE0008F887 /* SpriteComponent.swift in Sources */, 4E630EFA2B9F7E070008F887 /* ViewController.swift in Sources */, + 4E630F322B9F887C0008F887 /* PhysicsComponent.swift in Sources */, 4E630EF62B9F7E070008F887 /* AppDelegate.swift in Sources */, + 4E630F2A2B9F7EF60008F887 /* PositionComponent.swift in Sources */, 4E630EF82B9F7E070008F887 /* SceneDelegate.swift in Sources */, + 4E630F342B9F8FC00008F887 /* Player.swift in Sources */, + 4E630F372B9F91DE0008F887 /* PlayerSprite.swift in Sources */, + 4E630F2E2B9F81850008F887 /* HealthComponent.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Contents.json new file mode 100644 index 00000000..73c00596 --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (1).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (1).imageset/Contents.json new file mode 100644 index 00000000..5e5def7c --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (1).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (1).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (1).imageset/Run (1).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (1).imageset/Run (1).png new file mode 100644 index 00000000..cf5dcc63 Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (1).imageset/Run (1).png differ diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (2).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (2).imageset/Contents.json new file mode 100644 index 00000000..9b7210db --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (2).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (2).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (2).imageset/Run (2).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (2).imageset/Run (2).png new file mode 100644 index 00000000..d786f71c Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (2).imageset/Run (2).png differ diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (3).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (3).imageset/Contents.json new file mode 100644 index 00000000..2c492b07 --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (3).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (3).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (3).imageset/Run (3).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (3).imageset/Run (3).png new file mode 100644 index 00000000..447167c0 Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (3).imageset/Run (3).png differ diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (4).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (4).imageset/Contents.json new file mode 100644 index 00000000..7124cf1a --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (4).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (4).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (4).imageset/Run (4).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (4).imageset/Run (4).png new file mode 100644 index 00000000..8e13992e Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (4).imageset/Run (4).png differ diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (5).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (5).imageset/Contents.json new file mode 100644 index 00000000..3fc105c4 --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (5).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (5).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (5).imageset/Run (5).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (5).imageset/Run (5).png new file mode 100644 index 00000000..87967ddb Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (5).imageset/Run (5).png differ diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (6).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (6).imageset/Contents.json new file mode 100644 index 00000000..c45f5200 --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (6).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (6).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (6).imageset/Run (6).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (6).imageset/Run (6).png new file mode 100644 index 00000000..443644f8 Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (6).imageset/Run (6).png differ diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (7).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (7).imageset/Contents.json new file mode 100644 index 00000000..6007409f --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (7).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (7).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (7).imageset/Run (7).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (7).imageset/Run (7).png new file mode 100644 index 00000000..eaa52160 Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (7).imageset/Run (7).png differ diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (8).imageset/Contents.json b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (8).imageset/Contents.json new file mode 100644 index 00000000..b630afe8 --- /dev/null +++ b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (8).imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Run (8).png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (8).imageset/Run (8).png b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (8).imageset/Run (8).png new file mode 100644 index 00000000..d0a63134 Binary files /dev/null and b/star-dash/star-dash/Assets.xcassets/red-nose.spriteatlas/Run (8).imageset/Run (8).png differ diff --git a/star-dash/star-dash/Entities/Components/Component.swift b/star-dash/star-dash/Entities/Components/Component.swift new file mode 100644 index 00000000..a1735b37 --- /dev/null +++ b/star-dash/star-dash/Entities/Components/Component.swift @@ -0,0 +1,20 @@ +// +// Component.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation +typealias ComponentId = UUID +class Component { + var entityId: UUID + var id: UUID + + init(id: UUID, entityId: UUID) { + self.id = id + self.entityId = entityId + + } + +} diff --git a/star-dash/star-dash/Entities/Components/HealthComponent.swift b/star-dash/star-dash/Entities/Components/HealthComponent.swift new file mode 100644 index 00000000..31be8287 --- /dev/null +++ b/star-dash/star-dash/Entities/Components/HealthComponent.swift @@ -0,0 +1,21 @@ +// +// HealthComponent.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation + +class HealthComponent: Component { + var health: Int + + init(id: UUID, entityId: UUID, health: Int) { + self.health = health + super.init(id: id, entityId: entityId) + } + + convenience init(entityId: UUID, health: Int) { + self.init(id: UUID(), entityId: entityId, health: health) + } +} diff --git a/star-dash/star-dash/Entities/Components/PhysicsComponent.swift b/star-dash/star-dash/Entities/Components/PhysicsComponent.swift new file mode 100644 index 00000000..e10d1e37 --- /dev/null +++ b/star-dash/star-dash/Entities/Components/PhysicsComponent.swift @@ -0,0 +1,29 @@ +// +// PhysicsComponent.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation + +class PhysicsComponent: Component { + var mass: CGFloat + var velocity: CGVector + var force: CGVector + var collisionMask: UInt32 + var affectedByGravity: Bool + + init(id: UUID, entityId: UUID, mass: CGFloat, velocity: CGVector, force: CGVector, collisionMask: UInt32, affectedByGravity: Bool) { + self.mass = mass + self.velocity = velocity + self.force = force + self.collisionMask = collisionMask + self.affectedByGravity = affectedByGravity + super.init(id: id, entityId: entityId) + } + + convenience init(entityId: UUID, mass: CGFloat, velocity: CGVector, force: CGVector, collisionMask: UInt32, affectedByGravity: Bool) { + self.init(id: UUID(), entityId: entityId, mass: mass, velocity: velocity, force: force, collisionMask: collisionMask, affectedByGravity: affectedByGravity) + } +} diff --git a/star-dash/star-dash/Entities/Components/PositionComponent.swift b/star-dash/star-dash/Entities/Components/PositionComponent.swift new file mode 100644 index 00000000..2404ec7d --- /dev/null +++ b/star-dash/star-dash/Entities/Components/PositionComponent.swift @@ -0,0 +1,33 @@ +// +// PositionComponent.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation + + +class PositionComponent: Component { + var position: CGPoint + var rotation: Float + + init(id: UUID, entityId: UUID, position: CGPoint, rotation: Float) { + self.position = position + self.rotation = rotation + super.init(id: id, entityId: entityId) + } + + convenience init(entityId: UUID, position: CGPoint, rotation: Float) { + self.init(id: UUID(), entityId: entityId, position: position, rotation: rotation) + } + + func setPosition(position: CGPoint) { + self.position = position + } + + func setRotation(rotation: Float) { + self.rotation = rotation + } + +} diff --git a/star-dash/star-dash/Entities/Components/SpriteComponent.swift b/star-dash/star-dash/Entities/Components/SpriteComponent.swift new file mode 100644 index 00000000..aab3cded --- /dev/null +++ b/star-dash/star-dash/Entities/Components/SpriteComponent.swift @@ -0,0 +1,24 @@ +// +// SpriteComponent.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation +class SpriteComponent: Component { + // sprite could be single image or sprite set, could use enum to seperate, for sprite set will need to discuss how to rep animation + var image: String + var size: CGSize + + init(id: UUID, entityId: UUID, image: String, size: CGSize) { + self.image = image + self.size = size + super.init(id: id, entityId: entityId) + } + + convenience init(entityId: UUID, image: String, size: CGSize) { + self.init(id: UUID(), entityId: entityId, image: image, size: size) + } + +} diff --git a/star-dash/star-dash/Entities/Entity.swift b/star-dash/star-dash/Entities/Entity.swift new file mode 100644 index 00000000..ab6f6051 --- /dev/null +++ b/star-dash/star-dash/Entities/Entity.swift @@ -0,0 +1,15 @@ +// +// Entity.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation +typealias EntityId = UUID + +protocol Entity { + var id: UUID {get} + + func setUpAndAdd(to: EntityManager) +} diff --git a/star-dash/star-dash/Entities/EntityManager.swift b/star-dash/star-dash/Entities/EntityManager.swift new file mode 100644 index 00000000..60d8978a --- /dev/null +++ b/star-dash/star-dash/Entities/EntityManager.swift @@ -0,0 +1,38 @@ +// +// EntityManager.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation +typealias ComponentSet = Set +class EntityManager { + var componentMap: [ComponentId: Component] + var entityMap: [EntityId: Entity] + var entityComponentMap: [EntityId: ComponentSet] + init(componentMap: [ComponentId : Component], entityMap: [EntityId : Entity], entityComponentMap: [EntityId: ComponentSet]) { + self.componentMap = componentMap + self.entityMap = entityMap + self.entityComponentMap = entityComponentMap + } + + convenience init() { + self.init(componentMap: .init(), entityMap: .init(), entityComponentMap: .init()) + } + + func add(component: Component) { + guard self.componentMap[component.id] != nil else { + return + } + self.componentMap[component.id] = component + self.entityComponentMap[component.entityId]?.insert(component.id) + } + + func add(entity: Entity) { + guard self.entityMap[entity.id] != nil else { + return + } + self.entityMap[entity.id] = entity + } +} diff --git a/star-dash/star-dash/Entities/Player.swift b/star-dash/star-dash/Entities/Player.swift new file mode 100644 index 00000000..a1a207f3 --- /dev/null +++ b/star-dash/star-dash/Entities/Player.swift @@ -0,0 +1,36 @@ +// +// Player.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation +class Player: Entity { + let id: UUID + private let position: CGPoint + private let playerSprite: PlayerSprite + init(id: UUID, position: CGPoint, playerSprite: PlayerSprite) { + self.id = id + self.position = position + self.playerSprite = playerSprite + } + + convenience init(position: CGPoint, playerSprite: PlayerSprite) { + self.init(id: UUID(), position: position, playerSprite: playerSprite) + } + + + func setUpAndAdd(to: EntityManager) { + let positionComponent = PositionComponent(entityId: self.id, position: self.position, rotation: .zero) + let healthComponent = HealthComponent(entityId: self.id, health: 100) + let physicsComponent = PhysicsComponent(entityId: self.id, mass: .zero, velocity: .zero, force: .zero, collisionMask: .zero, affectedByGravity: true) + + let spriteComponent = SpriteComponent(entityId: self.id, image: "", size: .zero) + + to.add(entity: self) + to.add(component: positionComponent) + to.add(component: healthComponent) + to.add(component: physicsComponent) + } +} diff --git a/star-dash/star-dash/Enums/PlayerSprite.swift b/star-dash/star-dash/Enums/PlayerSprite.swift new file mode 100644 index 00000000..f43a8db9 --- /dev/null +++ b/star-dash/star-dash/Enums/PlayerSprite.swift @@ -0,0 +1,11 @@ +// +// PlayerSprite.swift +// star-dash +// +// Created by Lau Rui han on 12/3/24. +// + +import Foundation +enum PlayerSprite: String { + case RedNose = "red-nose" +}