Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopson97 committed Jul 28, 2019
2 parents 5774448 + 2598b8b commit 05698d8
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 66 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#Visual Studio
Debug/
Release/

*.sln
*.vcxproj
*.filters
*.users
*.user

*.dll
.vs

# Visual Studio Code
.vscode/
Expand Down
1 change: 1 addition & 0 deletions Source/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions Source/Input/Keyboard.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions Source/Input/Keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>

#include <array>

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<bool, sf::Keyboard::KeyCount> m_keys;
sf::Keyboard::Key m_recentlyReleased;
};
File renamed without changes.
File renamed without changes.
4 changes: 1 addition & 3 deletions Source/Maths/NoiseGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
23 changes: 12 additions & 11 deletions Source/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include <sstream>
#include <iomanip>

#include "../World/World.h"

#include "../Input/Keyboard.h"
#include "../World/World.h"
#include "../Renderer/RenderMaster.h"

sf::Font f;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -205,36 +206,36 @@ 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))
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;
}
Expand All @@ -255,7 +256,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;

Expand Down
7 changes: 4 additions & 3 deletions Source/Player/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

#include "../Entity.h"
#include "../Item/ItemStack.h"
#include "../ToggleKey.h"
#include "../Input/ToggleKey.h"

class Keyboard;
class World;
class RenderMaster;

Expand All @@ -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);
Expand All @@ -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;
Expand Down
26 changes: 8 additions & 18 deletions Source/States/PlayingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,27 @@ 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)
{ }
{
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;

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 = ray.getEnd().x;
int y = ray.getEnd().y;
int z = ray.getEnd().z;
int x = static_cast<int>(ray.getEnd().x);
int y = static_cast<int>(ray.getEnd().y);
int z = static_cast<int>(ray.getEnd().z);

auto block = m_world.getBlock(x, y, z);
auto id = (BlockId)block.id;
Expand Down Expand Up @@ -71,15 +65,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)
Expand All @@ -97,7 +88,6 @@ void StatePlaying::render(RenderMaster& renderer)
if (drawGUI)
{
m_fpsCounter.draw(renderer);
renderer.drawSFML(m_crosshair);
m_player.draw(renderer);
}

Expand Down
6 changes: 2 additions & 4 deletions Source/States/PlayingState.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,12 +25,10 @@ class StatePlaying : public StateBase
void onOpen() override;

private:
Keyboard m_keyboard;
Player m_player;
World m_world;

sf::RectangleShape m_crosshair;
sf::Texture m_chTexture;

FPSCounter m_fpsCounter;
};

Expand Down
4 changes: 2 additions & 2 deletions Source/Texture/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ std::array<GLfloat, 8> 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;
Expand Down
9 changes: 6 additions & 3 deletions Source/Util/Random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned>(std::time(nullptr))
);
for (int i = 0; i < 5; i++) {
intInRange(i, i * 5);
}
}
19 changes: 13 additions & 6 deletions Source/World/Event/PlayerDigEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(m_digSpot.x),
static_cast<int>(m_digSpot.z)
);

if (world.getChunkManager().chunkLoadedAt(chunkLocation.x, chunkLocation.z))
{
dig(world);
Expand All @@ -21,10 +25,13 @@ void PlayerDigEvent::handle(World& world)

void PlayerDigEvent::dig(World& world)
{
int x = static_cast<int>(m_digSpot.x);
int y = static_cast<int>(m_digSpot.y);
int z = static_cast<int>(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);
/*
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Expand Down
Loading

0 comments on commit 05698d8

Please sign in to comment.