commit 12c719225c3de8103b2afea05d37bd0558f94b09 Author: Kishan Takoordyal Date: Sun Nov 15 10:56:33 2020 +0400 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..879cc29 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode/ +main.o +sfml-app \ No newline at end of file diff --git a/gameTile.cpp b/gameTile.cpp new file mode 100644 index 0000000..ba035eb --- /dev/null +++ b/gameTile.cpp @@ -0,0 +1,27 @@ +#include +#include "gameTile.h" + +bool GameTile::setupSprite(std::string textureName) { + if (!texture.loadFromFile("images/" + textureName)) { + std::cout << "Can't load texture " << textureName << std::endl; + return false; + } + + texture.setSmooth(true); + sprite.setTexture(texture); + sprite.setTextureRect(sf::IntRect(0, 0, 50, 50)); + + return true; +} + +GameTile::GameTile(std::string textureName, float x, float y, bool passable, bool exit) { + if (!setupSprite(textureName)) { + return; + } + + pos = sf::Vector2f(x, y); + sprite.setPosition(pos); + + isPassable = passable; + isExit = exit; +} diff --git a/gameTile.h b/gameTile.h new file mode 100644 index 0000000..ba01dfa --- /dev/null +++ b/gameTile.h @@ -0,0 +1,20 @@ +#include + +#ifndef GAMETILE_H +#define GAMETILE_H + +class GameTile { + public: + bool isPassable; + bool isExit; + + sf::Vector2f pos; + sf::Texture texture; + sf::Sprite sprite; + + GameTile(std::string, float, float, bool, bool); + + bool setupSprite(std::string); +}; + +#endif diff --git a/gameWorld.cpp b/gameWorld.cpp new file mode 100644 index 0000000..6abaa4c --- /dev/null +++ b/gameWorld.cpp @@ -0,0 +1,246 @@ +#include +#include + +#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 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 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 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 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 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 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 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 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 GameWorld::getFreeCoordinates(sf::Vector2i currentPos) { + std::vector freePositions; + std::vector 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 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(); +} diff --git a/gameWorld.h b/gameWorld.h new file mode 100644 index 0000000..ae1de59 --- /dev/null +++ b/gameWorld.h @@ -0,0 +1,41 @@ +#include +#include + +#include "gameTile.h" + +#ifndef GAMEWORLD_H +#define GAMEWORLD_H + +class GameWorld { + sf::Vector2i exitPos; + sf::Vector2i playerPos; + std::vector enemyPositions; + + void setupInitialState(); + void setupEnemyPositions(); + void setupTiles(); + + void redrawSprites(); + + std::vector getFreeCoordinates(sf::Vector2i currentPos); + bool checkIfPositionIsFree(sf::Vector2i); + + void moveEnemies(); + + bool checkIfReachedExit(); + bool checkIfTouchedEnemy(); + void checkCollisionsAndMoveEnemies(); + + public: + std::vector< std::vector > tiles; + int gridLength; + + GameWorld(); + + void moveLeft(); + void moveUp(); + void moveRight(); + void moveDown(); +}; + +#endif diff --git a/images/.DS_Store b/images/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/images/.DS_Store differ diff --git a/images/door.png b/images/door.png new file mode 100644 index 0000000..9b6c56e Binary files /dev/null and b/images/door.png differ diff --git a/images/enemy.png b/images/enemy.png new file mode 100644 index 0000000..c77247b Binary files /dev/null and b/images/enemy.png differ diff --git a/images/ground.png b/images/ground.png new file mode 100644 index 0000000..3aac705 Binary files /dev/null and b/images/ground.png differ diff --git a/images/player.png b/images/player.png new file mode 100644 index 0000000..16c6c04 Binary files /dev/null and b/images/player.png differ diff --git a/images/wall.png b/images/wall.png new file mode 100644 index 0000000..8035dd1 Binary files /dev/null and b/images/wall.png differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..07592d0 --- /dev/null +++ b/main.cpp @@ -0,0 +1,42 @@ +#include +#include "gameTile.cpp" +#include "gameWorld.cpp" + +int main() { + float windowWidth = 400; + float windowHeight = 400; + + sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Roguelike game"); + + GameWorld gameWorld = GameWorld(); + + while (window.isOpen()) { + sf::Event event; + + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) { + window.close(); + } else if (event.type == sf::Event::KeyPressed) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + gameWorld.moveLeft(); + } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { + gameWorld.moveUp(); + } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + gameWorld.moveRight(); + } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { + gameWorld.moveDown(); + } + } + } + + window.clear(); + + for (int i = 0; i < gameWorld.gridLength; i++) { + for (int j = 0; j < gameWorld.gridLength; j++) { + window.draw(gameWorld.tiles[i][j]->sprite); + } + } + + window.display(); + } +} diff --git a/runningSFML.sh b/runningSFML.sh new file mode 100755 index 0000000..1dc8666 --- /dev/null +++ b/runningSFML.sh @@ -0,0 +1,7 @@ +#! /bin/bash + +SFML=/usr/local/SFML + +g++ -c main.cpp -I $SFML/include +g++ main.o -o sfml-app -L $SFML/lib -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system +export LD_LIBRARY_PATH=$SFML/lib && ./sfml-app