Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 8th, 2013  |  syntax: C#  |  size: 4.80 KB  |  views: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.IO;
  3. using SFML.Graphics;
  4. using SFML.Window;
  5.  
  6. namespace Blashyrkh.Game {
  7.     public enum MotionType : int
  8.     {
  9.         Walking = 0,
  10.         Jogging = 1
  11.     }
  12.  
  13.     public abstract class Character : GameObject, Activatable, Movable {
  14.         private IniParser CommonTraits;
  15.         private IniParser RaceTraits;
  16.         protected string Race;
  17.         protected Attribute[] Attributes;
  18.         protected float[] Speed;
  19.  
  20.         public Attribute this[AttributeName name] {
  21.             get { return this.Attributes[(int)name]; }
  22.             set { this.Attributes[(int)name] = value; }
  23.         }
  24.  
  25.         public Character()
  26.         {
  27.             // DO NOTHING.
  28.         }
  29.  
  30.         // FIXME: This function needs to be cleaned up.
  31.         public Character(string name, string race, Vector2u position)
  32.         : base(name, TextureType.Sprite, position)
  33.         {
  34.             // Load attributes/traits.
  35.             new LogMessage("Loading attributes for race {0}.", race);
  36.             this.CommonTraits = new IniParser(Path.Combine(Game.BasePath, "Data/Data/Races/Common.ini"));
  37.             this.RaceTraits = new IniParser(Path.Combine(Game.BasePath, "Data/Data/Races/", race + ".ini"));
  38.             // Set up the attributes.
  39.             this.Speed = new float[] {
  40.                 float.Parse(this.CommonTraits["Speed"]["Walking"]),
  41.                 float.Parse(this.CommonTraits["Speed"]["Jogging"])
  42.             };
  43.             this.Size = new Vector2u(
  44.                 uint.Parse(this.RaceTraits["Size"]["Width"]),
  45.                 uint.Parse(this.RaceTraits["Size"]["Height"])
  46.             );
  47.             AttributeName[] attribs = (AttributeName[])Enum.GetValues(typeof(AttributeName));
  48.             this.Attributes = new Attribute[attribs.Length];
  49.             foreach(AttributeName attrib in attribs) {
  50.                 string attribName = ((AttributeName)attrib).ToString();
  51.                 // Find the second capital letter. This is the
  52.                 // start of the property name. Everything before
  53.                 // that is the section name.
  54.                 // FIXME: It's not good to have the format of
  55.                 // the strings hard-coded like this, nor to have
  56.                 // a total lack of error-checking.
  57.                 string section = "", property = "";
  58.                 for (int i = 1; i < attribName.Length; ++i) {
  59.                     if (Char.IsUpper(attribName[i])) {
  60.                         section = attribName.Substring(0, i);
  61.                         property = attribName.Substring(i);
  62.                         break;
  63.                     }
  64.                 }
  65.                 // Store the values. Some of these variables
  66.                 // could do with renaming..
  67.                 string valuesStr = this.RaceTraits[section][property];
  68.                 string[] strValues = valuesStr.Split(' ');
  69.                 this[attrib] = new Attribute();
  70.                 this[attrib].Current = float.Parse(strValues[0]);
  71.                 this[attrib].Maximum = float.Parse(strValues[1]);
  72.                 this[attrib].Experience = float.Parse(strValues[2]);
  73.             }
  74.            
  75.         }
  76.  
  77.         public void DamageAttribute(AttributeName attribute, float damage)
  78.         {
  79.             int index = (int)attribute;
  80.             this.Attributes[index].Current -= damage;
  81.             // Make sure the value is between 0 and the maximum
  82.             // inclusive.
  83.             if (this.Attributes[index].Current < 0)
  84.                 this.Attributes[index].Current = 0;
  85.             else if (this.Attributes[index].Current > this.Attributes[index].Maximum)
  86.                 this.Attributes[index].Current = this.Attributes[index].Maximum;
  87.         }
  88.  
  89.         public virtual void Move(Direction direction, float time, FloatRect rect)
  90.         {
  91.             uint distance = (uint)(this.CurrentSpeed * time);
  92.             switch(direction) {
  93.             case Direction.Down:
  94.                 this.Position = new Vector2u(this.Position.X,
  95.                                                  this.Position.Y + distance);
  96.                 break;
  97.             case Direction.Left:
  98.                 this.Position = new Vector2u(this.Position.X - distance,
  99.                                                  this.Position.Y);
  100.                 break;
  101.             case Direction.Right:
  102.                 this.Position = new Vector2u(this.Position.X + distance,
  103.                                                  this.Position.Y);
  104.                 break;
  105.             case Direction.Up:
  106.                 this.Position = new Vector2u(this.Position.X,
  107.                                                  this.Position.Y - distance);
  108.                 break;
  109.             }
  110.         }
  111.        
  112.         /// <summary> Activates the character. </summary>
  113.         public virtual void Activate()
  114.         {
  115.             this.AttachLabel("");
  116.         }
  117.     }
  118. }