UE5--023--SuperSideScrollerGame--002


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



posted @ 2025-04-12 17:00  ParamousGIS  阅读(16)  评论(0)    收藏  举报