feat(npc): 添加NPC数据加载、动画和对象功能

实现NPC系统的核心功能,包括:
1. 新增NpcDataLoader用于加载NPC索引和配置数据
2. 添加NpcAnimation处理NPC动画显示
3. 创建NpcObject实现NPC交互和显示逻辑
4. 在GameMapTestScene中集成测试NPC功能
This commit is contained in:
2026-04-07 23:02:03 +08:00
parent 6855860d64
commit caad22cca7
9 changed files with 628 additions and 7 deletions

View File

@@ -0,0 +1,25 @@
#pragma once
#include "npc/NpcDataLoader.h"
#include <frostbite2D/2d/actor.h>
#include <frostbite2D/animation/animation.h>
#include <string>
namespace frostbite2D {
class NpcAnimation : public Actor {
public:
bool Init(const npc::NpcConfig& config);
void SetDirection(int direction);
bool IsReady() const { return displayAnimation_ != nullptr; }
bool IsAnimationFinished() const;
const std::string& GetAnimationPath() const { return animationPath_; }
bool GetDisplayLocalBounds(Rect& outBounds) const;
private:
RefPtr<Animation> displayAnimation_ = nullptr;
std::string animationPath_;
int direction_ = 1;
};
} // namespace frostbite2D

View File

@@ -0,0 +1,20 @@
#pragma once
#include <map>
#include <optional>
#include <string>
namespace frostbite2D::npc {
struct NpcConfig {
int id = -1;
std::string path;
std::string name;
std::string fieldName;
std::string fieldAnimationPath;
};
bool loadNpcIndex(std::map<int, std::string>& outIndex);
std::optional<NpcConfig> loadNpcConfig(int npcId);
} // namespace frostbite2D::npc

View File

@@ -0,0 +1,59 @@
#pragma once
#include "character/CharacterActionTypes.h"
#include "npc/NpcAnimation.h"
#include <frostbite2D/2d/text_sprite.h>
#include <frostbite2D/2d/actor.h>
#include <array>
#include <optional>
#include <string>
namespace frostbite2D {
class NpcObject : public Actor {
public:
NpcObject() = default;
~NpcObject() override = default;
bool Construction(int npcId);
void SetDirection(int direction);
void SetNpcPosition(const Vec2& pos);
void SetWorldPosition(const CharacterWorldPosition& pos);
void SetInteractable(bool interactable);
void BeginInteract();
void EndInteract();
bool CanInteract() const;
bool IsInteracting() const { return interacting_; }
int GetNpcId() const { return npcId_; }
int GetDirection() const { return direction_; }
const std::string& GetName() const;
const std::string& GetFieldName() const;
const std::string& GetDisplayName() const;
const CharacterWorldPosition& GetWorldPosition() const { return worldPosition_; }
bool IsAnimationFinished() const;
const npc::NpcConfig* GetConfig() const {
return config_ ? &config_.value() : nullptr;
}
void Update(float deltaTime) override;
private:
void EnsureNameLabel();
void RefreshNameLabel();
void SyncActorPositionFromWorld();
int npcId_ = -1;
int direction_ = 1;
CharacterWorldPosition worldPosition_;
std::optional<npc::NpcConfig> config_;
RefPtr<NpcAnimation> animation_ = nullptr;
std::array<RefPtr<TextSprite>, 8> nameOutlineLabels_;
RefPtr<TextSprite> nameLabel_ = nullptr;
bool interactable_ = true;
bool interacting_ = false;
};
} // namespace frostbite2D

View File

@@ -3,6 +3,7 @@
#include "camera/GameCameraController.h"
#include "character/CharacterObject.h"
#include "map/GameMap.h"
#include "npc/NpcObject.h"
#include "scene/GameDebugUIScene.h"
#include <frostbite2D/scene/scene.h>
@@ -20,6 +21,7 @@ public:
private:
GameCameraController cameraController_;
RefPtr<CharacterObject> character_;
RefPtr<NpcObject> npc_;
RefPtr<GameDebugUIScene> debugScene_;
bool initialized_ = false;
RefPtr<GameMap> map_;