Entity.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Entity.h
 *
 *  Created on: 17/ott/2012
 *      Author: Vittorio
 */

#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
16
17
18
19
20
21
22
23
24
25
26
/*
 * Entity.cpp
 *
 *  Created on: 17/ott/2012
 *      Author: Vittorio
 */

#include <algorithm>
#include <vector>
#include "Entity.h"
#include "Component.h"

class Component;

Entity::~Entity() {
	for (auto &componentPtr : componentPtrs) delete componentPtr;
}
void Entity::addComponent(Component* mComponentPtr) {
	componentPtrs.push_back(mComponentPtr);
	mComponentPtr->setParent(this);
}
void Entity::delComponent(Component* mComponentPtr) {
	componentPtrs.erase(remove(componentPtrs.begin(), componentPtrs.end(), mComponentPtr), componentPtrs.end());
	delete mComponentPtr;
}
void Entity::update() { for (auto &componentPtr : componentPtrs) componentPtr->update(); }

Component.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Component.h
 *
 *  Created on: 17/ott/2012
 *      Author: Vittorio
 */

#ifndef COMPONENT_H_
#define COMPONENT_H_

class Entity;

class Component {
private:
	Entity* parentPtr;

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

#endif /* COMPONENT_H_ */

Component.cpp

1
2
3
4
5
6
7
8
9
10
11
/*
 * Component.cpp
 *
 *  Created on: 17/ott/2012
 *      Author: Vittorio
 */

#include "Component.h"

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