Giter Club home page Giter Club logo

mix's Introduction

Mix

A minimal entity-component-system.

Features

  • entity–component–system implementation
  • tags and groups
  • rudimentary event handling

Install

Include Mix folder in your project.

The Basics

0. include Mix
#include "Mix/World.h"
1. define components
// note: must have default constructor

struct PositionComponent
{
    PositionComponent(int x = 0, int y = 0) : x(x), y(y) {}
    int x, y;
};

struct VelocityComponent
{
    VelocityComponent(int dx = 0, int dy = 0) : dx(dx), dy(dy) {}
    int dx, dy;
};
2. define systems
class MoveSystem : public Mix::System
{
public:
    MoveSystem()
    {
        // which components an entity must have for the system to be interested
        requireComponent<PositionComponent>();
        requireComponent<VelocityComponent>();
    }

    void update()
    {
        // do stuff with all the entities of interest
        for (auto e : getEntities()) {
            auto &position = e.getComponent<PositionComponent>();
            const auto &velocity = e.getComponent<VelocityComponent>();
            position.x += velocity.x;
            position.y += velocity.y;
        }
    }
};
3. create the world and add systems
Mix::World world;
world.getSystemManager().addSystem<MoveSystem>();
4. create entities
auto e = world.createEntity();
e.addComponent<PositionComponent>(100, 100);
e.addComponent<VelocityComponent>(10, 10);
// entity is created on the next call to world.update(),
// so that no entities appear only for some systems mid-frame
5. kill entities
// inside system's update method
entity.kill();
if (!entity.isAlive()) { ... };
// entity is alive until next call to world.update(),
// so that every system gets the chance handle the entity
6. update world and systems in game loop
while (!done) {
    world.update(); // actually creates and kills entities since the last call to this method
    world.getSystemManager().getSystem<MoveSystem>().update();
}

Tags & Groups

1. tags
auto player = world.createEntity();
player.tag("player");
if (player.hasTag("player")) { ... };
auto entity = world.getEntity("player");
2. groups
auto enemy = world.createEntity();
enemy.group("enemies");
if (enemy.hasGroup("enemies")) { ... };
auto enemies = world.getEntityGroup("enemies");

Events

Events are a way for inter-system communication (e.g. CollisionSystem emits collision event which DamageSystem is interested in).

1. define events
// note: must have default constructor

struct CollisionEvent
{
    PositionComponent(Entity a = Entity(), Entity b = Entity()) : a(a), b(b) {}
    Entity a, b;
};
2. emit events
// inside system's update method
Entity player, enemy;
getWorld().getEventManager().emitEvent<CollisionEvent>(player, enemy);
3. receive events
// inside other system's update method
auto collisionEvents = getWorld().getEventManager().getEvents<CollisionEvent>();
for (auto event : collisionEvents) { // handle collision };

// events can be received by any system
// events exist until the next call to world.update()

What else?

Code is fairly well documented. Read and experiment! :)

License

MIT (c) Pär Arvidsson 2014-2018

mix's People

Contributors

arvidsson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mix's Issues

Maybe think of some way to get components through base component type

struct PhysicsBodyComponent
{
// some general data
};
struct RigidBodyComponent : public PhysicsBodyComponent
{
// some specific data
};
struct SoftBodyComponent : public PhysicsBodyComponent
{
// some specific data
};
auto e = world.CreateEntity();
e.AddComponent();

// somewhere else maybe a system's requirements
RequireComponent();
auto& component = e.GetComponent();

The component type Id's are not consistent throughout different executions

execution 1:
auto e = world.CreateEntity();
e.AddComponent(100, 100); - ID = 0
e.AddComponent(10, 10); - ID = 1

execution 2:
auto e = world.CreateEntity();
e.AddComponent(10, 10); - ID = 0 // different from the last time
e.AddComponent(100, 100); - ID = 1 // different from the last time

This may cause problems if something like serialization of the entities and pools takes place. Though it can be worked around.

You can map the type_index to the runtime generated ones, but the type_index hash function returns a size_t type which is not consistent on 32 bit and 64 bit architectures.
My suggestion is some kind of consistent uint32_t hash on the component's type name but that would require some dependency on a rtti/reflection library. Your call :). Though a really nice library :)

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.