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: 5.59 KB  |  views: 27  |  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.Collections.Generic;
  3. using System.IO;
  4. using SFML.Graphics;
  5. using SFML.Window;
  6.  
  7. namespace Blashyrkh.Game
  8. {
  9.     public enum Orientation
  10.     {
  11.         Horizontal,
  12.         Vertical
  13.     }
  14.    
  15.     public enum TextureType
  16.     {
  17.         None,
  18.         Icon,
  19.         Map,
  20.         Scenery,
  21.         Sprite,
  22.         Tile
  23.     }
  24.    
  25.     public abstract class GameObject : Drawable
  26.     {
  27.         private static Dictionary <string, Texture> Textures;
  28.         protected string Name;
  29.         public Vector2u Position;
  30.         public Vector2u Size;
  31.         protected List <GameObject> Inventory;
  32.         protected bool IsContainer;
  33.         protected string ID;
  34.         protected TextureType TextureType;
  35.         protected Text LabelText;
  36.  
  37.         static GameObject()
  38.         {
  39.             GameObject.Textures = new Dictionary<string, Texture>();
  40.         }
  41.  
  42.         public GameObject()
  43.         {
  44.             // DO NOTHING.
  45.         }
  46.  
  47.         public GameObject(string name, TextureType type, Vector2u position)
  48.         {
  49.             this.Name = name;
  50.             this.Position = position;
  51.             this.Inventory = new List<GameObject>();
  52.             this.IsContainer = true;
  53.             this.TextureType = type;
  54.         }
  55.  
  56.         public GameObject(string name, TextureType type,
  57.                         Vector2u position, Vector2u size)
  58.         {
  59.             this.Name = name;
  60.             this.Position = position;
  61.             this.Size = size;
  62.             this.Inventory = new List<GameObject>();
  63.             this.IsContainer = true;
  64.             this.TextureType = type;
  65.         }
  66.  
  67.         public virtual bool AddToInventory(GameObject item)
  68.         {
  69.             if (!(this.IsContainer)) {
  70.                 Console.Error.WriteLine("{0} cannot contain other objects.",
  71.                                 this.ToString());
  72.                 return false;
  73.             }
  74.             this.Inventory.Add(item);
  75.             return true;
  76.         }
  77.        
  78.         public virtual bool Contains(GameObject item)
  79.         {
  80.             if (!(this.IsContainer)) {
  81.                 Console.Error.WriteLine("{0} cannot contain other objects.",
  82.                                         this.ToString());
  83.                 return false;
  84.             }
  85.             return this.Inventory.Contains(item);
  86.         }
  87.        
  88.         public void AttachLabel(string text)
  89.         {
  90.             this.LabelText = new Text(text, Font.DefaultFont, 10);
  91.             this.LabelText.Color = new Color(0, 0, 0);
  92.             this.LabelText.Position = new Vector2f(this.Position.X,
  93.                                                    this.Position.Y);
  94.         }
  95.        
  96.         public void DetachLabel()
  97.         {
  98.             this.LabelText = null;
  99.         }
  100.  
  101.         protected static Texture LoadTexture(TextureType type, string name)
  102.         {
  103.             if (GameObject.Textures.ContainsKey(name))
  104.                 return GameObject.Textures[name];
  105.             // Find the texture,
  106.             string path = Path.Combine(Game.BasePath, "Data",
  107.                                        "Images", type.ToString() + "s", name + ".png");
  108.             new LogMessage(LogLevel.Debug, "Loading texture: {0}",
  109.                            path);
  110.             // Load, add and return the texture.
  111.             Texture texture = new Texture(path);
  112.             GameObject.Textures.Add(name, texture);
  113.             return texture;
  114.         }
  115.        
  116.         public static Texture GetTexture(TextureType type, string name)
  117.         {
  118.             try {
  119.                 Texture texture = GameObject.Textures[name];
  120.                 return texture;
  121.             } catch(KeyNotFoundException) {
  122.                 // If the texture wasn't found, try to load it.
  123.                 return GameObject.LoadTexture(type, name);
  124.             }
  125.         }
  126.  
  127.         public virtual void Draw(RenderTarget target, RenderStates states)
  128.         {
  129.             Texture texture = GameObject.GetTexture(this.TextureType, this.Name);
  130.             Sprite sprite = new Sprite(texture);
  131.             sprite.Position = new Vector2f((float)this.Position.X,
  132.                       (float)this.Position.Y);
  133.             target.Draw(sprite, states);
  134.             if (this.LabelText != null)
  135.                 target.Draw(this.LabelText, states);
  136.         }
  137.  
  138.         public override string ToString()
  139.         {
  140.             return String.Format("<{0}:{1}>", this.ID, this.Name);
  141.         }
  142.  
  143.         public bool IsCollidingWith(GameObject entity)
  144.         {
  145.             // The top/left co-ords are moved down/right by 10%
  146.             // and the bottom/right co-ords are moved up/left by 10%
  147.             // to make the overall bounding box 20% smaller than the
  148.             // size of the objects, to give tighter detection.
  149.             uint top1 = (uint)((float)this.Position.Y * 1.1f),
  150.             top2 = (uint)((float)entity.Position.Y * 1.1f),
  151.             left1 = (uint)((float)this.Position.X * 1.1f),
  152.             left2 = (uint)((float)entity.Position.Y * 1.1f),
  153.             right1 = (uint)((float)(this.Position.X + this.Size.X) * 0.9f),
  154.             right2 = (uint)((float)(entity.Position.X + entity.Size.X) * 0.9f),
  155.             bottom1 = (uint)((float)(this.Position.Y + this.Size.Y) * 0.9f),
  156.             bottom2 = (uint)((float)(entity.Position.Y + entity.Size.Y) * 0.9f);
  157.             if (top1 > bottom2)
  158.                 return false;
  159.             if (left1 > right2)
  160.                 return false;
  161.             if (right1 < left2)
  162.                 return false;
  163.             if (bottom1 < top2)
  164.                 return false;
  165.             return true;
  166.         }
  167.     }
  168. }
clone this paste RAW Paste Data