aa
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include <game.hpp>
|
||||
#include <mesh.hpp>
|
||||
|
||||
namespace openceg::render {
|
||||
class InputMap;
|
||||
}
|
||||
|
||||
namespace openceg {
|
||||
|
||||
class Actor {
|
||||
private:
|
||||
size_t id;
|
||||
std::string nickName_;
|
||||
mutable std::mutex transform_mutex_;
|
||||
std::array<float, 2> position_{0.0F, 0.0F};
|
||||
std::array<float, 2> facing_{1.0F, 0.0F};
|
||||
std::shared_ptr<Mesh> mesh_;
|
||||
|
||||
float age_ = 0.0F;
|
||||
bool initialized_ = false;
|
||||
std::function<void(Actor&)> init_handler_;
|
||||
std::function<void(Actor&, float, const render::InputMap&)> 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> 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<void(Actor&)> handler) {
|
||||
init_handler_ = std::move(handler);
|
||||
}
|
||||
|
||||
void set_tick_handler(
|
||||
std::function<void(Actor&, float, const render::InputMap&)> 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<float, 2> position() const {
|
||||
std::lock_guard<std::mutex> lock(transform_mutex_);
|
||||
return position_;
|
||||
}
|
||||
|
||||
void set_position(std::array<float, 2> position) {
|
||||
std::lock_guard<std::mutex> lock(transform_mutex_);
|
||||
position_ = position;
|
||||
}
|
||||
|
||||
std::array<float, 2> facing() const {
|
||||
std::lock_guard<std::mutex> lock(transform_mutex_);
|
||||
return facing_;
|
||||
}
|
||||
|
||||
void set_facing(std::array<float, 2> facing) {
|
||||
std::lock_guard<std::mutex> lock(transform_mutex_);
|
||||
facing_ = facing;
|
||||
}
|
||||
|
||||
const std::shared_ptr<Mesh>& mesh() const { return mesh_; }
|
||||
void set_mesh(std::shared_ptr<Mesh> mesh) { mesh_ = std::move(mesh); }
|
||||
};
|
||||
|
||||
} // namespace openceg
|
||||
Reference in New Issue
Block a user