Last active
January 10, 2018 02:10
-
-
Save sjgriffiths/515644e9cf48cddc85af70c50d14b692 to your computer and use it in GitHub Desktop.
Primitive game entity (C++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Entity.cpp | |
Implements the Entity class, representing | |
an object within the game engine. | |
@author Sam Griffiths | |
www.computist.xyz | |
*/ | |
#include "Entity.h" | |
EntityID Entity::idCount = 0; | |
std::queue<EntityID> Entity::idRemoved; | |
Entity::Entity(std::string name) : _name(name) | |
{ | |
//Take re-usable IDs before claiming new ones | |
if (!idRemoved.empty()) | |
{ | |
_id = idRemoved.front(); | |
idRemoved.pop(); | |
} | |
else _id = idCount++; | |
} | |
Entity::~Entity() | |
{ | |
idRemoved.push(_id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Entity.h | |
Declares the Entity class, representing | |
an object within the game engine. | |
@author Sam Griffiths | |
www.computist.xyz | |
*/ | |
#pragma once | |
#include <string> | |
#include <queue> | |
typedef unsigned long int EntityID; | |
class Entity | |
{ | |
private: | |
EntityID _id; | |
static EntityID idCount; | |
static std::queue<EntityID> idRemoved; | |
std::string _name; | |
public: | |
Entity(std::string); | |
~Entity(); | |
//Returns the unique ID of the Entity | |
EntityID id() const { return _id; } | |
//Returns the name of the Entity | |
std::string name() const { return _name; } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment