Game Objects
Note: This is an automatic translation from Russian. It looks good enough, but there may be inaccuracies. We are sorry for the inconvenience.
GameObject class
The GameObject class represents any object that can be placed in the game world and with which the player can interact. All other types of game objects, such as items, containers and NPCs, are also inherited from this class. Let's take a look at some of the properties of GameObject.
Name. Stores the name of the object. This name is displayed, for example, in the inventory panel.
Description. Description of the object.
DescriptionOnLocation. Description for showing an object at a location. If not specified, the content of the Description property is used.
IsUnique. Determines if the object is unique. If true, then only one such object can exist in the game world, and if you try to add another one, an error will occur.
IsVisibleOnLocation. Indicates whether the object should be displayed at the location, if this object is in the collection of location objects.
IsActive. If set to true, then the object in the location will be clickable.
Also, the GameObject class has a virtual method called Activate, which describes the actions that must be performed when clicking on this object.
Let's create, for example, a stone object with an inscription on it and place it on the Forest location. Create a MileStone class with the following content:
using TeravQuest;
namespace MyGame
{
[Serializable]
class MileStone : GameObject
{
public MileStone()
{
Name = "Stone";
Description = "If the stone says \"If you go to the left, you will lose your horse, if you go to the right, you will lose your wife ... \", then before you go, make sure that it was not your wife or horse who wrote it ...";
DescriptionOnLocation = "A stone is installed in the middle of the clearing.";
}
public override void Activate(Game game)
{
game.IO.Message(Description, "The inscription on the stone");
}
}
}
The name of the object, its description and description at the location are set here. The values of the IsUnique, IsActive and IsVisibleOnlocation properties are left by default and equal, respectively, false, true, true, which, at the moment, is completely fine with us.
In the overridden Activate method, a message is displayed to the player that contains the text from the Description property.
Now place an object of class MileStone in the Forest location by changing the constructor.
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>";
Objects.Add(new MileStone());
AddOption(new Option("Go to home", ToHome));
AddOption(new Option("Go to elf", ToElf));
AddOption(new Option("Explore a cave", ToCave));
}
Objects are added to the location using the Add method of the Objects property, which is represented by the GameObjectsCollection class. The methods of this class can be found in the full API description.
Now a stone will appear on the Forest location, when clicking on which the player will be shown an inscription on this stone.
Item class
This class is a game item. It has the same properties as the GameObject class, but objects of this class can be stored in the character's inventory and chests, which will be discussed later.
Let's create an object "key", which is so necessary for the player to get into the cave. We will need it in the next articles. Create a Key class, inheriting from Item:
using TeravQuest;
namespace MyGame
{
[Serializable]
class Key : Item
{
public Key()
{
Name = "Shabby key";
Description = "The key that opens the entrance to the cave.";
IsUnique = true;
}
public override void Activate(Game game)
{
game.IO.Info(Description);
}
}
}
NPC class
This class represents the characters that the player can interact with. Characters have an inventory that stores in-game items (inherited from the Item class) and a set of dialogues. This class has already implemented the virtual method Activate, which opens an auxiliary location for a conversation with this character and displays available dialogs on the action panel. Dialogues will be discussed in the next article.
The NPC class inherits from the GameObject class, so it has the same properties, such as name and description., And can also be added to the Objects collection of the Location class. Also, the NPC class has some additional properties.
Dialogs. A collection of Dialog objects.
Inventory. A collection of Item objects.
Appearance. The content of this property is displayed on the location panel after the player clicks on this NPC.
GoodByeText. This property sets the text for the action, by clicking on which the conversation with this NPC will be ended.
Now let's create a friend of our main character. Add the Elf class to your project:
using TeravQuest;
namespace MyGame
{
[Serializable]
class Elf : NPC
{
public Elf()
{
Name = "Miloslav";
Description = "Old elf Miloslav";
DescriptionOnLocation = $"{Description} sits in his chair by the small fireplace and sips ale.";
Appearance = "The elf gazes at you with his sly eyes.";
GoodByeText = "I'll go...";
IsUnique = true;
Inventory.Add(new Key());
}
}
}
Please note that we put the previously created cave key to the elf's inventory.
Now create an elf object and place it on the 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>";
Objects.Add(new Elf());
AddOption(new Option("Go to forest", ToForest));
}
Now, having entered this location, the player will see an elf there and will be able to talk to him.