129 lines
2.3 KiB
C++
129 lines
2.3 KiB
C++
|
|
#include <scene/scene.h>
|
||
|
|
#include <scene/components/camera_component.h>
|
||
|
|
#include <queue>
|
||
|
|
|
||
|
|
namespace extra2d {
|
||
|
|
|
||
|
|
Scene::Scene() {
|
||
|
|
}
|
||
|
|
|
||
|
|
Scene::~Scene() {
|
||
|
|
if (entered_) {
|
||
|
|
onExit();
|
||
|
|
}
|
||
|
|
removeAllChildren();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::addChild(Ptr<Node> node) {
|
||
|
|
if (!node) return;
|
||
|
|
|
||
|
|
node->removeFromParent();
|
||
|
|
node->onEnter();
|
||
|
|
rootNodes_.push_back(node);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::removeChild(Node* node) {
|
||
|
|
if (!node) return;
|
||
|
|
|
||
|
|
for (auto it = rootNodes_.begin(); it != rootNodes_.end(); ++it) {
|
||
|
|
if (it->get() == node) {
|
||
|
|
node->onExit();
|
||
|
|
rootNodes_.erase(it);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::removeAllChildren() {
|
||
|
|
for (auto& node : rootNodes_) {
|
||
|
|
node->onExit();
|
||
|
|
}
|
||
|
|
rootNodes_.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
Node* Scene::findNode(const std::string& name) {
|
||
|
|
// 广度优先搜索
|
||
|
|
std::queue<Node*> queue;
|
||
|
|
|
||
|
|
for (auto& node : rootNodes_) {
|
||
|
|
queue.push(node.get());
|
||
|
|
}
|
||
|
|
|
||
|
|
while (!queue.empty()) {
|
||
|
|
Node* current = queue.front();
|
||
|
|
queue.pop();
|
||
|
|
|
||
|
|
if (current->getName() == name) {
|
||
|
|
return current;
|
||
|
|
}
|
||
|
|
|
||
|
|
for (auto& child : current->getChildren()) {
|
||
|
|
queue.push(child.get());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
Node* Scene::findNodeByTag(int32 tag) {
|
||
|
|
// 广度优先搜索
|
||
|
|
std::queue<Node*> queue;
|
||
|
|
|
||
|
|
for (auto& node : rootNodes_) {
|
||
|
|
queue.push(node.get());
|
||
|
|
}
|
||
|
|
|
||
|
|
while (!queue.empty()) {
|
||
|
|
Node* current = queue.front();
|
||
|
|
queue.pop();
|
||
|
|
|
||
|
|
if (current->getTag() == tag) {
|
||
|
|
return current;
|
||
|
|
}
|
||
|
|
|
||
|
|
for (auto& child : current->getChildren()) {
|
||
|
|
queue.push(child.get());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::setMainCamera(CameraComponent* camera) {
|
||
|
|
mainCamera_ = camera;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::onEnter() {
|
||
|
|
entered_ = true;
|
||
|
|
|
||
|
|
// 通知所有根节点
|
||
|
|
for (auto& node : rootNodes_) {
|
||
|
|
node->onEnter();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::onExit() {
|
||
|
|
entered_ = false;
|
||
|
|
|
||
|
|
// 通知所有根节点
|
||
|
|
for (auto& node : rootNodes_) {
|
||
|
|
node->onExit();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::update(float dt) {
|
||
|
|
// 更新所有根节点
|
||
|
|
for (auto& node : rootNodes_) {
|
||
|
|
node->update(dt);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Scene::render() {
|
||
|
|
// 渲染所有根节点
|
||
|
|
for (auto& node : rootNodes_) {
|
||
|
|
node->render();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace extra2d
|