"helper": {
"start_pos": [1000, 50],
"scale": [0.3, 0.3],
- "max_speed": 5,
+ "max_speed": 2,
"direction": [0.35, 1],
"stop_top": 50,
"game_constants":
{
"bottles_per_second" : 2
+ },
+ "map":
+ {
+ "points_pos": [100, 100],
+ "font_size": 24
}
}
\ No newline at end of file
#include "constants.h"
#include "States.h"
#include "Context.h"
+#include <cmath>
#include <iostream>
void Helper::collision(Object& other)
{
-
-
+ move(false);
}
void Helper::update(Context& context)
-{
- position += direction * max_speed;
-
- if (position.y >= stop_bot || position.y <= stop_top )
- {
- direction *= -1.0f;
- }
-
-
+{
+ move();
//Game_state* game = static_cast<Game_state*>(context.current_state.get());
+
+}
+
+void Helper::move(bool forward)
+{
+ position += static_cast<float>(pow(-1, forward - 1)) * direction * max_speed;
+ if (position.y >= stop_bot || position.y <= stop_top )
+ {
+ direction *= -1.0f;
+ }
sprite.setPosition(position);
}
protected:
//void move(Time);
private:
+ void move(bool forward=true);
float stop_bot;
float stop_top;
float max_speed;
#include "Map.h"
#include "constants.h"
+#include <string>
-Map::Map() : texture{}, sprite{}
+Map::Map(json& params) : texture{}, sprite{}, point_text{}, font{}
{
texture.loadFromFile("assets/bakgrund.png");
sprite.setTexture(texture);
sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF);
sprite.setPosition(0,0);
+
+ 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()};
+ point_text.setOrigin(gbts.width / 2, gbts.height / 2);
+
+ point_text.setPosition (params["points_pos"][0], params["points_pos"][1]);
+ point_text.setFillColor(sf::Color::Red);
}
-void Map::update(Context& context)
-{}
+void Map::update(int points)
+{
+ point_text.setString("Points: " + std::to_string(points));
+}
void Map::render(sf::RenderWindow& window) const
{
window.draw(sprite);
+ window.draw(point_text);
}
\ No newline at end of file
#define MAP_H
#include <SFML/Graphics.hpp>
+#include "json.hpp"
-struct Context;
+using json = nlohmann::json;
class Map
{
public:
- Map();
+ Map(json& params);
~Map() = default;
- void update(Context& context);
+ void update(int points);
void render(sf::RenderWindow& window) const;
protected:
private:
sf::Texture texture;
sf::Sprite sprite;
+ sf::Text point_text;
+ sf::Font font;
};
#endif
\ No newline at end of file
void Player::collision(Object& other)
{
- return;
+ move(false);
}
void Player::update(Context& context)
{
// Get game_state from context, static_cast since we know that game is currently running
// game_state is still managed by context
- Game_state* game = static_cast<Game_state*>(context.current_state.get());
+ //Game_state* game = static_cast<Game_state*>(context.current_state.get());
+ move();
- position += max_speed * direction;
-
+ return;
+}
+
+void Player::move(bool forward)
+{
+ position += static_cast<float>(pow(-1, forward - 1)) * max_speed * direction;
sprite.setPosition(position);
sack.setPosition(position + sack_offset);
- return;
}
void Player::render(sf::RenderWindow& window) const
}
}
-int Player::add_collected()
+void Player::add_collected()
{
collected += 1;
+}
+int Player::get_collected()
+{
return collected;
+}
+
+void Player::zero_collected()
+{
+ collected = 0;
}
\ No newline at end of file
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
void handle_input(sf::Event& event);
- int add_collected();
+ void add_collected();
+ int get_collected();
+ void zero_collected();
protected:
private:
+ void move(bool forward=true);
int collected;
float max_speed;
sf::Sprite sack;
time_since_last_bottle{0.0f},
bottle_texture {},
data{},
- enemy{}
+ enemy{},
+ points{0}
{
std::ifstream f("assets/data.json");
data = json::parse(f);
player = std::make_unique<Player>(data["player"]);
helper = std::make_unique<Helper>(data["helper"]);
+ game_map = std::make_unique<Map>(data["map"]);
bottle_texture.loadFromFile("assets/kir.png");
}
}
}
+ helper->update(context);
+ player->update(context);
if (player->collides(*helper))
{
- //reset add collected
- // reset hitbox of sack
+ points += player->get_collected();
+ player->zero_collected();
+ player->collision(*helper);
+ helper->collision(*player);
}
+
time_since_last_bottle += context.time.asSeconds();
//std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl;
std::cout << "placed bottle"<< std::endl;
time_since_last_bottle = 0;
}
- game_map.update(context);
- if (!(player->collides(*helper)))
- {
- helper->update(context);
- }
- player->update(context);
+ game_map->update(points);
enemy.update(context);
}
void Game_state::render(sf::RenderWindow &window) const
{
- game_map.render(window);
+ game_map->render(window);
player->render(window);
enemy.render(window);
helper->render(window);
private:
float time_since_last_bottle;
- Map game_map;
+ std::unique_ptr<Map> game_map;
std::unique_ptr<Player> player;
std::unique_ptr<Helper> helper;
Main_enemy enemy;
bool pause_game;
sf::Texture bottle_texture;
json data;
+ int points;
};