sprite.setOrigin(gb.width / 2, gb.height / 2);
sprite.setPosition(position);
}
-bool Bottle::collides(Object& other)
-{
-}
void Bottle::collision(Object& other)
{
{
public:
Bottle();
-
-
- bool collides(Object& other) override;
void collision(Object& other) override;
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
Object() : position{}, sprite{}, texture{} {};
virtual ~Object() = default;
- virtual bool collides(Object& other) = 0;
+ bool collides(Object& other)
+ {
+ return get_hitbox().intersects(other.get_hitbox());
+ }
virtual void collision(Object& other) = 0;
virtual void update(Context& context) = 0;
virtual void render(sf::RenderWindow& window) const = 0;
-
+ sf::FloatRect get_hitbox()
+ {
+ return sprite.getGlobalBounds();
+ }
protected:
sf::Vector2f position;
sf::Sprite sprite;
sprite.setPosition(position);
}
-bool Player::collides(Object& other)
-{
- return false;
-}
void Player::collision(Object& other)
{
direction.x *= M_SQRT1_2;
direction.y *= M_SQRT1_2;
}
+}
+
+void Player::add_collected()
+{
+ collected += 1;
}
\ No newline at end of file
public:
Player();
~Player() = default;
- bool collides(Object& other) override;
+
void collision(Object& other) override;
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
void handle_input(sf::Event& event);
+ void add_collected();
protected:
private:
int collected;
Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}
{
- Bottle* bottle = new Bottle {};
- bottles.push_back(bottle);
+
+ bottles.push_back(std::make_unique<Bottle> ());
}
void Game_state::update(Context& context)
pause_game = false;
return;
}
+ //std::vector<Bottle> :: iterator it = bottles.begin();
+
+ /*for(std::unique_ptr<Bottle> &b: bottles)
+ {
+ if(player.collides(*b))
+ {
+ std::cout <<"crash" << std::endl;
+
+ player.add_collected();
+ }
+ }*/
+ for(unsigned int i = 0; i < bottles.size(); ++i)
+ {
+ if(player.collides(*(bottles[i])))
+ {
+ //std::cout <<"crash" << std::endl;
+ bottles.erase(bottles.begin() + i);
+ player.add_collected();
+ }
+ }
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)
{
if (bottles.size() > 10)
{
bottles.erase(bottles.begin());
}
- Bottle* bottle = new Bottle {};
- bottles.push_back(bottle);
- std::cout << "placed bottle"<< std::endl;
+
+ bottles.push_back(std::make_unique<Bottle>());
+ //std::cout << "placed bottle"<< std::endl;
time_since_last_bottle = 0;
}
#include "SFML/Graphics.hpp"
#include <vector>
+#include <memory>
#include "Map.h"
#include "Player.h"
protected:
private:
float time_since_last_bottle;
- std::vector<Bottle*> bottles;
+ std::vector<std::unique_ptr<Bottle>> bottles;
Map game_map;
Player player;
bool pause_game;