+#include "Game_state.h"
+
+Game_state::Game_state() : game_map{}, player{}, pause_game{false}
+{
+}
+
+void Game_state::update(Context& context)
+{
+ if (pause_game)
+ {
+ context.saved_game = std::move(context.current_state);
+ context.next_state = new Pause_menu{};
+ pause_game = false;
+ return;
+ }
+ game_map.update(context);
+ player.update(context);
+}
+
+void Game_state::render(sf::RenderWindow& window) const
+{
+ game_map.render(window);
+ player.render(window);
+}
+
+void Game_state::handle_input(sf::Event& event)
+{
+ switch (event.type)
+ {
+ case sf::Event::KeyPressed:
+ switch(event.key.code)
+ {
+ case sf::Keyboard::Escape:
+ pause_game = true;
+ return;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+
+ player.handle_input(event);
+}
\ No newline at end of file
#define GAME_STATE_H
#include <SFML/Graphics.hpp>
+#include <memory>
#include "State.h"
+#include "Map.h"
+#include "Player.h"
+#include "Menus.h"
class Game_state : public State
{
- public:
- void update() override;
- void render(sf::RenderWindow& window) override;
- void handle_input();
-
- protected:
-
- private:
+public:
+ Game_state();
+ ~Game_state() = default;
+ void update(Context& context) override;
+ void render(sf::RenderWindow& window) const override;
+ void handle_input(sf::Event& event) override;
+ Map game_map;
+protected:
+private:
+
+ Player player;
+ bool pause_game;
};
#endif
\ No newline at end of file
sprite.setPosition(0,0);
}
-void Map::update()
+void Map::update(Context& context)
{}
void Map::render(sf::RenderWindow& window) const
#include <SFML/Graphics.hpp>
#include "constants.h"
+#include "State.h"
class Map
{
Map();
~Map() = default;
- void update();
+ void update(Context& context);
void render(sf::RenderWindow& window) const;
+ int value {69};
protected:
private:
sf::Texture texture;
+#include "Menus.h"
+
+#include <iostream> //TA BORT!
+
+Start_menu::Start_menu() : texture{}, sprite{}, exit_menu{false}
+{
+ texture.loadFromFile("assets/sack.png");
+ sprite.setTexture(texture);
+
+ sf::FloatRect gb {sprite.getGlobalBounds()};
+ sprite.setOrigin(gb.width / 2, gb.height / 2);
+ sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2);
+}
+
+void Start_menu::update(Context& context)
+{
+ if (exit_menu)
+ {
+ context.next_state = new Game_state{};
+ }
+}
+
+void Start_menu::render(sf::RenderWindow& window) const
+{
+ window.draw(sprite);
+}
+
+void Start_menu::handle_input(sf::Event& event)
+{
+ switch (event.type)
+ {
+ case sf::Event::KeyPressed:
+ switch(event.key.code)
+ {
+ case sf::Keyboard::Enter:
+ exit_menu = true;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+Pause_menu::Pause_menu() : texture{}, sprite{}, exit_game{false}, resume_game{false}
+{
+ texture.loadFromFile("assets/kir.png");
+ sprite.setTexture(texture);
+
+ sf::FloatRect gb {sprite.getGlobalBounds()};
+ sprite.setOrigin(gb.width / 2, gb.height / 2);
+ sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2);
+}
+
+void Pause_menu::update(Context& context)
+{
+ if (resume_game)
+ {
+ context.next_state = std::move(context.saved_game);
+ }
+ else if (exit_game)
+ {
+ context.next_state = new Start_menu{};
+ }
+}
+
+void Pause_menu::render(sf::RenderWindow& window) const
+{
+ window.draw(sprite);
+}
+
+void Pause_menu::handle_input(sf::Event& event)
+{
+ switch (event.type)
+ {
+ case sf::Event::KeyPressed:
+ switch(event.key.code)
+ {
+ case sf::Keyboard::Enter:
+ resume_game = true;
+ break;
+ case sf::Keyboard::Escape:
+ exit_game = true;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+}
#ifndef MENUS_H
#define MENUS_H
+
+#include <memory>
+
+#include "SFML/Graphics.hpp"
#include "State.h"
-class Start_menu
+#include "constants.h"
+#include "Game_state.h"
+
+class Start_menu : public State
{
public:
- void update(Context) override;
- void rednder(Window) override;
- void handle_input() override;
+ Start_menu();
+ ~Start_menu() = default;
+ void update(Context& context) override;
+ void render(sf::RenderWindow& window) const override;
+ void handle_input(sf::Event& event) override;
+
+private:
+ sf::Texture texture;
+ sf::Sprite sprite;
+ bool exit_menu;
};
-class Paus_menu
+
+
+class Pause_menu : public State
{
public:
- void update(Context) override;
- void rednder(Window) override;
- void handle_input() override;
+ Pause_menu();
+ ~Pause_menu() = default;
+ void update(Context& context) override;
+ void render(sf::RenderWindow& window) const override;
+ void handle_input(sf::Event& event) override;
+private:
+ sf::Texture texture;
+ sf::Sprite sprite;
+ bool resume_game;
+ bool exit_game;
};
#endif
\ No newline at end of file
#define OBJECT_H
#include <SFML/Graphics.hpp>
+#include "State.h"
class Object
{
virtual bool collides(Object& other) = 0;
virtual void collision(Object& other) = 0;
- virtual void update() = 0;
+ virtual void update(Context& context) = 0;
virtual void render(sf::RenderWindow& window) const = 0;
protected:
#include "Player.h"
+#include <iostream>
+
Player::Player() : collected{0}, max_speed{10}
{
texture.loadFromFile("assets/4V_figur.png");
return;
}
-void Player::update()
+void Player::update(Context& context)
{
+ //std::cout << context.current_state->game_map.value << std::endl;
position += max_speed * direction;
sprite.setPosition(position);
#include "Moving_object.h"
#include "constants.h"
+#include "State.h"
class Player : public Moving_object
{
~Player() = default;
bool collides(Object& other) override;
void collision(Object& other) override;
- void update() override;
+ void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
void handle_input(sf::Event& event);
protected:
#ifndef STATE_H
#define STATE_H
+#include "SFML/Graphics.hpp"
+#include <memory>
+
+struct Context;
+
class State
{
public:
- virtual void update(Context) = 0;
- virtual void render(Window) = 0;
- virtual void hande_input() = 0;
+ virtual void update(Context& context) = 0;
+ virtual void render(sf::RenderWindow& window) const = 0;
+ virtual void handle_input(sf::Event& event) = 0;
virtual ~State() = default;
};
+
+struct Context
+{
+ State* current_state {nullptr};
+ State* next_state {nullptr};
+ State* saved_game {nullptr};
+};
+
#endif
\ No newline at end of file
*/
#include <SFML/Graphics.hpp>
+#include <iostream>
+#include <memory>
#include "constants.h"
-#include "Player.h"
-#include "Map.h"
+#include "Game_state.h"
+#include "Menus.h"
+#include "State.h"
+
int main ()
{
"DespYrat"
};
- Map game_map{};
- Player player{};
+ Context game_context{};
+ game_context.current_state = new Start_menu{};
sf::Clock game_clock;
while (window.isOpen())
break;
default:
break;
- }
+ }
- player.handle_input(event);
+ game_context.current_state->handle_input(event);
}
-
// Update
- game_map.update();
- player.update();
+ game_context.current_state->update(game_context);
+
+ if (game_context.next_state != nullptr)
+ {
+ game_context.current_state = std::move(game_context.next_state);
+ game_context.next_state = nullptr;
+ }
//Draw
window.clear();
-
- game_map.render(window);
- player.render(window);
-
-
+ game_context.current_state->render(window);
window.display();
// Make game run at constant fps
+ // std::cout << "frametime: " << game_clock.getElapsedTime().asSeconds() << std::endl;
sf::sleep(sf::milliseconds(1000.0 / FPS) - game_clock.getElapsedTime());
game_clock.restart();