Created
September 8, 2016 03:59
-
-
Save bendangelo/67302261204126e8e1132298f0df9c10 to your computer and use it in GitHub Desktop.
Unity3D class used for health bars and anything else that has a min / max.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class StatRange { | |
public int min { get; set; } | |
public int max { get; set; } | |
public int value { get; set; } | |
public StatRange(int min = 0, int max = 0) { | |
this.max = max; | |
this.min = min; | |
} | |
public void Reset() { | |
value = max; | |
} | |
public float Percent { | |
get { return (float)value / max; } | |
set { this.value = (int)((float)max * value); } | |
} | |
public void Add(int val) { | |
value += val; | |
Clamp(); | |
} | |
public void Minus(int val) { | |
value -= val; | |
Clamp(); | |
} | |
public void Clamp() { | |
value = Mathf.Clamp(value, min, max); | |
} | |
public bool IsMin { | |
get { return value == min; } | |
} | |
public bool IsMax { | |
get { return value == max; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment