Skip to content

Commit

Permalink
#77 Added impulse by damage
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat committed Dec 23, 2023
1 parent 654f26b commit 147836e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 29 deletions.
Binary file modified Content/Blueprints/Weapons/WeaponTableDefinitions.uasset
Binary file not shown.
15 changes: 8 additions & 7 deletions Source/Cloud9/Weapon/Assets/WeaponDefinitionsAsset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@

#include "WeaponDefinitionsAsset.h"

#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Tools/Macro/Logging.h"

#include "Cloud9/Weapon/Extensions/EWeaponId.h"

const FPrimaryAssetId UWeaponDefinitionsAsset::PrimaryAssetId = FPrimaryAssetId("Weapon:Definitions");

template <typename WeaponIdType, typename ValidatorType>
TOptional<FWeaponDefinition> UWeaponDefinitionsAsset::GetWeaponDefinition(
FWeaponDefinition UWeaponDefinitionsAsset::GetWeaponDefinition(
UDataTable* WeaponsInfoTable,
ValidatorType Validator,
WeaponIdType WeaponId) const
Expand Down Expand Up @@ -73,28 +76,26 @@ TOptional<FWeaponDefinition> UWeaponDefinitionsAsset::GetWeaponDefinition(
return {};
}

return FWeaponDefinition(*WeaponInfo, *Montages);
return FWeaponDefinition(*WeaponInfo, *Montages, WeaponCommonData);
}

bool UWeaponDefinitionsAsset::GetWeaponDefinition(const FWeaponId& WeaponId, FWeaponDefinition& WeaponDefinition) const
{
var Found = Visit(
WeaponDefinition = Visit(
ETVariant::Overloaded{
[&](EFirearm Id) { return GetWeaponDefinition(FirearmsWeaponsInfoTable, UWeaponType::IsFirearm, Id); },
[&](EMelee Id) { return GetWeaponDefinition(MeleeWeaponsInfoTable, UWeaponType::IsMelee, Id); },
[&](EGrenade Id) { return GetWeaponDefinition(GrenadeWeaponInfoTable, UWeaponType::IsGrenade, Id); },
[](auto) { return TOptional<FWeaponDefinition>(); }
[](auto) { return FWeaponDefinition{}; }
}, WeaponId
);

if (not Found.IsSet())
if (not IsValid(WeaponDefinition))
{
log(Error, "Can't get weapon definition for WeaponId='%s'", WeaponId | EFWeaponId::ToName() | EFName::ToCStr());
return false;
}

WeaponDefinition = MoveTemp(*Found);

return true;
}

Expand Down
9 changes: 4 additions & 5 deletions Source/Cloud9/Weapon/Assets/WeaponDefinitionsAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@

#include "Engine/DataTable.h"

#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Tools/Macro/Logging.h"
#include "Cloud9/Weapon/Enums/WeaponId.h"
#include "Cloud9/Weapon/Enums/WeaponType.h"
#include "Cloud9/Weapon/Structures/WeaponDefinition.h"
#include "Cloud9/Weapon/Tables/WeaponCommonData.h"
#include "Cloud9/Weapon/Tables/WeaponMontages.h"

#include "WeaponDefinitionsAsset.generated.h"
Expand Down Expand Up @@ -74,14 +73,14 @@ class CLOUD9_API UWeaponDefinitionsAsset : public UPrimaryDataAsset
TMap<EWeaponType, FWeaponPosesMontages> WeaponActionMontages;

/**
* Weapon tracer
* Weapon common data
*/
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category=Weapon)
UParticleSystem* FirearmTracer;
FWeaponCommonData WeaponCommonData;

private: // functions
template <typename WeaponIdType, typename ValidatorType>
TOptional<FWeaponDefinition> GetWeaponDefinition(
FWeaponDefinition GetWeaponDefinition(
UDataTable* WeaponsInfoTable,
ValidatorType Validator,
WeaponIdType WeaponId) const;
Expand Down
14 changes: 8 additions & 6 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@

#include "Cloud9WeaponFirearm.h"

#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Tools/Macro/Logging.h"
#include "Cloud9/Tools/Extensions/AActor.h"
#include "Cloud9/Tools/Extensions/TVariant.h"
#include "Cloud9/Tools/Extensions/USoundBase.h"
#include "Cloud9/Game/Cloud9DeveloperSettings.h"
#include "Cloud9/Game/Cloud9PlayerController.h"
#include "Cloud9/Character/Cloud9Character.h"
#include "Cloud9/Tools/Extensions/FVector.h"

#include "Cloud9/Weapon/Tables/WeaponTableFirearm.h"

Expand Down Expand Up @@ -146,6 +149,7 @@ void ACloud9WeaponFirearm::Tick(float DeltaSeconds)
let Character = GetOwner<ACloud9Character>();
let WeaponInfo = WeaponDefinition.GetWeaponInfo<FFirearmWeaponInfo>();
let PoseMontages = WeaponDefinition.GetPoseMontages(Character->bIsCrouched);
let CommonData = WeaponDefinition.GetCommonData();

if (bIsDeploying)
{
Expand All @@ -171,7 +175,7 @@ void ACloud9WeaponFirearm::Tick(float DeltaSeconds)
PlayRandomSound(WeaponInfo->Sounds.FireSounds, Settings->Volume))
{
MuzzleFlash->Activate(true);
return Fire();
return Fire(WeaponInfo, CommonData->ImpulseMultiplier);
}

return false;
Expand All @@ -187,7 +191,7 @@ void ACloud9WeaponFirearm::Tick(float DeltaSeconds)
else {}
}

