diff --git a/include/states/game-state.hpp b/include/states/game-state.hpp index b711e97..4f86966 100644 --- a/include/states/game-state.hpp +++ b/include/states/game-state.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "engine/include/state/state.hpp" #include "engine/include/game/game.hpp" #include "states/pause-state.hpp" @@ -17,6 +18,16 @@ class GameState : public pte::State sf::Sprite background; sf::Sprite pause_button; + // demo things + float pi = 3.14159f; + float ballRadius = 10.f; + float ballSpeed = 400.f; + float ballAngle = 0.f; + sf::CircleShape ball; + + void init_ball(); + void update_ball(float delta_time); + public: GameState(pte::game_data_ref data); diff --git a/src/states/game-state.cpp b/src/states/game-state.cpp index 4a1a14a..c38b689 100644 --- a/src/states/game-state.cpp +++ b/src/states/game-state.cpp @@ -9,6 +9,8 @@ void GameState::init() this->data->assets.load_texture("Pause Button", PAUSE_BUTTON); pause_button.setTexture(this->data->assets.get_texture("Pause Button")); pause_button.setPosition(this->data->window.getSize().x - pause_button.getLocalBounds().width - 10, pause_button.getPosition().y + 10); + + init_ball(); } void GameState::handle_input() @@ -32,11 +34,57 @@ void GameState::handle_input() void GameState::update(float delta_time) { + update_ball(delta_time); } void GameState::draw(float delta_time) { this->data->window.clear(sf::Color(56, 42, 55)); + this->data->window.draw(this->ball); this->data->window.draw(this->pause_button); this->data->window.display(); } + +void GameState::init_ball() +{ + ball.setRadius(ballRadius - 3); + ball.setOutlineThickness(3); + ball.setOutlineColor(sf::Color::White); + ball.setFillColor(sf::Color::White); + ball.setOrigin(ballRadius / 2, ballRadius / 2); + ball.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); + + do + { + ballAngle = (std::rand() % 360) * 2 * pi / 360; + } while (std::abs(std::cos(ballAngle)) < 0.7f); +} + +void GameState::update_ball(float delta_time) +{ + // move ball + float factor = ballSpeed * delta_time; + ball.move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor); + + // check colisions + if (ball.getPosition().x - ballRadius < 0.f) + { + ballAngle = -ballAngle + pi; + ball.setPosition(ballRadius + 0.1f, ball.getPosition().y); + } + if (ball.getPosition().x + ballRadius > SCREEN_WIDTH) + { + ballAngle = -ballAngle + pi; + ball.setPosition(SCREEN_WIDTH - ballRadius - 0.1f, ball.getPosition().y); + } + if (ball.getPosition().y - ballRadius < 0.f) + { + ballAngle = -ballAngle; + ball.setPosition(ball.getPosition().x, ballRadius + 0.1f); + } + if (ball.getPosition().y + ballRadius > SCREEN_HEIGHT) + { + ballAngle = -ballAngle; + ball.setPosition(ball.getPosition().x, SCREEN_HEIGHT - ballRadius - 0.1f); + } +} \ No newline at end of file