#pragma once #include #include #include #include #include #include #include #include namespace openceg::render { class InputMap; } namespace openceg { class Actor { private: size_t id; std::string nickName_; mutable std::mutex transform_mutex_; std::array position_{0.0F, 0.0F}; std::array facing_{1.0F, 0.0F}; std::shared_ptr mesh_; float age_ = 0.0F; bool initialized_ = false; std::function init_handler_; std::function tick_handler_; public: Actor() noexcept = default; virtual ~Actor() = default; Actor(const Actor&) = delete; Actor& operator=(const Actor&) = delete; Actor(Actor&&) noexcept = delete; Actor& operator=(Actor&&) noexcept = delete; Actor(std::string nickName, Game& game) { id = game.new_id(); nickName_ = std::move(nickName); } Actor(std::string nickName, Game& game, std::shared_ptr mesh) : Actor(std::move(nickName), game) { mesh_ = std::move(mesh); } static Actor& getInstance() { static Actor instance; return instance; } const std::string& name() const { return nickName_; } float age() const { return age_; } void set_init_handler(std::function handler) { init_handler_ = std::move(handler); } void set_tick_handler( std::function handler) { tick_handler_ = std::move(handler); } // 类似 Godot 的 _ready()。 void initialize() { if (initialized_) { return; } initialized_ = true; if (init_handler_) { init_handler_(*this); } } // 类似 Godot 的 _process(delta)。 void process(float dt, const render::InputMap& input) { age_ += dt; if (tick_handler_) { tick_handler_(*this, dt, input); } } std::array position() const { std::lock_guard lock(transform_mutex_); return position_; } void set_position(std::array position) { std::lock_guard lock(transform_mutex_); position_ = position; } std::array facing() const { std::lock_guard lock(transform_mutex_); return facing_; } void set_facing(std::array facing) { std::lock_guard lock(transform_mutex_); facing_ = facing; } const std::shared_ptr& mesh() const { return mesh_; } void set_mesh(std::shared_ptr mesh) { mesh_ = std::move(mesh); } }; } // namespace openceg