From: Arvid Sjöblom Date: Mon, 13 Nov 2023 10:06:28 +0000 (+0100) Subject: börjat på placering av flaskor arvid, stina X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=d8ba79088f894abedc7814031ad166ed555e41f8;p=TDDC76_proj.git börjat på placering av flaskor arvid, stina --- diff --git a/src/Bottle.cc b/src/Bottle.cc index e69de29..709ce16 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -0,0 +1,32 @@ +#include "Bottle.h" +#include "constants.h" +#include "Context.h" +#include + +Bottle::Bottle() +{ + texture.loadFromFile("assets/kir.png"); + sprite.setTexture(texture); + position ={rand()%(S_WIDTH*5/7 - S_WIDTH/5 +1) + S_WIDTH/5, rand()%S_HEIGHT+1}; //x-pixel WIDTH/5 - WIDTH*5/7 + sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setOrigin(gb.width / 2, gb.height / 2); + sprite.setPosition(position); +} +bool Bottle::collides(Object& other) +{ + +} +void Bottle::collision(Object& other) +{ + +} +void Bottle::update(Context& context) +{ + +} + +void Bottle::render(sf::RenderWindow& window) const +{ + window.draw(sprite); +} \ No newline at end of file diff --git a/src/Bottle.h b/src/Bottle.h index ea25e43..1dfabfa 100644 --- a/src/Bottle.h +++ b/src/Bottle.h @@ -1,22 +1,20 @@ -#include "Static_object.h" -#include - - #ifndef BOTTLE_H #define BOTTLE_H +#include "Static_object.h" +#include -class Bottle : Static_object +class Bottle : public Static_object { - public: - Object(); - ~Object(); - - bool collides(Object& other) override; - void collision(Object& other) override; - void update() override; - void render(sf::RenderWindow& window) override; - private: +public: + Bottle(); + + + bool collides(Object& other) override; + void collision(Object& other) override; + void update(Context& context) override; + void render(sf::RenderWindow& window) const override; +private: }; diff --git a/src/Context.h b/src/Context.h index 6f99ae2..bdf0d1c 100644 --- a/src/Context.h +++ b/src/Context.h @@ -2,12 +2,14 @@ #define CONTEXT_H #include "States.h" +#include struct Context { std::unique_ptr current_state {nullptr}; std::unique_ptr next_state {nullptr}; std::unique_ptr saved_game {nullptr}; + sf::Int32 time; }; #endif \ No newline at end of file diff --git a/src/Moving_object.h b/src/Moving_object.h index aa26add..d017195 100644 --- a/src/Moving_object.h +++ b/src/Moving_object.h @@ -1,5 +1,5 @@ -#ifndef MOVING_OBJECTS_H -#define MOVING_OBJECTS_H +#ifndef MOVING_OBJECT_H +#define MOVING_OBJECT_H #include "Object.h" diff --git a/src/States.cc b/src/States.cc index 97faf60..b700251 100644 --- a/src/States.cc +++ b/src/States.cc @@ -1,11 +1,13 @@ #include - +#include #include "States.h" #include "Context.h" #include "constants.h" -Game_state::Game_state() : game_map{}, player{}, pause_game{false} +Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{} { + Bottle* bottle = new Bottle {}; + bottles.push_back(bottle); } void Game_state::update(Context& context) @@ -18,6 +20,16 @@ void Game_state::update(Context& context) pause_game = false; return; } + time_since_last_bottle += context.time; + //std::cout << time_since_last_bottle << std::endl; + if (time_since_last_bottle >= 2) + { + Bottle* bottle = new Bottle {}; + bottles.push_back(bottle); + std::cout << "placed bottle"<< std::endl; + time_since_last_bottle = 0; + + } game_map.update(context); player.update(context); } @@ -26,6 +38,11 @@ void Game_state::render(sf::RenderWindow& window) const { game_map.render(window); player.render(window); + for (int i {}; i < int(bottles.size()); ++i) + { + bottles[i]->render(window); + } + } void Game_state::handle_input(sf::Event& event) diff --git a/src/States.h b/src/States.h index 8664b0f..a9a7358 100644 --- a/src/States.h +++ b/src/States.h @@ -2,9 +2,11 @@ #define STATES_H #include "SFML/Graphics.hpp" +#include #include "Map.h" #include "Player.h" +#include "Bottle.h" struct Context; //finns en strukt som säger att Context finns innan den är deklarerad @@ -28,6 +30,8 @@ public: void handle_input(sf::Event& event) override; protected: private: + float time_since_last_bottle {0}; + std::vector bottles; Map game_map; Player player; bool pause_game; diff --git a/src/Static_object.h b/src/Static_object.h index 0f62fdb..2b63c0a 100644 --- a/src/Static_object.h +++ b/src/Static_object.h @@ -1,18 +1,14 @@ #ifndef STATIC_OBJECT_H #define STATIC_OBJECT_H -#include +#include "Object.h" #include -class Static_obejct : public Object +class Static_object : public Object { public: Static_object() = default; - virtual ~Static_object() = default; - virtual bool collides(Object &other) = 0; - virtual void collision(Object &other) = 0; - virtual void update() = 0; - virtual void render(sf::RenderWindow &window) = 0; + ~Static_object() = default; private: }; diff --git a/src/_main.cc b/src/_main.cc index d61c135..7b58650 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -41,6 +41,7 @@ int main () game_context.current_state->handle_input(event); } + //game_context.time = game_clock.getElapsedtime().asSeconds() // Update startmeny game_context.current_state->update(game_context);