The Entity class represents a unique entity in an Entity-Component-System (ECS) architecture. Entities are identified by a unique ID and are compared based on this ID.
namespace ecs {
class Entity {
public:
Entity(std::size_t id) : _id(id) {}
std::size_t getId() const
{
return _id;
}
std::size_t operator=(const std::size_t &id)
{
_id = id;
return id;
}
bool operator==(const Entity &other) const
{
return _id == other._id;
}
private:
std::size_t _id;
};
} // namespace ecs
Public Members:
- Constructor:
Entity(std::size_t id)
- Initializes an entity with a unique identifier (ID).
- Parameters:
id: A std::size_t value representing the entity's unique ID.
std::size_t getId() const
- Returns the unique identifier (ID) of the entity.
- Returns:
- A
std::size_t value that represents the entity's ID.
std::size_t operator=(const std::size_t &id)
- Assigns a new ID to the entity.
- Parameters:
id: A std::size_t value representing the new ID.
- Returns:
bool operator==(const Entity &other) const
- Compares two entities for equality by checking their IDs.
- Parameters:
other: Another Entity object to compare with.
- Returns:
true if the IDs of both entities are the same, otherwise false.
Private Members:
std::size_t _id
- A
std::size_t variable that stores the unique identifier of the entity.
Supplement:
- Entity Identification:
- The class revolves around the concept of a unique ID (
_id) that identifies each entity in the ECS system. Entities are distinguished and compared based on this ID.
- Comparison:
- The equality operator (
==) allows comparison between two Entity objects. Two entities are considered equal if they have the same ID.
- ID Assignment:
- The assignment operator allows an entity to be given a new ID after its creation. This operator ensures that an entity’s ID can be dynamically modified.