247 lines
9.3 KiB
C++
247 lines
9.3 KiB
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "gameWorld.h"
|
|
|
|
void GameWorld::setupInitialState() {
|
|
exitPos = sf::Vector2i(1, 0);
|
|
playerPos = sf::Vector2i(gridLength - 1, gridLength - 1);
|
|
setupEnemyPositions();
|
|
setupTiles();
|
|
}
|
|
|
|
void GameWorld::setupEnemyPositions() {
|
|
enemyPositions.clear();
|
|
|
|
enemyPositions.push_back(sf::Vector2i(0, 2));
|
|
enemyPositions.push_back(sf::Vector2i(6, 0));
|
|
enemyPositions.push_back(sf::Vector2i(2, 7));
|
|
}
|
|
|
|
void GameWorld::setupTiles() {
|
|
tiles.clear();
|
|
|
|
std::vector<GameTile *> firstRow;
|
|
firstRow.push_back(new GameTile("wall.png", 0, 0, false, false));
|
|
firstRow.push_back(new GameTile("door.png", 50, 0, true, true));
|
|
firstRow.push_back(new GameTile("wall.png", 100, 0, false, false));
|
|
firstRow.push_back(new GameTile("wall.png", 150, 0, false, false));
|
|
firstRow.push_back(new GameTile("wall.png", 200, 0, false, false));
|
|
firstRow.push_back(new GameTile("wall.png", 250, 0, false, false));
|
|
firstRow.push_back(new GameTile("enemy.png", 300, 0, true, false));
|
|
firstRow.push_back(new GameTile("wall.png", 350, 0, false, false));
|
|
tiles.push_back(firstRow);
|
|
|
|
std::vector<GameTile *> secondRow;
|
|
secondRow.push_back(new GameTile("wall.png",0,50,false,false));
|
|
secondRow.push_back(new GameTile("ground.png",50,50,true,false));
|
|
secondRow.push_back(new GameTile("ground.png",100,50,true,false));
|
|
secondRow.push_back(new GameTile("ground.png",150,50,true,false));
|
|
secondRow.push_back(new GameTile("ground.png",200,50,true,false));
|
|
secondRow.push_back(new GameTile("ground.png",250,50,true,false));
|
|
secondRow.push_back(new GameTile("ground.png",300,50,true,false));
|
|
secondRow.push_back(new GameTile("ground.png",350,50,true,false));
|
|
tiles.push_back(secondRow);
|
|
|
|
std::vector<GameTile *> thirdRow;
|
|
thirdRow.push_back(new GameTile("enemy.png",0,100,true,false));
|
|
thirdRow.push_back(new GameTile("ground.png",50,100,true,false));
|
|
thirdRow.push_back(new GameTile("ground.png",100,100,true,false));
|
|
thirdRow.push_back(new GameTile("wall.png",150,100,false,false));
|
|
thirdRow.push_back(new GameTile("ground.png",200,100,true,false));
|
|
thirdRow.push_back(new GameTile("ground.png",250,100,true,false));
|
|
thirdRow.push_back(new GameTile("wall.png",300,100,false,false));
|
|
thirdRow.push_back(new GameTile("wall.png",350,100,false,false));
|
|
tiles.push_back(thirdRow);
|
|
|
|
std::vector<GameTile *> fourthRow;
|
|
fourthRow.push_back(new GameTile("wall.png",0,150,false,false));
|
|
fourthRow.push_back(new GameTile("ground.png",50,150,true,false));
|
|
fourthRow.push_back(new GameTile("ground.png",100,150,true,false));
|
|
fourthRow.push_back(new GameTile("wall.png",150,150,false,false));
|
|
fourthRow.push_back(new GameTile("ground.png",200,150,true,false));
|
|
fourthRow.push_back(new GameTile("ground.png",250,150,true,false));
|
|
fourthRow.push_back(new GameTile("ground.png",300,150,true,false));
|
|
fourthRow.push_back(new GameTile("wall.png",350,150,false,false));
|
|
tiles.push_back(fourthRow);
|
|
|
|
std::vector<GameTile *> fifthRow;
|
|
fifthRow.push_back(new GameTile("wall.png",0,200,false,false));
|
|
fifthRow.push_back(new GameTile("ground.png",50,200,true,false));
|
|
fifthRow.push_back(new GameTile("ground.png",100,200,true,false));
|
|
fifthRow.push_back(new GameTile("wall.png",150,200,false,false));
|
|
fifthRow.push_back(new GameTile("wall.png",200,200,false,false));
|
|
fifthRow.push_back(new GameTile("ground.png",250,200,true,false));
|
|
fifthRow.push_back(new GameTile("ground.png",300,200,true,false));
|
|
fifthRow.push_back(new GameTile("ground.png",350,200,true,false));
|
|
tiles.push_back(fifthRow);
|
|
|
|
std::vector<GameTile *> sixthRow;
|
|
sixthRow.push_back(new GameTile("ground.png",0,250,true,false));
|
|
sixthRow.push_back(new GameTile("ground.png",50,250,true,false));
|
|
sixthRow.push_back(new GameTile("ground.png",100,250,true,false));
|
|
sixthRow.push_back(new GameTile("ground.png",150,250,true,false));
|
|
sixthRow.push_back(new GameTile("wall.png",200,250,false,false));
|
|
sixthRow.push_back(new GameTile("ground.png",250,250,true,false));
|
|
sixthRow.push_back(new GameTile("ground.png",300,250,true,false));
|
|
sixthRow.push_back(new GameTile("wall.png",350,250,false,false));
|
|
tiles.push_back(sixthRow);
|
|
|
|
std::vector<GameTile *> seventhRow;
|
|
seventhRow.push_back(new GameTile("wall.png",0,300,false,false));
|
|
seventhRow.push_back(new GameTile("wall.png",50,300,false,false));
|
|
seventhRow.push_back(new GameTile("ground.png",100,300,true,false));
|
|
seventhRow.push_back(new GameTile("ground.png",150,300,true,false));
|
|
seventhRow.push_back(new GameTile("ground.png",200,300,true,false));
|
|
seventhRow.push_back(new GameTile("ground.png",250,300,true,false));
|
|
seventhRow.push_back(new GameTile("wall.png",300,300,false,false));
|
|
seventhRow.push_back(new GameTile("wall.png",350,300,false,false));
|
|
tiles.push_back(seventhRow);
|
|
|
|
std::vector<GameTile *> eighthRow;
|
|
eighthRow.push_back(new GameTile("wall.png",0,350,false,false));
|
|
eighthRow.push_back(new GameTile("wall.png",50,350,false,false));
|
|
eighthRow.push_back(new GameTile("enemy.png",100,350,true,false));
|
|
eighthRow.push_back(new GameTile("wall.png",150,350,false,false));
|
|
eighthRow.push_back(new GameTile("wall.png",200,350,false,false));
|
|
eighthRow.push_back(new GameTile("ground.png",250,350,true,false));
|
|
eighthRow.push_back(new GameTile("ground.png",300,350,true,false));
|
|
eighthRow.push_back(new GameTile("player.png",350,350,true,false));
|
|
tiles.push_back(eighthRow);
|
|
}
|
|
|
|
void GameWorld::redrawSprites() {
|
|
tiles[playerPos.y][playerPos.x]->setupSprite("player.png");
|
|
|
|
for (int i = 0; i < enemyPositions.size(); i++) {
|
|
sf::Vector2i currentEnemyPos = enemyPositions[i];
|
|
tiles[currentEnemyPos.y][currentEnemyPos.x]->setupSprite("enemy.png");
|
|
}
|
|
}
|
|
|
|
void GameWorld::moveLeft() {
|
|
if (playerPos.x == 0 || !tiles[playerPos.y][playerPos.x - 1]->isPassable) {
|
|
return;
|
|
}
|
|
|
|
tiles[playerPos.y][playerPos.x]->setupSprite("ground.png");
|
|
playerPos.x--;
|
|
|
|
checkCollisionsAndMoveEnemies();
|
|
}
|
|
|
|
void GameWorld::moveUp() {
|
|
if (playerPos.y == 0 || !tiles[playerPos.y - 1][playerPos.x]->isPassable) {
|
|
return;
|
|
}
|
|
|
|
tiles[playerPos.y][playerPos.x]->setupSprite("ground.png");
|
|
playerPos.y--;
|
|
|
|
checkCollisionsAndMoveEnemies();
|
|
}
|
|
|
|
void GameWorld::moveRight() {
|
|
if (playerPos.x == gridLength - 1 || !tiles[playerPos.y][playerPos.x + 1]->isPassable) {
|
|
return;
|
|
}
|
|
|
|
tiles[playerPos.y][playerPos.x]->setupSprite("ground.png");
|
|
playerPos.x++;
|
|
|
|
checkCollisionsAndMoveEnemies();
|
|
}
|
|
|
|
void GameWorld::moveDown() {
|
|
if (playerPos.y == gridLength - 1 || !tiles[playerPos.y + 1][playerPos.x]->isPassable) {
|
|
return;
|
|
}
|
|
|
|
tiles[playerPos.y][playerPos.x]->setupSprite("ground.png");
|
|
playerPos.y++;
|
|
|
|
checkCollisionsAndMoveEnemies();
|
|
}
|
|
|
|
std::vector<sf::Vector2i> GameWorld::getFreeCoordinates(sf::Vector2i currentPos) {
|
|
std::vector<sf::Vector2i> freePositions;
|
|
std::vector<sf::Vector2i> allPositions;
|
|
|
|
allPositions.push_back(sf::Vector2i(currentPos.x - 1, currentPos.y));
|
|
allPositions.push_back(sf::Vector2i(currentPos.x - 1, currentPos.y - 1));
|
|
allPositions.push_back(sf::Vector2i(currentPos.x, currentPos.y - 1));
|
|
allPositions.push_back(sf::Vector2i(currentPos.x + 1, currentPos.y - 1));
|
|
allPositions.push_back(sf::Vector2i(currentPos.x + 1, currentPos.y));
|
|
allPositions.push_back(sf::Vector2i(currentPos.x + 1, currentPos.y + 1));
|
|
allPositions.push_back(sf::Vector2i(currentPos.x, currentPos.y + 1));
|
|
allPositions.push_back(sf::Vector2i(currentPos.x - 1, currentPos.y + 1));\
|
|
|
|
for (int i = 0; i < allPositions.size(); i++) {
|
|
if (checkIfPositionIsFree(allPositions[i])) {
|
|
freePositions.push_back(allPositions[i]);
|
|
}
|
|
}
|
|
|
|
return freePositions;
|
|
}
|
|
|
|
bool GameWorld::checkIfPositionIsFree(sf::Vector2i pos) {
|
|
if (pos.x < 0 || pos.y < 0 || pos.x > gridLength - 1 || pos.y > gridLength - 1) {
|
|
return false;
|
|
}
|
|
|
|
if (!tiles[pos.y][pos.x]->isPassable || tiles[pos.y][pos.x]->isExit) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void GameWorld::moveEnemies() {
|
|
for (int i = 0; i < enemyPositions.size(); i++) {
|
|
sf::Vector2i currentEnemyPosition = enemyPositions[i];
|
|
std::vector<sf::Vector2i> freePositions = getFreeCoordinates(currentEnemyPosition);
|
|
|
|
int randomIndex = rand() % (freePositions.size() - 1);
|
|
sf::Vector2i newPos = freePositions[randomIndex];
|
|
|
|
tiles[currentEnemyPosition.y][currentEnemyPosition.x]->setupSprite("ground.png");
|
|
enemyPositions[i] = newPos;
|
|
}
|
|
}
|
|
|
|
bool GameWorld::checkIfReachedExit() {
|
|
return playerPos.x == exitPos.x && playerPos.y == exitPos.y;
|
|
}
|
|
|
|
bool GameWorld::checkIfTouchedEnemy() {
|
|
for (int i = 0; i < enemyPositions.size(); i++) {
|
|
sf::Vector2i currentEnemyPos = enemyPositions[i];
|
|
if (playerPos.x == currentEnemyPos.x && playerPos.y == currentEnemyPos.y) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void GameWorld::checkCollisionsAndMoveEnemies() {
|
|
if (checkIfReachedExit()) {
|
|
setupInitialState();
|
|
return;
|
|
}
|
|
|
|
moveEnemies();
|
|
if (checkIfTouchedEnemy()) {
|
|
setupInitialState();
|
|
return;
|
|
}
|
|
|
|
redrawSprites();
|
|
}
|
|
|
|
GameWorld::GameWorld() {
|
|
gridLength = 8;
|
|
setupInitialState();
|
|
}
|