Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. using RPGEngine;
  10. using RPGEngine.Utility;
  11.  
  12. namespace RPGEngine.Entity
  13. {
  14.  
  15.     /// <summary>
  16.     /// Contains the definition of a material
  17.     /// </summary>
  18.     public class Material : DataObject
  19.     {
  20.  
  21.         internal int _id;                   // Unique ID to refer to this material
  22.         internal int _name;                 // Pointer to name
  23.         internal int _description;          // Pointer to description
  24.         internal Color _color;              // What color the material is
  25.         internal float _weight;             // How much a cubic cm of this weighs
  26.         internal float _meltingPoint;       // In degrees K, when material turns to liquid
  27.         internal float _boilingPoint;       // In degrees K, when material turns to gas
  28.         internal float _conductHeat;        // Heat resistance/transferrance
  29.         internal float _conductElectricity; // Heat resistance/transferrance
  30.         internal float _affinity;           // Electronegativity
  31.         internal float _pierceFactor;       // "Young's Modulus" wikipedia
  32.         internal float _slashFactor;        // "Shear Modulus" wikipedia
  33.         internal float _crushFactor;        // "Bulk Modulus" wikipedia
  34.         internal List<string> _properties;  // Contains a list of properties. Other methods can check if one is present
  35.         internal List<int> _parents;        // List of pointers to other materials, used for alloying and inheritance
  36.  
  37.         // Getters and setters
  38.         public int id
  39.         {
  40.             get { return this._id; }
  41.         }
  42.         public string name
  43.         {
  44.             get { return Data.Strings.Get("MaterialNames", this._id); }
  45.         }
  46.         public string description
  47.         {
  48.             get { return Data.Strings.Get("MaterialDescriptions", this._id); }
  49.         }
  50.         public Color color
  51.         {
  52.             get { return this._color; }
  53.         }
  54.         public float weight
  55.         {
  56.             get { return this._weight; }
  57.         }
  58.         public float meltingPoint
  59.         {
  60.             get { return this._meltingPoint; }
  61.         }
  62.         public float boilingPoint
  63.         {
  64.             get { return this._boilingPoint; }
  65.         }
  66.         public float conductHeat
  67.         {
  68.             get { return this._conductHeat; }
  69.         }
  70.         public float conductElectricity
  71.         {
  72.             get { return this._conductElectricity; }
  73.         }
  74.         public float affinity
  75.         {
  76.             get { return this._affinity; }
  77.         }
  78.         public float pierceFactor
  79.         {
  80.             get { return this._pierceFactor; }
  81.         }
  82.         public float slashFactor
  83.         {
  84.             get { return this._slashFactor; }
  85.         }
  86.         public float crushFactor
  87.         {
  88.             get { return this._crushFactor; }
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Default constructor
  93.         /// </summary>
  94.         public Material(int index = -1) : base()
  95.         {
  96.             // Initialize lists
  97.             this._properties = new List<string>();
  98.             this._parents = new List<int>();
  99.  
  100.             // Mimic an existing material
  101.             if (index >= 0)
  102.             {
  103.                 // try { return materialHandler.Get(index); }
  104.                 // catch { }
  105.             }
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Determines if a property is present in the list
  110.         /// </summary>
  111.         /// <param name="index">Name of property</param>
  112.         /// <returns>Whether present or absent</returns>
  113.         public bool HasProperty(string index)
  114.         {
  115.             return _properties.Contains(index);
  116.         }
  117.  
  118.         /// <summary>
  119.         /// Determines if a parent is present in the list
  120.         /// </summary>
  121.         /// <param name="index">ID of parent</param>
  122.         /// <returns>Whether present or absent</returns>
  123.         public bool HasParent(int index)
  124.         {
  125.             return _parents.Contains(index);
  126.         }
  127.  
  128.         // Not implemented
  129.         protected override void Load(string file, string path, string contentPath = IO.contentDirectory)
  130.         {
  131.             throw new NotImplementedException();
  132.         }
  133.         protected override void Save(string file, string path, string contentPath = IO.contentDirectory)
  134.         {
  135.             throw new NotImplementedException();
  136.         }
  137.  
  138.     }
  139.  
  140.     public class MaterialHandler : DataObjectByIndexByName<Material>
  141.     {
  142.  
  143.         /// <summary>
  144.         /// Default constructor
  145.         /// </summary>
  146.         /// <param name="file"></param>
  147.         /// <param name="path"></param>
  148.         /// <param name="contentPath"></param>
  149.         public MaterialHandler(string file, string path, string contentPath = IO.contentDirectory)
  150.             : base()
  151.         {
  152.             this.Load(file, path, contentPath);
  153.         }
  154.  
  155.         /// <summary>
  156.         /// Loads data from a file
  157.         /// </summary>
  158.         /// <param name="file">The file name</param>
  159.         /// <param name="path">The location of the file</param>
  160.         /// <param name="contentPath">Content directory</param>
  161.         protected override void Load(string file, string path, string contentPath = IO.contentDirectory)
  162.         {
  163.            
  164.             //$$ While this works, we should find a way to implement an [ALLOY:aa:bb] tag, and smartly load
  165.             // materials and check if they exist, etc. For now, we can load all sorts of single materials.
  166.  
  167.             string[] fileData = IO.ReadFile(file, path, contentPath); // Load the data from a fil
  168.  
  169.             List<Vector2> dataChunk = new List<Vector2>(); // Contains start/end pairs
  170.             IO.CheckValidity(ref dataChunk, fileData);     // Validate and find where the data is grouped
  171.  
  172.             // Iterate the data chunks
  173.             while (dataChunk.Count != 0)
  174.             {
  175.                
  176.                 // Which list element to use
  177.                 int index = 0;
  178.  
  179.                 // Create a string array from a single chunk
  180.                 string[] data = new string[(int)(dataChunk[index].Y - dataChunk[index].X + 1)];
  181.                 for (int i = 0; i < data.Length; i++)
  182.                     data[i] = fileData[(int)dataChunk[index].X + i];
  183.  
  184.                 // Ensure the chunk contains enough basic information for a new material
  185.                 if (IO.Contains(data, @"\[ID:.+\]") &&        // Contains "[ID:???]"
  186.                     IO.Contains(data, @"\[NAME:.+\]") &&      // Contains "[NAME:???]"
  187.                     IO.Contains(data, @"\[DESCRIPTION:.+\]")) // Contains "[DESCRIPTION:???]"
  188.                 {  
  189.  
  190.                     Material newMat = new Material();
  191.                     for (int i = 0; i < data.Length; i++) // Check each property
  192.                     {
  193.  
  194.                         // Split the property into different parts
  195.                         string[] str = (data[i].Substring(1, data[i].Length - 2)).Split(':');
  196.  
  197.                         //$$ This could be more generalized by simply having a list of parameters that are checked.
  198.                         // At this time, we don't need to do such a thing, but we could if we wanted more flexibility.
  199.                         switch (str[0])
  200.                         {
  201.  
  202.                             case "ID":
  203.                                 newMat._id = Convert.ToInt32(str[1]);
  204.                                 break;
  205.  
  206.                             case "NAME":
  207.                                 newMat._name = Convert.ToInt32(str[1]);
  208.                                 break;
  209.  
  210.                             case "DESCRIPTION":
  211.                                 newMat._description = Convert.ToInt32(str[1]);
  212.                                 break;
  213.  
  214.                             case "COLOR":
  215.                                 newMat._color = new Color(
  216.                                     Convert.ToByte(str[1]),
  217.                                     Convert.ToByte(str[2]),
  218.                                     Convert.ToByte(str[3]),
  219.                                     Convert.ToByte(str[4]));
  220.                                 break;
  221.  
  222.                             case "WEIGHT":
  223.                                 newMat._weight = Convert.ToSingle(str[1]);
  224.                                 break;
  225.  
  226.                             case "MELT":
  227.                                 newMat._meltingPoint = Convert.ToSingle(str[1]);
  228.                                 break;
  229.  
  230.                             case "BOIL":
  231.                                 newMat._boilingPoint = Convert.ToSingle(str[1]);
  232.                                 break;
  233.  
  234.                             case "CONDUCT HEAT":
  235.                                 newMat._conductHeat = Convert.ToSingle(str[1]);
  236.                                 break;
  237.  
  238.                             case "CONDUCT ELECTRICITY":
  239.                                 newMat._conductElectricity = Convert.ToSingle(str[1]);
  240.                                 break;
  241.  
  242.                             case "AFFINITY":
  243.                                 newMat._affinity = Convert.ToSingle(str[1]);
  244.                                 break;
  245.  
  246.                             case "PIERCE FACTOR":
  247.                                 newMat._pierceFactor = Convert.ToSingle(str[1]);
  248.                                 break;
  249.  
  250.                             case "SLASH FACTOR":
  251.                                 newMat._slashFactor = Convert.ToSingle(str[1]);
  252.                                 break;
  253.  
  254.                             case "CRUSH FACTOR":
  255.                                 newMat._crushFactor = Convert.ToSingle(str[1]);
  256.                                 break;
  257.  
  258.                             default:
  259.                                 newMat._properties.Add(str[0]);
  260.                                 break;
  261.  
  262.                         }
  263.  
  264.                     }
  265.  
  266.                     // Finally, we can add it to the index.
  267.                     this._ByIndex.Add(newMat._id, newMat);
  268.  
  269.                 }
  270.  
  271.                 // Remove this object from the list
  272.                 dataChunk.RemoveAt(index);
  273.  
  274.             }
  275.  
  276.         }
  277.  
  278.         /// <summary>
  279.         /// Saves the data to a file (not implemented)
  280.         /// </summary>
  281.         /// <param name="file">File name</param>
  282.         /// <param name="path">File location</param>
  283.         protected override void Save(string file, string path, string contentPath = IO.contentDirectory)
  284.         {
  285.             throw new NotImplementedException();
  286.         }
  287.  
  288.     }
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement