Skip to content

Instantly share code, notes, and snippets.

@sjgriffiths
Last active January 10, 2018 02:10
Show Gist options
  • Save sjgriffiths/515644e9cf48cddc85af70c50d14b692 to your computer and use it in GitHub Desktop.
Save sjgriffiths/515644e9cf48cddc85af70c50d14b692 to your computer and use it in GitHub Desktop.
Primitive game entity (C++)
/**
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);
}
/**
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