示例#1
0
void GameScreen::BuildQuadTree(){
	if (m_quadTree){
		delete m_quadTree;
		m_quadTree = NULL;
	}

	m_quadTree = new QuadTree();

	m_quadTree->buildFullTree(NBR_LEVELS, (m_width + 0.1f) / 2, (m_height + 0.1f) / 2, 0.5f, m_width + 0.1f, m_height + 0.1f, 1);

	for (int i = 0; i < m_land->size(); i++){
		BoxCollider* temp = new BoxCollider;
		temp->setDimension(m_landCube->getScale(), m_landCube->getScale(), m_landCube->getScale());
		temp->setPos(m_land->at(i));
		m_quadTree->addObject(temp);
	}
}
示例#2
0
void GameScreen::placeUnit(Unit* unit){
	bool done = false, regen = false;
	int attempts = 0;

	while (!done){
		unit->setPosition(random::rnd.number(m_width), random::rnd.number(m_height), 1);

		for (int i = 0; i < m_land->size(); i++){
			Vector point1, point2;
			point1 = unit->getPosition().x - (unit->getModel()->getCollider().getWidth() / 2);
			point2 = unit->getPosition().x + (unit->getModel()->getCollider().getWidth() / 2);

			BoxCollider tempBCol;
			tempBCol = m_landCube->getCollider();
			tempBCol.setPos(m_land->at(i));

			if ((point1.x <= tempBCol.getPos().x + (tempBCol.getWidth() / 2) && point1.x >= tempBCol.getPos().x - (tempBCol.getWidth() / 2)) || (point2.x <= tempBCol.getPos().x + (tempBCol.getWidth() / 2) && point2.x >= tempBCol.getPos().x - (tempBCol.getWidth() / 2))){
				unit->setPosition(m_land->at(i).x, m_land->at(i).y + m_landCube->getCollider().getHeight(), 1);

				for (int j = 0; j < m_land->size(); j++){
					if (unit->getPosition().x == m_land->at(j).x){
						if (unit->getPosition().y < m_land->at(j).y){
							float temp;

							temp = m_land->at(j).y - unit->getPosition().y;

							if (temp <= 0.1f){
								unit->setPosition(m_land->at(j).x, m_land->at(j).y + m_landCube->getCollider().getHeight(), 1);
							}
						}
					}
				}

				if (unit->getPosition().y < m_height){
					done = true;
				}

				for (int k = 0; k < NBR_TEAMS; k++){
					for (int l = 0; l < NBR_UNITS; l++){
						if (m_teams[k]->getUnit(l) == unit){
							continue;
						}

						if (m_teams[k]->getUnit(l)->getPosition() == unit->getPosition()){
							done = false;
						}
					}
				}

				break;
			}
		}

		if (attempts >= 100){
			done = true;
			regen = true;
		}

		attempts++;
	}

	if (regen){
		createGame(m_tGen.getCurrentType());
	}

	unit->setPosition(unit->getPosition().x, unit->getPosition().y + m_landCube->getCollider().getHeight(), 1);
}