Skip to content

Commit

Permalink
End of Day 19
Browse files Browse the repository at this point in the history
Refactored buff system
  • Loading branch information
Papiertig0r committed Jan 19, 2018
1 parent 421feb1 commit 8bca49f
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 165 deletions.
10 changes: 0 additions & 10 deletions Assets/Buffs.meta

This file was deleted.

14 changes: 0 additions & 14 deletions Assets/Buffs/CurrentHealth.asset

This file was deleted.

10 changes: 0 additions & 10 deletions Assets/Buffs/CurrentHealth.asset.meta

This file was deleted.

23 changes: 4 additions & 19 deletions Assets/Editor/CharaEditor.cs
Expand Up @@ -5,23 +5,6 @@
[CustomEditor(typeof(CharaController), true)]
public class CharaEditor : Editor
{
private float damage;

private void OnEnable()
{
if (EditorPrefs.HasKey("damage"))
{
damage = EditorPrefs.GetFloat("damage");
}
}

private void OnDisable()
{
if(!Application.isPlaying)
{
EditorPrefs.SetFloat("damage", damage);
}
}

public override void OnInspectorGUI()
{
Expand All @@ -32,10 +15,12 @@ public override void OnInspectorGUI()
EditorGUILayout.Space();
EditorGUILayout.LabelField("Editor functionalites", EditorStyles.boldLabel);

damage = EditorGUILayout.FloatField("Damage", damage);
if (GUILayout.Button("Take damage"))
{
chara.TakeDamage(damage);
//RepeatTimedBuff rtb = new RepeatTimedBuff(-2f, 10f, 1f);
//rtb.Apply(chara.stats, chara.stats.health);
TimedBuff tb = new TimedBuff(-2f, 10f);
tb.Apply(chara.stats, chara.stats.healthRegen);
}
}
}
4 changes: 2 additions & 2 deletions Assets/HealthSliderUI.cs
Expand Up @@ -16,7 +16,7 @@ public void UpdateHealth(Stat health)
{
slider.minValue = health.min;
slider.maxValue = health.max;
slider.value = health.value;
slider.fillRect.GetComponent<Image>().color = Color.Lerp(Color.red, Color.green, health.value / health.max);
slider.value = health.current;
slider.fillRect.GetComponent<Image>().color = Color.Lerp(Color.red, Color.green, health.current / health.max);
}
}
21 changes: 11 additions & 10 deletions Assets/Scripts/Buff.cs
Expand Up @@ -2,20 +2,21 @@
using System.Collections.Generic;
using UnityEngine;

public class Buff : ScriptableObject
[System.Serializable]
public class Buff
{
public GameObject particle;
public void Apply(CharaController target, float value)
public float value;

public Buff(float value)
{
ApplyBuff(target, value);
if (value > 0f)
{
Instantiate<GameObject>(particle, target.transform);
}
this.value = value;

Debug.Log("Created buff");
}

protected virtual void ApplyBuff(CharaController target, float value)
public virtual void Apply(Stats stats, Stat stat)
{

stat -= value;
Debug.Log("Applied buff");
}
}
4 changes: 2 additions & 2 deletions Assets/Scripts/Buff.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions Assets/Scripts/BuffCurrentHealth.cs

This file was deleted.

9 changes: 0 additions & 9 deletions Assets/Scripts/BuffDuration.cs

This file was deleted.

