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

A Tilemap tile. More...

#include <Tile.h>

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

Public Types

using Ptr = std::shared_ptr< Object >
 Shared 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
 Move constructor. More...
 
Tileoperator= (Tile &&) noexcept
 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 tilemap. More...
 
Index getIndex () const
 Get the index of the tile in the tilemap. 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::shared_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 draw (Window &renderTarget) const override
 Draw object on a render target. More...
 
void swap (Tile &other)
 Swap the contents of this tile with that of another tile. 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 alias of the object. More...
 
unsigned int getObjectId () const
 Get the id of the object. More...
 
int onPropertyChange (const std::string &property, const Callback< Property > &callback)
 Add an event listener to a specific property change event. More...
 
void onPropertyChange (const Callback< Property > &callback)
 Add an event listener to a property change event. More...
 
int onEvent (const std::string &event, const Callback<> &callback)
 Add an event listener to an event. More...
 
bool unsubscribe (const std::string &event, int id)
 Remove an event listener from an event. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool removeDestructionListener (int id)
 Remove a destruction listener form the object. More...
 
bool operator== (const Object &rhs) const
 Check if two objects are the same object or not. More...
 
bool operator!= (const Object &rhs) const
 Check if two objects are not the same object. More...
 

Protected Member Functions

void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emit (const std::string &event)
 Dispatch an action event. More...
 

Detailed Description

A Tilemap tile.

Definition at line 41 of file Tile.h.

Member Typedef Documentation

◆ Ptr

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

Shared object pointer.

Definition at line 45 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 &&  )
noexcept

Move constructor.

◆ ~Tile()

ime::Tile::~Tile ( )
override

Destructor.

Member Function Documentation

◆ attachCollider()

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

Add a collider to the tile.

Parameters
colliderThe collider to be added
Warning
It is required that all tilemap tiles that are collidable have a collider attached to them. The collider must be attached to a Static rigid body. Providing a collider that is not attached to a rigid body is undefined behavior. In addition calling the function setCollidable with an argument of true on a tile without a collider is undefined behavior.

Note that a tile can only have one collider attached to it. The current collider must be removed first before attaching a new one

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 ( Window renderTarget) const
overridevirtual

Draw object on a render target.

Parameters
renderTargetTarget to draw object on

Implements ime::Drawable.

◆ emit()

void ime::Object::emit ( const std::string &  event)
protectedinherited

Dispatch an action event.

Parameters
eventThe name of the event to be dispatched

This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)

See also
emitChange

◆ 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

◆ 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
inlineoverridevirtualinherited

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.

Definition at line 48 of file Drawable.h.

◆ 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 tilemap.

Returns
The index of the tile in the tilemap

Note that each tile in the tilemap 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 id of the object.

Returns
The id of the object

Each object has a unique id

◆ 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 alias of the object.

Returns
The alias of the object
See also
setTag and getObjectId

◆ 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

◆ 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 id of the destruction listener

Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time

Warning
The callback is called when the object is destroyed. Do not try to access the object in the callback. Doing so is undefined behavior
See also
removeDestructionListener

◆ onEvent()

int ime::Object::onEvent ( const std::string &  event,
const Callback<> &  callback 
)
inherited

Add an event listener to an event.

Parameters
eventThe name of the event to add an an event listener to
callbackThe function to be executed when the event takes place
Returns
The unique identification number of the event listener

Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:

// Add event listeners to the object
object.onEvent("attachRigidBody", [] {
std::cout << "Rigid body attached to object << std::endl;
});
object.onEvent("removeRigidBody", [] {
std::cout << "Rigid body removed from object << std::endl;
});
// Invokes event listener(s)
object.attachRigidBody(body);
object.removeRigidBody();
See also
onPropertyChange and unsubscribe

◆ onPropertyChange() [1/2]

void ime::Object::onPropertyChange ( const Callback< Property > &  callback)
inherited

Add an event listener to a property change event.

Parameters
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.

See also
onPropertyChange(std::string, Callback)

◆ onPropertyChange() [2/2]

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

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to listen for
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, for the setTag function, the property that the function modifies is Tag.

Note that 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");

Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time

See also
unsubscribe and onPropertyChange(Callback)

◆ operator!=()

bool ime::Object::operator!= ( const Object rhs) const
inherited

Check if two objects are not the same object.

Parameters
rhsObject to compare against this object
Returns
True if rhs is NOT the same object as this object, otherwise false

Two objects are different from each other if they have different object id's

See also
getObjectId and operator==

◆ operator=() [1/2]

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

Move assignment operator.

◆ operator=() [2/2]

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

Copy assignment operator.

◆ operator==()

bool ime::Object::operator== ( const Object rhs) const
inherited

Check if two objects are the same object or not.

Parameters
rhsObject to compare against this object
Returns
True if rhs is the same object as this object, otherwise false

Two objects are the same object if they have the same object id. Recall that each object instance has a unique id

See also
getObjectId and operator!=

◆ removeCollider()

void ime::Tile::removeCollider ( )

Remove the collider added to the tile.

Note that if the tile is collidable and the collider is removed, it will no longer be collidable. This function has no effect if the tile does not have a collider attached to it

See also
setCollidable and hasCollider

◆ removeDestructionListener()

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

Remove a destruction listener form the object.

Parameters
Theid of the destruction listener to be removed
Returns
True if the destruction listener was removed or false if the listener with the given id does not exist

◆ setCollidable()

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

Set whether tile is collidable or not.

Parameters
collidableTrue to set collidable, otherwise false
Warning
It is required for all tiles that are collidable to have a collider attached to them. As such, invoking this function with an argument of true when there is no collider attached to the tile is undefined behavior.

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

By default, the tile does not have a collider attached to it, hence 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 tilemap.

Parameters
indexThe index of the tile in the tilemap

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

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
nameThe alias of the object

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

By default, the tag is an empty string

◆ 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

◆ 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

◆ unsubscribe()

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

Remove an event listener from an event.

Parameters
eventThe name of the event to remove 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 name of the object to console every time it changes
auto nameChangeId = object.onPropertyChange("name", [](Property name) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the name of the object when it changes
object.unsubscribe("name", id);

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