Skip to content

Commit

Permalink
#230 Added background decals logic (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat authored Mar 5, 2024
1 parent cffabf8 commit 23683b1
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 21 deletions.
Binary file modified Content/Maps/warmup.umap
Binary file not shown.
Binary file modified Content/Physicals/PM_Character.uasset
Binary file not shown.
77 changes: 67 additions & 10 deletions Source/Cloud9/Physicals/Cloud9PhysicalMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,82 @@ UCloud9PhysicalMaterial::UCloud9PhysicalMaterial()
FirearmDecalSize = {8.0f, 8.0, 8.0f};
FirearmDecalRotationMin = 0.0f;
FirearmDecalRotationMax = 180.0f;
}

UMaterialInterface* UCloud9PhysicalMaterial::GetRandomFirearmDecal() const { return GetRandomItem(FirearmDecals); }
FirearmBackgroundDecalSize = {8.0f, 64.0, 64.0f};
FirearmBackgroundDecalOffsetMin = -50.0f;
FirearmBackgroundDecalOffsetMax = 50.0f;
FirearmBackgroundDecalRotationMin = 0.0f;
FirearmBackgroundDecalRotationMax = 180.0f;
FirearmBackgroundDecalProbability = 0.5f;
FirearmBackgroundDecalMaxDistance = 200.0f;

UNiagaraSystem* UCloud9PhysicalMaterial::GetRandomFirearmSquib() const { return GetRandomItem(FirearmEffects); }
GrenadeDecalSize = {8.0f, 8.0f, 8.0f};
MeleeDecalSize = {8.0f, 8.0f, 8.0f};
}

FVector UCloud9PhysicalMaterial::GetFirearmDecalSize() const { return FirearmDecalSize; }
bool IsVertical(FVector Normal)
{
return FMath::IsNearlyEqual(Normal.Z, 1.0f, 0.1f);
}

FRotator UCloud9PhysicalMaterial::GetFirearmDecalRotation(FVector Normal) const
FRotator GetNormalSurfaceRotation(FVector Normal, float Roll)
{
constexpr float DecalVPitch = 90.0f;
constexpr float DecalVRoll = -180.0f;
constexpr float DecalHYaw = 90.0f;
constexpr float DecalHRoll = -90.0;

let Corrective = IsVertical(Normal)
? FRotator{DecalVPitch, Roll, DecalVRoll}
: FRotator{Roll, DecalHYaw, DecalHRoll};

return Normal.Rotation() + Corrective;
}

UMaterialInterface* UCloud9PhysicalMaterial::GetRandomFirearmDecal() const { return GetRandomItem(FirearmDecals); }

FVector UCloud9PhysicalMaterial::GetFirearmDecalSize() const { return FirearmDecalSize; }

FRotator UCloud9PhysicalMaterial::GetFirearmDecalRotation(FVector Normal) const
{
let RandomRotation = FMath::RandRange(FirearmDecalRotationMin, FirearmDecalRotationMax);
let BaseRotation = Normal.Rotation();
let CorrectiveRotation = FMath::IsNearlyEqual(Normal.Z, 1.0f, 0.1f)
? FRotator{DecalVPitch, RandomRotation, DecalVRoll}
: FRotator{RandomRotation, DecalHYaw, DecalHRoll};
return BaseRotation + CorrectiveRotation;
return GetNormalSurfaceRotation(Normal, RandomRotation);
}

UMaterialInterface* UCloud9PhysicalMaterial::GetRandomBackgroundDecal() const
{
return GetRandomItem(FirearmBackgroundDecals);
}

FVector UCloud9PhysicalMaterial::GetBackgroundDecalSize() const
{
return FirearmBackgroundDecalSize;
}

FVector UCloud9PhysicalMaterial::GetBackgroundDecalLocation(FVector Location, FVector Normal) const
{
let OffsetH = FMath::RandRange(FirearmBackgroundDecalOffsetMin, FirearmBackgroundDecalOffsetMax);
let OffsetV = FMath::RandRange(FirearmBackgroundDecalOffsetMin, FirearmBackgroundDecalOffsetMax);
return IsVertical(Normal)
? FVector{Location.X + OffsetH, Location.Y + OffsetV, Location.Z}
: FVector{Location.X + OffsetH, Location.Y, Location.Z + OffsetV};
}

