init
This commit is contained in:
commit
12c719225c
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.vscode/
|
||||||
|
main.o
|
||||||
|
sfml-app
|
27
gameTile.cpp
Normal file
27
gameTile.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
20
gameTile.h
Normal file
20
gameTile.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
#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
|
246
gameWorld.cpp
Normal file
246
gameWorld.cpp
Normal file
@ -0,0 +1,246 @@
|
|||||||
|
#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();
|
||||||
|
}
|
41
gameWorld.h
Normal file
41
gameWorld.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "gameTile.h"
|
||||||
|
|
||||||
|
#ifndef GAMEWORLD_H
|
||||||
|
#define GAMEWORLD_H
|
||||||
|
|
||||||
|
class GameWorld {
|
||||||
|
sf::Vector2i exitPos;
|
||||||
|
sf::Vector2i playerPos;
|
||||||
|
std::vector<sf::Vector2i> enemyPositions;
|
||||||
|
|
||||||
|
void setupInitialState();
|
||||||
|
void setupEnemyPositions();
|
||||||
|
void setupTiles();
|
||||||
|
|
||||||
|
void redrawSprites();
|
||||||
|
|
||||||
|
std::vector<sf::Vector2i> getFreeCoordinates(sf::Vector2i currentPos);
|
||||||
|
bool checkIfPositionIsFree(sf::Vector2i);
|
||||||
|
|
||||||
|
void moveEnemies();
|
||||||
|
|
||||||
|
bool checkIfReachedExit();
|
||||||
|
bool checkIfTouchedEnemy();
|
||||||
|
void checkCollisionsAndMoveEnemies();
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::vector< std::vector<GameTile *> > tiles;
|
||||||
|
int gridLength;
|
||||||
|
|
||||||
|
GameWorld();
|
||||||
|
|
||||||
|
void moveLeft();
|
||||||
|
void moveUp();
|
||||||
|
void moveRight();
|
||||||
|
void moveDown();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
BIN
images/.DS_Store
vendored
Normal file
BIN
images/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
images/door.png
Normal file
BIN
images/door.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.0 KiB |
BIN
images/enemy.png
Normal file
BIN
images/enemy.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.3 KiB |
BIN
images/ground.png
Normal file
BIN
images/ground.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.9 KiB |
BIN
images/player.png
Normal file
BIN
images/player.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.5 KiB |
BIN
images/wall.png
Normal file
BIN
images/wall.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.9 KiB |
42
main.cpp
Normal file
42
main.cpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#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();
|
||||||
|
}
|
||||||
|
}
|
7
runningSFML.sh
Executable file
7
runningSFML.sh
Executable file
@ -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
|
Loading…
x
Reference in New Issue
Block a user