Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using RPGEngine;
- using RPGEngine.Utility;
- namespace RPGEngine.Entity
- {
- /// <summary>
- /// Contains the definition of a material
- /// </summary>
- public class Material : DataObject
- {
- internal int _id; // Unique ID to refer to this material
- internal int _name; // Pointer to name
- internal int _description; // Pointer to description
- internal Color _color; // What color the material is
- internal float _weight; // How much a cubic cm of this weighs
- internal float _meltingPoint; // In degrees K, when material turns to liquid
- internal float _boilingPoint; // In degrees K, when material turns to gas
- internal float _conductHeat; // Heat resistance/transferrance
- internal float _conductElectricity; // Heat resistance/transferrance
- internal float _affinity; // Electronegativity
- internal float _pierceFactor; // "Young's Modulus" wikipedia
- internal float _slashFactor; // "Shear Modulus" wikipedia
- internal float _crushFactor; // "Bulk Modulus" wikipedia
- internal List<string> _properties; // Contains a list of properties. Other methods can check if one is present
- internal List<int> _parents; // List of pointers to other materials, used for alloying and inheritance
- // Getters and setters
- public int id
- {
- get { return this._id; }
- }
- public string name
- {
- get { return Data.Strings.Get("MaterialNames", this._id); }
- }
- public string description
- {
- get { return Data.Strings.Get("MaterialDescriptions", this._id); }
- }
- public Color color
- {
- get { return this._color; }
- }
- public float weight
- {
- get { return this._weight; }
- }
- public float meltingPoint
- {
- get { return this._meltingPoint; }
- }
- public float boilingPoint
- {
- get { return this._boilingPoint; }
- }
- public float conductHeat
- {
- get { return this._conductHeat; }
- }
- public float conductElectricity
- {
- get { return this._conductElectricity; }
- }
- public float affinity
- {
- get { return this._affinity; }
- }
- public float pierceFactor
- {
- get { return this._pierceFactor; }
- }
- public float slashFactor
- {
- get { return this._slashFactor; }
- }
- public float crushFactor
- {
- get { return this._crushFactor; }
- }
- /// <summary>
- /// Default constructor
- /// </summary>
- public Material(int index = -1) : base()
- {
- // Initialize lists
- this._properties = new List<string>();
- this._parents = new List<int>();
- // Mimic an existing material
- if (index >= 0)
- {
- // try { return materialHandler.Get(index); }
- // catch { }
- }
- }
- /// <summary>
- /// Determines if a property is present in the list
- /// </summary>
- /// <param name="index">Name of property</param>
- /// <returns>Whether present or absent</returns>
- public bool HasProperty(string index)
- {
- return _properties.Contains(index);
- }
- /// <summary>
- /// Determines if a parent is present in the list
- /// </summary>
- /// <param name="index">ID of parent</param>
- /// <returns>Whether present or absent</returns>
- public bool HasParent(int index)
- {
- return _parents.Contains(index);
- }
- // Not implemented
- protected override void Load(string file, string path, string contentPath = IO.contentDirectory)
- {
- throw new NotImplementedException();
- }
- protected override void Save(string file, string path, string contentPath = IO.contentDirectory)
- {
- throw new NotImplementedException();
- }
- }
- public class MaterialHandler : DataObjectByIndexByName<Material>
- {
- /// <summary>
- /// Default constructor
- /// </summary>
- /// <param name="file"></param>
- /// <param name="path"></param>
- /// <param name="contentPath"></param>
- public MaterialHandler(string file, string path, string contentPath = IO.contentDirectory)
- : base()
- {
- this.Load(file, path, contentPath);
- }
- /// <summary>
- /// Loads data from a file
- /// </summary>
- /// <param name="file">The file name</param>
- /// <param name="path">The location of the file</param>
- /// <param name="contentPath">Content directory</param>
- protected override void Load(string file, string path, string contentPath = IO.contentDirectory)
- {
- //$$ While this works, we should find a way to implement an [ALLOY:aa:bb] tag, and smartly load
- // materials and check if they exist, etc. For now, we can load all sorts of single materials.
- string[] fileData = IO.ReadFile(file, path, contentPath); // Load the data from a fil
- List<Vector2> dataChunk = new List<Vector2>(); // Contains start/end pairs
- IO.CheckValidity(ref dataChunk, fileData); // Validate and find where the data is grouped
- // Iterate the data chunks
- while (dataChunk.Count != 0)
- {
- // Which list element to use
- int index = 0;
- // Create a string array from a single chunk
- string[] data = new string[(int)(dataChunk[index].Y - dataChunk[index].X + 1)];
- for (int i = 0; i < data.Length; i++)
- data[i] = fileData[(int)dataChunk[index].X + i];
- // Ensure the chunk contains enough basic information for a new material
- if (IO.Contains(data, @"\[ID:.+\]") && // Contains "[ID:???]"
- IO.Contains(data, @"\[NAME:.+\]") && // Contains "[NAME:???]"
- IO.Contains(data, @"\[DESCRIPTION:.+\]")) // Contains "[DESCRIPTION:???]"
- {
- Material newMat = new Material();
- for (int i = 0; i < data.Length; i++) // Check each property
- {
- // Split the property into different parts
- string[] str = (data[i].Substring(1, data[i].Length - 2)).Split(':');
- //$$ This could be more generalized by simply having a list of parameters that are checked.
- // At this time, we don't need to do such a thing, but we could if we wanted more flexibility.
- switch (str[0])
- {
- case "ID":
- newMat._id = Convert.ToInt32(str[1]);
- break;
- case "NAME":
- newMat._name = Convert.ToInt32(str[1]);
- break;
- case "DESCRIPTION":
- newMat._description = Convert.ToInt32(str[1]);
- break;
- case "COLOR":
- newMat._color = new Color(
- Convert.ToByte(str[1]),
- Convert.ToByte(str[2]),
- Convert.ToByte(str[3]),
- Convert.ToByte(str[4]));
- break;
- case "WEIGHT":
- newMat._weight = Convert.ToSingle(str[1]);
- break;
- case "MELT":
- newMat._meltingPoint = Convert.ToSingle(str[1]);
- break;
- case "BOIL":
- newMat._boilingPoint = Convert.ToSingle(str[1]);
- break;
- case "CONDUCT HEAT":
- newMat._conductHeat = Convert.ToSingle(str[1]);
- break;
- case "CONDUCT ELECTRICITY":
- newMat._conductElectricity = Convert.ToSingle(str[1]);
- break;
- case "AFFINITY":
- newMat._affinity = Convert.ToSingle(str[1]);
- break;
- case "PIERCE FACTOR":
- newMat._pierceFactor = Convert.ToSingle(str[1]);
- break;
- case "SLASH FACTOR":
- newMat._slashFactor = Convert.ToSingle(str[1]);
- break;
- case "CRUSH FACTOR":
- newMat._crushFactor = Convert.ToSingle(str[1]);
- break;
- default:
- newMat._properties.Add(str[0]);
- break;
- }
- }
- // Finally, we can add it to the index.
- this._ByIndex.Add(newMat._id, newMat);
- }
- // Remove this object from the list
- dataChunk.RemoveAt(index);
- }
- }
- /// <summary>
- /// Saves the data to a file (not implemented)
- /// </summary>
- /// <param name="file">File name</param>
- /// <param name="path">File location</param>
- protected override void Save(string file, string path, string contentPath = IO.contentDirectory)
- {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement