完成动画组件与动画载入

This commit is contained in:
Ziyu
2025-06-16 14:44:47 +08:00
parent fc8a8cec5c
commit a31890fc20
20 changed files with 477 additions and 5 deletions
+46
View File
@@ -7,6 +7,7 @@
#include "../../engine/component/physics_component.h"
#include "../../engine/component/collider_component.h"
#include "../../engine/component/tilelayer_component.h"
#include "../../engine/component/animation_component.h"
#include "../../engine/physics/physics_engine.h"
#include "../../engine/scene/level_loader.h"
#include "../../engine/input/input_manager.h"
@@ -39,6 +40,11 @@ void GameScene::init() {
context_.getInputManager().setShouldQuit(true);
return;
}
if (!initEnemyAndItem()) {
spdlog::error("敌人和道具初始化失败,无法继续。");
context_.getInputManager().setShouldQuit(true);
return;
}
Scene::init();
spdlog::trace("GameScene 初始化完成。");
@@ -121,4 +127,44 @@ bool GameScene::initPlayer()
return true;
}
bool GameScene::initEnemyAndItem()
{
bool success = true;
for (auto& game_object : game_objects_){
if (game_object->getName() == "eagle"){
if (auto* ac = game_object->getComponent<engine::component::AnimationComponent>(); ac){
ac->playAnimation("fly");
} else {
spdlog::error("Eagle对象缺少 AnimationComponent,无法播放动画。");
success = false;
}
}
if (game_object->getName() == "frog"){
if (auto* ac = game_object->getComponent<engine::component::AnimationComponent>(); ac){
ac->playAnimation("idle");
} else {
spdlog::error("Frog对象缺少 AnimationComponent,无法播放动画。");
success = false;
}
}
if (game_object->getName() == "opossum"){
if (auto* ac = game_object->getComponent<engine::component::AnimationComponent>(); ac){
ac->playAnimation("walk");
} else {
spdlog::error("Opossum对象缺少 AnimationComponent,无法播放动画。");
success = false;
}
}
if (game_object->getTag() == "item"){
if (auto* ac = game_object->getComponent<engine::component::AnimationComponent>(); ac){
ac->playAnimation("idle");
} else {
spdlog::error("Item对象缺少 AnimationComponent,无法播放动画。");
success = false;
}
}
}
return success;
}
} // namespace game::scene