c_cpp 组件实体样本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 组件实体样本相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<vector>
#include<bitset>

const short MAX_COMPS = 4;

typedef std::bitset<MAX_COMPS> ComponentMask;

/************************************************************************/
/* COMPONENT                                                            */
/************************************************************************/
class Component {
protected:
public:
	virtual short getBit() = 0;
};
typedef std::vector<Component*> ComponentList;

/************************************************************************/
/* POSITION - COMPONENT                                                 */
/************************************************************************/
class Position : public Component {
	int _x;
	int _y;
	int _z;

public:
	short getBit(){
		return 0;
	}

	Position(int x, int y, int z){
		_x = x;
		_y = y;
		_z = z;
	}

	int getX(){
		return _x;
	}

	int getY(){
		return _y;
	}

	int getZ(){
		return _z;
	}

	void setX(int value){
		_x = value;
	}

	void setY(int value){
		_y = value;
	}

	void setZ(int value){
		_z = value;
	}
};

/************************************************************************/
/* SIZ - COMPONENT                                                      */
/************************************************************************/
class Size : public Component {
public:
	short getBit(){
		return 1;
	}
};

/************************************************************************/
/* ENTITY																*/
/************************************************************************/
class Entity{
private:
	ComponentList _components;
public:
	~Entity(){
		for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
		{
			delete (*it);
		}
	}

	void addComponent(Component* component){
		bool add = true;	
		for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
		{
			if ((*it)->getBit() == component->getBit()) add = false;
		}

		if (add) _components.push_back(component);
		else delete component;
	}

	ComponentMask getComponentMask(){
		ComponentMask m;
		for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
		{
			m.set((*it)->getBit());
		}
		return m;
	}

	Component* getComponent(short bit){
		for (ComponentList::iterator it = _components.begin(); it < _components.end(); it++)
		{
			if ((*it)->getBit() == bit) return (*it);
		}
		return NULL;
	}
};
typedef std::vector<Entity*> EntityList;

/************************************************************************/
/* SYSTEM																*/
/************************************************************************/
class System{
private:
	ComponentMask _componentMask;

protected:
	EntityList _entities;

public:
	System(ComponentMask componentMask){
		_componentMask = componentMask;
	}

	void addEntity(Entity* entity){
		if ((entity->getComponentMask() & _componentMask) == _componentMask){
			_entities.push_back(entity);
		}
	}

	void update(double delta){
		for (EntityList::iterator it = _entities.begin(); it < _entities.end(); it++)
		{
			callEntity((*it), delta);
		}
	}

	virtual void callEntity(Entity* entity, double delta) = 0;
};
typedef std::vector<System*> SystemList;

/************************************************************************/
/* POSITION - ENTITY													*/
/************************************************************************/
class PositionSystem : public System{
public:
	PositionSystem(ComponentMask componentMask) : System(componentMask){

	}

	void callEntity(Entity* entity, double delta){
		//TODO: Position* p = (Position*)e->getComponent(Position::ComponentId);
		Position* position = (Position*)entity->getComponent(0);
		position->setX(position->getX()+1);
	}
};

class World {
private:
	SystemList _systemList;
public:
	World(){
	}

	void addSystem(System* system){
		_systemList.push_back(system);
	}

	void update(double delta){
		for (SystemList::iterator it = _systemList.begin(); it < _systemList.end(); it++)
		{
			(*it)->update(delta);
		}
	}
};

int main(){
	World world;

	Entity entitiy;
	entitiy.addComponent(new Position(10,20,10));
	entitiy.addComponent(new Size());

	ComponentMask componentMask;
	//TODO: componentMask.set(Position::ComponentId);
	componentMask.set(0);

	PositionSystem positionSystem = PositionSystem(componentMask);
	positionSystem.addEntity(&entitiy);

	world.addSystem(&positionSystem);
	world.update(10);

	std::cout << entitiy.getComponentMask();
	std::cin.get();

	return 0;
}

以上是关于c_cpp 组件实体样本的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 内存泄漏样本

c_cpp 样本文件

c_cpp 样本类包裹着字符串

c_cpp 基于样本的图灵虚拟机c

c_cpp 样本二进制表数据交换语言设置。

c_cpp 难以解析的C ++文件的小样本