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 <SFML/Graphics.hpp>
#include "Game.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); const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
float Game::PlayerTurnSpeed = 200.f;
Game::Game() Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application") : mWindow(sf::VideoMode(1000, 1000), "Hertog Game")
, mTexture() , mTexture()
, mPlayer() { , mPlayer() {
if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) { if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
// Handle loading errors // Handle loading errors
} }
mPlayer.setTexture(mTexture); 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); mPlayer.setPosition(100.f, 100.f);
} }
void Game::run() { void Game::run() {
@ -36,10 +37,13 @@ void Game::processEvents() {
switch (event.type) { 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. //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: case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true); handleKeyboard(event.key.code, true);
break; break;
case sf::Event::KeyReleased: case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false); handleKeyboard(event.key.code, false);
break;
case sf::Event::MouseWheelScrolled:
handleScrollwheel(event.mouseWheelScroll.delta);
break; break;
case sf::Event::Closed: case sf::Event::Closed:
mWindow.close(); mWindow.close();
@ -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) if (key == sf::Keyboard::W)
mIsMovingUp = isPressed; mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S) else if (key == sf::Keyboard::S)
@ -59,17 +63,22 @@ void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
mIsMovingRight = isPressed; mIsMovingRight = isPressed;
} }
void Game::handleScrollwheel(float delta) {
mPlayerRotation += delta;
}
void Game::update(sf::Time deltaTime) { void Game::update(sf::Time deltaTime) {
sf::Vector2f movement(0.f, 0.f); sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp) if (mIsMovingUp)
movement.y -= 100.f; movement.y -= Game::PlayerMoveSpeed;
if (mIsMovingDown) if (mIsMovingDown)
movement.y += 100.f; movement.y += Game::PlayerMoveSpeed;
if (mIsMovingLeft) if (mIsMovingLeft)
movement.x -= 100.f; movement.x -= Game::PlayerMoveSpeed;
if (mIsMovingRight) if (mIsMovingRight)
movement.x += 100.f; movement.x += Game::PlayerMoveSpeed;
mPlayer.setRotation(mPlayerRotation * Game::PlayerTurnSpeed * deltaTime.asSeconds());
mPlayer.move(movement * deltaTime.asSeconds()); mPlayer.move(movement * deltaTime.asSeconds());
} }

View File

@ -2,11 +2,13 @@ class Game
{ {
public:Game(); public:Game();
void run(); void run();
static const float PlayerSpeed; static const float PlayerMoveSpeed;
static float PlayerTurnSpeed;
static const sf::Time TimePerFrame; static const sf::Time TimePerFrame;
private: private:
void processEvents(); 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 update(const sf::Time);
void render(); void render();
@ -15,8 +17,14 @@ class Game
sf::Texture mTexture; sf::Texture mTexture;
sf::Sprite mPlayer; sf::Sprite mPlayer;
float mPlayerRotation{0.0};
sf::Vector2f mPlayerDirection{0.0, 0.0};
bool mIsMovingUp{false}; bool mIsMovingUp{false};
bool mIsMovingDown{false}; bool mIsMovingDown{false};
bool mIsMovingLeft{false}; bool mIsMovingLeft{false};
bool mIsMovingRight{false}; bool mIsMovingRight{false};
bool mIsRotatingRight{false};
bool mIsRotatingLeft{false};
}; };