fireblade-engine / ecs

Licence: MIT license
A dependency free, lightweight, fast Entity-Component System (ECS) implementation in Swift

Programming Languages

swift
15916 projects
Makefile
30231 projects

Projects that are alternatives of or similar to ecs

Entitas Csharp
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Stars: ✭ 5,393 (+6726.58%)
Mutual labels:  gamedev, ecs, entity-component-system, entity
Entitas Cpp
Entitas++ is a fast Entity Component System (ECS) C++11 port of Entitas C#
Stars: ✭ 229 (+189.87%)
Mutual labels:  gamedev, ecs, entity-component-system, entity
ecs-demo
Minimal demo App for the Fireblade Entity-Component System (ECS)
Stars: ✭ 20 (-74.68%)
Mutual labels:  swift-package-manager, spm, fireblade-engine
Godex
Godex is a Godot Engine ECS library.
Stars: ✭ 307 (+288.61%)
Mutual labels:  gamedev, ecs, entity-component-system
Entt
Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more
Stars: ✭ 6,017 (+7516.46%)
Mutual labels:  gamedev, ecs, entity-component-system
js13k-ecs
A 1kb entity component system, designed for Js13kGames
Stars: ✭ 76 (-3.8%)
Mutual labels:  gamedev, ecs, entity-component-system
Shipyard
Entity Component System focused on usability and speed.
Stars: ✭ 247 (+212.66%)
Mutual labels:  gamedev, ecs, entity-component-system
ECS-Phyllotaxis
Learning ECS - 100k Cubes in Phyllotaxis pattern
Stars: ✭ 17 (-78.48%)
Mutual labels:  ecs, entity-component-system, entity
Kengine
Entity-Component-System (ECS) with a focus on ease-of-use, runtime extensibility and compile-time type safety and clarity.
Stars: ✭ 417 (+427.85%)
Mutual labels:  ecs, entity-component-system, entity
Entitas-Redux
An entity-component framework for Unity with code generation and visual debugging
Stars: ✭ 84 (+6.33%)
Mutual labels:  ecs, entity-component-system, entity
Flecs
A fast entity component system (ECS) for C & C++
Stars: ✭ 2,201 (+2686.08%)
Mutual labels:  gamedev, ecs, entity-component-system
Uecs
Ubpa Entity-Component-System (U ECS) in Unity3D-style
Stars: ✭ 174 (+120.25%)
Mutual labels:  gamedev, ecs, entity-component-system
bitECS
Functional, minimal, data-oriented, ultra-high performance ECS library written in JavaScript
Stars: ✭ 372 (+370.89%)
Mutual labels:  gamedev, ecs, entitycomponentsystem
polymorph
A fast and frugal entity-component-system library with a focus on code generation and compile time optimisation.
Stars: ✭ 74 (-6.33%)
Mutual labels:  gamedev, ecs, entity-component-system
Defaultecs
Entity Component System framework aiming for syntax and usage simplicity with maximum performance for game development.
Stars: ✭ 286 (+262.03%)
Mutual labels:  gamedev, ecs, entity-component-system
Actors.unity
🚀Actors is a framework empowering developers to make better games faster on Unity.
Stars: ✭ 437 (+453.16%)
Mutual labels:  ecs, entity-component-system, entity
Entitas Sync Framework
Networking framework for Entitas ECS. Targeted at turnbased games or other slow-paced genres.
Stars: ✭ 98 (+24.05%)
Mutual labels:  gamedev, ecs, entity-component-system
Entityplus
A C++14 Entity Component System
Stars: ✭ 181 (+129.11%)
Mutual labels:  gamedev, entity-component-system, entity
SpaceWar-ECS
A space war game made with ECS and JobSystem in Unity.
Stars: ✭ 26 (-67.09%)
Mutual labels:  ecs, entity-component-system, entity
uecs
Micro ECS
Stars: ✭ 30 (-62.03%)
Mutual labels:  ecs, entity

Fireblade ECS (Entity-Component System)

license macOS Linux Windows WASM documentation
codecov spi-swift-versions spi-swift-platforms

This is a dependency free, lightweight, fast and easy to use Entity-Component System implementation in Swift. It is developed and maintained as part of the Fireblade Game Engine project.

See the Fireblade ECS Demo App or have a look at documentation in the wiki to get started.

🚀 Getting Started

These instructions will get you a copy of the project up and running on your local machine and provide a code example.

📋 Prerequisites

💻 Installing

Fireblade ECS is available for all platforms that support Swift 5.1 and higher and the Swift Package Manager (SPM).

Extend the following lines in your Package.swift file or use it to create a new project.

// swift-tools-version:5.1

import PackageDescription

