惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
博客园 - 司徒正美
博客园 - 【当耐特】
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
宝玉的分享
宝玉的分享
V
V2EX
S
SegmentFault 最新的问题
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
Martin Fowler
Martin Fowler
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
L
LangChain Blog
D
Docker
腾讯CDC

博客园 - ParamousGIS

VS中CMakePresets的Path环境变量设置 VTK 类结构图 VSCode的C++项目缓存目录 VMWare Workstation 17.6安装Windows7 awesome-cpp 矢量切片工具--SparkVectorTileTool dll生成lib 自动布局 C++第三方库 QT + CMake环境配置 应用clang-format对c++代码时进行格式化 UE5--022--2D Blend Spaces UE5--025--SuperSideScrollerGame--004--AnimBP--AnimMontage UE5--024--SuperSideScrollerGame--003--AIController UE5--022--SuperSideScrollerGame--001 UE5--021--Animation Montage UE5--020--Material Instances UE5--019--LayeredBlendPerBone UE5--018--BlendSpace--AnimGraph
UE5--023--SuperSideScrollerGame--002
ParamousGIS · 2025-04-12 · via 博客园 - ParamousGIS

1. UAnim_ProjectileNotify

1.1 UAnim_ProjectileNotify.h

#pragma once

#include "CoreMinimal.h"
#include "Animation/AnimNotifies/AnimNotify.h"
#include "Anim_ProjectileNotify.generated.h"

/**
 * 
 */
UCLASS()
class SUPERSIDESCROLLER_API UAnim_ProjectileNotify : public UAnimNotify
{
    GENERATED_BODY()
    
public: 
    virtual void Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation, const FAnimNotifyEventReference& EventReference) override;

};

1.2 UAnim_ProjectileNotify.cpp

#include "Anim_ProjectileNotify.h"
#include "Components/SkeletalMeshComponent.h"
#include "SuperSideScroller_Player.h"

void UAnim_ProjectileNotify::Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation, const FAnimNotifyEventReference& EventReference)
{
    Super::Notify(MeshComp, Animation, EventReference);
    UE_LOG(LogTemp, Warning, TEXT("Throw Notify"));
    //
    ASuperSideScroller_Player* Player = Cast<ASuperSideScroller_Player>(MeshComp->GetOwner());
    if (Player)
    {
        Player->SpawnProjectile();
    }
}

2. AEnemyBase

2.1 AEnemyBase.h

#pragma once

#include "CoreMinimal.h"
#include "SuperSideScroller/SuperSideScrollerCharacter.h"
#include "EnemyBase.generated.h"

/**
 * 
 */
UCLASS()
class SUPERSIDESCROLLER_API AEnemyBase : public ASuperSideScrollerCharacter
{
    GENERATED_BODY()
    
public:
    void DestroyEnemy();

    UPROPERTY(EditAnywhere, BlueprintReadOnly)
    class UParticleSystem* DeathEffect;

    UPROPERTY(EditAnywhere, BlueprintReadOnly)
    class USoundBase* DeathSound;

};

2.2 AEnemyBase.cpp

#include "EnemyBase.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/World.h"


void AEnemyBase::DestroyEnemy()
{
    UWorld* World = GetWorld();
    if (World)
    {
        if (DeathEffect)
        {
            UGameplayStatics::SpawnEmitterAtLocation(World, DeathEffect, GetActorTransform());
        }
        if (DeathSound)
        {
            UGameplayStatics::SpawnSoundAtLocation(World, DeathSound, GetActorLocation());
        }
    }

    Destroy();
}

2.3 BP_Enemy

image

3. APickableActor_Base

3.1 APickableActor_Base.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PickableActor_Base.generated.h"

UCLASS()
class SUPERSIDESCROLLER_API APickableActor_Base : public AActor
{
    GENERATED_BODY()
    
public:    
    // Sets default values for this actor's properties
    APickableActor_Base();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    UPROPERTY(VisibleDefaultsOnly, Category = PickableItem)
    class USphereComponent* CollisionComp;

