using UnityEngine;
using System;

public static class Int32Extensions 
{
	static public int SubtractToMinimum(this int value, int toSubtract, int minimum)
	{
		return Mathf.Max(value - toSubtract, minimum);
	}
	
	static public int AddToMaximum(this int value, int toAdd, int maximum)
	{
		return Mathf.Min(value + toAdd, maximum);
	}
	
	static public int AddWithinRange(this int value, int toAdd, int minimum, int maximum)
	{
		return Mathf.Max(Mathf.Min(value + toAdd, maximum), minimum);
	}
	
	static public bool TryParseWithHex(string s, out int n)
	{
		if(s.StartsWith("0x") || s.StartsWith("0X"))
		{
			try
			{
				n = Convert.ToInt32(s, 16);
				return true;
			}
			catch(Exception)
			{
				n = 0;
				return false;
			}
		}
		else
		{
			return int.TryParse(s, out n);
		}
	}	
}
