Game State
It would be nice if a player could return to Inca Curse in the future and continue the game where he/she left off.
The easiest way to achieve this is to store the game state in a cookie. Cookies are simple text files that can contain data. They are stored by the browser on the computer of the player.
Inca Curse uses a cookie to store all relevant game data: The current location, the current screen (22 lines of text), the current location of all objects (or if it is being carried by the player) and a few special variables that are used in the game.
There are two methods 'ReadCookie' (which returns a string) and 'WriteCookie' (which receives a string) that deal with the storing and receiving of cookie data. The game data is in fact read and written with each turn of the game and it is the computer's way of retaining state in between browser requests.
If a cookie does not exist it must mean that a new player has arrived and a cookie is written with the initial game data (I am in a jungle clearing...).
Here is the code:
private bool CookieExists()
{
return this.ControllerContext.HttpContext.
Request.Cookies.AllKeys.Contains("IncaCurse");
}
private string ReadCookie()
{
string c = this.ControllerContext.HttpContext.
Request.Cookies["IncaCurse"].Value;
return c;
}
private void WriteCookie(string s)
{
HttpCookie HTTPcookie = new HttpCookie("IncaCurse");
HTTPcookie.Value = s + DateTime.Now.ToShortTimeString();
HTTPcookie.Expires = DateTime.Now.AddDays(+60);
this.ControllerContext.HttpContext.Response.
Cookies.Add(HTTPcookie);
return;
}
There is some additional code that converts the game state to a string and back which is not shown here.
This concludes the Project description. I Hope you are enjoying the game.
Feel free to have a look at my blog which is found here:
Digital Dreams