muddesigner/MudEngine/GameManagement/GameTime.cs
Scionwest_cp d212f5b854 MudEngine:
- Player login now creates the directories needed if they don't exist.
 - Added Update method that serves as the game loop now.
 - Added additional save code to Game.Save() now saves all realms, zones, rooms and players. Called during server shutdown (Game.Shutdown())
 - Began work on implementing a Unique Identifier system for all game objects. 
 - Renamed GetRealm() and GetZone() to GetRealmByID() and GetZoneByID. Now accepts a Int32 instead of a string as the parameter, returning a reference to the object.
 - Added GetRealmByName() and GetZoneByName(). Similar to the old GetRealm and GetZone methods. Accepts a string as a parameter, however it returns a list<> collection of the found objects due to objects with duplicate names are allowed to co-exist in the game world.
 - Temp directory that was being generated during server startup within Game.Start() is now deleted once script compilation is completed.
 - Added GameTime.cs; contains all the properties and methods needed to have a working time system built into the engine for the game world. Not fully complete.
 - Added CommandGetTime.cs which prints the current date and time to the players console. Prints the GameTime.DayNames and GameTime.MonthNames to the player. Defaults to real-world names, however these can be configured and changed, along with the number of days per week, weeks per month, months per year, seconds per minute, minutes per hour and hours per day.

MudGame:
 - Now supports Game.AutoSave. If AutoSave is true, then the server loop will call Game.Save() automatically.
 - Server Game Loop now calls Game.Update() every loop cycle.
2010-08-10 15:10:25 -07:00

260 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MudEngine.GameManagement
{
public class GameTime
{
public struct Time
{
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public int Hour { get; set; }
public int Minute { get; set; }
public int Second { get; set; }
private GameTime gameTime;
}
public enum TimeOfDayOptions
{
AlwaysDay,
AlwaysNight,
Transition,
}
internal Game ActiveGame { get; private set; }
/// <summary>
/// The time of day that the server actually started up.
/// </summary>
internal DateTime ServerStartTime { get; private set; }
/// <summary>
/// Gets the current World Time.
/// </summary>
public Time CurrentWorldTime { get; internal set; }
/// <summary>
/// Gets or Sets the current Time of the System
/// </summary>
private DateTime CurrentTime { get; set; }
/// <summary>
/// Gets or Sets how many Hours it takes to make a full day in the World
/// </summary>
public int HoursPerDay { get; set; }
/// <summary>
/// Gets or Sets how many minutes it takes to make a full Hour
/// </summary>
public int MinutesPerHour { get; set; }
/// <summary>
/// Gets or Sets how many seconds it takes to make a full minute
/// </summary>
public int SecondsPerMinute { get; set; }
/// <summary>
/// Gets or Sets how many Days it takes to make a full month in the world
/// </summary>
public int DaysPerMonth { get; set; }
/// <summary>
/// Gets or Sets how many Months it takes to make a full Year in the world
/// </summary>
public int MonthsPerYear { get; set; }
/// <summary>
/// Gets or Sets the name of each Day in a Week.
/// </summary>
public List<string> DayNames { get; set; }
/// <summary>
/// Gets or Sets the name of each Month in a Year.
/// </summary>
public List<string> MonthNames { get; set; }
/// <summary>
/// Gets or Sets what time of day the world is currently in.
/// </summary>
public TimeOfDayOptions DayTransitions { get; set; }
/// <summary>
/// Gets or Sets what time of day that it begins to transition to night.
/// </summary>
public int DawnTime { get; set; }
/// <summary>
/// /Gets or Sets what time of day that it begins to transition into day time.
/// </summary>
public int SunriseTime { get; set; }
/// <summary>
/// Gets or Sets the initial Time that the world starts in.
/// </summary>
public Time InitialGameTime { get; set; }
public GameTime(Game game)
{
ActiveGame = game;
ServerStartTime = DateTime.Now;
DayNames = new List<string>();
MonthNames = new List<string>();
DayNames.Add("Monday");
DayNames.Add("Tuesday");
DayNames.Add("Wednesday");
DayNames.Add("Thursday");
DayNames.Add("Friday");
DayNames.Add("Saturday");
DayNames.Add("Sunday");
MonthNames.Add("January");
MonthNames.Add("February");
MonthNames.Add("March");
MonthNames.Add("April");
MonthNames.Add("May");
MonthNames.Add("June");
MonthNames.Add("July");
MonthNames.Add("August");
MonthNames.Add("September");
MonthNames.Add("October");
MonthNames.Add("November");
MonthNames.Add("December");
}
public void Initialize()
{
Time t = InitialGameTime;
CurrentWorldTime = t;
}
public virtual void Update()
{
TimeSpan ts = CurrentTime - DateTime.Now;
//If the seconds that has passed inbetween the last Update call is greater than 0
//Then we need to increment a Second, which will start a domino effect if it needs to
//in order to increment minute/hours/days/months and years.
if (ts.Seconds != 0)
{
IncrementSecond();
}
CurrentTime = DateTime.Now;
}
public void IncrementSecond()
{
Time t = new Time();
t = CurrentWorldTime;
if (CurrentWorldTime.Second == SecondsPerMinute)
{
t.Second = 0;
IncrementMinute();
}
else
t.Second++;
CurrentWorldTime = t;
}
public void IncrementMinute()
{
Time t = new Time();
t = CurrentWorldTime;
if (CurrentWorldTime.Minute == MinutesPerHour)
{
t.Minute = 0;
IncrementHour();
}
else
t.Minute++;
CurrentWorldTime = t;
}
public void IncrementHour()
{
Time t = new Time();
t = CurrentWorldTime;
if (CurrentWorldTime.Hour == HoursPerDay)
{
t.Hour = 0;
IncrementDay();
}
else
t.Hour++;
CurrentWorldTime = t;
}
public void IncrementDay()
{
Time t = new Time();
t = CurrentWorldTime;
if (CurrentWorldTime.Day == DaysPerMonth)
{
t.Day = 1;
IncrementMonth();
}
else
t.Day++;
CurrentWorldTime = t;
}
public void IncrementMonth()
{
Time t = new Time();
t = CurrentWorldTime;
if (CurrentWorldTime.Month == MonthsPerYear)
{
t.Month = 1;
IncrementYear();
}
else
t.Month++;
CurrentWorldTime = t;
}
public void IncrementYear()
{
Time t = new Time();
t = CurrentWorldTime;
t.Year++;
CurrentWorldTime = t;
}
public string GetCurrentWorldTime()
{
if (DayNames.Count < CurrentWorldTime.Day)
{
return "Not enough Day Names specified to match up with DaysPerMonth property.";
}
else if (MonthNames.Count < CurrentWorldTime.Month)
{
return "Not enough Month names specified to match up with MonthsPerYear property.";
}
string day = DayNames[CurrentWorldTime.Day - 1];
string month = MonthNames[CurrentWorldTime.Month - 1];
return day + ", " + month + " " + CurrentWorldTime.Day + ", " + CurrentWorldTime.Year + ": " + CurrentWorldTime.Hour + ":" + CurrentWorldTime.Minute + ":" + CurrentWorldTime.Second;
}
}
}