Skip to content

Commit

Permalink
refactor weapon code
Browse files Browse the repository at this point in the history
attach weapon to player with code instead of BP
  • Loading branch information
filfreire committed Aug 7, 2023
1 parent f010a2f commit f14e3eb
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 37 deletions.
2 changes: 2 additions & 0 deletions Config/DefaultInput.ini
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ bCaptureMouseOnLaunch=True
bEnableLegacyInputScales=True
bEnableMotionControls=True
bFilterInputByPlatformUser=False
bEnableInputDeviceSubsystem=True
bShouldFlushPressedKeysOnViewportFocusLost=True
bEnableDynamicComponentInputBinding=True
bAlwaysShowTouchInterface=False
Expand All @@ -86,6 +87,7 @@ DoubleClickTime=0.200000
+ActionMappings=(ActionName="Crouch",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=C)
+ActionMappings=(ActionName="Jump",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=SpaceBar)
+ActionMappings=(ActionName="Zoom",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=RightMouseButton)
+ActionMappings=(ActionName="Fire",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=LeftMouseButton)
+AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=W)
+AxisMappings=(AxisName="MoveForward",Scale=-1.000000,Key=S)
+AxisMappings=(AxisName="MoveRight",Scale=1.000000,Key=D)
Expand Down
Binary file modified Content/Blueprints/PlayerPawn.uasset
Binary file not shown.
Binary file modified Content/Maps/P_TestMap.umap
Binary file not shown.
24 changes: 24 additions & 0 deletions Source/CoopGameFleep/Private/SCharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/PawnMovementComponent.h"
#include "SWeapon.h"

// Sets default values
ASCharacter::ASCharacter()
Expand All @@ -21,6 +22,8 @@ ASCharacter::ASCharacter()

CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
CameraComp->SetupAttachment(SpringArmComp);

WeaponAttachSocketName = "WeaponSocket";
}

// Called when the game starts or when spawned
Expand All @@ -31,6 +34,18 @@ void ASCharacter::BeginPlay()
DefaultFOV = CameraComp->FieldOfView;
ZoomedFOV = 65.f;
ZoomInterpSpeed = 20;

// Spawn a default weapon
FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
CurrentWeapon = GetWorld()->SpawnActor<ASWeapon>(StarterWeaponClass, FVector::ZeroVector, FRotator::ZeroRotator, SpawnParams);

if (CurrentWeapon)
{
CurrentWeapon->SetOwner(this);
CurrentWeapon->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, WeaponAttachSocketName);
}

}

void ASCharacter::MoveForward(float Value)
Expand Down Expand Up @@ -63,6 +78,13 @@ void ASCharacter::EndZoom()
bWantsToZoom = false;
}

void ASCharacter::Fire()
{
if (CurrentWeapon) {
CurrentWeapon->Fire();
}
}

// Called every frame
void ASCharacter::Tick(float DeltaTime)
{
Expand Down Expand Up @@ -94,6 +116,8 @@ void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponen

PlayerInputComponent->BindAction("Zoom", IE_Pressed, this, &ASCharacter::BeginZoom);
PlayerInputComponent->BindAction("Zoom", IE_Released, this, &ASCharacter::EndZoom);

PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASCharacter::Fire);
}

FVector ASCharacter::GetPawnViewLocation() const
Expand Down
46 changes: 17 additions & 29 deletions Source/CoopGameFleep/Private/SWeapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ FAutoConsoleVariableRef CVarDebugWeapon(TEXT("COOP.DebugWeapons"), DebugWeaponDr
// Sets default values
ASWeapon::ASWeapon()
{
// 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;

MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
RootComponent = MeshComp;

Expand All @@ -25,13 +22,6 @@ ASWeapon::ASWeapon()

}

// Called when the game starts or when spawned
void ASWeapon::BeginPlay()
{
Super::BeginPlay();

}

void ASWeapon::Fire()
{
// trace the world from pawn eyes to crosshair location
Expand Down Expand Up @@ -76,31 +66,29 @@ void ASWeapon::Fire()
if (DebugWeaponDrawing != 0) {
DrawDebugLine(GetWorld(), EyeLocation, TraceEnd, FColor::White, false, 1.0f, 0, 1.0f);
}


if (MuzzleEffect)
{
UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, MeshComp, MuzzleSocketName);
}
PlayFireEffects(TracerEndpoint);
}
}

void ASWeapon::PlayFireEffects(FVector TracerEnd)
{

if (TracerEffect)
if (MuzzleEffect)
{
UGameplayStatics::SpawnEmitterAttached(MuzzleEffect, MeshComp, MuzzleSocketName);
}


if (TracerEffect)
{
FVector MuzzleLocation = MeshComp->GetSocketLocation(MuzzleSocketName);
UParticleSystemComponent* TracerComp = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TracerEffect, MuzzleLocation);
if (TracerComp)
{
FVector MuzzleLocation = MeshComp->GetSocketLocation(MuzzleSocketName);
UParticleSystemComponent* TracerComp = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), TracerEffect, MuzzleLocation);
if (TracerComp)
{
TracerComp->SetVectorParameter(TracerTargetName, TracerEndpoint);
}
TracerComp->SetVectorParameter(TracerTargetName, TracerEnd);
}

}
}

// Called every frame
void ASWeapon::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

12 changes: 12 additions & 0 deletions Source/CoopGameFleep/Public/SCharacter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "GameFramework/Character.h"
#include "SCharacter.generated.h"


class UCameraComponent;
class USpringArmComponent;
class ASWeapon;

UCLASS()
class COOPGAMEFLEEP_API ASCharacter : public ACharacter
Expand Down Expand Up @@ -51,6 +53,16 @@ class COOPGAMEFLEEP_API ASCharacter : public ACharacter

void EndZoom();

ASWeapon* CurrentWeapon;

UPROPERTY(EditDefaultsOnly, Category = "Player")
TSubclassOf<ASWeapon> StarterWeaponClass;

UPROPERTY(VisibleDefaultsOnly, Category = "Player")
FName WeaponAttachSocketName;

void Fire();

public:
// Called every frame
virtual void Tick(float DeltaTime) override;
Expand Down
12 changes: 4 additions & 8 deletions Source/CoopGameFleep/Public/SWeapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ class COOPGAMEFLEEP_API ASWeapon : public AActor
ASWeapon();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
USkeletalMeshComponent* MeshComp;

UFUNCTION(BlueprintCallable, Category = "Weapon")
virtual void Fire();
void PlayFireEffects(FVector TracerEnd);

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
TSubclassOf<UDamageType> DamageType;
Expand All @@ -48,7 +44,7 @@ class COOPGAMEFLEEP_API ASWeapon : public AActor
UParticleSystem* TracerEffect;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

// BlueprintProtected not found for UE 5
UFUNCTION(BlueprintCallable, Category = "Weapon")
virtual void Fire();
};

0 comments on commit f14e3eb

Please sign in to comment.