Entities

SadConsole provides support for rendering individual textures to a surface, while giving them access to interact with the game scene.

What are entities?

Game entities are objects that interact with the game and respond to player input or other entities. Other terms for entities are actors, or game objects.

How is an entity defined?

An entity used to be define as a single glyph, but since v10 it can now represent a surface.
There are several constructor overloads, in this part we’ll cover the single glyph overload:

var playerEntity = new Entity(
	foreground: Color.Red, 
	background: Color.Transparent, 
	glyph: '@', 
	zIndex: 0); // Defines the layer the entity is on (overlap)

playerEntity.Position = new Point(5, 5);

To actually render this entity, you have to add the EntityManager component which is found in the SadConsole.Entities namespace.
It handles all the entity rendering for you.

var entityManager = new EntityManager();
Game.Instance.StartingConsole.SadComponents.Add(entityManager);

This adds the EntityManager as a component to the StartingConsole, now all that is left is to Add the entity to the EntityManager.

entityManager.Add(playerEntity);

The red @ glyph is now automatically rendered onto the console.
Removing entities is just as simple:

entityManager.Remove(playerEntity);

SadConsole.Extended

The SadConsole.Extended package also provides a EntityManagerZoned object which works similar to an EntityManager, but it also uses Zones that you can define.

Each entity that enters/exits or moves in a zone will raise an event particular to the action that happened.

Example:

var entityManager = new EntityManagerZoned
{
	new Zone(new Area(new Rectangle(0, 0, 10, 10).Positions()))
};
entityManager.EnterZone += (sender, args) => { System.Console.WriteLine($"{args.Entity.Name} entered zone."); };
entityManager.ExitZone += (sender, args) => { System.Console.WriteLine($"{args.Entity.Name} exited zone."); };

Game.Instance.StartingConsole.SadComponents.Add(entityManager);

var playerEntity = new Entity(
	foreground: Color.Cyan,
	background: Color.Transparent,
	glyph: '@',
	zIndex: 0) // Defines the layer the entity is on
{
	Name = "Player",
	Position = new Point(5, 5) // Spawn him inside the zone
};
entityManager.Add(playerEntity);

// Move out of the zone
playerEntity.Position = new Point(12, 12);

Leave a comment

Design a site like this with WordPress.com
Get started