Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
Added Reloading and a simple ammo counter
Browse files Browse the repository at this point in the history
Modified SCREENLOG sow that it shows the class/line so that the logs are better found
  • Loading branch information
RoyAwesome committed Nov 5, 2014
1 parent 271e35d commit 363a3d6
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 34 deletions.
1 change: 1 addition & 0 deletions Config/DefaultInput.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bUseMouseForTouch=False
+ActionMappings=(ActionName="PreviousWeapon",Key=MouseScrollDown,bShift=False,bCtrl=False,bAlt=False,bCmd=False)
+ActionMappings=(ActionName="LastWeapon",Key=Q,bShift=False,bCtrl=False,bAlt=False,bCmd=False)
+ActionMappings=(ActionName="ChangeFiremode",Key=B,bShift=False,bCtrl=False,bAlt=False,bCmd=False)
+ActionMappings=(ActionName="Reload",Key=R,bShift=False,bCtrl=False,bAlt=False,bCmd=False)
+AxisMappings=(AxisName="MoveForward",Key=W,Scale=1.000000)
+AxisMappings=(AxisName="MoveForward",Key=S,Scale=-1.000000)
+AxisMappings=(AxisName="MoveForward",Key=Up,Scale=1.000000)
Expand Down
Binary file added Content/Blueprints/AmmoCounter.uasset
Binary file not shown.
Binary file modified Content/Blueprints/EmpiresCharacter.uasset
Binary file not shown.
Binary file modified Content/Blueprints/Weapons/BE_Heavy_Rifle.uasset
Binary file not shown.
Binary file modified Content/Blueprints/Weapons/BE_Rifle.uasset
Binary file not shown.
11 changes: 10 additions & 1 deletion Source/Empires2/Private/Empires2Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void AEmpires2Character::SetupPlayerInputComponent(class UInputComponent* InputC
InputComponent->BindAction("PreviousWeapon", IE_Pressed, this, &AEmpires2Character::SelectPreviousWeapon);
InputComponent->BindAction("LastWeapon", IE_Pressed, this, &AEmpires2Character::SelectLastWeapon);
InputComponent->BindAction("ChangeFiremode", EInputEvent::IE_Pressed, this, &AEmpires2Character::ChangeFiremode);
InputComponent->BindAction("Reload", EInputEvent::IE_Pressed, this, &AEmpires2Character::ReloadWeapon);


//Movement
Expand Down Expand Up @@ -237,7 +238,15 @@ void AEmpires2Character::SelectLastWeapon()
void AEmpires2Character::ChangeFiremode()
{
UBaseInfantryWeapon* Weapon = GetActiveWeapon();
if (Weapon == nullptr) return; //No weapon? Don't bother chaning firemode
if (Weapon == nullptr) return; //No weapon? Don't bother changing firemode

Weapon->NextFiremode();
}

void AEmpires2Character::ReloadWeapon()
{
UBaseInfantryWeapon* Weapon = GetActiveWeapon();
if (Weapon == nullptr) return; //No weapon? Don't bother reloading

Weapon->Reload();
}
81 changes: 75 additions & 6 deletions Source/Empires2/Private/Weapons/BaseEmpiresWeapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ UBaseEmpiresWeapon::UBaseEmpiresWeapon(const class FPostConstructInitializePrope

GunOffset = FVector(100.0f, 30.0f, 10.0f);
ActiveFiremode = 0;

bReloading = false;
}

//////////////////////GENERAL
Expand Down Expand Up @@ -97,13 +97,24 @@ bool UBaseEmpiresWeapon::CanFire()
{
UBaseFiremode* firemode = GetActiveFiremode();
if (firemode == nullptr) return false; //No active firemode
if (bReloading) return false; //Can't fire when we are reloading
if (GetCurrentAmmoPool().AmmoInClip <= 0) return false; //If we have no ammo, we can't fire

//TODO: Check if the firemode is capable of firing


return true;
}

void UBaseEmpiresWeapon::BeginFire()
{
if (!CanFire()) return; //Don't Fire if we can't fire
if (GetCurrentAmmoPool().AmmoInClip <= 0) //If we are out of ammo, attempt to reload
{
Reload();
return;
}

UBaseFiremode* firemode = GetActiveFiremode();
check(firemode);
firemode->BeginFire();
Expand Down Expand Up @@ -155,12 +166,30 @@ void UBaseEmpiresWeapon::FireShot()
if (GetAmmoInClip() <= 0)
{
EndFire();
//Reload();
Reload();
}

}

///////////////////////////////////// FIREMODES
FWeaponData UBaseEmpiresWeapon::GetActiveFiremodeData()
{

return FiremodeData[ActiveFiremode];
}


UBaseFiremode* UBaseEmpiresWeapon::GetActiveFiremode()
{
return Firemodes[ActiveFiremode];
}


FAmmoPool UBaseEmpiresWeapon::GetCurrentAmmoPool()
{
return AmmoPools[FiremodeData[ActiveFiremode].AmmoPoolIndex];
}


void UBaseEmpiresWeapon::NextFiremode()
{
Expand Down Expand Up @@ -221,10 +250,9 @@ FAmmoPool UBaseEmpiresWeapon::GetAmmoPool(int32 FromAmmoPool)

void UBaseEmpiresWeapon::ConsumeAmmo(int32 HowMuch, int32 FromAmmoPool)
{
FAmmoPool AmmoPool = GetAmmoPool(FromAmmoPool);
int32 AmmoPoolidx = FromAmmoPool == CurrentAmmopool ? GetActiveFiremodeData().AmmoPoolIndex : FromAmmoPool;

AmmoPool.AmmoInClip -= HowMuch;
AmmoPool.CurrentAmmo -= HowMuch;
AmmoPools[AmmoPoolidx].AmmoInClip -= HowMuch;
}

int32 UBaseEmpiresWeapon::GetAmmoInClip(int32 FromAmmoPool)
Expand All @@ -249,7 +277,48 @@ void UBaseEmpiresWeapon::AddAmmo(int32 Ammount, int32 ToAmmoPool)
}
}

void UBaseEmpiresWeapon::Reload(int32 AmmoPool)
void UBaseEmpiresWeapon::Reload()
{
if (GetTotalAmmo() <= 0) return; //We don't have any ammo to reload

SCREENLOG(TEXT("Starting Reload"));

//Get the current reload animation
UAnimMontage* ReloadAnim = GetActiveWeaponAnimationSet().ReloadAnimation;
float ReloadTime = GetAmmoPool().ReloadTime;

if (ReloadAnim == nullptr)
{
PlayAnimation(ReloadAnim);
}

GetWorld()->GetTimerManager().SetTimer(this, &UBaseEmpiresWeapon::DoReload, ReloadTime, false);

bReloading = true;
}

void UBaseEmpiresWeapon::DoReload()
{
SCREENLOG(TEXT("Ending Reload"));

int Idx = GetActiveFiremodeData().AmmoPoolIndex;


if (AmmoPools[Idx].CurrentAmmo < AmmoPools[Idx].ClipSize) //We don't have enough ammo to fill out a full clip
{
AmmoPools[Idx].AmmoInClip = AmmoPools[Idx].CurrentAmmo; //So set the ammo to whatever is left
AmmoPools[Idx].CurrentAmmo = 0;
}
else //Otherwise, max out our clip
{
//Figure out how many bullets we need
int32 bulletsNeeded = AmmoPools[Idx].ClipSize - AmmoPools[Idx].AmmoInClip;

//Max out the clip size
AmmoPools[Idx].AmmoInClip = AmmoPools[Idx].ClipSize;
//And take the diff from the pool
AmmoPools[Idx].CurrentAmmo -= bulletsNeeded;
}
bReloading = false;

}
2 changes: 1 addition & 1 deletion Source/Empires2/Public/Empires2.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DECLARE_LOG_CATEGORY_EXTERN(EmpiresGameplay, Display, All);
DECLARE_LOG_CATEGORY_EXTERN(EmpiresNetwork, Log, All);

#define SCREENLOG(text, ...) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::White, FString::Printf__VA(text, ##__VA_ARGS__))
#define SCREENLOG(text, ...) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::White, FString::Printf__VA(TEXT("[%s:%d] %s"), TEXT(__FUNCTION__), __LINE__, *FString::Printf__VA(text, ##__VA_ARGS__)))


#endif
7 changes: 5 additions & 2 deletions Source/Empires2/Public/Empires2Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ class AEmpires2Character : public ACharacter
void DrawWeapon(UBaseInfantryWeapon* Weapon);
void SwitchToWeapon(EInfantryInventorySlots::Type Weapon);

UFUNCTION(BlueprintCallable, Category = Weapon)
UBaseInfantryWeapon* GetActiveWeapon();

/* Weapon Input Events*/
/* Weapon Input Events */
void SelectNextWeapon();
void SelectPreviousWeapon();
void SelectLastWeapon();

void ChangeFiremode();
void ReloadWeapon();
/* End Weapon Input Events */

protected:

EInfantryInventorySlots::Type SelectedWeapon;
Expand Down
56 changes: 32 additions & 24 deletions Source/Empires2/Public/Weapon/BaseEmpiresWeapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ struct FAmmoPool
int32 MaxAmmo;
UPROPERTY(EditDefaultsOnly, Category = General)
int32 ClipSize;
UPROPERTY(EditDefaultsOnly, Category = General)
float ReloadTime;

UPROPERTY(EditDefaultsOnly, Category = Ammo)
TSubclassOf<class ABaseEmpiresProjectile> ProjectileClass;


UPROPERTY(BlueprintReadWrite, Category=Ammo)
int32 CurrentAmmo;
UPROPERTY(BlueprintReadWrite, Category = Ammo)
int32 AmmoInClip;

FAmmoPool()
Expand All @@ -98,23 +104,26 @@ struct FWeaponAnimationSet
{
GENERATED_USTRUCT_BODY()
public:
/** Sound to play each time we fire */
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Display)
class USoundBase* FireSound;


/** AnimMontage to play each time we fire */
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Display)
class UAnimMontage* FireAnimation;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Display)
USoundBase* ChangeFiremodeSound;
class UAnimMontage* FireAnimation;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Display)
UAnimMontage* ChangeFiremodeAnimation;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Display)
UAnimMontage* ReloadAnimation;

/** Sound to play each time we fire */
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Sound)
class USoundBase* FireSound;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Sound)
USoundBase* ChangeFiremodeSound;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Sound)
USoundBase* ReloadSound;
};

/**
Expand Down Expand Up @@ -207,22 +216,14 @@ class EMPIRES2_API UBaseEmpiresWeapon : public UObject
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Firemodes)
TArray<FAmmoPool> AmmoPools;

UFUNCTION(BlueprintCallable, Category = Firemode)
virtual FWeaponData GetActiveFiremodeData();

virtual FWeaponData GetActiveFiremodeData()
{

return FiremodeData[ActiveFiremode];
}
UFUNCTION(BlueprintCallable, Category = Firemode)
virtual UBaseFiremode* GetActiveFiremode();

virtual UBaseFiremode* GetActiveFiremode()
{
return Firemodes[ActiveFiremode];
}

virtual FAmmoPool GetCurrentAmmoPool()
{
return AmmoPools[FiremodeData[ActiveFiremode].AmmoPoolIndex];
}
UFUNCTION(BlueprintCallable, Category = Firemode)
virtual FAmmoPool GetCurrentAmmoPool();

void NextFiremode();

Expand All @@ -241,16 +242,23 @@ class EMPIRES2_API UBaseEmpiresWeapon : public UObject
public:
virtual void ConsumeAmmo(int32 HowMuch = 1, int32 FromAmmoPool = CurrentAmmopool );

//UFUNCTION(BlueprintCallable, Category = Firemode) //TODO: Figure out how to do default params with UFUNCTIONS
virtual int32 GetAmmoInClip(int32 FromAmmoPool = CurrentAmmopool);

//UFUNCTION(BlueprintCallable, Category = Firemode)
virtual int32 GetTotalAmmo(int32 FromAmmoPool = CurrentAmmopool);

virtual void AddAmmo(int32 Ammount, int32 ToAmmoPool = CurrentAmmopool);

virtual void Reload(int32 AmmoPool = CurrentAmmopool);
virtual void Reload();

virtual void DoReload();

protected:
FAmmoPool GetAmmoPool(int32 FromAmmoPool);
FAmmoPool GetAmmoPool(int32 FromAmmoPool = CurrentAmmopool);

bool bReloading;



};

0 comments on commit 363a3d6

Please sign in to comment.