aboutsummaryrefslogtreecommitdiffstats
path: root/yage/entity/entity.cpp
blob: c20d15d64ed660a0a85e5d8753d66ccdc21e9995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "entity.h"

#include "component.h"

#include <algorithm>
#include <iostream>
#include <memory>

namespace yage
{

Entity EntityManager::create_entity()
{
    Entity entity = update_next_entity();
    component_masks_.push_back(ComponentMask(0));
    return entity;
}

EntityManager &EntityManager::delete_entity(Entity entity)
{
    deleted_.push_back(entity);
    return *this;
}

bool EntityManager::is_valid(Entity entity) const
{
    auto it = std::find(deleted_.begin(), deleted_.end(), entity);
    if (it == deleted_.end()) {
        return true;
    }
    return false;
}

EntityManager &
EntityManager::add_component(Entity entity,
                             std::unique_ptr<BaseComponent> &&component)
{
    auto id = component->getGroup();
    component_masks_[entity] =
        component_masks_[entity] | ComponentMask(1 << id);
    if (id+1 > component_group_.size()) {
        component_group_.resize(id+1);
    }
    component_group_[id].add(std::move(component));
    return *this;
}

Entity EntityManager::update_next_entity()
{
    if (deleted_.empty()) {
        return next_entity_++;
    }
    Entity ent = deleted_.back();
    deleted_.pop_back();
    return ent;
}

} // namespace yage