Clearer accessor naming on ScriptBehavior (Get -> GetComponent, TryGet -> TryGetComponent), WASDMove follows. Teleport marked const (JPH Ref does not propagate constness). Spinner: auto-rotation disabled for the moment, registration switched from the registration macro to an explicit lambda to verify the mechanism.
48 lines
1.4 KiB
C++
48 lines
1.4 KiB
C++
//
|
||
// Spinner.cpp:C++ 原生脚本示范——Y 轴匀速自转。
|
||
//
|
||
// 写脚本三步:
|
||
// 1. 新建 .cpp 放本目录(CMake 自动进构建)
|
||
// 2. 继承 ScriptBehavior,覆写 Name/OnCreate/OnUpdate/OnDestroy
|
||
// 3. 文件级 ENGINE_REGISTER_SCRIPT(类名) 自注册——Inspector 下拉
|
||
// 与场景序列化按该名解析
|
||
//
|
||
|
||
#include <Engine/Scripting/Script.h>
|
||
|
||
#include <glm/gtc/quaternion.hpp>
|
||
|
||
namespace {
|
||
|
||
class Spinner final : public Engine::Scripting::ScriptBehavior {
|
||
public:
|
||
const char* Name() const override { return "Spinner"; }
|
||
|
||
void OnCreate() override {
|
||
// 实体与组件已就位;需要别的组件时用 Get<T>()(不存在会炸)
|
||
}
|
||
|
||
void OnUpdate(float dt) override {
|
||
// 四元数复合绕 Y(避免每帧欧拉↔四元数往返累积失真)
|
||
|
||
/*
|
||
const glm::quat step = glm::angleAxis(
|
||
glm::radians(m_Speed * dt), glm::vec3(0.0f, 1.0f, 0.0f));
|
||
Transform().SetRotationQuat(step * Transform().GetRotationQuat());
|
||
*/
|
||
}
|
||
|
||
private:
|
||
float m_Speed = 90.0f; // 度/秒
|
||
};
|
||
|
||
static const bool s_Spinner_registered = [] {
|
||
::Engine::Scripting::ScriptRegistry::Register(
|
||
"Spinner", []() -> ::Engine::Scope<::Engine::Scripting::ScriptBehavior> {
|
||
return ::Engine::CreateScope<Spinner>();
|
||
});
|
||
return true;
|
||
}();
|
||
|
||
} // namespace
|