FRotator UCloud9PhysicalMaterial::GetBackgroundDecalRotation(FVector Normal) const
{
let RandomRotation = FMath::RandRange(FirearmBackgroundDecalRotationMin, FirearmBackgroundDecalRotationMax);
return GetNormalSurfaceRotation(Normal, RandomRotation);
}


float UCloud9PhysicalMaterial::GetBackgroundDecalMaxDistance() const
{
return FirearmBackgroundDecalMaxDistance;
}

bool UCloud9PhysicalMaterial::TestBackgroundDecalProbability() const
{
return FMath::RandRange(0.0f, 1.0f) < FirearmBackgroundDecalProbability;
}

UNiagaraSystem* UCloud9PhysicalMaterial::GetRandomFirearmSquib() const { return GetRandomItem(FirearmEffects); }
66 changes: 57 additions & 9 deletions Source/Cloud9/Physicals/Cloud9PhysicalMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,83 @@ class CLOUD9_API UCloud9PhysicalMaterial : public UPhysicalMaterial
UMaterialInterface* GetRandomFirearmDecal() const;
UNiagaraSystem* GetRandomFirearmSquib() const;

UMaterialInterface* GetRandomBackgroundDecal() const;
FVector GetBackgroundDecalSize() const;
FVector GetBackgroundDecalLocation(FVector Location, FVector Normal) const;
FRotator GetBackgroundDecalRotation(FVector Normal) const;
float GetBackgroundDecalMaxDistance() const;
bool TestBackgroundDecalProbability() const;

FVector GetFirearmDecalSize() const;
FRotator GetFirearmDecalRotation(FVector Normal) const;

protected:
UPROPERTY(EditDefaultsOnly, Category=Decal)
UPROPERTY(EditDefaultsOnly, Category="Firearm Hit Decal")
TSet<UMaterialInterface*> FirearmDecals;

UPROPERTY(EditDefaultsOnly, Category=Decal)
UPROPERTY(EditDefaultsOnly, Category="Firearm Hit Decal")
FVector FirearmDecalSize;

UPROPERTY(EditDefaultsOnly, Category=Decal, meta=(UIMin="-180", UIMax="180", ClampMin="-180", ClampMax="180"))
UPROPERTY(EditDefaultsOnly, Category="Firearm Hit Decal",
meta=(UIMin="-180", UIMax="180", ClampMin="-180", ClampMax="180"))
float FirearmDecalRotationMin;

UPROPERTY(EditDefaultsOnly, Category=Decal, meta=(UIMin="-180", UIMax="180", ClampMin="-180", ClampMax="180"))
UPROPERTY(EditDefaultsOnly, Category="Firearm Hit Decal",
meta=(UIMin="-180", UIMax="180", ClampMin="-180", ClampMax="180"))
float FirearmDecalRotationMax;

UPROPERTY(EditDefaultsOnly, Category=Effect)
UPROPERTY(EditDefaultsOnly, Category="Firearm Hit Effect")
TSet<UNiagaraSystem*> FirearmEffects;

UPROPERTY(EditDefaultsOnly, Category=Decal)

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal")
TSet<UMaterialInterface*> FirearmBackgroundDecals;

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal")
FVector FirearmBackgroundDecalSize;

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal",
meta=(UIMin="-100", UIMax="100", ClampMin="-100", ClampMax="100"))
float FirearmBackgroundDecalOffsetMin;

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal",
meta=(UIMin="-100", UIMax="100", ClampMin="-100", ClampMax="100"))
float FirearmBackgroundDecalOffsetMax;

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal",
meta=(UIMin="-180", UIMax="180", ClampMin="-180", ClampMax="180"))
float FirearmBackgroundDecalRotationMin;

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal",
meta=(UIMin="-180", UIMax="180", ClampMin="-180", ClampMax="180"))
float FirearmBackgroundDecalRotationMax;

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal",
meta=(UIMin="0", UIMax="1.0", ClampMin="0", ClampMax="1.0"))
float FirearmBackgroundDecalProbability;