    UPROPERTY(VisibleDefaultsOnly, Category = PickableItem)
    class UStaticMeshComponent* MeshComp;

    UPROPERTY(VisibleDefaultsOnly, Category = PickableItem)
    class URotatingMovementComponent* RotationComp;

    virtual void PlayerPickedUp(class ASuperSideScroller_Player* Player);

private:

    UFUNCTION()
    void BeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

public:    
    // Called every frame
    //virtual void Tick(float DeltaTime) override;
    //
    UPROPERTY(EditAnywhere, BlueprintReadOnly)
    USoundBase* PickupSound;
};

3.2 APickableActor_Base.cpp

#include "PickableActor_Base.h"
#include "Components/SphereComponent.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/RotatingMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "SuperSideScroller_Player.h"

// Sets default values
APickableActor_Base::APickableActor_Base()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    //PrimaryActorTick.bCanEverTick = false;
    //
    CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
    CollisionComp->InitSphereRadius(30.0f);
    CollisionComp->BodyInstance.SetCollisionProfileName("OverlapAllDynamic");
    RootComponent = CollisionComp;
    //
    MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
    MeshComp->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
    MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
    //
    RotationComp = CreateDefaultSubobject<URotatingMovementComponent>(TEXT("RotationComp"));


}

// Called when the game starts or when spawned
void APickableActor_Base::BeginPlay()
{
    Super::BeginPlay();
    CollisionComp->OnComponentBeginOverlap.AddDynamic(this,    &APickableActor_Base::BeginOverlap);
}

// Called every frame
//void APickableActor_Base::Tick(float DeltaTime)
//{
//    Super::Tick(DeltaTime);
//
//}

void APickableActor_Base::BeginOverlap(UPrimitiveComponent*    OverlappedComponent, AActor* OtherActor,    UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    ASuperSideScroller_Player* Player =    Cast<ASuperSideScroller_Player>(OtherActor);
    if (Player)
    {
        PlayerPickedUp(Player);
    }
}

void APickableActor_Base::PlayerPickedUp(ASuperSideScroller_Player* Player)
{
    const UWorld* World = GetWorld();
    if (World)
    {
        if (PickupSound)
        {
            UGameplayStatics::SpawnSoundAtLocation(World, PickupSound, GetActorLocation());

        }
        
    }
    Destroy();
}

3.3 BP_PickableActor_Base

image

4. APickableActor_Collectable

4.1 APickableActor_Collectable.h

#pragma once

#include "CoreMinimal.h"
#include "PickableActor_Base.h"
#include "PickableActor_Collectable.generated.h"

/**
 * 
 */
UCLASS()
class SUPERSIDESCROLLER_API APickableActor_Collectable : public APickableActor_Base
{
    GENERATED_BODY()
    
protected:
    virtual void BeginPlay() override;

    virtual void PlayerPickedUp(class ASuperSideScroller_Player* Player)override;

public:
    UPROPERTY(EditAnywhere, Category = Collectable)
    int32 CollectableValue = 1;


};

4.2 APickableActor_Collectable.cpp

#include "PickableActor_Collectable.h"
#include "SuperSideScroller_Player.h"

void APickableActor_Collectable::PlayerPickedUp(class ASuperSideScroller_Player* Player)
{
    Player->IncrementNumberofCollectables(CollectableValue);

    Super::PlayerPickedUp(Player);    

}


void APickableActor_Collectable::BeginPlay()
{
    Super::BeginPlay();
}

4.3 BP_Collectable

image

5. APickableActor_Powerup

5.1 APickableActor_Powerup.h

#pragma once

#include "CoreMinimal.h"
#include "PickableActor_Base.h"
#include "PickableActor_Powerup.generated.h"

/**
 * 
 */
