Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include "CoreMinimal.h"
- #include "GameFramework/Actor.h"
- #include "SWeapon.generated.h"
- class USkeletalMeshComponent;
- class UDamageType;
- class UParticleSystem;
- UCLASS()
- class CPPSHOOTER_API ASWeapon : public AActor
- {
- GENERATED_BODY()
- public:
- // Sets default values for this actor's properties
- ASWeapon();
- protected:
- virtual void BeginPlay() override;
- UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
- USkeletalMeshComponent* MeshComp;
- void PlayFireEffects(FVector TraceEnd);
- UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
- TSubclassOf<UDamageType> DamageType;
- UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
- FName MuzzleSocketName;
- UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
- FName TracerTargetName;
- UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
- UParticleSystem* MuzzleEffect;
- UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
- UParticleSystem* DefaultImpactEffect;
- UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
- UParticleSystem* FleshImpactEffect;
- UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
- UParticleSystem* TracerEffect;
- UPROPERTY(EditDefaultsOnly, Category = "Weapon")
- TSubclassOf<UCameraShake> FireCamShake;
- UPROPERTY(EditDefaultsOnly, Category = "Weapon")
- float BaseDamage;
- FTimerHandle TimerHandle_Reloading;
- UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = "Weapon", meta = (ToolTip = "Number of bullets available to shoot"))
- int32 TotalAmmo = 1000;
- UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon", meta = (ToolTip = "Max number of bullets in the chamber"))
- int32 MaxChamberAmmo = 10;
- UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = "Weapon", meta = (ToolTip = "Current number of bullets in the chamber"))
- int32 CurrentChamberAmmo = 10;
- UPROPERTY(Replicated, VisibleAnywhere, BlueprintReadOnly, Category = "Weapon", meta = (ToolTip = "Flag to know when the weapon is in reloading time"))
- bool IsReloading = false;
- UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon", meta = (ToolTip = "Time in seconds to reload the weapon"))
- float TimeToReload = 0.9f;
- void Fire();
- FTimerHandle TimerHandle_TimeBetweenShots;
- float LastFireTime;
- /*RPM - Bullets per minute fired by weapon */
- UPROPERTY(EditDefaultsOnly, Category = "Weapon")
- float RateOfFire;
- // Derived from rate of fire
- float TimeBetweenShots;
- UFUNCTION(BlueprintCallable, Category = "Weapons")
- virtual bool CheckAmmo() {
- if (IsReloading) return false;
- if (!HasAmmo())
- {
- if (CanReloadAmmo())
- {
- Reload();
- return false;
- }
- else
- {
- //No ammo
- StopFire();
- return false;
- }
- return true;
- UFUNCTION(BlueprintCallable, Category = "Weapons")
- virtual bool HasAmmo() const { return CurrentChamberAmmo > 0 && TotalAmmo > 0; }
- UFUNCTION(BlueprintCallable, Category = "Weapons")
- virtual bool CanReloadAmmo() const { return TotalAmmo > 0 && CurrentChamberAmmo < MaxChamberAmmo; }
- UFUNCTION(BlueprintCallable, Category = "Weapons")
- virtual void Reload() {
- if (IsReloading) return;
- IsReloading = true;
- int32 oldammo = CurrentChamberAmmo;
- int32 newammo = MaxChamberAmmo - oldammo;
- CurrentChamberAmmo = TotalAmmo >= newammo ? newammo : TotalAmmo;
- TotalAmmo -= newammo;
- GetWorldTimerManager().SetTimer(
- TimerHandle_Reloading, [this]() {
- IsReloading = false; }, TimeToReload, false, TimeToReload);
- }
- public:
- void StartFire();
- void StopFire();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement