https://github.com/forkercat/mothecs
Simple Entity Component System in Swift 📦
https://github.com/forkercat/mothecs
component ecs engine entity entity-component-system game-dev game-development swift
Last synced: 19 days ago
JSON representation
Simple Entity Component System in Swift 📦
- Host: GitHub
- URL: https://github.com/forkercat/mothecs
- Owner: forkercat
- License: mit
- Created: 2022-01-13T05:51:49.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-13T09:05:58.000Z (about 4 years ago)
- Last Synced: 2026-01-01T08:23:03.468Z (2 months ago)
- Topics: component, ecs, engine, entity, entity-component-system, game-dev, game-development, swift
- Language: Swift
- Homepage:
- Size: 13.7 KB
- Stars: 17
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MothECS: Simple Entity Component System in Swift 📦
[](https://github.com/forkercat/MothECS/actions/workflows/ci-macos.yml)
[](https://github.com/forkercat/MothECS/actions/workflows/ci-linux.yml)
[](https://github.com/forkercat/MothECS/actions/workflows/ci-windows.yml)
[](LICENSE)
[](https://swiftpackageindex.com/forkercat/MothECS)
[](https://swiftpackageindex.com/forkercat/MothECS)
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](https://www.pinterest.com/pin/587649451369676935/) in MothECS - It refers to new players in [Sky](https://www.thatskygame.com/). Explanation is [here](https://www.reddit.com/r/SkyGame/comments/q03tj4/why_are_new_players_called_moths/)!
## 🔧 Install
You package file would be like:
```swift
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
```swift
import MothECS
let moth = Moth()
moth.log() // use this for showing bitmasks
```
#### Define Your Own Component Classes
```swift
class TagComponent: MothComponent {
required init() { }
}
class TransformComponent: MothComponent {
required init() { }
}
class LightComponent: MothComponent {
required init() { }
}
```
#### Initialize & Create Entity
```swift
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
```swift
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
```swift
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
```swift
if moth.hasComponent(LightComponent.self, in: e2) {
_ = moth.getComponent(LightComponent.self, from: e2) // put getComponent() inside hasComponent()
}
```
#### View Operation
```swift
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++](https://www.david-colson.com/2020/02/09/making-a-simple-ecs.html) by David Colson
- [fireblade-engine/ecs](https://github.com/fireblade-engine/ecs) by [@ctreffs](https://github.com/ctreffs)