view api/Objects/Stats.cs @ 38:548cfc776f54

Fixes #34 - Remove "Choices" and "Base/Increment" from Category * Remove Choices and Base/Increment from code Re #47: Remove magic numbers * Replace "-1" magic number with WarFoundryCore.INFINITY * Use INFINITY instead of -1 in code * Use INF in schemas instead of -1 * Handle and parse INF as a special value in XML Factory
author IBBoard <dev@ibboard.co.uk>
date Thu, 19 Mar 2009 20:11:07 +0000
parents 306558904c2a
children e6200220ece3
line wrap: on
line source

// This file (Stats.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
//
// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

using System;
using System.Collections.Generic;
using IBBoard.Lang;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Stats defines the statistic/attribute values for an entity (for example a unit or any of their equipment that has a stat line) paired against a <see cref=" SystemStats"/> stat line definition.
	/// </summary>
	public class Stats
	{
		private Stat[] stats;
		private List<string> statOrder;
		private SystemStats sysStats;
		
		public Stats(SystemStats systemStats)
		{
			sysStats = systemStats;
		}
		
		public Stat[] StatsArray
		{
			get { return (Stat[])stats.Clone(); }
		}
		
		protected internal void SetStats(List<Stat> statList)
		{
			stats = statList.ToArray();
			statOrder = new List<string>();
			
			foreach (Stat stat in statList)
			{
				statOrder.Add(stat.ParentSlot.Name);
			}
		}
		
		public Stat this[string id]
		{
			get
			{
				Stat stat = null;
				int pos = statOrder.IndexOf(id);
				
				try
				{
					stat = this[pos];
				}
				catch (ArgumentException ex)
				{
					throw new ArgumentException(Translation.GetTranslation("InvalidStatPos", "Invalid statistic ID {0} for stats based on system stats set {1}", new object[]{id, sysStats.ID}), ex);
				}
				
				return stat;
			}
		}
		
		public Stat this[int pos]
		{
			get
			{
				if (pos < stats.Length && pos >= 0)
				{
					return stats[pos];
				}
				else
				{
					throw new ArgumentException(Translation.GetTranslation("InvalidStatPos", "Invalid statistic position {0} for stats based on system stats set {1}", new object[]{pos, sysStats.ID})); 
				}
			}
		}
		
		public int StatCount
		{
			get { return stats.Length; }
		}
	}
}