技术分析

为了兼顾游戏运行效率,增加一套cpp的脚本系统。

class CameraController : public ScriptableEntity
{
public:
    void OnCreate()
    {
        auto& translation = GetComponent<TransformComponent>().Translation;
        translation.x = rand() % 10 - 5.0f;
    }

    void OnDestroy()
    {
    }

    void OnUpdate(Timestep ts)
    {
        auto& translation = GetComponent<TransformComponent>().Translation;
        float speed = 5.0f;

        if (Input::IsKeyPressed(KeyCode::A))
            translation.x -= speed * ts;
        if (Input::IsKeyPressed(KeyCode::D))
            translation.x += speed * ts;
        if (Input::IsKeyPressed(KeyCode::W))
            translation.y += speed * ts;
        if (Input::IsKeyPressed(KeyCode::S))
            translation.y -= speed * ts;
    }
};

mCameraEntity.AddComponent<NativeScriptComponent>().Bind<CameraController>();
mSecondCamera.AddComponent<NativeScriptComponent>().Bind<CameraController>();

后续可以考虑使用反射的方式,把cpp脚本拖过去,自动Bind好对应的方法。

Last updated