Class GameObject
Base class for any object that has a grid position and can be added to a Map. Implements basic attributes generally common to all objects on a map, as well as properties/methods that the Map class needs to function. It also implements IHasComponents, which means you can attach components to it. In cases where this class cannot be inherited from, have your class implement IGameObject via a private GameObject field.
Inherited Members
Namespace: GoRogue.GameFramework
Assembly: GoRogue.dll
Syntax
public class GameObject : ComponentContainer, IGameObject, IHasID, IHasLayer, IHasComponents
Remarks
This class is designed to serve as a base class for your own game objects in your game. It implements basic common functionality such as walkability and transparency, provides some infrastructure that allows it to be added to instances of Map, and implements the framework for GoRogue's component system. It also implements the necessary functionality that allows GameObjects to be added to an ISpatialMap<T> implementation.
Generally, you would create one or more classes (say, MyGameObject or MyTerrain), that derives from this one (GameObject), or implement IGameObject by composition using a private field of this class, and use that as the base class for your game's objects. There is an example of doing this here. If you utilize the component system, a subclass of GameObject isn't strictly necessary, as you could just construct basic GameObject instances and add components to them. In either case, a Map instance can be used to store these objects efficiently. As well, Map provides functionality that will allow you to retrieve your objects as references of their derived type (MyGameObject or MyTerrain, in the example above), meaning that you can implement any common, game-specific functionality you need and have easy access to that information when objects are retrieved from the map. This also means that if you prefer an inheritance-based approach, you can simply not utilize the component system, and you are still able to use these Map functions to differentiate functionality.
Although the component system will accept items of any type as components, there is an optional IGameObjectComponent interface that GoRogue provides, that has a field for the parent, eg. the IGameObject that it is attached to. This field is automatically kept up to date as you add/remove components if you implement this interface for your components.
Constructors
| Improve this Doc View SourceGameObject(Coord, Int32, IGameObject, Boolean, Boolean, Boolean)
Constructor.
Declaration
public GameObject(Coord position, int layer, IGameObject parentObject, bool isStatic = false, bool isWalkable = true, bool isTransparent = true)
Parameters
Type | Name | Description |
---|---|---|
Coord | position | Position to start the object at. |
System.Int32 | layer | The layer of of a Map the object is assigned to. |
IGameObject | parentObject | Object holding this GameObject instance. If you are inheriting from GameObject, or using a backing field of type GameObject to implement IGameObject, for example, you would pass langword_csharp_this at construction. Otherwise, if you are simply instantiating base GameObject instances and adding them to a Map, you would pass null. |
System.Boolean | isStatic | Whether or not the object can be moved (true if the object CANNOT be moved, false otherwise). |
System.Boolean | isWalkable | Whether or not the object is to be considered "walkable", eg. whether or not the square it resides on can be traversed by other, non-walkable objects on the same Map. Effectively, whether or not this object collides. |
System.Boolean | isTransparent | Whether or not the object is considered "transparent", eg. whether or not light passes through it for the sake of calculating the FOV of a Map. |
Properties
| Improve this Doc View SourceCurrentMap
The current Map which this object resides on. Returns null if the object has not been added to a map. A GameObject is allowed to reside on only one map.
Declaration
public Map CurrentMap { get; }
Property Value
Type | Description |
---|---|
Map |
ID
ID of the object. Used for the sake of putting instances of this class in an ISpatialMap<T> implementation, and is NOT guaranteed to be entirely unique, though this can be modified by overriding the GenerateID() function.
Declaration
public uint ID { get; }
Property Value
Type | Description |
---|---|
System.UInt32 |
IsStatic
Whether or not the object is "static". Static objects CANNOT be moved, and only static objects may be placed on layer 0 of a Map.
Declaration
public bool IsStatic { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsTransparent
Whether or not the object is considered "transparent", eg. whether or not light passes through it for the sake of calculating the FOV of a Map.
Declaration
public virtual bool IsTransparent { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsWalkable
Whether or not the object is to be considered "walkable", eg. whether or not the square it resides on can be traversed by other, non-walkable objects on the same Map. Effectively, whether or not this object collides.
Declaration
public virtual bool IsWalkable { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
Layer
Layer of a Map that this object will reside on.
Declaration
public int Layer { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Position
The position of this object on the grid. Any time this value is changed, the Moved event is fired.
Declaration
public virtual Coord Position { get; set; }
Property Value
Type | Description |
---|---|
Coord |
Remarks
This property may be overriden to implement custom functionality, however it is highly recommended that you call the base set in the overridden setter, as it performs collision detection.
Methods
| Improve this Doc View SourceAddComponent(Object)
Adds the given object as a component. Throws an exception if that specific instance is already attached to this GameObject.
Declaration
public override void AddComponent(object component)
Parameters
Type | Name | Description |
---|---|---|
System.Object | component | Component to add. |
Overrides
| Improve this Doc View SourceGenerateID()
Function used at construction to assign an ID to the object.
Declaration
protected virtual uint GenerateID()
Returns
Type | Description |
---|---|
System.UInt32 | An ID to assign to the current object. |
Remarks
The default implementation simply assigns a random number in range of valid uints. This is sufficiently distinct for the purposes of placing the objects in an ISpatialMap<T> implementation, however obviously does NOT guarantee true uniqueness. If uniqueness or some other implementation is required, override this function to return an appropriate ID. Keep in mind a relatively high degree of uniqueness is necessary for efficient placement in an ISpatialMap implementation.
MoveIn(Direction)
Attempts to move the object in the given direction, and returns true if the object was successfully moved, false otherwise.
Declaration
public bool MoveIn(Direction direction)
Parameters
Type | Name | Description |
---|---|---|
Direction | direction | The direction in which to try to move the object. |
Returns
Type | Description |
---|---|
System.Boolean | True if the object was successfully moved, false otherwise. |
OnMapChanged(Map)
Internal use only, do not call manually! Must, at minimum, update the CurrentMap field of the GameObject to reflect the change.
Declaration
public void OnMapChanged(Map newMap)
Parameters
Type | Name | Description |
---|---|---|
Map | newMap | New map to which the GameObject has been added. |
RemoveComponents(Object[])
Removes the given component(s) from the GameObject. Throws an exception if a component given is not attached to the GameObject.
Declaration
public override void RemoveComponents(params object[] components)
Parameters
Type | Name | Description |
---|---|---|
System.Object[] | components | One or more component instances to remove. |
Overrides
Events
| Improve this Doc View SourceMoved
Event fired whenever this object's grid position is successfully changed. Fired regardless of whether the object is part of a Map.
Declaration
public event EventHandler<ItemMovedEventArgs<IGameObject>> Moved
Event Type
Type | Description |
---|---|
System.EventHandler<ItemMovedEventArgs<IGameObject>> |