First commit
This commit is contained in:
15
Source/ProjectLighthouse.Target.cs
Normal file
15
Source/ProjectLighthouse.Target.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
using UnrealBuildTool;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class ProjectLighthouseTarget : TargetRules
|
||||
{
|
||||
public ProjectLighthouseTarget(TargetInfo Target) : base(Target)
|
||||
{
|
||||
Type = TargetType.Game;
|
||||
DefaultBuildSettings = BuildSettingsVersion.V5;
|
||||
IncludeOrderVersion = EngineIncludeOrderVersion.Unreal5_4;
|
||||
ExtraModuleNames.Add("ProjectLighthouse");
|
||||
}
|
||||
}
|
||||
13
Source/ProjectLighthouse/ProjectLighthouse.Build.cs
Normal file
13
Source/ProjectLighthouse/ProjectLighthouse.Build.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
using UnrealBuildTool;
|
||||
|
||||
public class ProjectLighthouse : ModuleRules
|
||||
{
|
||||
public ProjectLighthouse(ReadOnlyTargetRules Target) : base(Target)
|
||||
{
|
||||
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
|
||||
|
||||
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });
|
||||
}
|
||||
}
|
||||
7
Source/ProjectLighthouse/ProjectLighthouse.cpp
Normal file
7
Source/ProjectLighthouse/ProjectLighthouse.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "ProjectLighthouse.h"
|
||||
#include "Modules/ModuleManager.h"
|
||||
|
||||
IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, ProjectLighthouse, "ProjectLighthouse" );
|
||||
|
||||
5
Source/ProjectLighthouse/ProjectLighthouse.h
Normal file
5
Source/ProjectLighthouse/ProjectLighthouse.h
Normal file
@@ -0,0 +1,5 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
95
Source/ProjectLighthouse/ProjectLighthouseCharacter.cpp
Normal file
95
Source/ProjectLighthouse/ProjectLighthouseCharacter.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "ProjectLighthouseCharacter.h"
|
||||
#include "ProjectLighthouseProjectile.h"
|
||||
#include "Animation/AnimInstance.h"
|
||||
#include "Camera/CameraComponent.h"
|
||||
#include "Components/CapsuleComponent.h"
|
||||
#include "Components/SkeletalMeshComponent.h"
|
||||
#include "EnhancedInputComponent.h"
|
||||
#include "EnhancedInputSubsystems.h"
|
||||
#include "InputActionValue.h"
|
||||
#include "Engine/LocalPlayer.h"
|
||||
|
||||
DEFINE_LOG_CATEGORY(LogTemplateCharacter);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// AProjectLighthouseCharacter
|
||||
|
||||
AProjectLighthouseCharacter::AProjectLighthouseCharacter()
|
||||
{
|
||||
// Set size for collision capsule
|
||||
GetCapsuleComponent()->InitCapsuleSize(55.f, 96.0f);
|
||||
|
||||
// Create a CameraComponent
|
||||
FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
|
||||
FirstPersonCameraComponent->SetupAttachment(GetCapsuleComponent());
|
||||
FirstPersonCameraComponent->SetRelativeLocation(FVector(-10.f, 0.f, 60.f)); // Position the camera
|
||||
FirstPersonCameraComponent->bUsePawnControlRotation = true;
|
||||
|
||||
// Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
|
||||
Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
|
||||
Mesh1P->SetOnlyOwnerSee(true);
|
||||
Mesh1P->SetupAttachment(FirstPersonCameraComponent);
|
||||
Mesh1P->bCastDynamicShadow = false;
|
||||
Mesh1P->CastShadow = false;
|
||||
//Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f));
|
||||
Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f));
|
||||
|
||||
}
|
||||
|
||||
void AProjectLighthouseCharacter::BeginPlay()
|
||||
{
|
||||
// Call the base class
|
||||
Super::BeginPlay();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////// Input
|
||||
|
||||
void AProjectLighthouseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
||||
{
|
||||
// Set up action bindings
|
||||
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
|
||||
{
|
||||
// Jumping
|
||||
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);
|
||||
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
|
||||
|
||||
// Moving
|
||||
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AProjectLighthouseCharacter::Move);
|
||||
|
||||
// Looking
|
||||
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AProjectLighthouseCharacter::Look);
|
||||
}
|
||||
else
|
||||
{
|
||||
UE_LOG(LogTemplateCharacter, Error, TEXT("'%s' Failed to find an Enhanced Input Component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void AProjectLighthouseCharacter::Move(const FInputActionValue& Value)
|
||||
{
|
||||
// input is a Vector2D
|
||||
FVector2D MovementVector = Value.Get<FVector2D>();
|
||||
|
||||
if (Controller != nullptr)
|
||||
{
|
||||
// add movement
|
||||
AddMovementInput(GetActorForwardVector(), MovementVector.Y);
|
||||
AddMovementInput(GetActorRightVector(), MovementVector.X);
|
||||
}
|
||||
}
|
||||
|
||||
void AProjectLighthouseCharacter::Look(const FInputActionValue& Value)
|
||||
{
|
||||
// input is a Vector2D
|
||||
FVector2D LookAxisVector = Value.Get<FVector2D>();
|
||||
|
||||
if (Controller != nullptr)
|
||||
{
|
||||
// add yaw and pitch input to controller
|
||||
AddControllerYawInput(LookAxisVector.X);
|
||||
AddControllerPitchInput(LookAxisVector.Y);
|
||||
}
|
||||
}
|
||||
71
Source/ProjectLighthouse/ProjectLighthouseCharacter.h
Normal file
71
Source/ProjectLighthouse/ProjectLighthouseCharacter.h
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/Character.h"
|
||||
#include "Logging/LogMacros.h"
|
||||
#include "ProjectLighthouseCharacter.generated.h"
|
||||
|
||||
class UInputComponent;
|
||||
class USkeletalMeshComponent;
|
||||
class UCameraComponent;
|
||||
class UInputAction;
|
||||
class UInputMappingContext;
|
||||
struct FInputActionValue;
|
||||
|
||||
DECLARE_LOG_CATEGORY_EXTERN(LogTemplateCharacter, Log, All);
|
||||
|
||||
UCLASS(config=Game)
|
||||
class AProjectLighthouseCharacter : public ACharacter
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
/** Pawn mesh: 1st person view (arms; seen only by self) */
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Mesh, meta = (AllowPrivateAccess = "true"))
|
||||
USkeletalMeshComponent* Mesh1P;
|
||||
|
||||
/** First person camera */
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
|
||||
UCameraComponent* FirstPersonCameraComponent;
|
||||
|
||||
/** Jump Input Action */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
|
||||
UInputAction* JumpAction;
|
||||
|
||||
/** Move Input Action */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
|
||||
UInputAction* MoveAction;
|
||||
|
||||
public:
|
||||
AProjectLighthouseCharacter();
|
||||
|
||||
protected:
|
||||
virtual void BeginPlay();
|
||||
|
||||
public:
|
||||
|
||||
/** Look Input Action */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
|
||||
class UInputAction* LookAction;
|
||||
|
||||
protected:
|
||||
/** Called for movement input */
|
||||
void Move(const FInputActionValue& Value);
|
||||
|
||||
/** Called for looking input */
|
||||
void Look(const FInputActionValue& Value);
|
||||
|
||||
protected:
|
||||
// APawn interface
|
||||
virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;
|
||||
// End of APawn interface
|
||||
|
||||
public:
|
||||
/** Returns Mesh1P subobject **/
|
||||
USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }
|
||||
/** Returns FirstPersonCameraComponent subobject **/
|
||||
UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; }
|
||||
|
||||
};
|
||||
|
||||
14
Source/ProjectLighthouse/ProjectLighthouseGameMode.cpp
Normal file
14
Source/ProjectLighthouse/ProjectLighthouseGameMode.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "ProjectLighthouseGameMode.h"
|
||||
#include "ProjectLighthouseCharacter.h"
|
||||
#include "UObject/ConstructorHelpers.h"
|
||||
|
||||
AProjectLighthouseGameMode::AProjectLighthouseGameMode()
|
||||
: Super()
|
||||
{
|
||||
// set default pawn class to our Blueprinted character
|
||||
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/FirstPerson/Blueprints/BP_FirstPersonCharacter"));
|
||||
DefaultPawnClass = PlayerPawnClassFinder.Class;
|
||||
|
||||
}
|
||||
19
Source/ProjectLighthouse/ProjectLighthouseGameMode.h
Normal file
19
Source/ProjectLighthouse/ProjectLighthouseGameMode.h
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/GameModeBase.h"
|
||||
#include "ProjectLighthouseGameMode.generated.h"
|
||||
|
||||
UCLASS(minimalapi)
|
||||
class AProjectLighthouseGameMode : public AGameModeBase
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
AProjectLighthouseGameMode();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
|
||||
#include "ProjectLighthousePlayerController.h"
|
||||
#include "EnhancedInputSubsystems.h"
|
||||
#include "Engine/LocalPlayer.h"
|
||||
|
||||
void AProjectLighthousePlayerController::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
// get the enhanced input subsystem
|
||||
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
|
||||
{
|
||||
// add the mapping context so we get controls
|
||||
Subsystem->AddMappingContext(InputMappingContext, 0);
|
||||
}
|
||||
}
|
||||
31
Source/ProjectLighthouse/ProjectLighthousePlayerController.h
Normal file
31
Source/ProjectLighthouse/ProjectLighthousePlayerController.h
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/PlayerController.h"
|
||||
#include "ProjectLighthousePlayerController.generated.h"
|
||||
|
||||
class UInputMappingContext;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
UCLASS()
|
||||
class PROJECTLIGHTHOUSE_API AProjectLighthousePlayerController : public APlayerController
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
protected:
|
||||
|
||||
/** Input Mapping Context to be used for player input */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
|
||||
UInputMappingContext* InputMappingContext;
|
||||
|
||||
// Begin Actor interface
|
||||
protected:
|
||||
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
// End Actor interface
|
||||
};
|
||||
43
Source/ProjectLighthouse/ProjectLighthouseProjectile.cpp
Normal file
43
Source/ProjectLighthouse/ProjectLighthouseProjectile.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "ProjectLighthouseProjectile.h"
|
||||
#include "GameFramework/ProjectileMovementComponent.h"
|
||||
#include "Components/SphereComponent.h"
|
||||
|
||||
AProjectLighthouseProjectile::AProjectLighthouseProjectile()
|
||||
{
|
||||
// Use a sphere as a simple collision representation
|
||||
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
|
||||
CollisionComp->InitSphereRadius(5.0f);
|
||||
CollisionComp->BodyInstance.SetCollisionProfileName("Projectile");
|
||||
CollisionComp->OnComponentHit.AddDynamic(this, &AProjectLighthouseProjectile::OnHit); // set up a notification for when this component hits something blocking
|
||||
|
||||
// Players can't walk on it
|
||||
CollisionComp->SetWalkableSlopeOverride(FWalkableSlopeOverride(WalkableSlope_Unwalkable, 0.f));
|
||||
CollisionComp->CanCharacterStepUpOn = ECB_No;
|
||||
|
||||
// Set as root component
|
||||
RootComponent = CollisionComp;
|
||||
|
||||
// Use a ProjectileMovementComponent to govern this projectile's movement
|
||||
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileComp"));
|
||||
ProjectileMovement->UpdatedComponent = CollisionComp;
|
||||
ProjectileMovement->InitialSpeed = 3000.f;
|
||||
ProjectileMovement->MaxSpeed = 3000.f;
|
||||
ProjectileMovement->bRotationFollowsVelocity = true;
|
||||
ProjectileMovement->bShouldBounce = true;
|
||||
|
||||
// Die after 3 seconds by default
|
||||
InitialLifeSpan = 3.0f;
|
||||
}
|
||||
|
||||
void AProjectLighthouseProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
|
||||
{
|
||||
// Only add impulse and destroy projectile if we hit a physics
|
||||
if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics())
|
||||
{
|
||||
OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());
|
||||
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
37
Source/ProjectLighthouse/ProjectLighthouseProjectile.h
Normal file
37
Source/ProjectLighthouse/ProjectLighthouseProjectile.h
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/Actor.h"
|
||||
#include "ProjectLighthouseProjectile.generated.h"
|
||||
|
||||
class USphereComponent;
|
||||
class UProjectileMovementComponent;
|
||||
|
||||
UCLASS(config=Game)
|
||||
class AProjectLighthouseProjectile : public AActor
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
/** Sphere collision component */
|
||||
UPROPERTY(VisibleDefaultsOnly, Category=Projectile)
|
||||
USphereComponent* CollisionComp;
|
||||
|
||||
/** Projectile movement component */
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement, meta = (AllowPrivateAccess = "true"))
|
||||
UProjectileMovementComponent* ProjectileMovement;
|
||||
|
||||
public:
|
||||
AProjectLighthouseProjectile();
|
||||
|
||||
/** called when projectile hits something */
|
||||
UFUNCTION()
|
||||
void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
|
||||
|
||||
/** Returns CollisionComp subobject **/
|
||||
USphereComponent* GetCollisionComp() const { return CollisionComp; }
|
||||
/** Returns ProjectileMovement subobject **/
|
||||
UProjectileMovementComponent* GetProjectileMovement() const { return ProjectileMovement; }
|
||||
};
|
||||
|
||||
31
Source/ProjectLighthouse/TP_PickUpComponent.cpp
Normal file
31
Source/ProjectLighthouse/TP_PickUpComponent.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#include "TP_PickUpComponent.h"
|
||||
|
||||
UTP_PickUpComponent::UTP_PickUpComponent()
|
||||
{
|
||||
// Setup the Sphere Collision
|
||||
SphereRadius = 32.f;
|
||||
}
|
||||
|
||||
void UTP_PickUpComponent::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
// Register our Overlap Event
|
||||
OnComponentBeginOverlap.AddDynamic(this, &UTP_PickUpComponent::OnSphereBeginOverlap);
|
||||
}
|
||||
|
||||
void UTP_PickUpComponent::OnSphereBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
|
||||
{
|
||||
// Checking if it is a First Person Character overlapping
|
||||
AProjectLighthouseCharacter* Character = Cast<AProjectLighthouseCharacter>(OtherActor);
|
||||
if(Character != nullptr)
|
||||
{
|
||||
// Notify that the actor is being picked up
|
||||
OnPickUp.Broadcast(Character);
|
||||
|
||||
// Unregister from the Overlap Event so it is no longer triggered
|
||||
OnComponentBeginOverlap.RemoveAll(this);
|
||||
}
|
||||
}
|
||||
34
Source/ProjectLighthouse/TP_PickUpComponent.h
Normal file
34
Source/ProjectLighthouse/TP_PickUpComponent.h
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "Components/SphereComponent.h"
|
||||
#include "ProjectLighthouseCharacter.h"
|
||||
#include "TP_PickUpComponent.generated.h"
|
||||
|
||||
// Declaration of the delegate that will be called when someone picks this up
|
||||
// The character picking this up is the parameter sent with the notification
|
||||
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPickUp, AProjectLighthouseCharacter*, PickUpCharacter);
|
||||
|
||||
UCLASS(Blueprintable, BlueprintType, ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
|
||||
class PROJECTLIGHTHOUSE_API UTP_PickUpComponent : public USphereComponent
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
|
||||
/** Delegate to whom anyone can subscribe to receive this event */
|
||||
UPROPERTY(BlueprintAssignable, Category = "Interaction")
|
||||
FOnPickUp OnPickUp;
|
||||
|
||||
UTP_PickUpComponent();
|
||||
protected:
|
||||
|
||||
/** Called when the game starts */
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
/** Code for when something overlaps this component */
|
||||
UFUNCTION()
|
||||
void OnSphereBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
|
||||
};
|
||||
119
Source/ProjectLighthouse/TP_WeaponComponent.cpp
Normal file
119
Source/ProjectLighthouse/TP_WeaponComponent.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
|
||||
#include "TP_WeaponComponent.h"
|
||||
#include "ProjectLighthouseCharacter.h"
|
||||
#include "ProjectLighthouseProjectile.h"
|
||||
#include "GameFramework/PlayerController.h"
|
||||
#include "Camera/PlayerCameraManager.h"
|
||||
#include "Kismet/GameplayStatics.h"
|
||||
#include "EnhancedInputComponent.h"
|
||||
#include "EnhancedInputSubsystems.h"
|
||||
#include "Animation/AnimInstance.h"
|
||||
#include "Engine/LocalPlayer.h"
|
||||
#include "Engine/World.h"
|
||||
|
||||
// Sets default values for this component's properties
|
||||
UTP_WeaponComponent::UTP_WeaponComponent()
|
||||
{
|
||||
// Default offset from the character location for projectiles to spawn
|
||||
MuzzleOffset = FVector(100.0f, 0.0f, 10.0f);
|
||||
}
|
||||
|
||||
|
||||
void UTP_WeaponComponent::Fire()
|
||||
{
|
||||
if (Character == nullptr || Character->GetController() == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Try and fire a projectile
|
||||
if (ProjectileClass != nullptr)
|
||||
{
|
||||
UWorld* const World = GetWorld();
|
||||
if (World != nullptr)
|
||||
{
|
||||
APlayerController* PlayerController = Cast<APlayerController>(Character->GetController());
|
||||
const FRotator SpawnRotation = PlayerController->PlayerCameraManager->GetCameraRotation();
|
||||
// MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
|
||||
const FVector SpawnLocation = GetOwner()->GetActorLocation() + SpawnRotation.RotateVector(MuzzleOffset);
|
||||
|
||||
//Set Spawn Collision Handling Override
|
||||
FActorSpawnParameters ActorSpawnParams;
|
||||
ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
|
||||
|
||||
// Spawn the projectile at the muzzle
|
||||
World->SpawnActor<AProjectLighthouseProjectile>(ProjectileClass, SpawnLocation, SpawnRotation, ActorSpawnParams);
|
||||
}
|
||||
}
|
||||
|
||||
// Try and play the sound if specified
|
||||
if (FireSound != nullptr)
|
||||
{
|
||||
UGameplayStatics::PlaySoundAtLocation(this, FireSound, Character->GetActorLocation());
|
||||
}
|
||||
|
||||
// Try and play a firing animation if specified
|
||||
if (FireAnimation != nullptr)
|
||||
{
|
||||
// Get the animation object for the arms mesh
|
||||
UAnimInstance* AnimInstance = Character->GetMesh1P()->GetAnimInstance();
|
||||
if (AnimInstance != nullptr)
|
||||
{
|
||||
AnimInstance->Montage_Play(FireAnimation, 1.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool UTP_WeaponComponent::AttachWeapon(AProjectLighthouseCharacter* TargetCharacter)
|
||||
{
|
||||
Character = TargetCharacter;
|
||||
|
||||
// Check that the character is valid, and has no weapon component yet
|
||||
if (Character == nullptr || Character->GetInstanceComponents().FindItemByClass<UTP_WeaponComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Attach the weapon to the First Person Character
|
||||
FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, true);
|
||||
AttachToComponent(Character->GetMesh1P(), AttachmentRules, FName(TEXT("GripPoint")));
|
||||
|
||||
// add the weapon as an instance component to the character
|
||||
Character->AddInstanceComponent(this);
|
||||
|
||||
// Set up action bindings
|
||||
if (APlayerController* PlayerController = Cast<APlayerController>(Character->GetController()))
|
||||
{
|
||||
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
|
||||
{
|
||||
// Set the priority of the mapping to 1, so that it overrides the Jump action with the Fire action when using touch input
|
||||
Subsystem->AddMappingContext(FireMappingContext, 1);
|
||||
}
|
||||
|
||||
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerController->InputComponent))
|
||||
{
|
||||
// Fire
|
||||
EnhancedInputComponent->BindAction(FireAction, ETriggerEvent::Triggered, this, &UTP_WeaponComponent::Fire);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void UTP_WeaponComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
|
||||
{
|
||||
if (Character == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (APlayerController* PlayerController = Cast<APlayerController>(Character->GetController()))
|
||||
{
|
||||
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
|
||||
{
|
||||
Subsystem->RemoveMappingContext(FireMappingContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
Source/ProjectLighthouse/TP_WeaponComponent.h
Normal file
60
Source/ProjectLighthouse/TP_WeaponComponent.h
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "Components/SkeletalMeshComponent.h"
|
||||
#include "TP_WeaponComponent.generated.h"
|
||||
|
||||
class AProjectLighthouseCharacter;
|
||||
|
||||
UCLASS(Blueprintable, BlueprintType, ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
|
||||
class PROJECTLIGHTHOUSE_API UTP_WeaponComponent : public USkeletalMeshComponent
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
/** Projectile class to spawn */
|
||||
UPROPERTY(EditDefaultsOnly, Category=Projectile)
|
||||
TSubclassOf<class AProjectLighthouseProjectile> ProjectileClass;
|
||||
|
||||
/** Sound to play each time we fire */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay)
|
||||
USoundBase* FireSound;
|
||||
|
||||
/** AnimMontage to play each time we fire */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
|
||||
UAnimMontage* FireAnimation;
|
||||
|
||||
/** Gun muzzle's offset from the characters location */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Gameplay)
|
||||
FVector MuzzleOffset;
|
||||
|
||||
/** MappingContext */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
|
||||
class UInputMappingContext* FireMappingContext;
|
||||
|
||||
/** Fire Input Action */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
|
||||
class UInputAction* FireAction;
|
||||
|
||||
/** Sets default values for this component's properties */
|
||||
UTP_WeaponComponent();
|
||||
|
||||
/** Attaches the actor to a FirstPersonCharacter */
|
||||
UFUNCTION(BlueprintCallable, Category="Weapon")
|
||||
bool AttachWeapon(AProjectLighthouseCharacter* TargetCharacter);
|
||||
|
||||
/** Make the weapon Fire a Projectile */
|
||||
UFUNCTION(BlueprintCallable, Category="Weapon")
|
||||
void Fire();
|
||||
|
||||
protected:
|
||||
/** Ends gameplay for this component. */
|
||||
UFUNCTION()
|
||||
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
|
||||
|
||||
private:
|
||||
/** The Character holding this weapon*/
|
||||
AProjectLighthouseCharacter* Character;
|
||||
};
|
||||
15
Source/ProjectLighthouseEditor.Target.cs
Normal file
15
Source/ProjectLighthouseEditor.Target.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright Epic Games, Inc. All Rights Reserved.
|
||||
|
||||
using UnrealBuildTool;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class ProjectLighthouseEditorTarget : TargetRules
|
||||
{
|
||||
public ProjectLighthouseEditorTarget(TargetInfo Target) : base(Target)
|
||||
{
|
||||
Type = TargetType.Editor;
|
||||
DefaultBuildSettings = BuildSettingsVersion.V5;
|
||||
IncludeOrderVersion = EngineIncludeOrderVersion.Unreal5_4;
|
||||
ExtraModuleNames.Add("ProjectLighthouse");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user