ExoWest/Source/Exo/Private/Characters/Components/ShootingComponent.cpp
Kubson96 9e7fab3544 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.
2025-02-21 20:42:52 +01:00

61 lines
1.6 KiB
C++

// 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);
// ...
}