Entity.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef ENTITY_H_
#define ENTITY_H_

#include <vector>

class Component;

class Entity {
private:
	std::vector<Component*> componentPtrs;

public:
	~Entity();
	void addComponent(Component* mComponentPtr);
	void delComponent(Component* mComponentPtr);
	void update();
};

#endif /* ENTITY_H_ */

Entity.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <sstream>
#include <algorithm>

#include "Entity.h"
#include "Component.h"

using namespace std;

void Entity::update() {
	componentPtrs[0]->update();
}

Component.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef COMPONENT_H_
#define COMPONENT_H_

class Entity;

class Component {
private:
	Entity* parentPtr;

public:
	virtual void init();
	virtual void update() = 0;
	virtual ~Component();
	void setParent(Entity* mParentPtr);
};

#endif /* COMPONENT_H_ */

Component.cpp

1
2
3
4
#include "Component.h"

void Component::setParent(Entity* mParentPtr) { parentPtr = mParentPtr; }
void Component::update() { }