chore(scripts): rename script API to GetComponent/TryGetComponent; misc

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.
This commit is contained in:
2026-10-04 00:33:47 +08:00
parent 134812dae3
commit 74a2044441
5 changed files with 15 additions and 6 deletions
+10 -1
View File
@@ -24,15 +24,24 @@ namespace {
void OnUpdate(float dt) override { void OnUpdate(float dt) override {
// 四元数复合绕 Y(避免每帧欧拉↔四元数往返累积失真) // 四元数复合绕 Y(避免每帧欧拉↔四元数往返累积失真)
/*
const glm::quat step = glm::angleAxis( const glm::quat step = glm::angleAxis(
glm::radians(m_Speed * dt), glm::vec3(0.0f, 1.0f, 0.0f)); glm::radians(m_Speed * dt), glm::vec3(0.0f, 1.0f, 0.0f));
Transform().SetRotationQuat(step * Transform().GetRotationQuat()); Transform().SetRotationQuat(step * Transform().GetRotationQuat());
*/
} }
private: private:
float m_Speed = 90.0f; // 度/秒 float m_Speed = 90.0f; // 度/秒
}; };
ENGINE_REGISTER_SCRIPT(Spinner) static const bool s_Spinner_registered = [] {
::Engine::Scripting::ScriptRegistry::Register(
"Spinner", []() -> ::Engine::Scope<::Engine::Scripting::ScriptBehavior> {
return ::Engine::CreateScope<Spinner>();
});
return true;
}();
} // namespace } // namespace
+1 -1
View File
@@ -49,7 +49,7 @@ namespace {
} }
const glm::vec3 vel = move * m_Speed; const glm::vec3 vel = move * m_Speed;
if (auto* rb = TryGet<Engine::ECS::RigidBodyComponent>(); if (auto* rb = TryGetComponent<Engine::ECS::RigidBodyComponent>();
rb != nullptr && rb->bodyValid && rb != nullptr && rb->bodyValid &&
rb->type == Engine::ECS::RigidBodyComponent::BodyType::Dynamic) { rb->type == Engine::ECS::RigidBodyComponent::BodyType::Dynamic) {
// 物理驱动:只覆写水平速度,Y 保留物理积分离散结果 // 物理驱动:只覆写水平速度,Y 保留物理积分离散结果
@@ -55,7 +55,7 @@ namespace Engine {
void Jump(f32 speed); // 垂直速度置 speed(着地方向才有效) void Jump(f32 speed); // 垂直速度置 speed(着地方向才有效)
bool IsGrounded() const; bool IsGrounded() const;
glm::vec3 GetPosition() const; glm::vec3 GetPosition() const;
void Teleport(const glm::vec3& position); // 编辑器拖动 void Teleport(const glm::vec3& position) const; // 编辑器拖动
const Params& GetParams() const { return m_Params; } const Params& GetParams() const { return m_Params; }
+2 -2
View File
@@ -51,12 +51,12 @@ namespace Engine {
} }
template<typename T> template<typename T>
T& Get() { T& GetComponent() {
return m_Scene->GetRegistry().get<T>(m_Entity); return m_Scene->GetRegistry().get<T>(m_Entity);
} }
template<typename T> template<typename T>
T* TryGet() { T* TryGetComponent() {
return m_Scene->GetRegistry().try_get<T>(m_Entity); return m_Scene->GetRegistry().try_get<T>(m_Entity);
} }
+1 -1
View File
@@ -84,7 +84,7 @@ namespace Engine {
static_cast<f32>(p.GetZ())}; static_cast<f32>(p.GetZ())};
} }
void CharacterController::Teleport(const glm::vec3 &position) { void CharacterController::Teleport(const glm::vec3 &position) const {
m_Character->SetPosition( m_Character->SetPosition(
JPH::RVec3(position.x, position.y, position.z)); JPH::RVec3(position.x, position.y, position.z));
} }