},
"map":
{
- "points_pos": [5, 0],
- "font_size": 40
+ "points_pos": [5, 38],
+ "collected_pos": [5, 76],
+ "time_pos": [5, 0],
+ "font_size": 36
},
"main_enemy":
{
"scale" : [0.35, 0.35],
"start_pos" : [50, 600],
- "max_speed": 2.0
+ "max_speed": 2.0,
+ "degrees_per_second": 360
},
"yf":
{
#include "Main_enemy.h"
#include "States.h"
#include "Context.h"
+#include "constants.h"
-Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_speed"]}, moving_to_bottle{false}
+Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_speed"]}, moving_to_bottle{false}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]}
{
//texture.loadFromFile("assets/6Ling_figur2.png");
texture = txtr;
sprite.setTexture(texture);
position = {params["start_pos"][0], params["start_pos"][1]};
sprite.setScale(params["scale"][0], params["scale"][1]);
- sf::FloatRect gb {sprite.getGlobalBounds()};
+ sf::FloatRect gb {sprite.getLocalBounds()};
sprite.setOrigin(gb.width / 2, gb.height / 2);
sprite.setPosition(position);
}
void Main_enemy::update(Context& context)
{
-
- Game_state* game = static_cast<Game_state*>(context.current_state.get());
- if ((game->bottles).size() != 0)
+ if(tumbling)
{
- moving_to_bottle = true;
- direction = find_closest_bottle(game->bottles) - position;
- float magnitude = sqrt(direction.x*direction.x + direction.y*direction.y);
- direction /= magnitude;
- move();
+ sprite.rotate(rotation/FPS);
+ tumble_degrees += rotation/FPS;
+ if (tumble_degrees >= 360)
+ {
+ tumbling = false;
+ tumble_degrees = 0;
+ direction = {0,0};
+ }
}
else
{
- moving_to_bottle = false;
+ Game_state* game = static_cast<Game_state*>(context.current_state.get());
+ if ((game->bottles).size() != 0)
+ {
+ moving_to_bottle = true;
+ direction = find_closest_bottle(game->bottles) - position;
+ float magnitude = sqrt(direction.x*direction.x + direction.y*direction.y);
+ direction /= magnitude;
+ move();
+ }
+ else
+ {
+ moving_to_bottle = false;
+ }
}
}
void Main_enemy::render(sf::RenderWindow& window) const
{
window.draw(sprite);
}
+
+void Main_enemy::collision(Player& player)
+{
+ move(false);
+ tumbling = true;
+}
+
void Main_enemy::move(bool forward)
{
if (moving_to_bottle)
#include "Bottle.h"
#include "Moving_object.h"
+#include "Player.h"
#include <vector>
Main_enemy(sf::Texture& txtr, json& params);
~Main_enemy() = default;
void collision(Object& other) override;
+ void collision(Player& player);
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
private:
float max_speed;
+ float tumble_degrees;
+ int rotation;
+ bool tumbling;
bool moving_to_bottle;
void move(bool forward = true);
float bottle_dist(std::unique_ptr<Bottle> const& bottle);
#include "constants.h"
#include <string>
-Map::Map(json& params) : texture{}, sprite{}, point_text{}, font{}
+Map::Map(json& params) : texture{}, sprite{}, point_text{}, collected_text{}, time_text{}, bottle_texture{}, bottle_sprite{}, font{}
{
texture.loadFromFile("assets/bakgrund.png");
sprite.setTexture(texture);
+ bottle_texture.loadFromFile("assets/kir.png");
+ bottle_sprite.setTexture(bottle_texture);
sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF);
sprite.setPosition(0,0);
+ bottle_sprite.setScale(0.5, 0.5);
+ bottle_sprite.setPosition(params["collected_pos"][0], params["collected_pos"][1]);
+
if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") )
throw std::invalid_argument ("Unable to load font");
point_text = sf::Text{"Points: 0", font, params["font_size"]};
- sf::FloatRect gbts {point_text.getGlobalBounds()};
+ collected_text = sf::Text{"0", font, params["font_size"]};
+ time_text = sf::Text{"Time left: 0", font, params["font_size"]};
point_text.setPosition (params["points_pos"][0], params["points_pos"][1]);
point_text.setFillColor(sf::Color::Black);
+ collected_text.setPosition (static_cast<float>(params["collected_pos"][0]) + 40, params["collected_pos"][1]);
+ collected_text.setFillColor(sf::Color::Black);
+ time_text.setPosition (params["time_pos"][0], params["time_pos"][1]);
+ time_text.setFillColor(sf::Color::Black);
}
void Map::update(int const collected, int const points, int const time_left)
{
- point_text.setString("Collected: " + std::to_string(collected) + "\n" + "Points: " + std::to_string(points) + "\n" + "Time left: " + std::to_string(time_left) + " s");
+ if (collected == 0)
+ {
+ collected_text.setFillColor(sf::Color::Red);
+ }
+ else
+ {
+ collected_text.setFillColor(sf::Color::Black);
+ }
+ if (points == 0)
+ {
+ point_text.setFillColor(sf::Color::Red);
+ }
+ else
+ {
+ point_text.setFillColor(sf::Color::Black);
+ }
+ if (time_left <= 5)
+ {
+ time_text.setFillColor(sf::Color::Red);
+ }
+ else
+ {
+ time_text.setFillColor(sf::Color::Black);
+ }
+ collected_text.setString("x" + std::to_string(collected));
+ point_text.setString("Points: " + std::to_string(points));
+ time_text.setString("Time left: " + std::to_string(time_left) + " s");
}
void Map::render(sf::RenderWindow& window) const
{
window.draw(sprite);
+ window.draw(bottle_sprite);
window.draw(point_text);
+ window.draw(collected_text);
+ window.draw(time_text);
}
bool Map::collides(Object& other)
private:
sf::Texture texture;
sf::Sprite sprite;
+ sf::Texture bottle_texture;
+ sf::Sprite bottle_sprite;
sf::Text point_text;
+ sf::Text collected_text;
+ sf::Text time_text;
sf::Font font;
};
void Player::collision(Object& other)
{
- move(false);
+
+
}
void Player::collision(Map& map)
{
- move(false);
+ if (tumbling)
+ {
+ move();
+ }
+ else
+ {
+ move(false);
+ }
}
void Player::collision(YF& yf)
player->collision(*helper);
helper->collision(*player);
}
+ if (player->collides(*enemy))
+ {
+ enemy->collision(*player);
+ }
if(yf != nullptr)
{
if (player->collides(*yf))