#include <SFML/Graphics.hpp>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <Thor\Resources.hpp>
#define resource thor::Resources::fromFile<sf::Texture>
std::multimap<std::string, sf::Vector2<float> > _levelData;
std::vector<sf::String> _levelLayout;
thor::ResourceHolder<sf::Texture, std::string> holder;
std::vector<std::unique_ptr<sf::Sprite> > sprites;
std::vector<std::pair<std::string, sf::Vector2f> > enemyPos;
std::vector<int> enemyIndexes;
sf::Vector2f playerPos;
int playerIndex;
void loadLevel(const std::string levelName) {
std::ifstream input;
input.open(levelName);
if (input.fail()) {
throw std::runtime_error("Failed to load level: " + levelName + "\n");
}
std::string temp = "";
while (std::getline(input, temp)) {
_levelLayout.push_back(std::move(temp));
}
}
void populate() {
sf::Sprite temp;
for (const auto& i : _levelData) {
if (i.first == "Wall") {
temp.setTexture(holder["tree"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
}
else if (i.first == "Floor") {
temp.setTexture(holder["grass"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
}
else if (i.first == "Player") {
temp.setTexture(holder["grass"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
temp.setTexture(holder["warrior"]);
playerPos = i.second;
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
playerIndex = sprites.size() - 1;
}
else if (i.first == "Warlock") {
temp.setTexture(holder["grass"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
temp.setTexture(holder["warlock"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
enemyPos.push_back(std::make_pair("warlock", i.second));
enemyIndexes.push_back(sprites.size() - 1);
}
else if (i.first == "Monster") {
temp.setTexture(holder["grass"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
temp.setTexture(holder["monster"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
enemyPos.push_back(std::make_pair("monster", i.second));
enemyIndexes.push_back(sprites.size() - 1);
}
else if (i.first == "Brute") {
temp.setTexture(holder["grass"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
temp.setTexture(holder["warlock"]);
temp.setPosition(i.second);
sprites.push_back(std::make_unique<sf::Sprite>(temp));
enemyPos.push_back(std::make_pair("brute", i.second));
enemyIndexes.push_back(sprites.size() - 1);
}
}
}
sf::Vector2f getCoords(int x, int y) {
return sf::Vector2f(x * 32, y * 32);
}
std::multimap<std::string, sf::Vector2<float>> textToSprite() {
std::multimap<std::string, sf::Vector2<float>> temp;
for (unsigned int i = 0; i < _levelLayout.size(); ++i) {
for (unsigned j = 0; j < _levelLayout[i].getSize(); ++j) {
switch (_levelLayout[i][j]) {
case '@':
temp.insert(std::pair<std::string, sf::Vector2f>("Player", getCoords(j, i)));
break;
case '.':
temp.insert(std::pair<std::string, sf::Vector2f>("Floor", getCoords(j, i)));
break;
case '#':
temp.insert(std::pair<std::string, sf::Vector2f>("Wall", getCoords(j, i)));
break;
case 'M':
temp.insert(std::pair<std::string, sf::Vector2f>("Monster", getCoords(j, i)));
break;
case 'W':
temp.insert(std::pair<std::string, sf::Vector2f>("Warlock", getCoords(j, i)));
break;
case 'B':
temp.insert(std::pair<std::string, sf::Vector2f>("Brute", getCoords(j, i)));
break;
}
}
}
return temp;
}
int main() {
sf::RenderWindow window(sf::VideoMode(1280, 760, 32), "Test");
holder.acquire("grass", resource("Graphics/grass.png"));
holder.acquire("tree", resource("Graphics/tree.png"));
holder.acquire("warrior", resource("Graphics/warrior.png"));
holder.acquire("warlock", resource("Graphics/warlock.png"));
holder.acquire("monster", resource("Graphics/monster.png"));
window.setFramerateLimit(30);
loadLevel("Level1.txt");
_levelData = textToSprite();
populate();
while (window.isOpen()) {
sf::Event e;
while (window.pollEvent(e)) {
switch (e.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if (e.key.code == sf::Keyboard::Escape) {
window.close();
break;
}
if (e.key.code == sf::Keyboard::W) {
(*sprites[playerIndex]).move(0, -32);
(*sprites[enemyIndexes[1]]).move(0, -32);
}
if (e.key.code == sf::Keyboard::S) {
(*sprites[playerIndex]).move(0, 32);
(*sprites[enemyIndexes[1]]).move(0, 32);
}
if (e.key.code == sf::Keyboard::A) {
(*sprites[playerIndex]).move(-32, 0);
(*sprites[enemyIndexes[1]]).move(-32, 0);
}
if (e.key.code == sf::Keyboard::D) {
(*sprites[playerIndex]).move(32, 0);
(*sprites[enemyIndexes[1]]).move(32, 0);
}
break;
}
}
window.clear();
for (unsigned int i = 0; i < sprites.size(); ++i)
window.draw((*sprites[i]));
window.display();
}
return 0;
}