void Enemy::update() 
{
	if (this->position[0] >= windowBox.getRight())
		this->position[0] = position[0] - (windowBox.getRight() - windowBox.getLeft());

	this->boundBox->minX = this->position[0] + this->minX;
	this->boundBox->maxX = this->position[0] + this->maxX;
	this->boundBox->minY = this->position[1] + this->minY;
	this->boundBox->maxY = this->position[1] + this->maxY;
}
Exemple #2
0
//circle(zombie) to rectangle collision detection
bool Zombie::checkCollision(std::vector<BBox>* boxes, float p_x, float p_y){
	BBox box;
	Point A = Point(this->getX(), this->getY());
	Point B;
	for(int i = 0; i<boxes->size();i++){
		box = boxes->at(i);

		//finds a point on the rectangles bounding box and that point acts as a circle with a radius 0 radius
		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()){
		
		//FOLLOWS THE EDGES OF BOX TO REACH the player

			//if zombie is on below or above the box
			if((this->getBox().getBottom() > box.getTop() ||
				this->getBox().getTop()  < box.getBottom())
			){
					//IF ON TOP
				if(this->getBox().getBottom() > box.getTop()){
					if(p_x == this->getX()){
						this->angle = 0;
					}
					//if player is on the right of the zombie
					else if(p_x > this->getX()){
						//if the player is positioned higher than the zombie
						if(this->getY() < p_y)
							this->angle = 8;
						else
							this->angle = 0;
						//if player is on the left of the zombie
					}else {
						//if the player is positioned higher than the zombie
						if(this->getY() < p_y)
							this->angle = 172;
						else
							this->angle = 180;
					}
					//IF BELOW THE BOX
				}else if(this->getBox().getTop() < box.getBottom()){
					if(p_x == this->getX()){
						this->angle = 0;
					}
					//if player is on the right of the zombie
					else if(p_x > this->getX()){
						if(this->getY() > p_y)
							this->angle = 352;
						else
							this->angle = 0;
						//if player is on the left of the zombie
					}else{
						if(this->getY() > p_y)
							this->angle = 188;
						else
							this->angle = 180;
					}
				}
		//if zombie is on left or right side of the box
		}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 ON LEFT SIDE
				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);	
					}

				}
				//IF ON RIGHT SIDE 
				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;
}