let package = Package(
    name: "YourPackageName",
    dependencies: [
        .package(url: "https://github.com/fireblade-engine/ecs.git", from: "0.17.4")
    ],
    targets: [
        .target(
            name: "YourTargetName",
            dependencies: ["FirebladeECS"])
    ]
)

📝 Code Example

🏛️ Nexus

The core element in the Fireblade-ECS is the Nexus. It acts as a centralized way to store, access and manage entities and their components. A single Nexus may (theoretically) hold up to 4294967295 Entities at a time.
You may use more than one Nexus at a time.

Initialize a Nexus with

let nexus = Nexus()

👤 Entities

then create entities by letting the Nexus generate them.

// an entity without components
let newEntity = nexus.createEntity()

To define components, conform your class to the Component protocol

final class Position: Component {
	var x: Int = 0
	var y: Int = 0
}

and assign instances of it to an Entity with

let position = Position(x: 1, y: 2)
entity.assign(position)

You can be more efficient by assigning components while creating an entity.

// an entity with two components assigned.
nexus.createEntity {
	Position(x: 1, y: 2)
	Color(.red)
}

// bulk create entities with multiple components assigned.
nexus.createEntities(count: 100) { _ in
	Position()
	Color()
}

👪 Families

This ECS uses a grouping approach for entities with the same component types to optimize cache locality and ease up access to them.
Entities with the same component types may belong to one Family. A Family has entities as members and component types as family traits.

Create a family by calling .family with a set of traits on the nexus. A family that contains only entities with a Movement and PlayerInput component, but no Texture component is created by

let family = nexus.family(requiresAll: Movement.self, PlayerInput.self,
                          excludesAll: Texture.self)

These entities are cached in the nexus for efficient access and iteration. Families conform to the Sequence protocol so that members (components) may be iterated and accessed like any other sequence in Swift.
Access a family's components directly on the family instance. To get family entities and access components at the same time call family.entityAndComponents. If you are only interested in a family's entities call family.entities.

class PlayerMovementSystem {
	let family = nexus.family(requiresAll: Movement.self, PlayerInput.self,
                              excludesAll: Texture.self)

	func update() {
		family
			.forEach { (mov: Movement, input: PlayerInput) in
			
			// position & velocity component for the current entity
			
			// get properties
			_ = mov.position
			_ = mov.velocity
			
			// set properties
			mov.position.x = mov.position.x + 3.0
			...
			
			// current input command for the given entity
			_ = input.command
			...
			
		}
	}

	func update2() {
		family
			.entityAndComponents
			.forEach { (entity: Entity, mov: Movement, input: PlayerInput) in
			
			// the current entity instance
			_ = entity

			// position & velocity component for the current entity
			
			// get properties
			_ = mov.position
			_ = mov.velocity
			
			
		}
	}

	func update3() {
		family
			.entities
			.forEach { (entity: Entity) in
			
			// the current entity instance
			_ = entity
		}
	}
}

🧑 Singles

A Single on the other hand is a special kind of family that holds exactly one entity with exactly one component for the entire lifetime of the Nexus. This may come in handy if you have components that have a Singleton character. Single components must conform to the SingleComponent protocol and will not be available through regular family iteration.

final class GameState: SingleComponent {
    var quitGame: Bool = false
}
class GameLogicSystem {
    let gameState: Single<GameState>
    
    init(nexus: Nexus) {
        gameState = nexus.single(GameState.self)
    }
    
    func update() {
        // update your game sate here
        gameState.component.quitGame = true
        
        // entity access is provided as well
        _ = gameState.entity
    }
}

🔗 Serialization

To serialize/deserialize entities you must conform their assigned components to the Codable protocol.
Conforming components can then be serialized per family like this:

// MyComponent and YourComponent both conform to Component and Codable protocols.
let nexus = Nexus()
let family = nexus.family(requiresAll: MyComponent.self, YourComponent.self)

// JSON encode entities from given family.
var jsonEncoder = JSONEncoder()
let encodedData = try family.encodeMembers(using: &jsonEncoder)

// Decode entities into given family from JSON. 
// The decoded entities will be added to the nexus.
var jsonDecoder = JSONDecoder()
let newEntities = try family.decodeMembers(from: jsonData, using: &jsonDecoder)

🧪 Demo

See the Fireblade ECS Demo App to get started.

📖 Documentation

Consult the wiki for in-depth documentation.

💁 How to contribute

If you want to contribute please see the CONTRIBUTION GUIDE first.

To start your project contribution run these in your command line:

  1. git clone [email protected]:fireblade-engine/ecs.git fireblade-ecs
  2. cd fireblade-ecs
  3. make setupEnvironment

Before commiting code please ensure to run:

  • make precommit

This project is currently maintained by Christian Treffs.
See also the list of contributors who participated in this project.

🔏 License

This project is licensed under the MIT License - see the LICENSE file for details

🙏 Acknowledgments

Inspired by

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].