Skip to content

Instantly share code, notes, and snippets.

@xjjon
Last active December 30, 2022 22:23
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 xjjon/6bbdeaaf2c6d31dc8284b087ed8c88f1 to your computer and use it in GitHub Desktop.
Save xjjon/6bbdeaaf2c6d31dc8284b087ed8c88f1 to your computer and use it in GitHub Desktop.
public abstract class ScriptableBuff : ScriptableObject
{
/**
* Time duration of the buff in seconds.
*/
public float Duration;
/**
* Duration is increased each time the buff is applied.
*/
public bool IsDurationStacked;
/**
* Effect value is increased each time the buff is applied.
*/
public bool IsEffectStacked;
// Time between each tick
public float TickTime;
public abstract TimedBuff InitializeBuff(GameObject obj);
}
public abstract class TimedBuff
{
protected float Duration;
protected float TickTime; // time until ApplyTickEffect() is called
protected int EffectStacks;
public ScriptableBuff Buff { get; }
protected readonly GameObject Obj;
public bool IsFinished;
public TimedBuff(ScriptableBuff buff, GameObject obj)
{
Buff = buff;
Obj = obj;
}
public void Tick(float delta)
{
Duration -= delta;
if (Duration <= 0)
{
End();
IsFinished = true;
}
TickTime -= delta;
if (TickTime <= 0) {
ApplyTickEffect();
TickTime = Buff.TickTime;
}
}
/**
* Activates buff or extends duration if ScriptableBuff has IsDurationStacked or IsEffectStacked set to true.
*/
public void Activate()
{
if (Buff.IsEffectStacked || Duration <= 0)
{
ApplyEffect();
EffectStacks++;
}
if (Buff.IsDurationStacked || Duration <= 0)
{
Duration += Buff.Duration;
}
TickTime = Buff.TickTime;
}
protected abstract void ApplyEffect();
// Apply some 'tick' effect such as damage (bleed, poision, etc)
protected abstract void ApplyTickEffect();
public abstract void End();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment