Note: This is an automatic translation from Russian. It looks good enough, but there may be inaccuracies. We are sorry for the inconvenience.
Options
Let's add a couple more locations to the game. Create a Forest class with the following content:
using TeravQuest;
namespace MyGame
{
[Serializable]
class Forest : Location
{
public Forest()
{
Name = "Forest";
Content = $"<center><h1>{Name}</h1></center>" +
"<p>A magical forest full of amazing creatures, secrets and mysteries.</p>" +
"<p>In a clearing in the middle of the forest, your house and the elf's hut are located.</p>" +
"<p>The entrance to a mysterious cave is visible to the side.</p>";
}
}
}
From this location it will be possible to get into the house of the protagonist, already created by us, into the house of his friend the elf and into a mysterious cave. Create a location representing the elf's house:
using TeravQuest;
namespace MyGame
{
[Serializable]
class ElvenHouse : Location
{
public ElvenHouse()
{
Name = "Elf house";
Content = $"<center><h1>{Name}</h1></center>" +
"<p>Bows, bowstrings and quivers with arrows are hung on the walls.</p>";
}
}
}
We will add the location of the cave a little later.
Create objects of the created locations and add them to the game in the constructor of the MyGame class.
public MyGame()
{
Name = "Shortest quest";
AddLocation(new Home());
AddLocation(new Forest());
AddLocation(new ElvenHouse());
GoTo("Home");
}
Now we will implement the ability to move from location to location.
Adding actions to a location
Actions are the main, but not the only, mechanism for controlling the game by the user. They are used to navigate between locations, choose a dialogue option and implement your other ideas.
In TeravQuest, actions are represented by the Option class. Its constructor takes two parameters: the text that will be displayed on the action panel and the method that will be executed when the player clicks on the corresponding action.
The method passed as the second parameter must match the ActionHandler delegate. It must return void, that is, nothing, and take an object of the Game class as a parameter. An object of the Game class will be passed at the time the action is activated by the player and will allow access to all variables and methods of the game.
The created Option objects are passed to the AddOption method of the Location class.
Let's add an action to the “Home” location to go to the “Forest” location.
using TeravQuest;
namespace MyGame
{
[Serializable]
class Home : Location
{
public Home()
{
Name = "Home";
Content = $"<center><h1>{Name}</h1></center>A small hut with only one room. The decoration is very modest, but here it is clean and cozy.";
AddOption(new Option("Go to forest", ToForest));
}
private void ToForest(Game game)
{
game.GoTo("Forest");
}
}
}
First, the ToForest method is created. The name can be anything, as long as it makes sense. The method corresponds to the ActionHandler delegate. In this case, the method is private, because it is not planned to call it anywhere outside the Home class. The method takes a parameter of type Game, and now through the variable game we have access to the methods of the game. We use this opportunity and call the game.GoTo (“Forest”) method, passing the name of the location to which we need to go.
After that, in the constructor of the location, the AddOption method is called, into which the Option object created using the new operator is passed, into the constructor of which, in turn, the action text and the name of the method that will be called are passed: AddOption (new Option ("Go to forest", ToForest)).
Do the same for the ElvenHouse location.
using TeravQuest;
namespace MyGame
{
[Serializable]
class ElvenHouse : Location
{
public ElvenHouse()
{
Name = "Elf house";
Content = $"<center><h1>{Name}</h1></center>" +
"<p>Bows, bowstrings and quivers with arrows are hung on the walls.</p>";
AddOption(new Option("Go to forest", ToForest));
}
private void ToForest(Game game)
{
game.GoTo("Forest");
}
}
}
And in the Forest location, we will add actions to go to the player's house, to the elf's house and to the cave, although there is no cave yet, it will be added a little later.
using TeravQuest;
namespace MyGame
{
[Serializable]
class Forest : Location
{
public Forest()
{
Name = "Forest";
Content = $"<center><h1>{Name}</h1></center>" +
"<p>A magical forest full of amazing creatures, secrets and mysteries.</p>" +
"<p>In a clearing in the middle of the forest, your house and the elf's hut are located.</p>" +
"<p>The entrance to a mysterious cave is visible to the side.</p>";
AddOption(new Option("To home", ToHome));
AddOption(new Option("Go to elf", ToElf));
AddOption(new Option("Explore a cave", ToCave));
}
private void ToHome(Game game)
{
game.GoTo("Home");
}
private void ToElf(Game game)
{
game.GoTo("Elf house");
}
private void ToCave(Game game)
{
game.GoTo("Cave");
}
}
}