Add Item Pickup and Highlight for Item Stealing

This commit is contained in:
Philip W 2024-01-27 09:09:04 +00:00
parent 0c96707ff9
commit 97a23666d8
31 changed files with 236 additions and 31 deletions

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

BIN
SeagullGame/Content/AI/AIC_Enemy.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/AI/AI_Enemy.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/AI/BB_Enemy.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/AI/BTT_Chase.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/AI/BTT_FindRandomPatrol.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/AI/BT_Enemy.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
SeagullGame/Content/Fonts/upheavtt.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/Fonts/upheavtt_Font.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/Items/BPI_Cube.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
SeagullGame/Content/Items/BPI_Cube_NP.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
SeagullGame/Content/TopDown/Input/Actions/IAS_Pickup.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "ItemActor.h"
// Sets default values
AItemActor::AItemActor()
{
// 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;
}
// Called when the game starts or when spawned
void AItemActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AItemActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

View File

@ -0,0 +1,28 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ItemActor.generated.h"
UCLASS()
class SEAGULLGAME_API AItemActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AItemActor();
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Item")
TSubclassOf<AItemActor> ItemNoPhysics;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};

View File

@ -4,8 +4,10 @@
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "ItemActor.h"
#include "UObject/ConstructorHelpers.h"
#include "Camera/CameraComponent.h"
#include "Components/BoxComponent.h"
#include "Components/DecalComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
@ -51,6 +53,27 @@ ASeagullGameCharacter::ASeagullGameCharacter()
void ASeagullGameCharacter::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
if (!IsValid(PickupBox)) return;
TArray<AActor*> OverlappingActors;
PickupBox->GetOverlappingActors(OverlappingActors, AItemActor::StaticClass());
float ClosestDistance = 0;
for (AActor* Actor : OverlappingActors)
{
if (Actor == this) continue;
float Distance = FVector::Dist(Actor->GetActorLocation(), GetActorLocation());
if (ClosestItemActor == nullptr || Distance < ClosestDistance)
{
if (IsValid(ClosestItemActor))
{
Cast<UMeshComponent>(ClosestItemActor->GetComponentByClass(UMeshComponent::StaticClass()))->SetRenderCustomDepth(false);
}
ClosestItemActor = Actor;
ClosestDistance = Distance;
}
}
if (!IsValid(ClosestItemActor)) return;
Cast<UMeshComponent>(ClosestItemActor->GetComponentByClass(UMeshComponent::StaticClass()))->SetRenderCustomDepth(true);
}
void ASeagullGameCharacter::BeginPlay()
@ -65,6 +88,9 @@ void ASeagullGameCharacter::BeginPlay()
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
PickupBox = Cast<UBoxComponent>(GetComponentByClass(UBoxComponent::StaticClass()));
PickupBox->OnComponentBeginOverlap.AddDynamic(this, &ASeagullGameCharacter::OnPickupBoxBeginOverlap);
PickupBox->OnComponentEndOverlap.AddDynamic(this, &ASeagullGameCharacter::OnPickupBoxEndOverlap);
}
void ASeagullGameCharacter::DamagePlayer(float DamageAmount)
@ -86,6 +112,65 @@ void ASeagullGameCharacter::HealPlayer(float HealAmount)
}
}
void ASeagullGameCharacter::StartGame()
{
if (GetWorld()->GetTimerManager().IsTimerActive(GameTimerHandle))
{
GetWorld()->GetTimerManager().ClearTimer(GameTimerHandle);
}
GetWorld()->GetTimerManager().SetTimer(GameTimerHandle, this, &ASeagullGameCharacter::EndGame, GameTime, false);
}
void ASeagullGameCharacter::EndGame()
{
OnGameEnd.Broadcast();
}
void ASeagullGameCharacter::PickupItem()
{
if (IsValid(CurrentItem))
{
DropItem();
return;
}
USkeletalMeshComponent* MeshComponent = GetMesh();
if (!MeshComponent->HasAnySockets()) return;
if (!IsValid(ClosestItemActor)) return;
CurrentItem = GetWorld()->SpawnActor(Cast<AItemActor>(ClosestItemActor)->ItemNoPhysics);
CurrentItem->AttachToComponent(MeshComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale, "ItemSocket");
CurrentItem->SetActorEnableCollision(false);
ClosestItemActor->Destroy();
}
void ASeagullGameCharacter::DropItem()
{
if (!IsValid(CurrentItem)) return;
USkeletalMeshComponent* MeshComponent = GetMesh();
CurrentItem->DetachFromActor(FDetachmentTransformRules::KeepWorldTransform);
FVector DropLocation = MeshComponent->GetSocketLocation("ItemSocket");
DropLocation += GetActorForwardVector() * 100;
GetWorld()->SpawnActor(Cast<AItemActor>(CurrentItem)->ItemNoPhysics, &DropLocation);
CurrentItem->Destroy();
CurrentItem = nullptr;
}
void ASeagullGameCharacter::OnPickupBoxBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}
void ASeagullGameCharacter::OnPickupBoxEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (!Cast<AItemActor>(OtherActor)) return;
if (IsValid(OtherActor))
{
if (OtherActor == ClosestItemActor)
{
Cast<UMeshComponent>(OtherActor->GetComponentByClass(UMeshComponent::StaticClass()))->SetRenderCustomDepth(false);
ClosestItemActor = nullptr;
}
}
}
void ASeagullGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
// Set up action bindings
@ -100,6 +185,9 @@ void ASeagullGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInp
//Looking
// EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ATP_ThirdPersonCharacter::Look);
//Pickup
EnhancedInputComponent->BindAction(PickupAction, ETriggerEvent::Started, this, &ASeagullGameCharacter::PickupItem);
}
}

View File

@ -4,6 +4,8 @@
#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "ItemActor.h"
#include "Components/BoxComponent.h"
#include "GameFramework/Character.h"
#include "SeagullGameCharacter.generated.h"
@ -15,8 +17,12 @@ class ASeagullGameCharacter : public ACharacter
public:
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnPlayerDeath);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnGameEnd);
UPROPERTY(BlueprintAssignable, Category = "Events")
FOnPlayerDeath OnPlayerDeath;
UPROPERTY(BlueprintAssignable, Category = "Events")
FOnGameEnd OnGameEnd;
ASeagullGameCharacter();
@ -35,6 +41,10 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* JumpAction;
/** Pickup Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* PickupAction;
/** Move Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* MoveAction;
@ -57,6 +67,32 @@ public:
UFUNCTION(BlueprintCallable, Category = "Health")
void HealPlayer(float HealAmount);
UFUNCTION(Exec, BlueprintCallable, Category = "General")
void StartGame();
UFUNCTION(BlueprintCallable, Category = "General")
void EndGame();
UPROPERTY(BlueprintReadOnly, Category = "General")
FTimerHandle GameTimerHandle;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "General")
float GameTime = 120;
UFUNCTION(BlueprintCallable, Category = "Item")
void PickupItem();
UFUNCTION(BlueprintCallable, Category = "Item")
void DropItem();
UFUNCTION(BlueprintCallable, Category = "Item")
void OnPickupBoxBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION(BlueprintCallable, Category = "Item")
void OnPickupBoxEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
UPROPERTY(BlueprintReadOnly, Category = "Item")
AActor* CurrentItem;
UPROPERTY(BlueprintReadOnly, Category = "Item")
AActor* ClosestItemActor;
UPROPERTY(BlueprintReadOnly, Category = "Item")
UBoxComponent* PickupBox;
protected:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;