135 changes: 89 additions & 46 deletions Assets/Scripts/Character/Stat.cs
@@ -1,96 +1,134 @@
using UnityEngine;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Stat
{
public float max;
public float min;
public float value;
[HideInInspector]
public float max
{
get
{
float ret = _max;
foreach(Buff buff in buffMax)
{
ret += buff.value;
}
return ret;
}
set
{
_max = value;
MaxChanged();
StatChanged();
}
}

[HideInInspector]
public float min
{
get
{
float ret = _min;
foreach (Buff buff in buffMin)
{
ret += buff.value;
}
return ret;
}
set
{
_min = value;
MinChanged();
StatChanged();
}
}

public float current;

public delegate void HandleOutOfRange();
public HandleOutOfRange exceed;
public HandleOutOfRange fallBelow;

public delegate void OnChange(float value);
public OnChange valueChanged;
public OnChange currentChanged;
public OnChange maxChanged;
public OnChange minChanged;

public delegate void OnStatChange(Stat stat);
public OnStatChange statChanged;

public List<Buff> buffMax = new List<Buff>();
public List<Buff> buffMin = new List<Buff>();

[SerializeField]
private float _max;
[SerializeField]
private float _min;

public Stat()
{
value = max;
current = max;
}

public Stat(float value)
{
this.max = value;
this.value = value;
this.current = value;
}

public Stat(float min, float max)
{
this.min = min;
this.max = max;
this.value = max;
this.current = max;
}

public Stat(float value, float min, float max)
{
this.value = value;
this.current = value;
this.min = min;
this.max = max;
}

public void Max()
{
this.value = this.max;
if(valueChanged != null)
{
valueChanged.Invoke(this.value);
}
this.current = this.max;
}

public Vector3 ToVector3()
{
return new Vector3(this.min, this.value, this.max);
return new Vector3(this.min, this.current, this.max);
}

public void FromVector3(Vector3 v)
{
this.min = v.x;
this.value = v.y;
this.current = v.y;
this.max = v.z;

ValueChanged();
MaxChanged();
MinChanged();
StatChanged();
}

public void ValueChanged()
public void CurrentChanged()
{
if (valueChanged != null)
if (currentChanged != null)
{
valueChanged.Invoke(this.value);
currentChanged.Invoke(this.current);
}
}

public void MaxChanged()
{
if (maxChanged != null)
{
maxChanged.Invoke(this.value);
maxChanged.Invoke(this.current);
}
}

public void MinChanged()
{
if (minChanged != null)
{
minChanged.Invoke(this.value);
minChanged.Invoke(this.current);
}
}

Expand All @@ -115,47 +153,52 @@ public Vector3 Vector3
}
}

public void CheckOutOfBounds()
{
if (current >= max && exceed != null)
{
exceed.Invoke();
}
if (current <= min && fallBelow != null)
{
fallBelow.Invoke();
}
}

#region operator
public static Stat operator +(Stat s1, Stat s2)
{
s1.ValueChanged();
return s1 + s2.value;
return s1 + s2.current;
}

public static Stat operator +(Stat s, float change)
{
s.value += change;
if(s.value >= s.max && s.exceed != null)
{
s.exceed.Invoke();
}
s.value = Mathf.Clamp(s.value, s.min, s.max);
s.ValueChanged();
s.current += change;
s.CheckOutOfBounds();
s.current = Mathf.Clamp(s.current, s.min, s.max);

s.CurrentChanged();
s.StatChanged();
return s;
}

public static float operator *(Stat s, float multiplier)
{
return s.value * multiplier;
return s.current * multiplier;
}

public static Stat operator -(Stat s1, Stat s2)
{
s1.ValueChanged();
s1.StatChanged();
return s1 - s2.value;
return s1 - s2.current;
}

public static Stat operator -(Stat s, float change)
{
s.value -= change;
if (s.value <= s.min && s.fallBelow != null)
{
s.fallBelow.Invoke();
}
s.value = Mathf.Clamp(s.value, s.min, s.max);
s.ValueChanged();
s.current -= change;
s.CheckOutOfBounds();
s.current = Mathf.Clamp(s.current, s.min, s.max);

s.CurrentChanged();
s.StatChanged();
return s;
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Ingredients/Consumable.cs
Expand Up @@ -4,10 +4,10 @@

public class Consumable : MonoBehaviour, IConsumable
{
public Buff buff;

public float strength = 10f;
public void Consume(CharaController target)
{
buff.Apply(target, strength);

}
}

0 comments on commit 8bca49f

Please sign in to comment.