using System; using System.IO; namespace Otter { /// /// Class that represents a player session. Use this for maintaining and using information about /// a player. For example a two player game might have two sessions, one for each player, each with /// their own controls configured and save data. /// public class Session { static private int nextSessionId = 0; /// /// Create a new Session using the current Game.Instance. /// /// static public Session Create(string name) { return new Session(Game.Instance, name); } static public Session Create(Enum name) { return Session.Create(Util.EnumValueToString(name)); } /// /// The name of this Session. This is important as it will determine the name of save data /// files and you can also find a session by name. /// public string Name = ""; /// /// The Controller to use for this Session. /// public Controller Controller = new Controller(); /// /// Gets the Controller as a specific type of Controller. /// /// The type of Controller. /// The Controller as type T. public T GetController() where T : Controller { return Controller as T; } /// /// The Id of this session in the Game. /// public int Id { get; internal set; } public DataSaver Data { get; private set; } /// /// The game that manages this session. /// public Game Game { get; internal set; } /// /// Create a new Session. /// /// The Game that the session is tied to. public Session(Game game, string name) { Game = game; Name = name; var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/" + Game.GameFolder; var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/" + Game.GameFolder + "/" + Name + "."; if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } Data = new DataSaver(path); Id = nextSessionId; nextSessionId++; } internal void Update() { if (Controller != null) { Controller.UpdateFirst(); } } } }