Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ime::Tile Class Reference

A Grid2D tile. More...

#include <Tile.h>

Inheritance diagram for ime::Tile:
ime::Drawable ime::Object

Public Types

using Ptr = std::unique_ptr< Object >
 Unique object pointer. More...
 

Public Member Functions

 Tile (Vector2u size, Vector2f position)
 Construct a tile. More...
 
 Tile (const Tile &)
 Copy constructor. More...
 
Tileoperator= (Tile)
 Copy assignment operator. More...
 
 Tile (Tile &&) noexcept=default
 Move constructor. More...
 
Tileoperator= (Tile &&) noexcept=default
 Move assignment operator. More...
 
void setPosition (float x, float y)
 Set the position of the tile. More...
 
void setPosition (Vector2f pos)
 Set the position of the tile. More...
 
Vector2f getPosition () const
 Get the position of the tile. More...
 
void setSize (unsigned int width, unsigned int height)
 Set the size of the tile. More...
 
void setSize (Vector2u size)
 Set the size of the tile. More...
 
Vector2u getSize () const
 Get the size of the tile. More...
 
void setVisible (bool visible)
 Show or hide the tile. More...
 
bool isVisible () const
 Check whether or not the tile is visible. More...
 
void setIndex (Index index)
 Set the index of the tile in the grid. More...
 
Index getIndex () const
 Get the index of the tile in the grid. More...
 
void setId (char id)
 Assign the tile an identification token. More...
 
char getId () const
 Get the id of the tile. More...
 
void setCollidable (bool collidable)
 Set whether tile is collidable or not. More...
 
bool isCollidable () const
 Check if the tile is collidable or not. More...
 
void setFillColour (const Colour &colour)
 Set the fill colour of the tile. More...
 
Colour getFillColour () const
 Get the fill colour of the tile. More...
 
std::string getClassName () const override
 Get the name of this class. More...
 
Vector2f getLocalCentre () const
 Get the tile's centre point in local coordinates. More...
 
Vector2f getWorldCentre () const
 Get the tile's centre point in world coordinates. More...
 
bool contains (Vector2f point) const
 Check if the tile contains a world coordinate. More...
 
void attachCollider (std::unique_ptr< BoxCollider > collider)
 Add a collider to the tile. More...
 
void removeCollider ()
 Remove the collider added to the tile. More...
 
bool hasCollider () const
 Check if the tile has a collider or not. More...
 
void toggleVisibility ()
 Toggle the visibility of the tile. More...
 
void swap (Tile &other)
 Swap the contents of this tile with that of another tile. More...
 
void setBody (RigidBody::Ptr body)
 Set the tiles RigidBody. More...
 
void draw (priv::RenderTarget &renderTarget) const override
 Draw object on a render target. More...
 
 ~Tile () override
 Destructor. More...
 
std::string getClassType () const override
 Get the name of this class. More...
 
void setTag (const std::string &tag)
 Assign the object an alias. More...
 
const std::string & getTag () const
 Get the tag assigned to the object. More...
 
unsigned int getObjectId () const
 Get the unique id of the object. More...
 
int onPropertyChange (const std::string &property, const Callback< Property > &callback, bool oneTime=false)
 Add an event listener to a specific property change event. More...
 
int onPropertyChange (const Callback< Property > &callback, bool oneTime=false)
 Add an event listener to any property change event. More...
 
void suspendedEventListener (int id, bool suspend)
 Pause or resume execution of an event listener. More...
 
bool isEventListenerSuspended (int id) const
 Check if an event listener is suspended or not. More...
 
bool removeEventListener (const std::string &event, int id)
 Remove an event listener from an event. More...
 
bool removeEventListener (int id)
 Remove an event listener. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool isSameObjectAs (const Object &other) const
 Check if another object is the same instance as this object. More...
 

Protected Member Functions

void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emitDestruction ()
 Emit a destruction event. More...
 

Protected Attributes

EventEmitter eventEmitter_
 Event dispatcher. More...
 

Detailed Description

A Grid2D tile.

Definition at line 41 of file Tile.h.

Member Typedef Documentation

◆ Ptr

using ime::Object::Ptr = std::unique_ptr<Object>
inherited

Unique object pointer.

Definition at line 42 of file Object.h.

Constructor & Destructor Documentation

◆ Tile() [1/3]

ime::Tile::Tile ( Vector2u  size,
Vector2f  position 
)

Construct a tile.

Parameters
sizeSize of the tile
positionPosition of the tile

◆ Tile() [2/3]

ime::Tile::Tile ( const Tile )

Copy constructor.

◆ Tile() [3/3]

ime::Tile::Tile ( Tile &&  )
defaultnoexcept

Move constructor.

◆ ~Tile()

ime::Tile::~Tile ( )
override

Destructor.

Member Function Documentation

◆ attachCollider()

void ime::Tile::attachCollider ( std::unique_ptr< BoxCollider collider)

Add a collider to the tile.

Parameters
colliderThe collider to be added

Without a Collider, only game objects that are controlled by a GridMover can collide with the tile. Attaching a collider makes a GameObject with a RigidBody that has a Collider attached to it able to collide with the tile. The tile can can only have one collider attached to it. The current collider must be removed first before attaching a new one

By default, the tile does not have a collider attached to it

Warning
A RigidBody is required before attaching a Collider, doing so without a RigidBody is undefined behavior. Use the setBody() function to add a RigidBody
See also
removeCollider and hasCollider

◆ contains()

bool ime::Tile::contains ( Vector2f  point) const

Check if the tile contains a world coordinate.

Parameters
pointThe point (in world coordinates) to be checked
Returns
True if the tile contains the coordinates, otherwise false

◆ draw()

void ime::Tile::draw ( priv::RenderTarget &  renderTarget) const
overridevirtual

Draw object on a render target.

Parameters
renderTargetTarget to draw object on
Note
This function is intended for internal use only

Implements ime::Drawable.

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ emitDestruction()

void ime::Object::emitDestruction ( )
protectedinherited

Emit a destruction event.

Note
This function must be the first statement in the definition of a destructor to avoid undefined behavior. In addition, note that destruction listeners are invoked once. Therefore, multiple classes in a hierarchy may call this function but the class that makes the call first will be the one that invokes the destruction listeners

◆ getClassName()

std::string ime::Tile::getClassName ( ) const
overridevirtual

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

std::string ime::Drawable::getClassType ( ) const
overridevirtualinherited

Get the name of this class.

Returns
The name of this class

Note that this function is only overridden by child classes of Object which also serve as a base class for other classes

See also
Object::getClassType and Object::getClassName

Reimplemented from ime::Object.

Reimplemented in ime::Shape.

◆ getFillColour()

Colour ime::Tile::getFillColour ( ) const

Get the fill colour of the tile.

Returns
The fill colour of the tile

◆ getId()

char ime::Tile::getId ( ) const

Get the id of the tile.

Returns
The id of the tile

◆ getIndex()

Index ime::Tile::getIndex ( ) const

Get the index of the tile in the grid.

Returns
The index of the tile in the grid

Note that each tile in the grid has a unique index

◆ getLocalCentre()

Vector2f ime::Tile::getLocalCentre ( ) const

Get the tile's centre point in local coordinates.

Returns
The tiles centre point in local coordinates

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the unique id of the object.

Returns
The unique id of the object

Note that each instance of ime::Object has a unique id

See also
setTag

◆ getPosition()

Vector2f ime::Tile::getPosition ( ) const

Get the position of the tile.

Returns
The position of the tile

◆ getSize()

Vector2u ime::Tile::getSize ( ) const

Get the size of the tile.

Returns
The size of the tile

◆ getTag()

const std::string & ime::Object::getTag ( ) const
inherited

Get the tag assigned to the object.

Returns
The tag of the object
See also
setTag

◆ getWorldCentre()

Vector2f ime::Tile::getWorldCentre ( ) const

Get the tile's centre point in world coordinates.

Returns
The tiles centre point in world coordinates

◆ hasCollider()

bool ime::Tile::hasCollider ( ) const

Check if the tile has a collider or not.

Returns
True if the tile has a collider, otherwise false

◆ isCollidable()

bool ime::Tile::isCollidable ( ) const

Check if the tile is collidable or not.

Returns
True if the tile is collidable, otherwise false

◆ isEventListenerSuspended()

bool ime::Object::isEventListenerSuspended ( int  id) const
inherited

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ isSameObjectAs()

bool ime::Object::isSameObjectAs ( const Object other) const
inherited

Check if another object is the same instance as this object.

Parameters
otherThe object to compare against this object
Returns
True if other is the same instance as this object, otherwise false

◆ isVisible()

bool ime::Tile::isVisible ( ) const

Check whether or not the tile is visible.

Returns
True if visible, otherwise false

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The unique id of the destruction listener

The destruction listener is called when the object reaches the end of its lifetime. Note that an object may have multiple destruction listeners registered to it

Warning
It's not advised to call virtual functions in the destruction callback as some parts of the object may have already been destroyed by the time the callback is invoked. In such an event, the behavior is undefined
See also
removeEventListener

◆ onPropertyChange() [1/2]

