~vladh/peony

6f7df3f32f87faa9acf097236caa9a4f674ee3ae — Vlad-Stefan Harbuz 1 year, 7 months ago 49a30c6
refactor behavior state
7 files changed, 34 insertions(+), 38 deletions(-)

M src/behavior.cpp
M src/behavior.hpp
M src/core.cpp
M src/debug_ui.cpp
M src/engine.cpp
M src/engine.hpp
M src/models.cpp
M src/behavior.cpp => src/behavior.cpp +20 -4
@@ 57,10 57,9 @@ behavior::is_behavior_component_valid(behavior::Component *behavior_component)


void
behavior::update_behavior_components(
    behavior::ComponentSet *component_set
) {
    each (behavior_component, component_set->components) {
behavior::update_behavior_components()
{
    each (behavior_component, *get_components()) {
        if (!is_behavior_component_valid(behavior_component)) {
            continue;
        }


@@ 75,11 74,28 @@ behavior::update_behavior_components(
}


Array<behavior::Component> *
behavior::get_components()
{
    return &behavior::state->components;
}


behavior::Component *
behavior::get_component(entities::Handle entity_handle)
{
    return behavior::state->components[entity_handle];
}


void behavior::init(
    behavior::State *behavior_state,
    memory::Pool *asset_memory_pool,
    ::State *state
) {
    behavior::state = behavior_state;
    // NOTE: behavior needs the global state to pass to the behavior functions
    behavior::state->state = state;
    behavior::state->components = Array<behavior::Component>(
        asset_memory_pool, MAX_N_ENTITIES, "behavior_components", true, 1);
}

M src/behavior.hpp => src/behavior.hpp +5 -5
@@ 24,11 24,8 @@ public:
        Behavior behavior = Behavior::none;
    };

    struct ComponentSet {
        Array<Component> components;
    };

    struct State {
        Array<Component> components;
        ::State *state;
    };



@@ 39,9 36,12 @@ public:
    static char const * behavior_to_string(Behavior behavior);
    static Behavior behavior_from_string(const char *str);
    static bool is_behavior_component_valid(Component *behavior_component);
    static void update_behavior_components(ComponentSet *component_set);
    static void update_behavior_components();
    static Array<behavior::Component> * get_components();
    static behavior::Component * get_component(entities::Handle entity_handle);
    static void init(
        behavior::State *behavior_state,
        memory::Pool *asset_memory_pool,
        ::State *state
    );


M src/core.cpp => src/core.cpp +5 -4
@@ 133,6 133,11 @@ core::init_state(State *state, memory::Pool *asset_memory_pool)
    spatial::init(&state->spatial_state, asset_memory_pool);
    drawable::init(&state->drawable_state, asset_memory_pool);
    lights::init(&state->lights_state, asset_memory_pool);
    behavior::init(
        &state->behavior_state,
        asset_memory_pool,
        // NOTE: behavior needs the global state to pass to the behavior functions
        state);
    engine::init(&state->engine_state, asset_memory_pool);
    mats::init(&state->materials_state, asset_memory_pool);
    input::init(&state->input_state, state->window);


@@ 149,10 154,6 @@ core::init_state(State *state, memory::Pool *asset_memory_pool)
    tasks::init(&state->tasks_state, asset_memory_pool);
    anim::init(&state->anim_state, asset_memory_pool);
    cameras::init(&state->cameras_state, state->window_size.width, state->window_size.height);
    behavior::init(
        &state->behavior_state,
        // NOTE: behavior needs the global state to pass to the behavior functions
        state);

    return true;
}

M src/debug_ui.cpp => src/debug_ui.cpp +1 -1
@@ 148,7 148,7 @@ debug_ui::get_entity_text_representation(char *text, entities::Entity *entity, u
    bool has_light_component = lights::is_light_component_valid(
        lights::get_component(handle));
    bool has_behavior_component = behavior::is_behavior_component_valid(
        engine_state->behavior_component_set.components[handle]);
        behavior::get_component(handle));
    bool has_animation_component = anim::is_animation_component_valid(
        engine_state->animation_component_set.components[handle]);


M src/engine.cpp => src/engine.cpp +2 -20
@@ 64,13 64,6 @@ engine::get_entities()
}


Array<behavior::Component> *
engine::get_behavior_components()
{
    return &engine::state->behavior_component_set.components;
}


Array<anim::Component> *
engine::get_animation_components()
{


@@ 92,13 85,6 @@ engine::get_entity(entities::Handle entity_handle)
}


behavior::Component *
engine::get_behavior_component(entities::Handle entity_handle)
{
    return engine::state->behavior_component_set.components[entity_handle];
}


anim::Component *
engine::get_animation_component(entities::Handle entity_handle)
{


@@ 186,10 172,6 @@ void engine::init(engine::State *engine_state, memory::Pool *asset_memory_pool) 
        .entities = Array<entities::Entity>(
            asset_memory_pool, MAX_N_ENTITIES, "entities", true, 1)
    };
    engine::state->behavior_component_set = {
        .components = Array<behavior::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "behavior_components", true, 1)
    };
    engine::state->animation_component_set = {
        .components = Array<anim::Component>(
            asset_memory_pool, MAX_N_ENTITIES, "animation_components", true, 1)


@@ 231,7 213,7 @@ engine::destroy_non_internal_entities()
        engine::state->entity_set.first_non_internal_handle);
    drawable::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    engine::state->behavior_component_set.components.delete_elements_after_index(
    behavior::get_components()->delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);
    engine::state->animation_component_set.components.delete_elements_after_index(
        engine::state->entity_set.first_non_internal_handle);


@@ 578,7 560,7 @@ engine::update()

    lights::update_light_components(cameras::get_main()->position);

    behavior::update_behavior_components(&engine::state->behavior_component_set);
    behavior::update_behavior_components();

    anim::update_animation_components(&engine::state->animation_component_set);


M src/engine.hpp => src/engine.hpp +0 -3
@@ 58,7 58,6 @@ public:
        Array<models::ModelLoader> model_loaders;
        models::EntityLoaderSet entity_loader_set;
        entities::Set entity_set;
        behavior::ComponentSet behavior_component_set;
        anim::ComponentSet animation_component_set;
        physics::ComponentSet physics_component_set;
    };


@@ 69,11 68,9 @@ public:
    static void mark_first_non_internal_handle();
    static entities::Set * get_entity_set();
    static Array<entities::Entity> * get_entities();
    static Array<behavior::Component> * get_behavior_components();
    static Array<anim::Component> * get_animation_components();
    static Array<physics::Component> * get_physics_components();
    static entities::Entity * get_entity(entities::Handle entity_handle);
    static behavior::Component * get_behavior_component(entities::Handle entity_handle);
    static anim::Component * get_animation_component(entities::Handle entity_handle);
    static physics::Component * get_physics_component(entities::Handle entity_handle);
    static f64 get_t();

M src/models.cpp => src/models.cpp +1 -1
@@ 90,7 90,7 @@ models::prepare_entity_loader_and_check_if_done(
        *light_component = entity_loader->light_component;
        light_component->entity_handle = entity_loader->entity_handle;

        behavior::Component *behavior_component = engine::get_behavior_component(entity_loader->entity_handle);
        behavior::Component *behavior_component = behavior::get_component(entity_loader->entity_handle);
        *behavior_component = entity_loader->behavior_component;
        behavior_component->entity_handle = entity_loader->entity_handle;