From fbf27a2e0c32b698580d9d40d6e02d29a28d4a81 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sat, 27 Jul 2019 22:13:37 +0100 Subject: [PATCH 1/8] Update gitignore file --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 05b3f326..d84be798 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,13 @@ #Visual Studio Debug/ Release/ - +*.sln *.vcxproj *.filters +*.users *.user - *.dll +.vs # Visual Studio Code .vscode/ From 6eac9c330f99997b81fc6437206103763efc1d78 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sat, 27 Jul 2019 22:20:12 +0100 Subject: [PATCH 2/8] Fix conversion warnings in classic overworld generator --- Source/World/Event/PlayerDigEvent.cpp | 19 +++++++++++++------ .../Terrain/ClassicOverWorldGenerator.cpp | 18 +++++++++--------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Source/World/Event/PlayerDigEvent.cpp b/Source/World/Event/PlayerDigEvent.cpp index 93a5eb0d..1bad2e61 100644 --- a/Source/World/Event/PlayerDigEvent.cpp +++ b/Source/World/Event/PlayerDigEvent.cpp @@ -12,7 +12,11 @@ PlayerDigEvent::PlayerDigEvent(sf::Mouse::Button button, const glm::vec3& locati void PlayerDigEvent::handle(World& world) { - auto chunkLocation = World::getChunkXZ(m_digSpot.x, m_digSpot.z); + auto chunkLocation = World::getChunkXZ( + static_cast(m_digSpot.x), + static_cast(m_digSpot.z) + ); + if (world.getChunkManager().chunkLoadedAt(chunkLocation.x, chunkLocation.z)) { dig(world); @@ -21,10 +25,13 @@ void PlayerDigEvent::handle(World& world) void PlayerDigEvent::dig(World& world) { + int x = static_cast(m_digSpot.x); + int y = static_cast(m_digSpot.y); + int z = static_cast(m_digSpot.z); switch (m_buttonPress) { case sf::Mouse::Button::Left:{ - auto block = world.getBlock(m_digSpot.x, m_digSpot.y, m_digSpot.z); + auto block = world.getBlock(x, y, z); const auto& material = Material::toMaterial((BlockId)block.id); m_pPlayer->addItem(material); /* @@ -39,8 +46,8 @@ void PlayerDigEvent::dig(World& world) world.updateChunk (newX, newY, newZ); world.setBlock (newX, newY, newZ, 0); */ - world.updateChunk (m_digSpot.x, m_digSpot.y, m_digSpot.z); - world.setBlock (m_digSpot.x, m_digSpot.y, m_digSpot.z, 0); + world.updateChunk (x, y, z); + world.setBlock (x, y, z, 0); //} break; } @@ -56,8 +63,8 @@ void PlayerDigEvent::dig(World& world) else { stack.remove(); - world.updateChunk (m_digSpot.x, m_digSpot.y, m_digSpot.z); - world.setBlock (m_digSpot.x, m_digSpot.y, m_digSpot.z, material.toBlockID()); + world.updateChunk (x, y, z); + world.setBlock (x, y, z, material.toBlockID()); break; } } diff --git a/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp b/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp index 5ae899cc..13962448 100644 --- a/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp +++ b/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp @@ -80,10 +80,10 @@ void ClassicOverWorldGenerator::getHeightIn (int xMin, int zMin, int xMax, int z m_pChunk->getLocation().y); }; - int bottomLeft = getHeightAt(xMin, zMin); - int bottomRight = getHeightAt(xMax, zMin); - int topLeft = getHeightAt(xMin, zMax); - int topRight = getHeightAt(xMax, zMax); + float bottomLeft = getHeightAt(xMin, zMin); + float bottomRight = getHeightAt(xMax, zMin); + float topLeft = getHeightAt(xMin, zMax); + float topRight = getHeightAt(xMax, zMax); for (int x = xMin; x < xMax; ++x) for (int z = zMin; z < zMax; ++z) @@ -93,14 +93,14 @@ void ClassicOverWorldGenerator::getHeightIn (int xMin, int zMin, int xMax, int z if (z == CHUNK_SIZE) continue; - float h = smoothInterpolation(bottomLeft, topLeft, bottomRight, topRight, - xMin, xMax, - zMin, zMax, - x, z); + float h = smoothInterpolation( + bottomLeft, topLeft, bottomRight, topRight, + static_cast(xMin), static_cast(xMax), + static_cast(zMin), static_cast(zMax), + static_cast(x), static_cast(z)); m_heightMap.get(x, z) = h; } - //exit(0); } From 2e76906b43a35ef83f3ad8c61287227d4dde3de9 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sat, 27 Jul 2019 22:34:33 +0100 Subject: [PATCH 3/8] Fix many narrowing conversion warnings --- Source/Player/Player.cpp | 14 +++++++++----- Source/States/PlayingState.cpp | 18 +++--------------- Source/States/PlayingState.h | 3 --- Source/Texture/TextureAtlas.cpp | 4 ++-- Source/Util/Random.cpp | 9 ++++++--- 5 files changed, 20 insertions(+), 28 deletions(-) diff --git a/Source/Player/Player.cpp b/Source/Player/Player.cpp index c2c9ad43..591ed446 100644 --- a/Source/Player/Player.cpp +++ b/Source/Player/Player.cpp @@ -160,11 +160,15 @@ void Player::update(float dt, World& world) void Player::collide(World& world, const glm::vec3& vel, float dt) { - for (int x = position.x - box.dimensions.x; x < position.x + box.dimensions.x; x++) - for (int y = position.y - box.dimensions.y; y < position.y + 0.7 ; y++) - for (int z = position.z - box.dimensions.z; z < position.z + box.dimensions.z; z++) + for (float x = position.x - box.dimensions.x; x < position.x + box.dimensions.x; x++) + for (float y = position.y - box.dimensions.y; y < position.y + 0.7 ; y++) + for (float z = position.z - box.dimensions.z; z < position.z + box.dimensions.z; z++) { - auto block = world.getBlock(x, y, z); + auto block = world.getBlock( + static_cast(x), + static_cast(y), + static_cast(z) + ); if (block != 0 && block.getData().isCollidable) { @@ -255,7 +259,7 @@ void Player::mouseInput(const sf::Window& window) return; } - static float const BOUND = 89.9999; + static float const BOUND = 89.f; static auto lastMousePosition = sf::Mouse::getPosition(window); auto change = sf::Mouse::getPosition() - lastMousePosition; diff --git a/Source/States/PlayingState.cpp b/Source/States/PlayingState.cpp index af7a9918..c52a70a9 100644 --- a/Source/States/PlayingState.cpp +++ b/Source/States/PlayingState.cpp @@ -14,14 +14,6 @@ StatePlaying::StatePlaying(Application& app, const Config& config) , m_world (app.getCamera(), config, m_player) { app.getCamera().hookEntity(m_player); - - m_chTexture.loadFromFile("Res/Textures/ch.png"); - m_crosshair.setTexture(&m_chTexture); - m_crosshair.setSize({21, 21}); - m_crosshair.setOrigin(m_crosshair.getGlobalBounds().width / 2, - m_crosshair.getGlobalBounds().height / 2); - m_crosshair.setPosition(app.getWindow().getSize().x / 2, - app.getWindow().getSize().y / 2); } void StatePlaying::handleEvent(sf::Event e) @@ -38,9 +30,9 @@ void StatePlaying::handleInput() ray.getLength() < 6; ray.step(0.05)) { - int x = ray.getEnd().x; - int y = ray.getEnd().y; - int z = ray.getEnd().z; + int x = static_cast(ray.getEnd().x); + int y = static_cast(ray.getEnd().y); + int z = static_cast(ray.getEnd().z); auto block = m_world.getBlock(x, y, z); auto id = (BlockId)block.id; @@ -71,15 +63,12 @@ void StatePlaying::handleInput() void StatePlaying::update(float deltaTime) { - if (m_player.position.x < 0) m_player.position.x = 0; if (m_player.position.z < 0) m_player.position.z = 0; m_fpsCounter.update(); m_player.update(deltaTime, m_world); m_world.update(m_pApplication->getCamera()); - - } void StatePlaying::render(RenderMaster& renderer) @@ -97,7 +86,6 @@ void StatePlaying::render(RenderMaster& renderer) if (drawGUI) { m_fpsCounter.draw(renderer); - renderer.drawSFML(m_crosshair); m_player.draw(renderer); } diff --git a/Source/States/PlayingState.h b/Source/States/PlayingState.h index 2d767f99..01434225 100644 --- a/Source/States/PlayingState.h +++ b/Source/States/PlayingState.h @@ -28,9 +28,6 @@ class StatePlaying : public StateBase Player m_player; World m_world; - sf::RectangleShape m_crosshair; - sf::Texture m_chTexture; - FPSCounter m_fpsCounter; }; diff --git a/Source/Texture/TextureAtlas.cpp b/Source/Texture/TextureAtlas.cpp index 99cab7b4..1f257cf5 100644 --- a/Source/Texture/TextureAtlas.cpp +++ b/Source/Texture/TextureAtlas.cpp @@ -21,8 +21,8 @@ std::array TextureAtlas::getTexture(const sf::Vector2i& coords) static const GLfloat INDV_TEX_SIZE = 1.0f / TEX_PER_ROW; static const GLfloat PIXEL_SIZE = 1.0f / (float)m_imageSize; - GLfloat xMin = (coords.x * INDV_TEX_SIZE) + 0.5 * PIXEL_SIZE; - GLfloat yMin = (coords.y * INDV_TEX_SIZE) + 0.5 * PIXEL_SIZE; + GLfloat xMin = (coords.x * INDV_TEX_SIZE) + 0.5f * PIXEL_SIZE; + GLfloat yMin = (coords.y * INDV_TEX_SIZE) + 0.5f * PIXEL_SIZE; GLfloat xMax = (xMin + INDV_TEX_SIZE) - PIXEL_SIZE; GLfloat yMax = (yMin + INDV_TEX_SIZE) - PIXEL_SIZE; diff --git a/Source/Util/Random.cpp b/Source/Util/Random.cpp index 617fa8fd..f471150b 100644 --- a/Source/Util/Random.cpp +++ b/Source/Util/Random.cpp @@ -8,7 +8,10 @@ RandomSingleton& RandomSingleton::get() RandomSingleton::RandomSingleton() { - m_randomEngine.seed(std::time(nullptr)); - for (int i = 0; i < 5; i++) - intInRange(i, i * 5); + m_randomEngine.seed( + static_cast(std::time(nullptr)) + ); + for (int i = 0; i < 5; i++) { + intInRange(i, i * 5); + } } From 0a14f918724096270616f01307317db1fe052fd5 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sun, 28 Jul 2019 10:11:34 +0100 Subject: [PATCH 4/8] Fix all compiler warnings --- Source/Maths/NoiseGenerator.cpp | 4 +--- Source/States/PlayingState.cpp | 2 +- .../Terrain/ClassicOverWorldGenerator.cpp | 14 +++++++------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Source/Maths/NoiseGenerator.cpp b/Source/Maths/NoiseGenerator.cpp index eaad04a7..80ba0a54 100644 --- a/Source/Maths/NoiseGenerator.cpp +++ b/Source/Maths/NoiseGenerator.cpp @@ -19,8 +19,6 @@ void NoiseGenerator::setParameters(const NoiseParameters& params) noexcept m_noiseParameters = params; } - -//wtf? double NoiseGenerator::getNoise(int n) const noexcept { n += m_seed; @@ -32,7 +30,7 @@ double NoiseGenerator::getNoise(int n) const noexcept double NoiseGenerator::getNoise(double x, double z) const noexcept { - return getNoise(x + z * 57); + return getNoise(x + z * 57.0); } double NoiseGenerator::lerp(double a, double b, double z) const noexcept diff --git a/Source/States/PlayingState.cpp b/Source/States/PlayingState.cpp index c52a70a9..f45baa0d 100644 --- a/Source/States/PlayingState.cpp +++ b/Source/States/PlayingState.cpp @@ -28,7 +28,7 @@ void StatePlaying::handleInput() for (Ray ray({m_player.position.x, m_player.position.y + 0.6f, m_player.position.z}, m_player.rotation); //Corrected for camera offset ray.getLength() < 6; - ray.step(0.05)) + ray.step(0.05f)) { int x = static_cast(ray.getEnd().x); int y = static_cast(ray.getEnd().y); diff --git a/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp b/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp index 13962448..5fe82a8d 100644 --- a/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp +++ b/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp @@ -80,10 +80,10 @@ void ClassicOverWorldGenerator::getHeightIn (int xMin, int zMin, int xMax, int z m_pChunk->getLocation().y); }; - float bottomLeft = getHeightAt(xMin, zMin); - float bottomRight = getHeightAt(xMax, zMin); - float topLeft = getHeightAt(xMin, zMax); - float topRight = getHeightAt(xMax, zMax); + float bottomLeft = static_cast(getHeightAt(xMin, zMin)); + float bottomRight = static_cast(getHeightAt(xMax, zMin)); + float topLeft = static_cast(getHeightAt(xMin, zMax)); + float topRight = static_cast(getHeightAt(xMax, zMax)); for (int x = xMin; x < xMax; ++x) for (int z = zMin; z < zMax; ++z) @@ -99,7 +99,7 @@ void ClassicOverWorldGenerator::getHeightIn (int xMin, int zMin, int xMax, int z static_cast(zMin), static_cast(zMax), static_cast(x), static_cast(z)); - m_heightMap.get(x, z) = h; + m_heightMap.get(x, z) = static_cast(h); } } @@ -123,8 +123,8 @@ void ClassicOverWorldGenerator::getBiomeMap() for (int x = 0; x < CHUNK_SIZE + 1; x++) for (int z = 0; z < CHUNK_SIZE + 1; z++) { - int h = m_biomeNoiseGen.getHeight(x, z, location.x + 10, location.y + 10); - m_biomeMap.get(x, z) = h; + double h = m_biomeNoiseGen.getHeight(x, z, location.x + 10, location.y + 10); + m_biomeMap.get(x, z) = static_cast(h); } } From d5d0145d9f29194824f38ed5d2311cb5f556a263 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sun, 28 Jul 2019 16:42:44 +0100 Subject: [PATCH 5/8] Fix player collision --- Source/Input/Keyboard.cpp | 31 +++++++++++++++++++++++++++++++ Source/Input/Keyboard.h | 20 ++++++++++++++++++++ Source/Player/Player.cpp | 12 ++++++------ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 Source/Input/Keyboard.cpp create mode 100644 Source/Input/Keyboard.h diff --git a/Source/Input/Keyboard.cpp b/Source/Input/Keyboard.cpp new file mode 100644 index 00000000..d805adb4 --- /dev/null +++ b/Source/Input/Keyboard.cpp @@ -0,0 +1,31 @@ +#include "Keyboard.h" + + +Keyboard::Keyboard() { + std::fill(m_keys.begin(), m_keys.end(), false); +} + +void Keyboard::update(sf::Event e) { + m_recentlyReleased = sf::Keyboard::KeyCount; + switch (e.type) { + case sf::Event::KeyReleased: + m_keys[e.key.code] = false; + break; + + case sf::Event::KeyPressed: + m_recentlyReleased = e.key.code; + m_keys[e.key.code] = true; + break; + + default: + break; + } +} + +bool Keyboard::isKeyDown(sf::Keyboard::Key key) const { + return m_keys[key]; +} + +bool Keyboard::keyReleased(sf::Keyboard::Key key) const { + return m_recentlyReleased == key; +} \ No newline at end of file diff --git a/Source/Input/Keyboard.h b/Source/Input/Keyboard.h new file mode 100644 index 00000000..8c0aaebe --- /dev/null +++ b/Source/Input/Keyboard.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +#include + +class Keyboard { +public: + Keyboard(); + + void update(sf::Event e); + + bool isKeyDown(sf::Keyboard::Key key) const; + bool keyReleased(sf::Keyboard::Key key) const; + +private: + std::array m_keys; + sf::Keyboard::Key m_recentlyReleased; +}; \ No newline at end of file diff --git a/Source/Player/Player.cpp b/Source/Player/Player.cpp index 591ed446..fd80d5a9 100644 --- a/Source/Player/Player.cpp +++ b/Source/Player/Player.cpp @@ -160,14 +160,14 @@ void Player::update(float dt, World& world) void Player::collide(World& world, const glm::vec3& vel, float dt) { - for (float x = position.x - box.dimensions.x; x < position.x + box.dimensions.x; x++) - for (float y = position.y - box.dimensions.y; y < position.y + 0.7 ; y++) - for (float z = position.z - box.dimensions.z; z < position.z + box.dimensions.z; z++) + for (int x = position.x - box.dimensions.x; x < position.x + box.dimensions.x; x++) + for (int y = position.y - box.dimensions.y; y < position.y + 0.7 ; y++) + for (int z = position.z - box.dimensions.z; z < position.z + box.dimensions.z; z++) { auto block = world.getBlock( - static_cast(x), - static_cast(y), - static_cast(z) + x, + y, + z ); if (block != 0 && block.getData().isCollidable) From f35a39d2adc27f03e0f3291d04d6201d3ae4910e Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sun, 28 Jul 2019 16:48:59 +0100 Subject: [PATCH 6/8] Move togglekey to Input/ folder --- Source/Input/ToggleKey.cpp | 18 ++++++++++++++++++ Source/Input/ToggleKey.h | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Source/Input/ToggleKey.cpp create mode 100644 Source/Input/ToggleKey.h diff --git a/Source/Input/ToggleKey.cpp b/Source/Input/ToggleKey.cpp new file mode 100644 index 00000000..7291ec1b --- /dev/null +++ b/Source/Input/ToggleKey.cpp @@ -0,0 +1,18 @@ +#include "ToggleKey.h" + +ToggleKey::ToggleKey(sf::Keyboard::Key key) +: m_key (key) +{ } + +bool ToggleKey::isKeyPressed() +{ + if (m_delayTimer.getElapsedTime().asSeconds() > 0.2) + { + if (sf::Keyboard::isKeyPressed(m_key)) + { + m_delayTimer.restart(); + return true; + } + } + return false; +} diff --git a/Source/Input/ToggleKey.h b/Source/Input/ToggleKey.h new file mode 100644 index 00000000..bbb8d20e --- /dev/null +++ b/Source/Input/ToggleKey.h @@ -0,0 +1,19 @@ +#ifndef TOGGLEKEY_H_INCLUDED +#define TOGGLEKEY_H_INCLUDED + +#include + +class ToggleKey +{ + public: + ToggleKey(sf::Keyboard::Key); + + bool isKeyPressed(); + + private: + sf::Keyboard::Key m_key; + sf::Clock m_delayTimer; + +}; + +#endif // TOGGLEKEY_H_INCLUDED From 758451a505557af9f5cc46e990c00e54eba86d1d Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sun, 28 Jul 2019 16:56:45 +0100 Subject: [PATCH 7/8] Pass keyboard class into player class --- Source/Player/Player.cpp | 15 ++++++--------- Source/Player/Player.h | 7 ++++--- Source/States/PlayingState.cpp | 6 ++++-- Source/States/PlayingState.h | 3 ++- Source/ToggleKey.cpp | 18 ------------------ Source/ToggleKey.h | 19 ------------------- Source/World/World.cpp | 2 +- 7 files changed, 17 insertions(+), 53 deletions(-) delete mode 100644 Source/ToggleKey.cpp delete mode 100644 Source/ToggleKey.h diff --git a/Source/Player/Player.cpp b/Source/Player/Player.cpp index fd80d5a9..53ab2645 100644 --- a/Source/Player/Player.cpp +++ b/Source/Player/Player.cpp @@ -6,8 +6,9 @@ #include #include -#include "../World/World.h" +#include "../Input/Keyboard.h" +#include "../World/World.h" #include "../Renderer/RenderMaster.h" sf::Font f; @@ -74,9 +75,9 @@ ItemStack& Player::getHeldItems() } -void Player::handleInput(const sf::Window& window) +void Player::handleInput(const sf::Window& window, Keyboard& keyboard) { - keyboardInput(); + keyboardInput(keyboard); mouseInput(window); if(m_itemDown.isKeyPressed()) @@ -164,11 +165,7 @@ void Player::collide(World& world, const glm::vec3& vel, float dt) for (int y = position.y - box.dimensions.y; y < position.y + 0.7 ; y++) for (int z = position.z - box.dimensions.z; z < position.z + box.dimensions.z; z++) { - auto block = world.getBlock( - x, - y, - z - ); + auto block = world.getBlock(x, y, z); if (block != 0 && block.getData().isCollidable) { @@ -209,7 +206,7 @@ void Player::collide(World& world, const glm::vec3& vel, float dt) float speed = 0.2f; -void Player::keyboardInput() +void Player::keyboardInput(Keyboard& keyboard) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { diff --git a/Source/Player/Player.h b/Source/Player/Player.h index 0e0340ae..40ae191a 100644 --- a/Source/Player/Player.h +++ b/Source/Player/Player.h @@ -6,8 +6,9 @@ #include "../Entity.h" #include "../Item/ItemStack.h" -#include "../ToggleKey.h" +#include "../Input/ToggleKey.h" +class Keyboard; class World; class RenderMaster; @@ -16,7 +17,7 @@ class Player : public Entity public: Player(); - void handleInput(const sf::Window& window); + void handleInput(const sf::Window& window, Keyboard& keyboard); void update(float dt, World& wolrd); void collide(World& world, const glm::vec3& vel, float dt); @@ -30,7 +31,7 @@ class Player : public Entity private: void jump(); - void keyboardInput(); + void keyboardInput(Keyboard& keyboard); void mouseInput(const sf::Window& window); bool m_isOnGround = false; bool m_isFlying = false; diff --git a/Source/States/PlayingState.cpp b/Source/States/PlayingState.cpp index f45baa0d..c3c384da 100644 --- a/Source/States/PlayingState.cpp +++ b/Source/States/PlayingState.cpp @@ -17,11 +17,13 @@ StatePlaying::StatePlaying(Application& app, const Config& config) } void StatePlaying::handleEvent(sf::Event e) -{ } +{ + m_keyboard.update(e); +} void StatePlaying::handleInput() { - m_player.handleInput(m_pApplication->getWindow()); + m_player.handleInput(m_pApplication->getWindow(), m_keyboard); static sf::Clock timer; glm::vec3 lastPosition; diff --git a/Source/States/PlayingState.h b/Source/States/PlayingState.h index 01434225..2bac2a31 100644 --- a/Source/States/PlayingState.h +++ b/Source/States/PlayingState.h @@ -7,7 +7,7 @@ #include "../World/Chunk/Chunk.h" #include "../World/World.h" #include "../Util/FPSCounter.h" - +#include "../Input/Keyboard.h" class StatePlaying : public StateBase @@ -25,6 +25,7 @@ class StatePlaying : public StateBase void onOpen() override; private: + Keyboard m_keyboard; Player m_player; World m_world; diff --git a/Source/ToggleKey.cpp b/Source/ToggleKey.cpp deleted file mode 100644 index 7291ec1b..00000000 --- a/Source/ToggleKey.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "ToggleKey.h" - -ToggleKey::ToggleKey(sf::Keyboard::Key key) -: m_key (key) -{ } - -bool ToggleKey::isKeyPressed() -{ - if (m_delayTimer.getElapsedTime().asSeconds() > 0.2) - { - if (sf::Keyboard::isKeyPressed(m_key)) - { - m_delayTimer.restart(); - return true; - } - } - return false; -} diff --git a/Source/ToggleKey.h b/Source/ToggleKey.h deleted file mode 100644 index bbb8d20e..00000000 --- a/Source/ToggleKey.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TOGGLEKEY_H_INCLUDED -#define TOGGLEKEY_H_INCLUDED - -#include - -class ToggleKey -{ - public: - ToggleKey(sf::Keyboard::Key); - - bool isKeyPressed(); - - private: - sf::Keyboard::Key m_key; - sf::Clock m_delayTimer; - -}; - -#endif // TOGGLEKEY_H_INCLUDED diff --git a/Source/World/World.cpp b/Source/World/World.cpp index 6622095c..2f3f8661 100644 --- a/Source/World/World.cpp +++ b/Source/World/World.cpp @@ -5,7 +5,7 @@ #include "../Maths/Vector2XZ.h" #include "../Camera.h" -#include "../ToggleKey.h" +#include "../Input/ToggleKey.h" #include "../Renderer/RenderMaster.h" #include "../Player/Player.h" #include "../Util/Random.h" From 2598b8b53228fb0bd62afe76e82eba126601b93e Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Sun, 28 Jul 2019 17:02:47 +0100 Subject: [PATCH 8/8] Make player use the keyboard class --- Source/Application.cpp | 1 + Source/Player/Player.cpp | 12 ++++++------ Source/World/World.cpp | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Source/Application.cpp b/Source/Application.cpp index a341ecbb..d06d9ca4 100644 --- a/Source/Application.cpp +++ b/Source/Application.cpp @@ -52,6 +52,7 @@ void Application::handleEvents() sf::Event e; while (m_context.window.pollEvent(e)) { + m_states.back()->handleEvent(e); switch(e.type) { case sf::Event::Closed: diff --git a/Source/Player/Player.cpp b/Source/Player/Player.cpp index 53ab2645..5ab44af7 100644 --- a/Source/Player/Player.cpp +++ b/Source/Player/Player.cpp @@ -208,34 +208,34 @@ float speed = 0.2f; void Player::keyboardInput(Keyboard& keyboard) { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) + if (keyboard.isKeyDown(sf::Keyboard::W)) { float s = speed; if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) s *= 5; m_acceleration.x += -glm::cos(glm::radians(rotation.y + 90)) * s; m_acceleration.z += -glm::sin(glm::radians(rotation.y + 90)) * s; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) + if (keyboard.isKeyDown(sf::Keyboard::S)) { m_acceleration.x += glm::cos(glm::radians(rotation.y + 90)) * speed; m_acceleration.z += glm::sin(glm::radians(rotation.y + 90)) * speed; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) + if (keyboard.isKeyDown(sf::Keyboard::A)) { m_acceleration.x += -glm::cos(glm::radians(rotation.y)) * speed; m_acceleration.z += -glm::sin(glm::radians(rotation.y)) * speed; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) + if (keyboard.isKeyDown(sf::Keyboard::D)) { m_acceleration.x += glm::cos(glm::radians(rotation.y)) * speed; m_acceleration.z += glm::sin(glm::radians(rotation.y)) * speed; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) + if (keyboard.isKeyDown(sf::Keyboard::Space)) { jump(); } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) && m_isFlying) + else if (keyboard.isKeyDown(sf::Keyboard::LShift) && m_isFlying) { m_acceleration.y -= speed * 3; } diff --git a/Source/World/World.cpp b/Source/World/World.cpp index 2f3f8661..38e0f00b 100644 --- a/Source/World/World.cpp +++ b/Source/World/World.cpp @@ -18,7 +18,7 @@ World::World(const Camera& camera, const Config& config, Player& player) setSpawnPoint(); player.position = m_playerSpawnPoint; - for (int i = 0; i < 2; i++) + for (int i = 0; i < 1; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(50)); m_chunkLoadThreads.emplace_back([&]()