#include "Context.h"
#include "constants.h"
- Game_state::Game_state() : game_map{}, player{}, enemy{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}, bottle_texture {}
+ #include "json.hpp"
+ #include <fstream>
+
+ using json = nlohmann::json;
+
+ Game_state::Game_state() :
+ game_map{},
+ pause_game{false},
+ player{},
+ bottles{},
+ time_since_last_bottle{0.0f},
+ bottle_texture {},
- data{}
++ data{},
++ enemy{}
{
+ std::ifstream f("assets/data.json");
+ data = json::parse(f);
+
+
+ player = std::make_unique<Player>(data["player"]);
+ helper = std::make_unique<Helper>(data["helper"]);
+
bottle_texture.loadFromFile("assets/kir.png");
}
player.add_collected();
}
}*/
- for (unsigned int i = 0; i < bottles.size(); ++i)
+ for (unsigned int i {0}; i < bottles.size(); ++i)
{
- if (player.collides(*(bottles[i])))
+ if (player->collides(*(bottles[i])))
{
// std::cout <<"crash" << std::endl;
bottles.erase(bottles.begin() + i);
- player.add_collected();
+ player->add_collected();
}
}
+ for (unsigned int i {0}; i < bottles.size(); ++i)
+ {
+ if (enemy.collides(*(bottles[i])))
+ {
+ // std::cout <<"crash" << std::endl;
+ bottles.erase(bottles.begin() + i);
+
+ }
+ }
time_since_last_bottle += context.time.asSeconds();
- std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl;
+ //std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl;
- if (time_since_last_bottle >= 2)
+ if (time_since_last_bottle >= data["game_constants"]["bottles_per_second"])
{
- bottles.push_back(std::make_unique<Bottle>(bottle_texture));
- //std::cout << "placed bottle"<< std::endl;
+ bottles.push_back(std::make_unique<Bottle>(bottle_texture, data["bottle"]));
+ std::cout << "placed bottle"<< std::endl;
time_since_last_bottle = 0;
}
game_map.update(context);
- player.update(context);
+ player->update(context);
+ helper->update(context);
+ enemy.update(context);
- helper.update(context);
}
void Game_state::render(sf::RenderWindow &window) const
#include "Map.h"
#include "Player.h"
+#include "Main_enemy.h"
#include "Helper.h"
#include "Bottle.h"
+ #include "json.hpp"
+
+ using json = nlohmann::json;
+
+
struct Context; //finns en strukt som säger att Context finns innan den är deklarerad
class State
protected:
private:
float time_since_last_bottle;
- std::vector<std::unique_ptr<Bottle>> bottles;
+
Map game_map;
- Player player;
- Helper helper;
+ std::unique_ptr<Player> player;
+ std::unique_ptr<Helper> helper;
+ Main_enemy enemy;
bool pause_game;
sf::Texture bottle_texture;
+ json data;
};