Ejemplo n.º 1
0
bool Monster::checkBoxes(std::vector<CollisionBox>* boxes, float p_x, float p_y){
	CollisionBox box;

	Point2D A = Point2D(this->getX(), this->getY());
	Point2D B;
	for (int i = 0; i < boxes->size(); i++){
		box = boxes->at(i);


		if (box.getRight() < this->getBox().getLeft())
			B.setX(box.getRight());
		else if (box.getLeft() > this->getBox().getRight())
			B.setX(box.getLeft());
		else
			B.setX(this->getX());

		if (box.getTop() < this->getBox().getBottom())
			B.setY(box.getTop());
		else if (box.getBottom() > this->getBox().getTop())
			B.setY(box.getBottom());
		else
			B.setY(this->getY());


		float difx = (B.getX() - A.getX()) * (B.getX() - A.getX());
		float dify = (B.getY() - A.getY()) * (B.getY() - A.getY());

		float dist = difx + dify;
		if (dist <= this->getRadius() * this->getRadius()){


			if ((this->getBox().getBottom() > box.getTop() ||
				this->getBox().getTop() < box.getBottom())
				){
				if (this->getBox().getBottom() > box.getTop()){
					if (p_x == this->getX()){
						this->angle = 0;
					}

					else if (p_x > this->getX()){

						if (this->getY() < p_y)
							this->angle = 8;
						else
							this->angle = 0;

					}
					else {

						if (this->getY() < p_y)
							this->angle = 172;
						else
							this->angle = 180;
					}

				}
				else if (this->getBox().getTop() < box.getBottom()){
					if (p_x == this->getX()){
						this->angle = 0;
					}

					else if (p_x > this->getX()){
						if (this->getY() > p_y)
							this->angle = 352;
						else
							this->angle = 0;

					}
					else{
						if (this->getY() > p_y)
							this->angle = 188;
						else
							this->angle = 180;
					}
				}

			}
			else if ((this->getBox().getRight() < box.getLeft() || this->getBox().getLeft() > box.getRight())
				){

				float up = box.getTop() - this->getY();
				float down = this->getY() - box.getBottom();

				if (this->getBox().getRight() < box.getLeft()){
					if (p_y == this->getY()){
						this->angle = 90;
					}
					else if (p_y > this->getY()){
						if (this->getX() > p_x)
							this->turn(this->getX() - 16, box.getTop() + 32);
						else
							this->turn(this->getX(), box.getTop() + 32);
					}
					else{
						if (this->getX() > p_x)
							this->turn(this->getX() - 16, box.getBottom() - 32);
						else
							this->turn(this->getX(), box.getBottom() - 32);
					}

				}
				else if (this->getBox().getLeft() > box.getRight()){

					if (p_y == this->getY()){
						this->angle = 90;
					}
					else if (p_y > this->getY()){
						if (this->getX() < p_x)
							this->turn(this->getX() + 16, box.getTop() + 32);
						else
							this->turn(this->getX(), box.getTop() + 32);
					}
					else{
						if (this->getX() < p_x)
							this->turn(this->getX() + 16, box.getBottom() - 32);
						else
							this->turn(this->getX(), box.getBottom() - 32);
					}
				}
			}
			return true;
		}
		else
			continue;
	}
	return false;
}