From: Arvid Sjöblom Date: Wed, 15 Nov 2023 13:29:32 +0000 (+0100) Subject: added enemy, arvid, stina X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=e3de0076a8f082ac4e1b3aef6a8d9edf7dff4d93;p=TDDC76_proj.git added enemy, arvid, stina --- diff --git a/src/Bottle.cc b/src/Bottle.cc index 7bc1e4d..16e6328 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -27,3 +27,7 @@ void Bottle::render(sf::RenderWindow& window) const { window.draw(sprite); } +sf::Vector2f Bottle::get_pos() +{ + return position; +} \ No newline at end of file diff --git a/src/Bottle.h b/src/Bottle.h index f7b2afa..3afc401 100644 --- a/src/Bottle.h +++ b/src/Bottle.h @@ -12,6 +12,7 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void set_texture(sf::Texture& txtr); + sf::Vector2f get_pos(); private: }; diff --git a/src/Main_enemy.cc b/src/Main_enemy.cc index e69de29..8b5dafe 100644 --- a/src/Main_enemy.cc +++ b/src/Main_enemy.cc @@ -0,0 +1,65 @@ +#include +#include +#include +#include "Main_enemy.h" +#include "States.h" +#include "Context.h" + +Main_enemy::Main_enemy() +{ + texture.loadFromFile("assets/6Ling_figur2.png"); + sprite.setTexture(texture); + + sprite.setScale(0.7, 0.7); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setOrigin(gb.width / 2, gb.height / 2); + sprite.setPosition(position); + +} + +void Main_enemy::collision(Object& other) +{ + return; +} +void Main_enemy::update(Context& context) +{ + Game_state* game = static_cast(context.current_state.get()); + if ((game->bottles).size() != 0) + { + direction = find_closest_bottle(game->bottles) - position; + float magnitude = sqrt(direction.x*direction.x + direction.y*direction.y); + direction /= magnitude; + position += direction*2.0f ; + sprite.setPosition(position); + //std::cout << find_closest_bottle(game->bottles).x << std::endl; + } + +} +void Main_enemy::render(sf::RenderWindow& window) const +{ + window.draw(sprite); +} + +sf::Vector2f Main_enemy::find_closest_bottle(std::vector> & bottles) +{ + //return {0,0}; + + unsigned int closest_bottle_index {0}; + float min_dist {10000000.0f}; + + for(unsigned int i {0}; i < bottles.size(); ++i) + { + float distance {bottle_dist(bottles[i])}; + if(distance < min_dist) + { + min_dist = distance; + closest_bottle_index = i; + } + } + return bottles[closest_bottle_index]->get_pos(); +} +float Main_enemy::bottle_dist(std::unique_ptr const& bottle) +{ + sf::Vector2f dist = position - bottle->get_pos(); + return sqrt(dist.x*dist.x + dist.y*dist.y); +} diff --git a/src/Main_enemy.h b/src/Main_enemy.h index 4370d69..4027848 100644 --- a/src/Main_enemy.h +++ b/src/Main_enemy.h @@ -4,22 +4,25 @@ #include #include "Bottle.h" +#include "Moving_object.h" #include -class Main_enemy +class Main_enemy : public Moving_object { public: - bool collides(Object& other) override; + Main_enemy(); + ~Main_enemy() = default; void collision(Object& other) override; - void update() override; - void render(sf::RenderWindow& window) override; + void update(Context& context) override; + void render(sf::RenderWindow& window) const override; protected: - void move(Time) override; + //void move(Time) override; private: - Bottle find_closest_bottle(std::vector); + float bottle_dist(std::unique_ptr const& bottle); + sf::Vector2f find_closest_bottle(std::vector> & bottles); }; #endif \ No newline at end of file diff --git a/src/States.cc b/src/States.cc index 64187f7..40ff025 100644 --- a/src/States.cc +++ b/src/States.cc @@ -5,7 +5,7 @@ #include "Context.h" #include "constants.h" -Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}, bottle_texture {} +Game_state::Game_state() : game_map{}, player{}, enemy{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}, bottle_texture {} { bottle_texture.loadFromFile("assets/kir.png"); } @@ -31,7 +31,7 @@ void Game_state::update(Context &context) 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]))) { @@ -40,16 +40,26 @@ void Game_state::update(Context &context) 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) { bottles.push_back(std::make_unique(bottle_texture)); - std::cout << "placed bottle"<< std::endl; + //std::cout << "placed bottle"<< std::endl; time_since_last_bottle = 0; } game_map.update(context); player.update(context); + enemy.update(context); helper.update(context); } @@ -57,6 +67,7 @@ void Game_state::render(sf::RenderWindow &window) const { game_map.render(window); player.render(window); + enemy.render(window); helper.render(window); for (int i{}; i < int(bottles.size()); ++i) { diff --git a/src/States.h b/src/States.h index 2c39109..5825ff6 100644 --- a/src/States.h +++ b/src/States.h @@ -7,9 +7,11 @@ #include "Map.h" #include "Player.h" +#include "Main_enemy.h" #include "Helper.h" #include "Bottle.h" + struct Context; //finns en strukt som säger att Context finns innan den är deklarerad class State @@ -30,13 +32,15 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; + std::vector> bottles; protected: private: float time_since_last_bottle; - std::vector> bottles; + Map game_map; Player player; Helper helper; + Main_enemy enemy; bool pause_game; sf::Texture bottle_texture;