跳转至

之前用lambda说是虚函数耗性能,但实际上虚函数也没有那么耗,所以就用虚函数来实现了

Component

struct NativeScriptComponent
    {
        ScriptableEntity* Instance = nullptr;
        ScriptableEntity* (*InstantiateScript)();
        void (*DestroyScript)(NativeScriptComponent*);

        template<typename T>
        void Bind()
        {
            InstantiateScript = []() { return static_cast<ScriptableEntity*>(new T()); };
            DestroyScript = [](NativeScriptComponent* nsc) { delete nsc->Instance; nsc->Instance = nullptr; };
        }
    };

ScruptableEntity

#pragma once
#include "Entity.h"
namespace CryDust {
    class ScriptableEntity
    {
    public:

        virtual ~ScriptableEntity(){}

        template<typename T>
        T& GetComponent()
        {
            return m_Entity.GetComponent<T>();
        }
    protected:
        virtual void OnCreate() {}
        virtual void OnDestroy() {}
        virtual void OnUpdate(Timestep ts) {}
    private:
        Entity m_Entity;
        friend class Scene;
    };
}