Skip to content

Commit

Permalink
gamestate: Cache created activities.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Dec 24, 2023
1 parent bd566e2 commit 7549a9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions libopenage/gamestate/entity_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ void EntityFactory::init_activity(const std::shared_ptr<openage::event::EventLoo
const std::shared_ptr<GameEntity> &entity,
const nyan::Object &ability) {
nyan::Object graph = ability.get_object("Activity.graph");

// Check if the activity is already exists in the cache
if (this->activity_cache.contains(graph.get_name())) {
auto activity = this->activity_cache.at(graph.get_name());
auto component = std::make_shared<component::Activity>(loop, activity);
entity->add_component(component);

return;
}

auto start_obj = api::APIActivity::get_start(graph);

size_t node_id = 0;
Expand Down Expand Up @@ -337,6 +347,7 @@ void EntityFactory::init_activity(const std::shared_ptr<openage::event::EventLoo
}

auto activity = std::make_shared<activity::Activity>(0, start_node, graph.get_name());
this->activity_cache.insert({graph.get_name(), activity});

auto component = std::make_shared<component::Activity>(loop, activity);
entity->add_component(component);
Expand Down
10 changes: 10 additions & 0 deletions libopenage/gamestate/entity_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class RenderFactory;
}

namespace gamestate {

namespace activity {
class Activity;
} // namespace activity

class GameEntity;
class GameState;
class Player;
Expand Down Expand Up @@ -128,6 +133,11 @@ class EntityFactory {

// TODO: Cache created game entities.

/**
* Cache for activities.
*/
std::unordered_map<nyan::fqon_t, std::shared_ptr<activity::Activity>> activity_cache;

/**
* Mutex for thread safety.
*/
Expand Down

0 comments on commit 7549a9e

Please sign in to comment.