{
window.draw(sprite);
}
+sf::Vector2f Bottle::get_pos()
+{
+ return position;
+}
\ No newline at end of file
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
void set_texture(sf::Texture& txtr);
+ sf::Vector2f get_pos();
private:
};
+#include <cmath>
+#include <memory>
+#include <iostream>
+#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<Game_state*>(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<std::unique_ptr<Bottle>> & 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<Bottle> const& bottle)
+{
+ sf::Vector2f dist = position - bottle->get_pos();
+ return sqrt(dist.x*dist.x + dist.y*dist.y);
+}
#include <SFML/Graphics.hpp>
#include "Bottle.h"
+#include "Moving_object.h"
#include <vector>
-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<Bottle>);
+ float bottle_dist(std::unique_ptr<Bottle> const& bottle);
+ sf::Vector2f find_closest_bottle(std::vector<std::unique_ptr<Bottle>> & bottles);
};
#endif
\ No newline at end of file
#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");
}
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])))
{
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>(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);
}
{
game_map.render(window);
player.render(window);
+ enemy.render(window);
helper.render(window);
for (int i{}; i < int(bottles.size()); ++i)
{
#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
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
void handle_input(sf::Event& event) override;
+ std::vector<std::unique_ptr<Bottle>> bottles;
protected:
private:
float time_since_last_bottle;
- std::vector<std::unique_ptr<Bottle>> bottles;
+
Map game_map;
Player player;
Helper helper;
+ Main_enemy enemy;
bool pause_game;
sf::Texture bottle_texture;