/*
* RefinerBuilding.cs
* Authors: Bradley Blankenship
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content;
using System.Xml;
namespace ColonySim.Buildings
{
///
/// Takes resouces that are gathered by Gatherer buildings and
/// refines them into their final form for use. They are then
/// sent to the closest, non-full storage building.
///
internal class RefinerBuilding : Building
{
#region Fields
// The string of the resources that are refinable and their rates
string mRefinableString;
// The map of resources and rates for refining them
Resource mRefinableResources;
#endregion
#region Properties
///
/// Allows access to the string value for the initialization
/// of the possible resources that can be refined.
///
public Resource RefinableResources
{
get { return mRefinableResources; }
set { mRefinableResources = value; }
}
#endregion
#region Constructs
///
/// Base constructor
///
public RefinerBuilding()
{
// Initialize the gatherable resource / rate map
mRefinableResources = new Resource();
}
#endregion
#region Initialization
///
/// Initialize the building.
///
public override void Initialize()
{
// Base call
base.Initialize();
// Split up the string of resources
string[] stringArray = mRefinableString.Split(';');
// Now generate the resources based on the pairs
foreach (string pair in stringArray)
{
// Get the string for the resource name and the refinable rate
string name = pair.Split(',')[0].Trim();
float value = float.Parse(pair.Split(',')[1].Trim());
// Add the new resource
mRefinableResources += new SimpleResource((Resources)Enum.Parse(typeof(Resources), name), value);
}
}
#endregion
#region Update
///
/// Update the game based on the given time.
///
/// The current game time.
public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
}
#endregion
#region Utility
///
/// Reads in the base data for a given building into the values. Requires
/// that the input text reader is already at the proper position for reading the data.
///
/// The reader to read the data.
protected override void ReadData(XmlTextReader input)
{
// Base call
base.ReadData(input);
// Read the specific building values
this.mRefinableString = input.GetAttribute("RefinableResources");
}
public override string ToString()
{
// Get the base value
StringBuilder retVal = new StringBuilder(base.ToString());
// State the gatherable resources
retVal.Append("Refinable Resources:");
retVal.Append('\n');
// Now add each of the gatherable resources
retVal.Append(mRefinableResources.ToString());
// Return the value
return retVal.ToString();
}
///
/// Read in a list of refiner buildings.
///
/// The file to read from.
/// The list of buildings.
public static List Read(string file)
{
// The return value
List retVal = new List();
// Create the XML reader for the XML file
XmlTextReader input = new XmlTextReader(file);
input.WhitespaceHandling = WhitespaceHandling.None;
// Read in the values for the input
while (input.Read())
{
// If this is the item we need
if (input.Name == "Building" && input.NodeType == XmlNodeType.Element
&& input.HasAttributes)
{
// Create the building
RefinerBuilding item = new RefinerBuilding();
// Read in the data
item.ReadData(input);
// Initialize the item
item.Initialize();
// Add the item
retVal.Add(item);
}
}
// Close the input stream
input.Close();
// Return the new entertainment building
return retVal;
}
#endregion
}
}