MothECS - Simple Entity Component System in Swift

Overview

MothECS: Simple Entity Component System in Swift πŸ“¦

macOS Linux Windows license

platform-compatibility swift-version-compatibility

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!

This is moth!

πŸ”§ 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

You might also like...
Folowed HackingWithSwift.com tutorial and created a simple flag guessing game.
Folowed HackingWithSwift.com tutorial and created a simple flag guessing game.

GuessTheFlag Getting more and more into SwiftUI. Created app as a simple flag guessing (knowing) game. Paul said once in a video... if you can do it b

Simple SwiftUI War Game

swiftui_war Simple SwiftUI War Game My implementation of: https://codewithchris.

Hitting only the bad PinGUYS! Simple SpriteKit game
Hitting only the bad PinGUYS! Simple SpriteKit game

Whack-a-Penguin Hitting only the bad PinGUYS! Simple SpriteKit game. You have to

IOS Spin Game - A simple spin game using SwiftUI
IOS Spin Game - A simple spin game using SwiftUI

IOS_Spin_Game A simple spin game using Swift UI.

Swift-WordleSolver - Solve and analyze Wordle games. Command-line tool written in Swift

Swift-WordleSolver - Solve and analyze Wordle games. Command-line tool written in Swift

Imagine Engine - a fast, high performance Swift 2D game engine for Apple's platforms
Imagine Engine - a fast, high performance Swift 2D game engine for Apple's platforms

Welcome to Imagine Engine, an ongoing project that aims to create a fast, high performance Swift 2D game engine for Apple's platforms that is also a j

Sage is a cross-platform chess library for Swift.
Sage is a cross-platform chess library for Swift.

Sage is not a chess engine; it's a move generator. Hexe, on the other hand, is able to both generate moves and evaluate them.

🐦 Flappy Bird reincarnation [Swift 5.3, GameplayKit, SpriteKit, iOS 12].
🐦 Flappy Bird reincarnation [Swift 5.3, GameplayKit, SpriteKit, iOS 12].

🐦 Flappy Bird reincarnation [Swift 5.3, GameplayKit, SpriteKit, iOS 12].

A game engine built with SDL and Swift.

Lark A game engine made with Swift and SDL. This is a pre-alpha work-in-progress. Don't try to use this unless you really know what you're doing. I ba

Owner
Junhao Wang
Protect the light in heart
Junhao Wang
A simple Chess game for iOS, written in Swift

Swift Chess This is a simple chess game for iPhone and iPad, designed for novice players. It features a very simple AI that plays much like a beginner

Nick Lockwood 135 Jan 6, 2023
Simple memory game written in Swift 4 using VIPER Architecture.

Viper Memory Game Simple memory game written in Swift 4.2 using VIPER Architecture. The Memory Game is a deck of cards where the user needs to find ma

Mati 23 Jun 6, 2022
it's simple swift find different colour game.

SWift_FindColourGame it's simple swift find different colour game. This is a sample project students or kids can use to learn how to find difference c

Jiten Engineer 0 Oct 20, 2021
A simple game running from the terminal

A simple game running from the terminal

Ilya Matsuev 2 Feb 8, 2022
This is a simple application simulating a basketball game.

This is a simple application simulating a basketball game.

NIKOLAY NIKITIN 0 Oct 20, 2022
A Simple iOS Game App - Designed in SwiftUI πŸš€

A Simple iOS Game App - Designed in SwiftUI ?? The game's aim is to make a cap to fill the color among them. On tapping any shapes, it will rotate 90

Mohammad Yasir 2 Jan 18, 2022
A simple dice roller made with pure SwiftUI.

Dice Roller A simple dice roller made with pure SwiftUI. Contributing Pull requests are welcome. For major changes, please open an issue first to disc

Adrien 0 Dec 13, 2021
it's simple kids game to find different colour.

SWift_FindColourGame it's simple swift find different colour game. This is a sample project students or kids can use to learn how to find difference c

Vnnovate Solutions Pvt Ltd 0 Oct 20, 2021
QuizButton is a simple buzzer app that is extremely useful when you play quiz games.

QuizButton is a simple buzzer app that is extremely useful when you play quiz games.

yuken 1 Dec 2, 2021
🎯 Bull's Eye is the simple game which is consist in guessing the random number (1 ... 100) by using a slider.

The very first project made in ?? Swift language called Bull's Eye. ?? Bull's Eye is the simple game which is consist in guessing the random number (1

Wiktor 0 Nov 14, 2021