Code Monkey home page Code Monkey logo

ecs-application's Introduction

Build Status

ECS-Application

Similar to Unity's ECS in C++ for fun and science

Usage

Simple operations

// Define a structure for your data. Primitive types can also be used.
struct Health {
    float Value;
};

// Create an archetype.
EntityArchetype archetype = CreateArchetype<Health>();

// Create an entity manager.
EntityManager entityManager;

// Create an entity based off of the archetype.
Entity e = entityManager.CreateEntity(archetype);

// Set data for the entity.
Health h = { 5 };
entityManager.SetComponentData(e, h);
entityManager.SetComponentData<Health>(e, { 10 });

// Get data for the entity.
std::cout << entityManager.GetComponentData<Health>(e).Value;

// Check if entity still exists.
bool exists = entityManager.Exists(e);

// Check if it has a component.
bool has = entityManager.HasComponent<Health>(e);

// Add components at run time.
entityManager.AddComponent<float>(e);
entityManager.AddComponent<int>(e, 27);

// Remove components at run time.
entityManager.RemoveComponent<float>(e);

// Destroy the entity.
entityManager.DestroyEntity(e);

Iterating over entities

// Create an entity group.
EntityGroup group = entityManager.GetEntityGroup(
    ComponentType::Create<Health>(), // Matches archetypes with a Health component.
    ComponentType::Subtractive<double>() // Matches archetypes without a double component.
);

// Get an iterator.
auto iter = group.GetComponentArray<Health>(); // Iterate directly over the component.
auto entityIter = group.GetEntityArray(); // Iterate over the entities.

// Loop over the matching entities.
// Element i on both iterators represent the same entity.
for (int i = 0; i < iter.Length(); i++) {
    std::cout << "Entity " << i << " health: " << iter[i].Value << "\n";
    std::cout << "Entity " << i << " health: " << entityManager.GetComponentData<Health>(entityIter[i]).Value << "\n";
}

ecs-application's People

Contributors

glauber-guimaraes avatar

Stargazers

Albert Tavares de Almeida avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.