UCLASS()
class SUPERSIDESCROLLER_API APickableActor_Powerup : public APickableActor_Base
{
    GENERATED_BODY()
    
protected:
    // 
    virtual void PlayerPickedUp(class ASuperSideScroller_Player* Player) override;

    //
    virtual void BeginPlay() override;

};

5.2 APickableActor_Powerup.cpp

#include "PickableActor_Powerup.h"
#include "SuperSideScroller_Player.h"

void APickableActor_Powerup::BeginPlay()
{
    Super::BeginPlay();
}

void APickableActor_Powerup::PlayerPickedUp(ASuperSideScroller_Player* Player)
{
    Super::PlayerPickedUp(Player);
    Player->IncreaseMovementPowerup();

}

5.3 BP_Powerup

image

6. ASuperSideScroller_Brick

6.1 ASuperSideScroller_Brick.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SuperSideScroller_Brick.generated.h"

UCLASS()
class SUPERSIDESCROLLER_API ASuperSideScroller_Brick : public AActor
{
    GENERATED_BODY()
    
public:    
    // Sets default values for this actor's properties
    ASuperSideScroller_Brick();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    //virtual void Tick(float DeltaTime) override;

    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Brick)
    class USoundBase* HitSound;

    //
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Brick)
    class UParticleSystem* Explosion;



private:
    //
    UPROPERTY(VisibleDefaultsOnly, Category = Brick)
    class UStaticMeshComponent* BrickMesh;
    //
    UPROPERTY(VisibleDefaultsOnly, Category = Brick)
    class UBoxComponent* BrickCollision;

    //
    UPROPERTY(EditAnywhere)
    bool bHasCollectable;

    //
    UPROPERTY(EditAnywhere)
    int32 CollectableValue = 1;


    //
    UFUNCTION()
    void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

    //
    void AddCollectable(class ASuperSideScroller_Player* Player);

    //
    void PlayHitSound();

    //
    void PlayHitExplosion();

};

6.2 ASuperSideScroller_Brick.cpp

#include "SuperSideScroller_Brick.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "Engine/World.h"
#include "Kismet/GameplayStatics.h"
#include "SuperSideScroller_Player.h"

// Sets default values
ASuperSideScroller_Brick::ASuperSideScroller_Brick()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    //PrimaryActorTick.bCanEverTick = true;
    BrickMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BrickMesh"));
    BrickMesh->SetCollisionProfileName("BlockAll");
    RootComponent = BrickMesh;
    //
    BrickCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("BrickCollision"));
    BrickCollision->SetCollisionProfileName("BlockAll");
    BrickCollision->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);
    //
    BrickCollision->OnComponentHit.AddDynamic(this,    &ASuperSideScroller_Brick::OnHit);


}

// Called when the game starts or when spawned
void ASuperSideScroller_Brick::BeginPlay()
{
    Super::BeginPlay();
    
}

// Called every frame
//void ASuperSideScroller_Brick::Tick(float DeltaTime)
//{
//    Super::Tick(DeltaTime);
//
//}

void ASuperSideScroller_Brick::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    ASuperSideScroller_Player* Player = Cast<ASuperSideScroller_Player>(OtherActor);
    if (Player && bHasCollectable)
    {
        AddCollectable(Player);
        PlayHitSound();
        PlayHitExplosion();
        Destroy();
    }

}

void ASuperSideScroller_Brick::AddCollectable(ASuperSideScroller_Player* Player)
{
    Player->IncrementNumberofCollectables(CollectableValue);
}

void ASuperSideScroller_Brick::PlayHitSound()
{
    UWorld* World = GetWorld();
    if (World && HitSound)
    {
        UGameplayStatics::SpawnSoundAtLocation(World, HitSound, GetActorLocation());
    }
}

void ASuperSideScroller_Brick::PlayHitExplosion()
{
    UWorld* World = GetWorld();
    if (World && Explosion)
    {
        UGameplayStatics::SpawnEmitterAtLocation(World,
            Explosion,
            GetActorTransform());
    }
}

6.3 BP_Brick

image