Map Mode Objects

From Hero of Allacrost Wiki
Jump to: navigation, search


Map objects are a very inclusive category that cover a wide variety of entities that exist on the map. The most common object is a map sprite, representing a NPC, enemy, or the character that the player is controlling. Objects can also represent physical structures such as trees or buildings as an alternative to representing those things with tiles. Objects can also represent non-physical entities that the player can not see or interact with. For example, the map camera is a type of map object.

The MapObject class is an abstract base class that all of these various types of objects inherit from. The data in this class shared by map objects includes a unique identifier, the object's position, the context that the object exists on, the dimensions of the collision rectangle, the dimensions of the image rectangle, and properties indicating whether the object is visible, collide-able, and update-able.

Core Functionality

This section lists the core functionality with the operation and management of map objects. The ObjectSupervisor class is responsible for all object management and contains implementations of common operations that various objects need such as collision detection and path finding.

Collision Detection

All sprite-type objects need to perform collision detection during movement to ensure that the sprite does not go outside the map's boundaries, collide with unwalkable areas of the map, or occupies the same collision space as another object. These cases are representing by the following enum. When a collision happens, it is important that we be able to detect what type of collision it was as we may take different actions to correct the collision.

//! \brief Represents the various types of collisions which may occur for a sprite
	NO_COLLISION        = 0, //!< Indicates that no collision has occurred
	BOUNDARY_COLLISION  = 1, //!< Happens when the sprite attempts to move outside any of the map's boundaries
	GRID_COLLISION      = 2, //!< Condition when the sprite's collision rectangle overlaps an invalid element of the map's collision grid
	OBJECT_COLLISION    = 3, //!< Occurs when the sprite collides with another map object in the same object layer

The function that performs the collision detection is ObjectSupervisor::DetectCollision(). This takes a pointer to a sprite to perform collision detection for and returns an enum value indicating if a collision occurred and what type. If a collision did occur, this function will also register a MapCollisionNotificationEvent to the notification engine with. The proper way to use this function is to do the following:

  • Move the sprite to it's desired position
  • Call DetectCollision to determine if the sprite at its new position produces a collision
  • If a collision is detected, take a proper resolution (typically: move the sprite back to its original position)

DetectCollision should be always be called immediately after changing the position of a sprite. Failing to do so may leave one or more map objects unable to move if they remain in a state of collision.

Collision Resolution