Highlight Enemies

This commit is contained in:
2024-11-17 21:09:41 -04:00
parent 787d39bca8
commit 22d70553ec
16 changed files with 156 additions and 14 deletions

View File

@ -4,3 +4,4 @@
#include "CoreMinimal.h"
#define CUSTOM_DEPTH_RED 250

View File

@ -2,4 +2,27 @@
#include "Character/AuraEnemy.h"
#include "Aura/Aura.h"
AAuraEnemy::AAuraEnemy()
{
GetMesh()->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
GetMesh()->SetRenderCustomDepth(false);
GetMesh()->SetCustomDepthStencilValue(CUSTOM_DEPTH_RED);
Weapon->SetRenderCustomDepth(false);
Weapon->SetCustomDepthStencilValue(CUSTOM_DEPTH_RED);
}
void AAuraEnemy::HighlightActor()
{
GetMesh()->SetRenderCustomDepth(true);
Weapon->SetRenderCustomDepth(true);
}
void AAuraEnemy::UnHighlightActor()
{
GetMesh()->SetRenderCustomDepth(false);
Weapon->SetRenderCustomDepth(false);
}

View File

@ -0,0 +1,7 @@
// Assets provided by DruidMechanics. Copyright Jonathan Rampersad 2024
#include "Interaction/EnemyInterface.h"
// Add default functionality here for any IMyClass functions that are not pure virtual.

View File

@ -4,12 +4,20 @@
#include "Player/AuraPlayerController.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "Interaction/EnemyInterface.h"
AAuraPlayerController::AAuraPlayerController()
{
bReplicates = true;
}
void AAuraPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
CursorTrace();
}
void AAuraPlayerController::BeginPlay()
{
Super::BeginPlay();
@ -52,3 +60,60 @@ void AAuraPlayerController::Move(const FInputActionValue& InputActionValue)
}
}
void AAuraPlayerController::CursorTrace()
{
FHitResult CursorHit;
GetHitResultUnderCursor(ECC_Visibility, false, CursorHit);
if (!CursorHit.bBlockingHit) return;
LastActor = ThisActor;
ThisActor = CursorHit.GetActor();
/**
* Line trace from cursor. There are several scenarios:
* A. LastActor is null && ThisActor is null
* - Do nothing
* B. LastActor is null && ThisActor is valid
* - Highlight ThisActor
* C. LastActor is valid && ThisActor is null
* - UnHighlight LastActor
* D. Both actors are valid, but LastActor != ThisActor
* - UnHighlight LastActor, and Highlight ThisActor
* E. Both actors are valid, and are the same actor
* - Do nothing
*/
if (LastActor == nullptr)
{
if (ThisActor != nullptr)
{
// Case B
ThisActor->HighlightActor();
}
else
{
// Case A - both are null, do nothing
}
}
else // LastActor is valid
{
if (ThisActor == nullptr)
{
// Case C
LastActor->UnHighlightActor();
}
else // both actors are valid
{
if (LastActor != ThisActor)
{
// Case D
LastActor->UnHighlightActor();
ThisActor->HighlightActor();
}
else
{
// Case E - do nothing
}
}
}
}

View File

@ -4,14 +4,19 @@
#include "CoreMinimal.h"
#include "AuraCharacterBase.h"
#include "Interaction/EnemyInterface.h"
#include "AuraEnemy.generated.h"
/**
*
*/
UCLASS()
class AURA_API AAuraEnemy : public AAuraCharacterBase
class AURA_API AAuraEnemy : public AAuraCharacterBase, public IEnemyInterface
{
GENERATED_BODY()
public:
AAuraEnemy();
virtual void HighlightActor() override;
virtual void UnHighlightActor() override;
};

View File

@ -0,0 +1,27 @@
// Assets provided by DruidMechanics. Copyright Jonathan Rampersad 2024
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "EnemyInterface.generated.h"
// This class does not need to be modified.
UINTERFACE()
class UEnemyInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class AURA_API IEnemyInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
virtual void HighlightActor() = 0;
virtual void UnHighlightActor() = 0;
};

View File

@ -9,6 +9,7 @@
struct FInputActionValue;
class UInputAction;
class UInputMappingContext;
class IEnemyInterface;
/**
*
@ -21,6 +22,8 @@ class AURA_API AAuraPlayerController : public APlayerController
public:
AAuraPlayerController();
virtual void PlayerTick(float DeltaTime) override;
protected:
virtual void BeginPlay() override;
@ -34,4 +37,8 @@ private:
TObjectPtr<UInputAction> MoveAction;
void Move(const FInputActionValue& InputActionValue);
void CursorTrace();
TScriptInterface<IEnemyInterface> LastActor;
TScriptInterface<IEnemyInterface> ThisActor;
};