- Home /
Question by
ObviousQuantity · May 02 at 01:02 AM ·
inheritance
Advice on my simple effect system?
I'm currently practicing with inheritance by trying to make a simple effect/buff system and I would like some advice on how I could improve the code I did because it has been quite the struggle, but I finally seem to have something that works it can add and remove an effect and after the duration ends it gets removed
public class Player : MonoBehaviour
{
public int speed = 5;
public int jump = 10;
public List<BaseEffect> effects = new List<BaseEffect>();
void Update()
{
if (Input.GetKeyDown("q"))
{
AddEffect(new SpeedEffect());
}
foreach (BaseEffect effect in effects)
{
effect.UpdateDuration();
if (effect.isFinished)
{
RemoveEffect(effect);
}
}
}
public void AddEffect(BaseEffect effect)
{
effects.Add(effect);
effect.ApplyEffect(gameObject);
}
public void RemoveEffect(BaseEffect effect)
{
effects.Remove(effect);
effect.RemoveEffect(gameObject);
}
}
public abstract class BaseEffect : ScriptableObject
{
public enum EffectType
{
harm,
self,
help
}
public EffectType type;
public float duration;
public string description;
public bool isFinished;
public void UpdateDuration()
{
duration -= Time.deltaTime;
if (duration <= 0)
{
isFinished = true;
}
}
public abstract void Effect(GameObject target);
public abstract void RemoveEffect(GameObject target);
}
public class SpeedEffect : BaseEffect
{
public SpeedEffect()
{
type = EffectType.harm;
duration = 5;
description = "Speed up";
}
public override void ApplyEffect(GameObject target)
{
target.GetComponent<Player>().speed += 5;
}
public override void RemoveEffect(GameObject target)
{
target.GetComponent<Player>().speed -= 5;
}
}
Comment
Your answer

Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Inheritance with Guns 0 Answers
Interaction script 2 Answers