Skip to content

Instantly share code, notes, and snippets.

@kevinruscoe
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinruscoe/daedad311f8bc3683fc0 to your computer and use it in GitHub Desktop.
Save kevinruscoe/daedad311f8bc3683fc0 to your computer and use it in GitHub Desktop.
Conditioned Weapon for UFPS
using UnityEngine;
using System.Collections;
public class ConditionedWeapon : MonoBehaviour {
private vp_FPPlayerEventHandler m_Player;
public float condition = 1f; // Item's condition ( 0% to 100% )
public float useDamage = .5f; // Each use removes x% off the condition
public bool isDamageRanged = false; // Should the damage be ranged? If it isn't it'll use the useDamage value, if it is it'll pick a random value between minRangeDamage and maxRangeDamage
public float minRangeDamage = .01f;
public float maxRangeDamage = .025f;
private float useDamageMultiplier = 1f; // Unused at the moment, but perhaps a skill, or perk, can increase/decrease the final amount of damage? For example a "caring hands" perk might only do 50% of the damanage a normal player would
void Awake(){
m_Player = GetComponentInParent<vp_FPPlayerEventHandler>();
}
// The following 2 methods registers this class to the EventHandler. That means we can listen to, and fire, events across the UFPS system.
protected virtual void OnEnable(){
if (m_Player != null){
m_Player.Register(this);
}
}
protected virtual void OnDisable(){
if (m_Player != null){
m_Player.Unregister(this);
}
}
// The CanStart_Attack() event is used throughout the codebase (both ours, and UFPS). *All* CanStart_Attack()'s must return true before the Attack() method can run.
// This means we have a opponunity to interupt the Attack() method, if the weapon is too damaged to use, prevent the Attack() method from being run.
bool CanStart_Attack(){
return !isTooDamagedToUse();
}
// This method is run when the Attack() method has been started (usually called by UFPS when the shoot button is used).
// So it's a good place to reduce our items condition, but *only* if we have ammo (The Attack() is run regardless of ammo, so he weapon shouldn't break if we aren't using it).
void OnStart_Attack(){
if( m_Player.CurrentWeaponAmmoCount.Get() > 0 ){
condition = condition - calculateDamage();
}
}
// Simply returns true or false if the condition is less than or equal to 0
private bool isTooDamagedToUse(){
return ( condition <= 0 );
}
// Returns the Damage caused by a use.
private float calculateDamage(){
float damage = 0f;
if( isDamageRanged ){
damage = Random.Range(minRangeDamage, maxRangeDamage);
}else{
damage = useDamage;
}
return damage * useDamageMultiplier;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment