Simple Entity Component System in Swift
MothECS: Simple Entity Component System in Swift ?
MothECS is a simple entity component system written in Swift. It supports the following features:
- Use bitmask to manage relationship between entities and components
- Support view operation with optional excepted type
- Reuse destroyed entity in next creation
Future Development:
- Get entity ID from a component
- Support more components (currently it supports 32)
- Use sparse array
Moth in MothECS – It refers to new players in Sky. Explanation is here!
? Install
You package file would be like:
let package = Package(
name: "YourPackageName",
dependencies: [
.package(url: "https://github.com/forkercat/MothECS.git", .branch("main")),
],
targets: [
// For Swift 5.5, use .executableTarget
.target(
name: "YourPackageName",
dependencies: [
.product(name: "MothECS", package: "MothECS")
]),
]
)
? Usage
Import & Initialize
import MothECS
let moth = Moth()
moth.log() // use this for showing bitmasks
Define Your Own Component Classes
class TagComponent: MothComponent {
required init() { }
}
class TransformComponent: MothComponent {
required init() { }
}
class LightComponent: MothComponent {
required init() { }
}
Initialize & Create Entity
let e0: MothEntityID = moth.createEntity()
let e1: MothEntityID = moth.createEntity()
let e2: MothEntityID = moth.createEntity()
let e4: MothEntityID = moth.createEntity()
assert(e0 != .invalid)
_ = moth.entityIDs // return all valid entity IDs
Create & Assign Comonent
moth.createComponent(TagComponent.self, to: e0)
moth.createComponent(TransformComponent.self, to: e0)
moth.createComponent(LightComponent.self, to: e0) // e0: [Tag, Transform, Light]
moth.assignComponent(TagComponent(), to: e1)
moth.assignComponent(TransformComponent(), to: e1) // e1: [Tag, Transform]
moth.assignComponent(LightComponent(), to: e2)
moth.assignComponent(TagComponent(), to: e2) // e2: [Tag, Light]
Remove Entity & Component
moth.removeAllComponents(from: e4) // e4: []
moth.removeEntity(entityID: e4) // e4 is not invalid, its ID will be reused in next createEntity()
moth.removeComponent(TagComponent.self, from: e2) // e2: [Light]
Get & Has Component
if moth.hasComponent(LightComponent.self, in: e2) {
_ = moth.getComponent(LightComponent.self, from: e2) // put getComponent() inside hasComponent()
}
View Operation
let v1 = moth.view(TagComponent.self, TransformComponent.self, LightComponent.self)
let v2 = moth.view(TagComponent.self, TransformComponent.self)
let v3 = moth.view(TagComponent.self, TransformComponent.self, excepts: LightComponent.self)
// v1: [e0]
// v2: [e0, e1]
// v2: [e1]
? Reference
- How to make a simple entity-component-system in C++ by David Colson
- fireblade-engine/ecs by @ctreffs