123 lines
2.4 KiB
C++
123 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include <scene/node.h>
|
|
#include <types/ptr/intrusive_ptr.h>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace extra2d {
|
|
|
|
// 前向声明
|
|
class CameraComponent;
|
|
|
|
/**
|
|
* @brief 场景类
|
|
*
|
|
* 场景是游戏内容的容器,管理所有根节点和相机
|
|
*/
|
|
class Scene : public RefCounted {
|
|
public:
|
|
/**
|
|
* @brief 构造函数
|
|
*/
|
|
Scene();
|
|
|
|
/**
|
|
* @brief 析构函数
|
|
*/
|
|
~Scene() override;
|
|
|
|
// 禁止拷贝
|
|
Scene(const Scene&) = delete;
|
|
Scene& operator=(const Scene&) = delete;
|
|
|
|
// ========================================
|
|
// 节点管理
|
|
// ========================================
|
|
|
|
/**
|
|
* @brief 添加子节点
|
|
* @param node 节点
|
|
*/
|
|
void addChild(Ptr<Node> node);
|
|
|
|
/**
|
|
* @brief 移除子节点
|
|
* @param node 节点
|
|
*/
|
|
void removeChild(Node* node);
|
|
|
|
/**
|
|
* @brief 移除所有子节点
|
|
*/
|
|
void removeAllChildren();
|
|
|
|
/**
|
|
* @brief 获取所有子节点
|
|
* @return 子节点列表
|
|
*/
|
|
const std::vector<Ptr<Node>>& getChildren() const { return rootNodes_; }
|
|
|
|
/**
|
|
* @brief 根据名称查找节点
|
|
* @param name 节点名称
|
|
* @return 节点指针,未找到返回 nullptr
|
|
*/
|
|
Node* findNode(const std::string& name);
|
|
|
|
/**
|
|
* @brief 根据标签查找节点
|
|
* @param tag 节点标签
|
|
* @return 节点指针,未找到返回 nullptr
|
|
*/
|
|
Node* findNodeByTag(int32 tag);
|
|
|
|
// ========================================
|
|
// 相机管理
|
|
// ========================================
|
|
|
|
/**
|
|
* @brief 设置主相机
|
|
* @param camera 相机组件
|
|
*/
|
|
void setMainCamera(CameraComponent* camera);
|
|
|
|
/**
|
|
* @brief 获取主相机
|
|
* @return 相机组件指针
|
|
*/
|
|
CameraComponent* getMainCamera() const { return mainCamera_; }
|
|
|
|
// ========================================
|
|
// 生命周期
|
|
// ========================================
|
|
|
|
/**
|
|
* @brief 场景进入时调用
|
|
*/
|
|
virtual void onEnter();
|
|
|
|
/**
|
|
* @brief 场景退出时调用
|
|
*/
|
|
virtual void onExit();
|
|
|
|
/**
|
|
* @brief 每帧更新
|
|
* @param dt 帧间隔时间
|
|
*/
|
|
virtual void update(float dt);
|
|
|
|
/**
|
|
* @brief 渲染场景
|
|
*/
|
|
virtual void render();
|
|
|
|
protected:
|
|
std::vector<Ptr<Node>> rootNodes_;
|
|
CameraComponent* mainCamera_ = nullptr;
|
|
bool entered_ = false;
|
|
};
|
|
|
|
} // namespace extra2d
|