完成AI组件与AI行为

This commit is contained in:
Ziyu
2025-06-16 14:44:47 +08:00
parent 5e4f6c587f
commit 45367bed41
14 changed files with 504 additions and 25 deletions
+16 -15
View File
@@ -14,6 +14,10 @@
#include "../../engine/input/input_manager.h"
#include "../../engine/render/camera.h"
#include "../../engine/render/animation.h"
#include "../component/ai_component.h"
#include "../component/ai/patrol_behavior.h"
#include "../component/ai/updown_behavior.h"
#include "../component/ai/jump_behavior.h"
#include <spdlog/spdlog.h>
#include <SDL3/SDL_rect.h>
@@ -136,27 +140,24 @@ 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 (auto* ai_component = game_object->addComponent<game::component::AIComponent>(); ai_component){
auto y_max = game_object->getComponent<engine::component::TransformComponent>()->getPosition().y;
auto y_min = y_max - 80.0f; // 让鹰的飞行范围 (当前位置与上方80像素 的区域)
ai_component->setBehavior(std::make_unique<game::component::ai::UpDownBehavior>(y_min, y_max));
}
}
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 (auto* ai_component = game_object->addComponent<game::component::AIComponent>(); ai_component){
auto x_max = game_object->getComponent<engine::component::TransformComponent>()->getPosition().x - 10.0f;
auto x_min = x_max - 90.0f; // 青蛙跳跃范围(右侧 - 10.0f 是为了增加稳定性)
ai_component->setBehavior(std::make_unique<game::component::ai::JumpBehavior>(x_min, x_max));
}
}
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 (auto* ai_component = game_object->addComponent<game::component::AIComponent>(); ai_component){
auto x_max = game_object->getComponent<engine::component::TransformComponent>()->getPosition().x;
auto x_min = x_max - 200.0f; // 负鼠巡逻范围
ai_component->setBehavior(std::make_unique<game::component::ai::PatrolBehavior>(x_min, x_max));
}
}
if (game_object->getTag() == "item"){