feat: add simple shooting component

Added interface for damage.
Added shooting component with simple shooting system.
Fix missing action bindings in player controller.
Added cylinder mesh to ExoEnemy and place him on TestMap.
This commit is contained in:
Kubson96 2025-02-21 20:42:52 +01:00
parent 40e5a37d91
commit 9e7fab3544
9 changed files with 144 additions and 3 deletions

Binary file not shown.

View File

@ -0,0 +1,60 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Characters/Components/ShootingComponent.h"
// Sets default values for this component's properties
UShootingComponent::UShootingComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UShootingComponent::BeginPlay()
{
Super::BeginPlay();
// ...
}
void UShootingComponent::Shoot()
{
AActor* Owner = GetOwner();
FVector LineStart = Owner->GetActorLocation();
FVector ForwardVector = Owner->GetActorForwardVector();
FVector LineEnd = LineStart + (ForwardVector * MaxRange);
FHitResult HitResult;
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(Owner);
bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams);
if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UDamageable>())
{
IDamageable::Execute_TakeDamage(HitResult.GetActor(), DamageValue);
UE_LOG(LogTemp, Warning, TEXT("Shoot"));
}
if (bShowDebugLine)
DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f);
}
// Called every frame
void UShootingComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}

View File

@ -29,6 +29,7 @@ void AExoPlayerController::BeginPlay()
}
InteractionComponent = PlayerCharacter->FindComponentByClass<UInteractionComponent>();
ShootingComponent = PlayerCharacter->FindComponentByClass<UShootingComponent>();
}
void AExoPlayerController::PlayerTick(float DeltaTime)
@ -47,6 +48,14 @@ void AExoPlayerController::SetupInputComponent()
EnhancedInputComponent->BindAction(InteractAction, ETriggerEvent::Triggered, this, &AExoPlayerController::Interact);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerJump);
EnhancedInputComponent->BindAction(DodgeAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerDodge);
//EnhancedInputComponent->BindAction(, ETriggerEvent::Started, this, &AExoPlayerController::ResetDodge);
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerCrouch);
EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerSprint);
EnhancedInputComponent->BindAction(ShootAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerShoot);
EnhancedInputComponent->BindAction(AimAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerAim);
EnhancedInputComponent->BindAction(MeleAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerMeleAttack);
EnhancedInputComponent->BindAction(ChangeWeaponAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerChangeWeapon);
EnhancedInputComponent->BindAction(ReloadAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerReload);
}
void AExoPlayerController::Move(const FInputActionValue& InputActionValue)
@ -74,7 +83,7 @@ void AExoPlayerController::Look(const FInputActionValue& InputActionValue)
}
void AExoPlayerController::Interact(const FInputActionValue& InputActionValue)
void AExoPlayerController::Interact()
{
if (InteractionComponent->InteractedActor)
IInteractable::Execute_Interact(InteractionComponent->InteractedActor);
@ -123,7 +132,7 @@ void AExoPlayerController::PlayerSprint()
void AExoPlayerController::PlayerShoot()
{
ShootingComponent->Shoot();
}
void AExoPlayerController::PlayerAim()

View File

@ -0,0 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Interfaces/Damageable.h"
#include "ShootingComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class EXO_API UShootingComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UShootingComponent();
UPROPERTY(EditAnywhere, Category = "Shooting")
float MaxRange = 2000.0f;
UPROPERTY(EditAnywhere, Category = "Shooting")
float DamageValue = 100.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Debug")
bool bShowDebugLine = true;
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UFUNCTION(Category = "Shooting")
void Shoot();
//UFUNCTION(Category = "Shooting")
//void Reload();
};

View File

@ -0,0 +1,27 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "Damageable.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI, Blueprintable)
class UDamageable : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class EXO_API IDamageable
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Damage")
void TakeDamage(float damageValue);
};

View File

@ -5,6 +5,7 @@
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include <Player/InteractionComponent.h>
#include <Characters/Components/ShootingComponent.h>
#include "ExoPlayerController.generated.h"
class AExoPlayerCharacter;
@ -35,7 +36,7 @@ protected:
void Look(const FInputActionValue& InputActionValue); // MouseXY
UFUNCTION(BlueprintCallable, Category = "Input")
void Interact(const FInputActionValue& InputActionValue); // E
void Interact(); // E
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerJump(); // Space
@ -125,4 +126,6 @@ protected:
private:
UInteractionComponent* InteractionComponent;
UShootingComponent* ShootingComponent;
};