aboutsummaryrefslogtreecommitdiff
path: root/src/entity/Entity.cpp
blob: 57f6110248c37aa95195379391972f59e9695402 (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
59
60
61
62
#include "../../include/entity/Entity.hpp"
#include <assert.h>

namespace amalgine {
    EntityWorld::EntityWorld() {
        for(int i = 0; i < NUM_ENTITIES; ++i) {
            mask[i] = ENTITY_MASK_NONE;
            transformation[i] = glm::mat4(1.0f);
            velocity[i] = glm::vec3(0.0f, 0.0f, 0.0f);
            mesh_instance[i] = MeshInstance();
        }
    }

    Entity EntityWorld::create_entity(EntityMask type) {
        for(int i = 0; i < NUM_ENTITIES; ++i) {
            if(mask[i] == ENTITY_MASK_NONE) {
                mask[i] = type;
                return i;
            }
        }
        return INVALID_ENTITY;
    }

    void EntityWorld::remove_entity(Entity entity) {
        mask[entity] = ENTITY_MASK_NONE;
    }

    glm::mat4& EntityWorld::get_transformation(Entity entity) {
        assert(mask[entity] & ENTITY_MASK_TRANSFORMATION);
        return transformation[entity];
    }

    glm::vec3& EntityWorld::get_velocity(Entity entity) {
        assert(mask[entity] & ENTITY_MASK_VELOCITY);
        return velocity[entity];
    }

    MeshInstance& EntityWorld::get_mesh_instance(Entity entity) {
        assert(mask[entity] & ENTITY_MASK_MESH_COMPONENT);
        return mesh_instance[entity];
    }

    void EntityWorld::update() {
        const float gravity = -9.8f;
        for(int i = 0; i < NUM_ENTITIES; ++i) {
            velocity[i].z -= gravity;
        }

        const EntityMaskType transform_velocity_mask = ENTITY_MASK_TRANSFORMATION | ENTITY_MASK_VELOCITY;
        for(int i = 0; i < NUM_ENTITIES; ++i) {
            if((mask[i] & transform_velocity_mask) == transform_velocity_mask) {
                transformation[i][3].x += velocity[i].x;
                transformation[i][3].y += velocity[i].y;
                transformation[i][3].z += velocity[i].z;
            }
        }

        for(int i = 0; i < NUM_ENTITIES; ++i) {
            mesh_instance[i].update();
        }
    }
}