Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level file #163

Merged
merged 11 commits into from
May 23, 2021
54 changes: 5 additions & 49 deletions bin/parryt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,8 @@
#include "user_view.hpp"
#include "game_state.hpp"

// remove these later, just here for the demo function:
#include "exit.hpp"
#include "platform.hpp"
#include "wall.hpp"
#include "cannon.hpp"
#include "cannonball.hpp"
#include "mast.hpp"
#include "grunt.hpp"
#include "cannon_view.hpp"
#include "patrol_ai.hpp"


/**
* Temporary demo method
*/
std::shared_ptr<GameState> makeDemo(void) {
std::shared_ptr<GameState> demo = std::make_shared<GameState>();

// add ground
std::shared_ptr<Platform> ground = std::make_shared<Platform>(b2Vec2(30, -10), 1000);
demo->addActor(ground);

// add cannon
std::shared_ptr<Cannon> cannon = std::make_shared<Cannon>(b2Vec2(15.2,-5), demo->getModel());
demo->addActor(cannon);
demo->addView(std::make_shared<CannonView>(demo->getModel(), cannon));

// Add grunt
std::shared_ptr<Grunt> grunt = std::make_shared<Grunt>(b2Vec2(100, -5), demo->getModel());
demo->addActor(grunt);
demo->addView(std::make_shared<PatrolAI>(demo->getModel(), grunt));

// Add mast platforms
for (int i = 0; i < 32; i += 2) {
demo->addActor(std::make_shared<Platform>(b2Vec2(24.8, -4.8+3.2*i), 3.2));
}

// Add Wall
// std::shared_ptr<Wall> wall1 = std::make_shared<Wall>(b2Vec2(12, 3.8), 3.7);
// demo->addActor(wall1);

return demo;
}


