Locations
Note: This is an automatic translation from Russian. It looks good enough, but there may be inaccuracies. We are sorry for the inconvenience.
Now let's start writing a small game, analyzing the main features of the TeravQuest library along the way.
We will not study the syntax of the C# language and the principles of programming here, we will touch only a few points as necessary. There is enough information on the C# language on the Internet, both in writing and in a video on YouTube. Basic knowledge will be enough to write not very complex games using the TeravQuest library.
Game Class
Open the project downloaded in the previous articles in Visual Studio and open the file MyGame.cs.
This is the main class of the future game. It will contain all locations, game objects, variables and other entities. You can rename this class, file and the whole project as you like. The most important thing is that this class inherits the Game class from the TeravQuest.dll library connected to the project.
Also, note that the class is marked with the [Serializable] attribute. All classes representing any significant for the game entity must be marked with this attribute for the save system to work in the game.
[Serializable]
public class MyGame : Game
Inheriting from the Game class allows you to use the game control mechanisms implemented in the library. For a complete list of methods and properties of the Game class, see the API description .
The main setting of the game and the addition of locations takes place in the class constructor. Let's change the name of the game. This name will be displayed in the title of the game window.
[Serializable]
public class MyGame : Game
{
public MyGame()
{
Name = "The shortest quest";
}
}
A class in object-oriented programming is a kind of plan, or a blueprint of some entity. To use it further in the program, you need to create a specific object (instance) of this class. Open the Program.cs file and take a look at the Main method.
[STAThread]
static void Main(string[] args)
{
MyGame game = new MyGame();
game.Run();
}
This is the entry point to the program, from here its execution begins. Here an object of class MyGame is created and placed in the variable game. Then the Run method is called on the game variable, which is described in the Game class of the TeravQuest library and is available to us, since it was inherited. At the time of object creation, all resources necessary for the game are initialized, including the game name set above. The Run method starts the game.
Creating and adding locations to the game
The project already has a file with the class of the starting location StartLocation.cs. You can delete it and create your own location to start the game. In these articles, a fantasy-style game will be created and it will begin at the home of the main character.
In Solution Explorer, right-click on the project name and select Add -> Class. Name it Home. Add the following content to the created file.
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.";
}
}
}
First of all, we include the namespace so that the library classes are available to us: using TeravQuest ;. Don't forget about the [Serializable] attribute. And we inherit from the Location class.
The Name property sets the name of the location. In the future, this name will be used to go to this location. The name is case sensitive, so “Home” and “home” will be different names.
The Content property sets the main text content of the location. The $ in front of the quotes means that we want to use variable names in the string to insert their contents into the string. In this case, we are inserting the name of the location. Now this is done just for demonstration, it could be done by hand.
Now change the constructor of the MyGame class to add the location to the game and immediately switch to it at start.
public MyGame()
{
Name = "Shortest quest";
AddLocation(new Home());
GoTo("Home");
}
The AddLocation method adds a location to the game, after which you can go to it. An object of the added location must be passed as a parameter. In this case, a separate variable for the object is not created, but the result of the new operator is immediately passed as a parameter.
The GoTo method is used to navigate locations. It takes the name of the location to go to as a parameter. If a location with this name was not added to the game using the AddLocation method, an error will occur.
To summarize, to add a location to the game, you need to create a class, inherit it from the Location class and pass the object of this class to the AddLocation method in the constructor of the game class.
Locations are the main game entity and they can represent not only the place of action, but also descriptions of characters, containers, perform some service functions, etc. This principle is used in many platforms for developing text-based games. However, the TeravQuest library is not limited to this and offers you a number of ready-made abstractions to facilitate development, which we will look at below.