Hertog SPIN!!!! WHEEEEEeeeee :3

main
Johannes Hendrik Gerard van der Weide 2023-10-13 18:33:55 +02:00
parent 4fa8157eac
commit ba9f77a574
2 changed files with 30 additions and 13 deletions

View File

@ -1,18 +1,19 @@
#include <SFML/Graphics.hpp>
#include "Game.hpp"
const float Game::PlayerSpeed = 100.f;
const float Game::PlayerMoveSpeed = 100.f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
float Game::PlayerTurnSpeed = 200.f;
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
: mWindow(sf::VideoMode(1000, 1000), "Hertog Game")
, mTexture()
, mPlayer() {
if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
// Handle loading errors
}
mPlayer.setTexture(mTexture);
mPlayer.setScale(0.25f, 0.25f);
mPlayer.setScale(0.1f, 0.1f);
mPlayer.setOrigin(372.f, 372.f);
mPlayer.setPosition(100.f, 100.f);
}
void Game::run() {
@ -36,11 +37,14 @@ void Game::processEvents() {
switch (event.type) {
//For each time the while loop iterates, it means a new event that was registered by the window is being handled. While there can be many different events, we will only check for some types of events, which are of our interest right now.
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
handleKeyboard(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
handleKeyboard(event.key.code, false);
break;
case sf::Event::MouseWheelScrolled:
handleScrollwheel(event.mouseWheelScroll.delta);
break;
case sf::Event::Closed:
mWindow.close();
break;
@ -48,7 +52,7 @@ void Game::processEvents() {
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
void Game::handleKeyboard(sf::Keyboard::Key key,bool isPressed) {
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
@ -59,17 +63,22 @@ void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
mIsMovingRight = isPressed;
}
void Game::handleScrollwheel(float delta) {
mPlayerRotation += delta;
}
void Game::update(sf::Time deltaTime) {
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= 100.f;
movement.y -= Game::PlayerMoveSpeed;
if (mIsMovingDown)
movement.y += 100.f;
movement.y += Game::PlayerMoveSpeed;
if (mIsMovingLeft)
movement.x -= 100.f;
movement.x -= Game::PlayerMoveSpeed;
if (mIsMovingRight)
movement.x += 100.f;
movement.x += Game::PlayerMoveSpeed;
mPlayer.setRotation(mPlayerRotation * Game::PlayerTurnSpeed * deltaTime.asSeconds());
mPlayer.move(movement * deltaTime.asSeconds());
}

View File

@ -2,11 +2,13 @@ class Game
{
public:Game();
void run();
static const float PlayerSpeed;
static const float PlayerMoveSpeed;
static float PlayerTurnSpeed;
static const sf::Time TimePerFrame;
private:
void processEvents();
void handlePlayerInput(sf::Keyboard::Key key,bool isPressed);
void handleKeyboard(sf::Keyboard::Key key,bool isPressed);
void handleScrollwheel(float difference);
void update(const sf::Time);
void render();
@ -14,9 +16,15 @@ class Game
sf::RenderWindow mWindow;
sf::Texture mTexture;
sf::Sprite mPlayer;
float mPlayerRotation{0.0};
sf::Vector2f mPlayerDirection{0.0, 0.0};
bool mIsMovingUp{false};
bool mIsMovingDown{false};
bool mIsMovingLeft{false};
bool mIsMovingRight{false};
bool mIsRotatingRight{false};
bool mIsRotatingLeft{false};
};