UPROPERTY(EditDefaultsOnly, Category="Firearm Background Decal",
meta=(UIMin="0", UIMax="1000", ClampMin="0", ClampMax="1000"))
float FirearmBackgroundDecalMaxDistance;


UPROPERTY(EditDefaultsOnly, Category="Grenade Hit Decal")
TSet<UMaterialInterface*> GrenadeDecals;

UPROPERTY(EditDefaultsOnly, Category=Effect)
UPROPERTY(EditDefaultsOnly, Category="Grenade Hit Decal")
FVector GrenadeDecalSize;

UPROPERTY(EditDefaultsOnly, Category="Grenade Hit Effect")
TSet<UNiagaraSystem*> GrenadeEffects;

UPROPERTY(EditDefaultsOnly, Category=Decal)

UPROPERTY(EditDefaultsOnly, Category="Melee Hit Decal")
TSet<UMaterialInterface*> MeleeDecals;

UPROPERTY(EditDefaultsOnly, Category=Effect)
UPROPERTY(EditDefaultsOnly, Category="Melee Hit Decal")
FVector MeleeDecalSize;

UPROPERTY(EditDefaultsOnly, Category="Melee Hit Effect")
TSet<UNiagaraSystem*> MeleeEffects;

private:
Expand Down
47 changes: 45 additions & 2 deletions Source/Cloud9/Weapon/Classes/Cloud9WeaponFirearm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,60 @@ EFirearmFireStatus ACloud9WeaponFirearm::Fire(
true);
}

if (let FirearmDecalMaterial = PhysicalMaterial->GetRandomFirearmDecal(); IsValid(FirearmDecalMaterial))
if (let HitDecal = PhysicalMaterial->GetRandomFirearmDecal(); IsValid(HitDecal))
{
GetWorld() | EUWorld::SpawnDecal{
.Material = FirearmDecalMaterial,
.Material = HitDecal,
.DecalSize = PhysicalMaterial->GetFirearmDecalSize(),
.Location = LineHit.Location,
.Rotator = PhysicalMaterial->GetFirearmDecalRotation(LineHit.Normal),
.Owner = DamagedActor,
.Instigator = Character
};
}

if (let BackgroundDecal = PhysicalMaterial->GetRandomBackgroundDecal(); IsValid(BackgroundDecal))
{
if (PhysicalMaterial->TestBackgroundDecalProbability())
{
var BackgroundCollisionParams = FCollisionQueryParams::DefaultQueryParam;
BackgroundCollisionParams.AddIgnoredActor(DamagedActor);

let StartBackgroundLocation = LineHit.Location;
let EndBackgroundLocation = FMath::Lerp(
FVector{StartBackgroundLocation},
StartBackgroundLocation + Direction,
PhysicalMaterial->GetBackgroundDecalMaxDistance());

FHitResult BackgroundHit;
let IsBackgroundHit = GetWorld()->LineTraceSingleByChannel(
BackgroundHit,
StartBackgroundLocation,
EndBackgroundLocation,
TRACE_CHANNEL,
BackgroundCollisionParams);

if (IsBackgroundHit)
{
let BackgroundActor = LineHit.Actor.Get();
if (not IsValid(BackgroundActor))
{
log(Error, "[Weapon='%s'] Background actor is invalid on hit", *GetName());
return EFirearmFireStatus::Success;
}

GetWorld() | EUWorld::SpawnDecal{
.Material = BackgroundDecal,
.DecalSize = PhysicalMaterial->GetBackgroundDecalSize(),
.Location = PhysicalMaterial->GetBackgroundDecalLocation(
BackgroundHit.Location, BackgroundHit.Normal),
.Rotator = PhysicalMaterial->GetBackgroundDecalRotation(BackgroundHit.Normal),
.Owner = BackgroundActor,
.Instigator = Character
};
}
}
}
}
}

Expand Down

0 comments on commit 23683b1

Please sign in to comment.