--- /dev/null
+#ifndef CONTEXT_H
+#define CONTEXT_H
+
+#include "States.h"
+
+struct Context
+{
+ std::unique_ptr<State> current_state {nullptr};
+ std::unique_ptr<State> next_state {nullptr};
+ std::unique_ptr<Game_state> saved_game {nullptr};
+};
+
+#endif
\ No newline at end of file
#include "Map.h"
+#include "constants.h"
Map::Map() : texture{}, sprite{}
{
#define MAP_H
#include <SFML/Graphics.hpp>
-#include "constants.h"
struct Context;
void update(Context& context);
void render(sf::RenderWindow& window) const;
- int value {69};
protected:
private:
sf::Texture texture;
+#include <cmath>
+#include <iostream> // TA BORT!
+
#include "Player.h"
+#include "States.h"
+#include "Context.h"
+#include "constants.h"
-#include <iostream>
Player::Player() : collected{0}, max_speed{10}
{
void Player::update(Context& context)
{
- //std::cout << context.current_state->game_map.value << std::endl;
+ // Get game_state from context, static_cast since we know that game is currently running
+ // game_state is still managed by context
+ Game_state* game = static_cast<Game_state*>(context.current_state.get());
+
position += max_speed * direction;
-
+
sprite.setPosition(position);
return;
}
#define _USE_MATH_DEFINES
#include <SFML/Graphics.hpp>
-#include <cmath>
-
#include "Moving_object.h"
-#include "constants.h"
class Player : public Moving_object
{
+#include <memory>
+
#include "States.h"
+#include "Context.h"
+#include "constants.h"
Game_state::Game_state() : game_map{}, player{}, pause_game{false}
{
{
if (pause_game)
{
- context.running_game.reset(static_cast<Game_state*>(context.current_state.release()));
+ context.saved_game.reset(static_cast<Game_state*>(context.current_state.release()));
context.next_state = std::make_unique<Pause_menu>();
pause_game = false;
{
if (resume_game)
{
- context.next_state = std::move(context.running_game);
+ context.next_state = std::move(context.saved_game);
}
else if (exit_game)
{
context.next_state = std::make_unique<Start_menu>();
- context.running_game.release();
+ context.saved_game.release();
}
}
#ifndef STATES_H
#define STATES_H
-#include <memory>
#include "SFML/Graphics.hpp"
-#include "constants.h"
#include "Map.h"
#include "Player.h"
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
void handle_input(sf::Event& event) override;
- Map game_map;
protected:
private:
+ Map game_map;
Player player;
bool pause_game;
};
bool exit_game;
};
-struct Context
-{
- std::unique_ptr<State> current_state {nullptr};
- std::unique_ptr<State> next_state {nullptr};
- std::unique_ptr<Game_state> running_game {nullptr};
-};
-
#endif
\ No newline at end of file
#include "constants.h"
#include "States.h"
-
+#include "Context.h"
int main ()
{