66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
#include <actor.hpp>
|
|
|
|
namespace openceg::render {
|
|
class InputMap;
|
|
}
|
|
|
|
namespace openceg {
|
|
|
|
using ActorInitHandler = std::function<void(Actor&)>;
|
|
using ActorTickHandler = std::function<void(Actor&, float, const render::InputMap&)>;
|
|
|
|
// 全局行为函数注册表。config.ceg 中的 init/tick 名字在这里解析为 C++ 处理函数。
|
|
class BehaviorRegistry {
|
|
public:
|
|
static BehaviorRegistry& getInstance() {
|
|
static BehaviorRegistry instance;
|
|
return instance;
|
|
}
|
|
|
|
BehaviorRegistry(const BehaviorRegistry&) = delete;
|
|
BehaviorRegistry& operator=(const BehaviorRegistry&) = delete;
|
|
|
|
void register_init(const std::string& name, ActorInitHandler handler) {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
init_handlers_[name] = std::move(handler);
|
|
}
|
|
|
|
void register_tick(const std::string& name, ActorTickHandler handler) {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
tick_handlers_[name] = std::move(handler);
|
|
}
|
|
|
|
ActorInitHandler init(const std::string& name) const {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
const auto it = init_handlers_.find(name);
|
|
if (it == init_handlers_.end()) {
|
|
return {};
|
|
}
|
|
return it->second;
|
|
}
|
|
|
|
ActorTickHandler tick(const std::string& name) const {
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
const auto it = tick_handlers_.find(name);
|
|
if (it == tick_handlers_.end()) {
|
|
return {};
|
|
}
|
|
return it->second;
|
|
}
|
|
|
|
private:
|
|
BehaviorRegistry() = default;
|
|
mutable std::mutex mutex_;
|
|
std::unordered_map<std::string, ActorInitHandler> init_handlers_;
|
|
std::unordered_map<std::string, ActorTickHandler> tick_handlers_;
|
|
};
|
|
|
|
} // namespace openceg
|