bool ACloud9WeaponFirearm::Fire() const
bool ACloud9WeaponFirearm::Fire(const FFirearmWeaponInfo* WeaponInfo, float ImpulseMultiplier) const
{
let Character = GetOwner<ACloud9Character>();

Expand Down Expand Up @@ -228,10 +232,8 @@ bool ACloud9WeaponFirearm::Fire() const

if (Target->IsSimulatingPhysics() and Target->Mobility == EComponentMobility::Movable)
{
var Direction = LineHit.Location - StartLocation;
Direction.Normalize();
// TODO: Add impulse based on weapon type (weapon power)
Target->AddImpulse(1000.0 * Direction, NAME_None, true);
let Direction = (LineHit.Location - StartLocation) | EFVector::Normalize{};
Target->AddImpulse(WeaponInfo->Damage * ImpulseMultiplier * Direction, NAME_None, true);
}

return true;
Expand Down
4 changes: 1 addition & 3 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
#include "CoreMinimal.h"
#include "NiagaraComponent.h"

#include "Cloud9/Tools/Macro/Common.h"
#include "Cloud9/Tools/Macro/Logging.h"
#include "Cloud9/Weapon/Classes/Cloud9WeaponBase.h"
#include "Cloud9/Weapon/Enums/FirearmNames.h"

Expand Down Expand Up @@ -69,7 +67,7 @@ class CLOUD9_API ACloud9WeaponFirearm : public ACloud9WeaponBase

virtual void Tick(float DeltaSeconds) override;

bool Fire() const;
bool Fire(const FFirearmWeaponInfo* WeaponInfo, float ImpulseMultiplier) const;

protected: // properties
/**
Expand Down
19 changes: 11 additions & 8 deletions Source/Cloud9/Weapon/Structures/WeaponDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include "Cloud9/Weapon/Enums/WeaponClass.h"
#include "Cloud9/Weapon/Tables/WeaponCommonData.h"
#include "Cloud9/Weapon/Tables/WeaponTableBase.h"
#include "Cloud9/Weapon/Tables/WeaponMontages.h"
#include "Cloud9/Weapon/Tables/WeaponTableFirearm.h"
Expand All @@ -44,23 +45,23 @@ struct FWeaponDefinition

FWeaponDefinition() = default;

FWeaponDefinition(FMeleeWeaponInfo WeaponInfo, FWeaponPosesMontages Montages, UParticleSystem* Tracer = nullptr)
FWeaponDefinition(FMeleeWeaponInfo WeaponInfo, FWeaponPosesMontages Montages, FWeaponCommonData WeaponCommonData)
: WeaponClass(EWeaponClass::Melee)
, MeleeWeaponInfo(MoveTemp(WeaponInfo))
, Montages(MoveTemp(Montages))
, Tracer(Tracer) {}
, WeaponCommonData(MoveTemp(WeaponCommonData)) {}

FWeaponDefinition(FFirearmWeaponInfo WeaponInfo, FWeaponPosesMontages Montages, UParticleSystem* Tracer = nullptr)
FWeaponDefinition(FFirearmWeaponInfo WeaponInfo, FWeaponPosesMontages Montages, FWeaponCommonData WeaponCommonData)
: WeaponClass(EWeaponClass::Firearm)
, FirearmWeaponInfo(MoveTemp(WeaponInfo))
, Montages(MoveTemp(Montages))
, Tracer(Tracer) {}
, WeaponCommonData(MoveTemp(WeaponCommonData)) {}

FWeaponDefinition(FGrenadeWeaponInfo WeaponInfo, FWeaponPosesMontages Montages, UParticleSystem* Tracer = nullptr)
FWeaponDefinition(FGrenadeWeaponInfo WeaponInfo, FWeaponPosesMontages Montages, FWeaponCommonData WeaponCommonData)
: WeaponClass(EWeaponClass::Grenade)
, GrenadeWeaponInfo(MoveTemp(WeaponInfo))
, Montages(MoveTemp(Montages))
, Tracer(Tracer) {}
, WeaponCommonData(MoveTemp(WeaponCommonData)) {}

// TODO: Refactor FWeaponDefinition somehow...
template <typename WeaponInfoType = FBaseWeaponInfo>
Expand Down Expand Up @@ -117,6 +118,8 @@ struct FWeaponDefinition
return not bIsCrouch ? &Montages.OnStand : &Montages.OnCrouch;
}

FORCEINLINE const FWeaponCommonData* GetCommonData() const { return &WeaponCommonData; }

void Reset()
{
if (WeaponClass == EWeaponClass::Melee)
Expand All @@ -135,7 +138,7 @@ struct FWeaponDefinition
}

WeaponClass = EWeaponClass::NoClass;
Tracer = nullptr;
WeaponCommonData = {};
}

protected:
Expand All @@ -155,5 +158,5 @@ struct FWeaponDefinition
FWeaponPosesMontages Montages{};

UPROPERTY()
UParticleSystem* Tracer = nullptr;
FWeaponCommonData WeaponCommonData{};
};
38 changes: 38 additions & 0 deletions Source/Cloud9/Weapon/Tables/WeaponCommonData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include "WeaponCommonData.generated.h"

USTRUCT(BlueprintType)
struct FWeaponCommonData
{
GENERATED_BODY()

UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category=Damage)
float ImpulseMultiplier = 50.0f;

UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category=Visual)
UParticleSystem* FirearmTracer;
};

0 comments on commit 147836e

Please sign in to comment.