"degrees_per_second": 360,
"sack" :
{
- "offset" : [30, 18],
+ "offset" : [25, -5],
"scale" : [0.3, 0.3],
"angle" : -25
}
},
"game_constants":
{
- "bottles_per_second" : 2,
+ "bottles_per_second" : 1,
"game_time" : 20,
"yf_per_second": 0.125,
- "bikes_per_second": 0.17,
+ "bikes_per_second": 0.25,
"scoreboard" : 5
},
"main_enemy":
{
"scale" : [0.35, 0.35],
- "start_pos" : [50, 600]
+ "start_pos" : [50, 600],
+ "max_speed": 2.0
},
"yf":
{
"scale" : [1, 1],
- "max_speed" : 5.0,
+ "max_speed" : 2.0,
"direction": [0, -1]
},
"bike_enemy":
{
- "scale" : [0.3, 0.3],
- "max_speed" : 3.0,
+ "scale" : [0.25, 0.25],
+ "max_speed" : 5.0,
"direction": [0, 1]
}
-Gorilla,4
-Gorilla,3
-Stina,2
-Gorilla,2
-Gorilla,2
+Gorilla,14
+Gorilla,10
+Gorilla,9
+Gorilla,7
+Gorilla,6
#include "States.h"
#include "Context.h"
-Main_enemy::Main_enemy(sf::Texture& txtr, json& params)
+Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_speed"]}, moving_to_bottle{false}
{
//texture.loadFromFile("assets/6Ling_figur2.png");
texture = txtr;
void Main_enemy::collision(Object& other)
{
- return;
+ move(false);
}
void Main_enemy::update(Context& context)
{
+
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;
- position += direction*2.0f ;
- sprite.setPosition(position);
- //std::cout << find_closest_bottle(game->bottles).x << std::endl;
- }
-
+ move();
+ }
+ else
+ {
+ moving_to_bottle = false;
+ }
}
void Main_enemy::render(sf::RenderWindow& window) const
{
window.draw(sprite);
}
+void Main_enemy::move(bool forward)
+{
+ if (moving_to_bottle)
+ {
+ position += static_cast<float>(pow(-1, forward - 1)) * max_speed * direction;
+ sprite.setPosition(position);
+ }
+}
sf::Vector2f Main_enemy::find_closest_bottle(std::vector<std::unique_ptr<Bottle>> & bottles)
{
void render(sf::RenderWindow& window) const override;
protected:
- //void move(Time) override;
private:
+ float max_speed;
+ bool moving_to_bottle;
+ void move(bool forward = true);
float bottle_dist(std::unique_ptr<Bottle> const& bottle);
sf::Vector2f find_closest_bottle(std::vector<std::unique_ptr<Bottle>> & bottles);
};
texture.loadFromFile("assets/4V_figur.png");
sprite.setTexture(texture);
sprite.setScale(params["scale"][0], params["scale"][1]);
- sf::FloatRect gb {sprite.getGlobalBounds()};
- sprite.setOrigin(gb.width / 2, gb.height / 2);
+ sf::FloatRect glb {sprite.getLocalBounds()};
+ sprite.setOrigin(glb.width / 2, glb.height / 2);
sprite.setPosition(position);
sack_texture.loadFromFile("assets/sack.png");
sack.setTexture(sack_texture);
sack.setScale(params["sack"]["scale"][0], params["sack"]["scale"][1]);
- sf::FloatRect gb_s {sack.getGlobalBounds()};
+ sf::FloatRect gb_s {sack.getLocalBounds()};
sack.setOrigin(gb_s.width / 3, gb_s.height / 5);
sack_offset = {params["sack"]["offset"][0], params["sack"]["offset"][1]};
sack.rotate(params["sack"]["angle"]);
move(false);
tumbling = true;
}
+void Player::collision(Bike_enemy& bike)
+{
+ move(false);
+ tumbling = true;
+}
void Player::update(Context& context)
{
}
void Player::handle_input(sf::Event& event)
-{
- //while (falling == false)
- //{
+{
if (!tumbling)
{
direction = {0,0};
direction.x *= M_SQRT1_2;
direction.y *= M_SQRT1_2;
}
- }
-
-
- //}
+ }
}
void Player::add_collected()
{
collected = 0;
}
-/*void Player::tumble()
-{
- falling = true;
-}*/
\ No newline at end of file
#include "Moving_object.h"
#include "json.hpp"
#include "YF.h"
+#include "Bike_enemy.h"
using json = nlohmann::json;
void collision(Object& other) override;
void collision(YF& yf);
+ void collision(Bike_enemy& bike);
void update(Context& context) override;
void render(sf::RenderWindow& window) const override;
void handle_input(sf::Event& event);
void add_collected();
int get_collected();
void zero_collected();
- //void tumble();
+
protected:
private:
void move(bool forward=true);
bool tumbling;
sf::Sprite sack;
sf::Texture sack_texture;
- sf::Vector2f sack_offset; //{20, -30};
+ sf::Vector2f sack_offset;
};
#endif
\ No newline at end of file
for (unsigned int i {0}; i < bottles.size(); ++i)
{
- if (player->collides(*(bottles[i])))
+ if (enemy->collides(*(bottles[i])))
{
- // std::cout <<"crash" << std::endl;
+
bottles.erase(bottles.begin() + i);
- player->add_collected();
+
}
- }
- for (unsigned int i {0}; i < bottles.size(); ++i)
- {
- if (enemy->collides(*(bottles[i])))
+ else if (player->collides(*(bottles[i])))
{
- // std::cout <<"crash" << std::endl;
bottles.erase(bottles.begin() + i);
-
+ player->add_collected();
}
}
+
player->update(context);
helper->update(context);
{
player->zero_collected();
player->collision(*yf);
-
- //player->tumble();
-
+ }
+ if(enemy->collides(*yf))
+ {
+ enemy->collision(*yf);
+ }
+ }
+ if(bike != nullptr)
+ {
+ if (player->collides(*bike))
+ {
+ player->zero_collected();
+ player->collision(*bike);
+ }
+ if(enemy->collides(*bike))
+ {
+ enemy->collision(*bike);
}
}
- /*
- if (player->get_x() < 0) player->set_x(0);
- if (player->get_x() > S_WIDTH) player->set_x(S_WIDTH);
- if (player->get_y() < 0) player->set_y(0);
- if (player->get_y() > S_HEIGHT) player->set_y(S_HEIGHT);
- */
time_since_last_bottle += context.time.asSeconds();
time_since_last_yf += context.time.asSeconds();
yf->render(window);
}
- for (int i{}; i < int(bottles.size()); ++i)
+ for (int i{0}; i < int(bottles.size()); ++i)
{
bottles[i]->render(window);
}