feat(npc): 添加NPC交互高亮效果及复合纹理支持
实现NPC交互时的绿色高亮效果,通过InteractionHighlightSprite类实现 重构NpcAnimation支持复合纹理渲染,优化高亮效果的性能 添加ShaderManager获取所有加载Shader的方法,优化渲染器uniform更新逻辑
This commit is contained in:
20
Game/include/common/InteractionHighlightSprite.h
Normal file
20
Game/include/common/InteractionHighlightSprite.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <frostbite2D/2d/sprite.h>
|
||||
|
||||
namespace frostbite2D {
|
||||
|
||||
class InteractionHighlightSprite : public Sprite {
|
||||
public:
|
||||
InteractionHighlightSprite();
|
||||
|
||||
void SetGroundAnchorInTexture(const Vec2& groundAnchorInTexture);
|
||||
|
||||
protected:
|
||||
void ConfigureShader(Shader* shader) const override;
|
||||
|
||||
private:
|
||||
Vec2 texelSize_ = Vec2::One();
|
||||
};
|
||||
|
||||
} // namespace frostbite2D
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "npc/NpcDataLoader.h"
|
||||
#include <frostbite2D/2d/actor.h>
|
||||
#include <frostbite2D/2d/canvas_actor.h>
|
||||
#include <frostbite2D/animation/animation.h>
|
||||
#include <string>
|
||||
|
||||
@@ -9,17 +10,47 @@ namespace frostbite2D {
|
||||
|
||||
class NpcAnimation : public Actor {
|
||||
public:
|
||||
struct CompositeFrameInfo {
|
||||
bool valid = false;
|
||||
Rect localBounds;
|
||||
Vec2 originInTexture = Vec2::Zero();
|
||||
Vec2 groundAnchorInTexture = Vec2::Zero();
|
||||
int width = 1;
|
||||
int height = 1;
|
||||
};
|
||||
|
||||
bool Init(const npc::NpcConfig& config);
|
||||
void Update(float deltaTime) override;
|
||||
void Render() override;
|
||||
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;
|
||||
bool EnsureCompositeTextureReady();
|
||||
bool HasCompositeTexture() const;
|
||||
Ptr<Texture> GetCompositeTexture() const;
|
||||
Vec2 GetCompositeTextureSize() const;
|
||||
Vec2 GetCompositeGroundAnchorInTexture() const;
|
||||
uint64 GetCompositeVersion() const { return compositeVersion_; }
|
||||
|
||||
private:
|
||||
CompositeFrameInfo ComputeCompositeFrameInfo(int direction) const;
|
||||
CompositeFrameInfo GetCurrentCompositeFrameInfo() const { return compositeFrameInfo_; }
|
||||
uint64 CaptureCurrentRenderSignature() const;
|
||||
void MarkCompositeDirty();
|
||||
void UpdateCompositeCamera();
|
||||
void RefreshCompositeTextureIfNeeded();
|
||||
void RenderCurrentAnimationToCompositeCanvas();
|
||||
|
||||
RefPtr<Animation> displayAnimation_ = nullptr;
|
||||
RefPtr<CanvasActor> compositeCanvas_ = nullptr;
|
||||
CompositeFrameInfo compositeFrameInfo_;
|
||||
std::string animationPath_;
|
||||
int direction_ = 1;
|
||||
bool compositeDirty_ = true;
|
||||
uint64 compositeVersion_ = 0;
|
||||
uint64 lastCompositeSignature_ = 0;
|
||||
};
|
||||
|
||||
} // namespace frostbite2D
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "character/CharacterActionTypes.h"
|
||||
#include "common/InteractionHighlightSprite.h"
|
||||
#include "npc/NpcAnimation.h"
|
||||
#include <frostbite2D/2d/text_sprite.h>
|
||||
#include <frostbite2D/2d/actor.h>
|
||||
#include <frostbite2D/2d/text_sprite.h>
|
||||
#include <array>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -21,11 +22,13 @@ public:
|
||||
void SetNpcPosition(const Vec2& pos);
|
||||
void SetWorldPosition(const CharacterWorldPosition& pos);
|
||||
void SetInteractable(bool interactable);
|
||||
void SetInteractionHighlighted(bool highlighted);
|
||||
void BeginInteract();
|
||||
void EndInteract();
|
||||
|
||||
bool CanInteract() const;
|
||||
bool IsInteracting() const { return interacting_; }
|
||||
bool IsInteractionHighlighted() const { return interactionHighlighted_; }
|
||||
int GetNpcId() const { return npcId_; }
|
||||
int GetDirection() const { return direction_; }
|
||||
const std::string& GetName() const;
|
||||
@@ -41,8 +44,10 @@ public:
|
||||
void Update(float deltaTime) override;
|
||||
|
||||
private:
|
||||
void EnsureInteractionHighlight();
|
||||
void EnsureNameLabel();
|
||||
void RefreshNameLabel();
|
||||
void SyncInteractionHighlight();
|
||||
void SyncActorPositionFromWorld();
|
||||
|
||||
int npcId_ = -1;
|
||||
@@ -50,10 +55,13 @@ private:
|
||||
CharacterWorldPosition worldPosition_;
|
||||
std::optional<npc::NpcConfig> config_;
|
||||
RefPtr<NpcAnimation> animation_ = nullptr;
|
||||
RefPtr<InteractionHighlightSprite> interactionHighlight_ = nullptr;
|
||||
std::array<RefPtr<TextSprite>, 8> nameOutlineLabels_;
|
||||
RefPtr<TextSprite> nameLabel_ = nullptr;
|
||||
bool interactable_ = true;
|
||||
bool interacting_ = false;
|
||||
bool interactionHighlighted_ = false;
|
||||
uint64 syncedHighlightCompositeVersion_ = 0;
|
||||
};
|
||||
|
||||
} // namespace frostbite2D
|
||||
|
||||
Reference in New Issue
Block a user