int ime::Object::onPropertyChange ( const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to any property change event.

Parameters
callbackThe function to be executed when any property changes
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The unique id of the event listener

When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event

See also
onPropertyChange(const std::string&, const ime::Callback<ime::Property>&)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to add an event listener to
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, the setTag() function, modifies the tag property of the object, thus will generate a "tag" change event each time it is called

Note that multiple event listeners may be registered to the same property change event. In addition, when adding a property change event listener, the name of the property must be in lowercase.

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");
See also
unsubscribe and onPropertyChange(const ime::Callback<ime::Property>&)

◆ operator=() [1/2]

Tile & ime::Tile::operator= ( Tile &&  )
defaultnoexcept

Move assignment operator.

◆ operator=() [2/2]

Tile & ime::Tile::operator= ( Tile  )

Copy assignment operator.

◆ removeCollider()

void ime::Tile::removeCollider ( )

Remove the collider added to the tile.

Note that when the collider is removed, the tile will no longer participate in RigidBody physics, however grid-based physics (see ime::GridMover) will continue as normal.

See also
attachCollider and hasCollider

◆ removeEventListener() [1/2]

bool ime::Object::removeEventListener ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventThe name of the event to remove an event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the tag of the object to console every time it changes
auto tagChangeId = object.onPropertyChange("tag", [](ime::Property tag) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the tag of the object when it changes
object.removeEventListener("tag", tagChangeId);
Class that can store a value of any type.
Definition: Property.h:38

◆ removeEventListener() [2/2]

bool ime::Object::removeEventListener ( int  id)
inherited

Remove an event listener.

Parameters
idThe id of the event listener to be removed
Returns
True if the event listener was removed or false if no such handler exists

◆ setBody()

void ime::Tile::setBody ( RigidBody::Ptr  body)

Set the tiles RigidBody.

Parameters
bodyThe physics body to set

The rigid body is only required if you intend to attach a Collider. The rigid body must be of type ime::RigidBody::Type::Static

See also
attachCollider

◆ setCollidable()

void ime::Tile::setCollidable ( bool  collidable)

Set whether tile is collidable or not.

Parameters
collidableTrue to set collidable, otherwise false

Note that disabling a collision for a tile that was previously had a Collider attached to it does not remove the collider. This removes the need to create a new collider everytime the collidability of the tile is re-enabled. The collider can be explicitly removed from the tile by calling the removeCollider() function

By default, the tile is not collidable

See also
attachCollider

◆ setFillColour()

void ime::Tile::setFillColour ( const Colour colour)

Set the fill colour of the tile.

Parameters
colourNew fill colour of the tile

◆ setId()

void ime::Tile::setId ( char  id)

Assign the tile an identification token.

Parameters
idIdentification token to assign

By default, the tile id is '\0'

Note that multiple tiles can have the same identification token

◆ setIndex()

void ime::Tile::setIndex ( Index  index)

Set the index of the tile in the grid.

Parameters
indexThe index of the tile in the grid

The index corresponds to the position of the tile in the grid

By default, the index is (-1, -1)

◆ setPosition() [1/2]

void ime::Tile::setPosition ( float  x,
float  y 
)

Set the position of the tile.

Parameters
xX coordinate of the tile
yY coordinate of the tile

By default, the position is (0, 0)

◆ setPosition() [2/2]

void ime::Tile::setPosition ( Vector2f  pos)

Set the position of the tile.

Parameters
posThe new position of the tile

By default, the position is (0, 0)

◆ setSize() [1/2]

void ime::Tile::setSize ( unsigned int  width,
unsigned int  height 
)

Set the size of the tile.

Parameters
widthThe horizontal size
heightThe vertical size

◆ setSize() [2/2]

void ime::Tile::setSize ( Vector2u  size)

Set the size of the tile.

Parameters
sizeNew tile size

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
tagThe alias of the object

This function is useful if you want to refer to the object by a tag instead of its object id. Unlike an object id, multiple objects may have the same tag

By default, the tag is an empty string

See also
getObjectId

◆ setVisible()

void ime::Tile::setVisible ( bool  visible)

Show or hide the tile.

Parameters
visibleTrue to show or false to hide

When hidden the tile will not be shown on the render target, However it can still be collided with if its collidable

By default, the tile is visible

See also
setCollidable

◆ suspendedEventListener()

void ime::Object::suspendedEventListener ( int  id,
bool  suspend 
)
inherited

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

◆ swap()

void ime::Tile::swap ( Tile other)

Swap the contents of this tile with that of another tile.

Parameters
otherThe tile to swap contents with

◆ toggleVisibility()

void ime::Tile::toggleVisibility ( )

Toggle the visibility of the tile.

This function will hide the tile if its currently visible or show it if it is currently hidden

Member Data Documentation

◆ eventEmitter_

EventEmitter ime::Object::eventEmitter_
protectedinherited

Event dispatcher.

Definition at line 289 of file Object.h.


The documentation for this class was generated from the following file: