Esempio n. 1
0
void Window::drawHitbox(const Hitbox &box, sf::Color color) {
	if(showHitbox) {
		sf::Shape boxA = sf::Shape::Rectangle(
				box.getMinX(), box.getMinY(), box.getMaxX(), box.getMaxY(),
				sf::Color(0, 0, 0, 0), 1.0f, color);
		app.Draw(boxA);
	}
}
Esempio n. 2
0
// Check for collision, correct position if necessary
// Colliding will stop the player and push them back.
void Player::collideWithTile(World *ptr, int id){
	if(ptr->world.find(id) == ptr->world.end()) return; //No such field on map

	Entity *entity = nullptr;

	if(ptr->world[id].find(LayerType::LAYER_STONES) != ptr->world[id].end())
		entity = ptr->world[id][LayerType::LAYER_STONES];
	else if(ptr->world[id].find(LayerType::LAYER_BLOCKS) != ptr->world[id].end())
		entity = ptr->world[id][LayerType::LAYER_BLOCKS];
	else
		return; //Field has no blocks and stones

	Hitbox self = getHitbox();
	Hitbox block = entity->getHitbox();

	if(Hitbox::collide(self, block)) {
		sf::Vector2f offset(0, 0);

		// calculate where to push the player
		if(goDown)  offset.y =  block.getMinY() - self.getMaxY() - 1; // Negative value -- push up and a little more
		if(goUp)    offset.y =  block.getMaxY() - self.getMinY() + 1; // Positive value -- push down
		if(goRight) offset.x =  block.getMinX() - self.getMaxX() - 1; // push left
		if(goLeft)  offset.x =  block.getMaxX() - self.getMinX() + 1; // push right

		// Correct position and update player
		position += offset;
		update(0); // instant correction, no velocity

		// TODO: Going towards a wall causes player's sprite to derp out -- find out why

		// Lock further movement
		if(goDown)  goDown  = lockChangeDirection = false;
		if(goUp)    goUp    = lockChangeDirection = false;
		if(goRight) goRight = lockChangeDirection = false;
		if(goLeft)  goLeft  = lockChangeDirection = false;
	}
}