void gameUpdate(std::shared_ptr<GameController> game, std::shared_ptr<UserView> user) {
void gameRunner(std::shared_ptr<GameController> game, std::shared_ptr<UserView> user) {
std::chrono::steady_clock::time_point nextUpdate = std::chrono::steady_clock::now();
while (user->isRunning()) {
nextUpdate += std::chrono::milliseconds(16);
Expand All @@ -64,20 +20,20 @@ void gameUpdate(std::shared_ptr<GameController> game, std::shared_ptr<UserView>
}


void drawUpdate(std::shared_ptr<GameController> game, std::shared_ptr<UserView> user) {
void drawRunner(std::shared_ptr<GameController> game, std::shared_ptr<UserView> user) {
while (user->isRunning()) {
user->drawScreen();
}
}


int main(int argc, char** argv) {
std::shared_ptr<GameController> game = std::make_shared<GameController>(makeDemo());
std::shared_ptr<GameController> game = std::make_shared<GameController>();
std::shared_ptr<UserView> user = std::make_shared<UserView>(game);
game->getGameState()->addView(user);

std::thread gameThread(gameUpdate, game, user);
//std::thread drawThread(drawUpdate, game, user);
std::thread gameThread(gameRunner, game, user);
//std::thread drawThread(drawRunner, game, user);
gameThread.join();
//drawThread.join();
return 0;
Expand Down
27 changes: 27 additions & 0 deletions include/game_state_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef GAME_STATE_FACTORY_HPP
#define GAME_STATE_FACTORY_HPP


#include <memory>

class GameState;


class GameStateFactory {
public:
GameStateFactory(void) {};

/**
* Build a game state.
*
* @param filename Name of .ini file to read in.
* @return the created game state.
*/
std::shared_ptr<GameState> build(std::string filename);

private:
std::ifstream readSection(std::ifstream file);
};


#endif
5 changes: 4 additions & 1 deletion include/user_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
#define USER_VIEW_HPP


#include <chrono>
#include <list>
#include <memory>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <box2d/box2d.h>

#include "view.hpp"
#include "game_state_factory.hpp"


class GameController;
Expand All @@ -19,7 +21,7 @@ class Pari;
/**
* Draw the screen for the player
*/
class UserView : public View, public std::enable_shared_from_this<UserView> {
class UserView : public View {
public:
UserView(std::shared_ptr<GameController> game);

Expand All @@ -40,6 +42,7 @@ class UserView : public View, public std::enable_shared_from_this<UserView> {
std::chrono::steady_clock::time_point lastUpdate;
std::shared_ptr<GameController> game;
std::shared_ptr<sf::RenderWindow> window;
GameStateFactory gameStateFactory;
std::shared_ptr<Pari> character;
sf::Music musicTrack;
//std::shared_ptr<sf::Music> Track;
Expand Down
6 changes: 4 additions & 2 deletions include/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
class Model;


class View {
class View : public std::enable_shared_from_this<View> {
public:
View(std::shared_ptr<Model> model);
View(void) {};

View(std::shared_ptr<Model> model) { this->model = model; };

virtual void update(void) = 0;

Expand Down
2 changes: 1 addition & 1 deletion levels/demo.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ width=1000
pos=15.2,-5

[cannon/cannonai]
pos=15.2,-5
pos=20.2,-9

[grunt/patrolai]
pos=30,-5
Expand Down
72 changes: 72 additions & 0 deletions src/game_state_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <memory>
#include <vector>
#include <unordered_map>
#include <fstream>

#include "game_state_factory.hpp"
#include "game_state.hpp"

#include "exit.hpp"
#include "platform.hpp"
#include "wall.hpp"
#include "cannon.hpp"
#include "mast.hpp"
#include "grunt.hpp"
#include "cannon_view.hpp"
#include "patrol_ai.hpp"


std::unordered_map<std::string, std::vector<std::string>> tokenize(std::ifstream &file) {
std::unordered_map<std::string, std::vector<std::string>> tokens;

std::string line;
while (getline(file, line) && !line.empty()) {
int start = 0;
int end = line.find("=");
std::string key = line.substr(start, end - start);

std::vector<std::string> values;
while (end != -1) {
start = end + 1;
end = line.find(",", start);
values.push_back(line.substr(start, end - start));
}

tokens[key] = values;
}
return tokens;
}


std::shared_ptr<GameState> GameStateFactory::build(std::string filename) {
std::shared_ptr<GameState> demo = std::make_shared<GameState>();
std::ifstream file;
file.open(filename, std::ios::in);

std::string line;
while (getline(file, line)) {
if (line.at(0) == '[') {
std::string header = line.substr(1, line.find(']') - 1);
auto tokens = tokenize(file);
if (header == "platform") {
std::vector<std::string> pos = tokens["pos"];
demo->addActor(std::make_shared<Platform>(b2Vec2(stoi(pos[0]), stoi(pos[1])), stoi(tokens["width"][0])));
}
else if (header == "cannon/cannonai") {
std::vector<std::string> pos = tokens["pos"];
auto actor = std::make_shared<Cannon>(b2Vec2(stoi(pos[0]), stoi(pos[1])), demo->getModel());
demo->addActor(actor);
demo->addView(std::make_shared<CannonView>(demo->getModel(), actor));
}
else if (header == "grunt/patrolai") {
std::vector<std::string> pos = tokens["pos"];
auto actor = std::make_shared<Grunt>(b2Vec2(stoi(pos[0]), stoi(pos[1])), demo->getModel());
demo->addActor(actor);
demo->addView(std::make_shared<PatrolAI>(demo->getModel(), actor));
}
}
}

file.close();
return demo;
}
8 changes: 5 additions & 3 deletions src/user_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#include "user_view.hpp"
#include "game_controller.hpp"
#include "game_state.hpp"
#include "game_state_factory.hpp"
#include "model.hpp"
#include "pari.hpp"


UserView::UserView(std::shared_ptr<GameController> game) : View(game->getGameState()->getModel()) {
UserView::UserView(std::shared_ptr<GameController> game) {
this->game = game;
this->game->setGameState(this->gameStateFactory.build("../levels/demo.ini"));

// create Pari
this->character = std::make_shared<Pari>(b2Vec2(-13,-5));
// add Pari to game
this->character = std::make_shared<Pari>(b2Vec2(-13,-5)); // TODO: read Pari position from level file
this->game->getGameState()->addActor(this->character);

// set window
Expand Down
7 changes: 0 additions & 7 deletions src/view.cpp

This file was deleted.