|
|
@ -35,63 +35,85 @@ void Map::CheckCollision(Entity *entity) { |
|
|
|
|
|
|
|
|
|
|
|
// Check collision
|
|
|
|
// Check collision
|
|
|
|
int index; |
|
|
|
int index; |
|
|
|
|
|
|
|
sf::Sprite tile; |
|
|
|
bool collided = false; |
|
|
|
bool collided = false; |
|
|
|
|
|
|
|
sf::Vector2i collidedTile; |
|
|
|
for (int y = checkHeight.x; y < checkHeight.y; y++) { |
|
|
|
for (int y = checkHeight.x; y < checkHeight.y; y++) { |
|
|
|
for (int x = checkWidth.x; x < checkWidth.y; x++) { |
|
|
|
for (int x = checkWidth.x; x < checkWidth.y; x++) { |
|
|
|
index = this->collision->data[y][x]; |
|
|
|
index = this->collision->data[y][x]; |
|
|
|
// If collision tile
|
|
|
|
// If collision tile
|
|
|
|
if(index != 0) { |
|
|
|
if(index != 0) { |
|
|
|
if (entity->getGlobalBounds().intersects(sf::FloatRect(x * this->tileSet->tileWidth, |
|
|
|
tile.setTexture(*this->tileSet->tile[index]); |
|
|
|
y * this->tileSet->tileHeight, |
|
|
|
tile.setPosition(sf::Vector2f(x * this->tileSet->tileWidth, y * this->tileSet->tileHeight)); |
|
|
|
this->tileSet->tileWidth, |
|
|
|
// Rotate oblique collisions
|
|
|
|
this->tileSet->tileHeight))) { |
|
|
|
// if(i == 1) {
|
|
|
|
|
|
|
|
// switch(x) {
|
|
|
|
|
|
|
|
// case 3:
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// case 4:
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// case 5:
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (tile.getGlobalBounds().intersects(entity->getGlobalBounds())) { |
|
|
|
collided = true; |
|
|
|
collided = true; |
|
|
|
|
|
|
|
collidedTile.x = x; |
|
|
|
|
|
|
|
collidedTile.y = y; |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Move back if entity collided)
|
|
|
|
// Move back if entity collided
|
|
|
|
if(collided) { |
|
|
|
if(collided) { |
|
|
|
|
|
|
|
// Get Entities center x, y position;
|
|
|
|
|
|
|
|
sf::Vector2f entityCenter = sf::Vector2f( |
|
|
|
|
|
|
|
(int)((entity->getPosition().x + entity->getGlobalBounds().width / 2) / this->tileSet->tileWidth), |
|
|
|
|
|
|
|
(int)((entity->getPosition().y + entity->getGlobalBounds().height / 2) / this->tileSet->tileHeight) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// DOWN_RIGHT
|
|
|
|
// DOWN_RIGHT
|
|
|
|
if(entity->velocity.x > 0 |
|
|
|
if(collidedTile.x > entityCenter.x + 1 |
|
|
|
&& entity->velocity.y > 0) { |
|
|
|
&& collidedTile.y > entityCenter.y + 1) { |
|
|
|
entity->velocity.x -= entity->velocity.x * 2; |
|
|
|
|
|
|
|
entity->velocity.y = 0; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// UP_RIGHT
|
|
|
|
// UP_RIGHT
|
|
|
|
else if(entity->velocity.x > 0 |
|
|
|
else if(collidedTile.x > entityCenter.x + 1 |
|
|
|
&& entity->velocity.y < 0) { |
|
|
|
&& collidedTile.y < entityCenter.y - 1) { |
|
|
|
entity->velocity.x = 0; |
|
|
|
|
|
|
|
entity->velocity.y -= entity->velocity.y * 2; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// UP_LEFT
|
|
|
|
// UP_LEFT
|
|
|
|
else if(entity->velocity.x < 0 |
|
|
|
else if(collidedTile.x < entityCenter.x - 1 |
|
|
|
&& entity->velocity.y < 0) { |
|
|
|
&& collidedTile.y < entityCenter.y - 1) { |
|
|
|
entity->velocity.x = 0; |
|
|
|
|
|
|
|
entity->velocity.y -= entity->velocity.y * 2; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// DOWN_LEFT
|
|
|
|
// DOWN_LEFT
|
|
|
|
else if(entity->velocity.x < 0 |
|
|
|
else if(collidedTile.x < entityCenter.x - 1 |
|
|
|
&& entity->velocity.y > 0) { |
|
|
|
&& collidedTile.y > entityCenter.y + 1) { |
|
|
|
entity->velocity.x -= entity->velocity.x * 2; |
|
|
|
|
|
|
|
entity->velocity.y = 0; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// RIGHT
|
|
|
|
// RIGHT
|
|
|
|
else if(entity->velocity.x > 0) { |
|
|
|
if(collidedTile.x > entityCenter.x |
|
|
|
|
|
|
|
&& collidedTile.y == entityCenter.y) { |
|
|
|
entity->velocity.x -= entity->velocity.x * 2; |
|
|
|
entity->velocity.x -= entity->velocity.x * 2; |
|
|
|
} |
|
|
|
} |
|
|
|
// LEFT
|
|
|
|
// LEFT
|
|
|
|
else if(entity->velocity.x < 0) { |
|
|
|
if(collidedTile.x < entityCenter.x |
|
|
|
entity->velocity.x -= entity->velocity.x * 2; |
|
|
|
&& collidedTile.y == entityCenter.y) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// DOWN
|
|
|
|
// DOWN
|
|
|
|
else if(entity->velocity.y > 0) { |
|
|
|
if(collidedTile.x == entityCenter.x |
|
|
|
entity->velocity.y -= entity->velocity.y * 2; |
|
|
|
&& collidedTile.y > entityCenter.y) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// UP
|
|
|
|
// UP
|
|
|
|
else if(entity->velocity.y < 0) { |
|
|
|
if(collidedTile.x == entityCenter.x |
|
|
|
|
|
|
|
&& collidedTile.y < entityCenter.y) { |
|
|
|
entity->velocity.y -= entity->velocity.y * 2; |
|
|
|
entity->velocity.y -= entity->velocity.y * 2; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -119,6 +141,9 @@ void Map::RenderAbove(sf::RenderWindow *window) { |
|
|
|
|
|
|
|
|
|
|
|
layer = this->above3; |
|
|
|
layer = this->above3; |
|
|
|
this->Render(window, layer); |
|
|
|
this->Render(window, layer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// layer = this->collision;
|
|
|
|
|
|
|
|
// this->Render(window, layer);
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Map::Render(sf::RenderWindow *window, Layer *layer) { |
|
|
|
void Map::Render(sf::RenderWindow *window, Layer *layer) { |
|
|
|