Skip to content

Commit

Permalink
Changed game logic and preparations for collissions (AAAAAAAAAAAAAAAA…
Browse files Browse the repository at this point in the history
…AAAAAAAAAAAAAAAAAAAAA)
  • Loading branch information
Jukitsu committed Nov 26, 2023
1 parent b03e739 commit bd1f759
Showing 17 changed files with 242 additions and 35 deletions.
6 changes: 6 additions & 0 deletions Jukcraft/Jukcraft.vcxproj
Original file line number Diff line number Diff line change
@@ -158,6 +158,10 @@
<ClInclude Include="src\core\Log.h" />
<ClInclude Include="src\core\Window.h" />
<ClInclude Include="src\core\util.h" />
<ClInclude Include="src\entity\Entity.h" />
<ClInclude Include="src\entity\Mob.h" />
<ClInclude Include="src\entity\player\Player.h" />
<ClInclude Include="src\models\Collider.h" />
<ClInclude Include="src\pch.h" />
<ClInclude Include="src\renderer\gfx\buffers\StagingBuffer.h" />
<ClInclude Include="src\renderer\Renderer.h" />
@@ -180,6 +184,8 @@
<ClCompile Include="src\core\HitRay.cpp" />
<ClCompile Include="src\core\Log.cpp" />
<ClCompile Include="src\core\Window.cpp" />
<ClCompile Include="src\entity\Mob.cpp" />
<ClCompile Include="src\entity\player\Player.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
6 changes: 6 additions & 0 deletions Jukcraft/Jukcraft.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -101,6 +101,10 @@
<Filter>world\chunk</Filter>
</ClInclude>
<ClInclude Include="src\renderer\gfx\buffers\StagingBuffer.h" />
<ClInclude Include="src\entity\Entity.h" />
<ClInclude Include="src\entity\player\Player.h" />
<ClInclude Include="src\entity\Mob.h" />
<ClInclude Include="src\models\Collider.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\core\App.cpp">
@@ -139,5 +143,7 @@
<Filter>world\chunk</Filter>
</ClCompile>
<ClCompile Include="src\renderer\gfx\buffers\DynamicBuffer.cpp" />
<ClCompile Include="src\entity\player\Player.cpp" />
<ClCompile Include="src\entity\Mob.cpp" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions Jukcraft/src/core/App.cpp
Original file line number Diff line number Diff line change
@@ -44,13 +44,25 @@ namespace Jukcraft {

void App::run() {
float lastTime = static_cast<float>(glfwGetTime());
float lastTime2 = lastTime;
while (!window->shouldClose()) {
const float time = static_cast<float>(glfwGetTime());
const float deltaTime = time - lastTime;
lastTime = time;

game->tick(deltaTime);

lastTime2 = lastTime;
while (glfwGetTime() - lastTime < 1.0f / TICK_RATE) {
const float time2 = static_cast<float>(glfwGetTime());
const float deltaTime2 = time2 - lastTime2;
lastTime2 = time2;
game->renderNewFrame(deltaTime2);

window->endFrame();
}


window->endFrame();
}
}
32 changes: 18 additions & 14 deletions Jukcraft/src/core/Camera.cpp
Original file line number Diff line number Diff line change
@@ -4,12 +4,12 @@
#include <GLFW/glfw3.h>
#include "core/App.h"

static constexpr float speed = 7.0f;
static constexpr float WALKING_SPEED = 4.317f;
static constexpr float sensitivity = 0.005f;

namespace Jukcraft {
Camera::Camera(gfx::Shader& shader, const glm::vec3& position, float yaw, float pitch)
:shader(shader), position(position), yaw(yaw), pitch(pitch) {
Camera::Camera(gfx::Shader& shader, Player& player)
:shader(shader), player(player), speed(WALKING_SPEED) {
ubo.allocate(sizeof(ShaderCameraData), nullptr, true);
mappedUbo = reinterpret_cast<ShaderCameraData*>(ubo.map(0, sizeof(ShaderCameraData)));

@@ -24,8 +24,8 @@ namespace Jukcraft {
lastCursorPos = cursorPos;


yaw = yaw + deltaCursor.x * sensitivity, 2.0f * glm::pi<float>();
pitch = glm::clamp(pitch - deltaCursor.y * sensitivity, -glm::pi<float>() / 2, glm::pi<float>() / 2);
player.yaw = player.yaw + deltaCursor.x * sensitivity, 2.0f * glm::pi<float>();
player.pitch = glm::clamp(player.pitch - deltaCursor.y * sensitivity, -glm::pi<float>() / 2, glm::pi<float>() / 2);
// It is negative because the origin is in the top left corner, not bottom left

if (!App::Get().isMouseCaptured())
@@ -39,14 +39,18 @@ namespace Jukcraft {
if (App::GetWindow().isKeyPressed(GLFW_KEY_LEFT_SHIFT)) input.y += -1;
if (App::GetWindow().isKeyPressed(GLFW_KEY_SPACE)) input.y += 1;

if (deltaTime * 20.0f > 1.0f) speed = WALKING_SPEED;

const float multiplier = speed * deltaTime;
else
speed += (WALKING_SPEED - speed) * deltaTime * 20;

const float multiplier = speed;
if (input.y) {
position.y += input.y * multiplier;
player.velocity.y = input.y * multiplier;
} if (input.x || input.z) {
const float angle = yaw - glm::atan<float>((float)input.z, (float)input.x) + glm::pi<float>() / 2;
position.x += glm::cos(angle) * multiplier;
position.z += glm::sin(angle) * multiplier;
const float angle = player.yaw - glm::atan<float>((float)input.z, (float)input.x) + glm::pi<float>() / 2;
player.velocity.x = glm::cos(angle) * multiplier;
player.velocity.z = glm::sin(angle) * multiplier;
}


@@ -56,13 +60,13 @@ namespace Jukcraft {
0.1f,
500.0f
);
glm::mat4 view = glm::rotate(glm::mat4(1.0f), pitch, -glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, yaw + glm::pi<float>() / 2, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -position);
glm::mat4 view = glm::rotate(glm::mat4(1.0f), player.pitch, -glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, player.yaw + glm::pi<float>() / 2, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -player.position);

mappedUbo->transform = proj * view;

mappedUbo->pos = glm::vec4(position, 1.0f);
mappedUbo->pos = glm::vec4(player.position, 1.0f);
ubo.flush(0, sizeof(ShaderCameraData));
}
}
14 changes: 5 additions & 9 deletions Jukcraft/src/core/Camera.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
#pragma once
#include "renderer/Renderer.h"

#include "entity/player/Player.h"
namespace Jukcraft {

class Camera {
public:
Camera(gfx::Shader& shader, const glm::vec3& position, float yaw, float pitch);
Camera(gfx::Shader& shader, Player& player);
void update(const float delta_time);
void onMouseMove(float x, float y);

constexpr const glm::vec3& getPos() const { return position; }
constexpr float getYaw() const { return yaw; }
constexpr float getPitch() const { return pitch; }
private:
gfx::Buffer ubo;
gfx::Shader& shader;
glm::vec3 position;
float yaw, pitch;

glm::vec2 lastCursorPos;
Player& player;

float speed;

ShaderCameraData *mappedUbo;

10 changes: 7 additions & 3 deletions Jukcraft/src/core/Game.cpp
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
namespace Jukcraft {

Game::Game()
:shader("assets/shaders/terrain/vert.glsl", "assets/shaders/terrain/frag.glsl"), camera(shader, glm::vec3(5.0f, 70.0f, 10.0f), glm::pi<float>() / 2.0f, 0.0f), textureManager(16) {
:shader("assets/shaders/terrain/vert.glsl", "assets/shaders/terrain/frag.glsl"), player(glm::vec3(5.0f, 70.0f, 10.0f), glm::vec3(0.0f), glm::vec3(0.0f), glm::pi<float>() / 2.0f, 0.0f), camera(shader, player), textureManager(16) {
textureManager.pushSubTexture("assets/textures/stone.png");
textureManager.pushSubTexture("assets/textures/grass.png");
textureManager.pushSubTexture("assets/textures/grass_side.png");
@@ -28,7 +28,7 @@ namespace Jukcraft {

void Game::onMousePress(int button) {

HitRay hitray(*world, camera);
HitRay hitray(*world, player);
World& w = *world;
while (hitray.distance < HIT_RANGE)
if (hitray.step(button,
@@ -54,8 +54,12 @@ namespace Jukcraft {


void Game::tick(const float deltaTime) {
world->tick(deltaTime);
world->tick(deltaTime);
}

void Game::renderNewFrame(const float deltaTime) {
camera.update(deltaTime);
player.tick(deltaTime);

Renderer::Begin(world->getSkyColor());
world->render();
4 changes: 4 additions & 0 deletions Jukcraft/src/core/Game.h
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
#include "core/Camera.h"
#include "world/World.h"
#include "blocks/Block.h"
#include "entity/player/Player.h"

namespace Jukcraft {

@@ -10,6 +11,7 @@ namespace Jukcraft {
Game();
void hitCallback(int button, const glm::vec3& currentBlock, const glm::vec3& nextBlock);
void tick(const float deltaTime);
void renderNewFrame(const float deltaTime);
void onMousePress(int button);
void speedTime() {
if (inWorld) {
@@ -24,6 +26,8 @@ namespace Jukcraft {
BlockID holding = 5;
bool inWorld = true;

Player player;

std::vector<Block> blocks;
};

10 changes: 5 additions & 5 deletions Jukcraft/src/core/HitRay.cpp
Original file line number Diff line number Diff line change
@@ -3,12 +3,12 @@


namespace Jukcraft {
HitRay::HitRay(World& world, Camera& camera)
:world(world), position(camera.getPos() - glm::vec3(0.5f)), block((glm::ivec3)glm::round(camera.getPos() - glm::vec3(0.5f))), distance(0.0f) {
HitRay::HitRay(World& world, Entity& entity)
:world(world), position(entity.getPos() - glm::vec3(0.5f)), block((glm::ivec3)glm::round(entity.getPos() - glm::vec3(0.5f))), distance(0.0f) {
vector = glm::vec3(
glm::cos(camera.getYaw()) * glm::cos(camera.getPitch()),
glm::sin(camera.getPitch()),
glm::sin(camera.getYaw()) * glm::cos(camera.getPitch())
glm::cos(entity.getYaw()) * glm::cos(entity.getPitch()),
glm::sin(entity.getPitch()),
glm::sin(entity.getYaw()) * glm::cos(entity.getPitch())
);
}

2 changes: 1 addition & 1 deletion Jukcraft/src/core/HitRay.h
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ namespace Jukcraft {
using HitCallback = std::function<void(int, const glm::vec3&, const glm::vec3&)>;

struct HitRay {
HitRay(World& world, Camera& camera);
HitRay(World& world, Entity& entity);
bool check(int button, HitCallback callback,
float distance, const glm::vec3& currentBlock, const glm::vec3& nextBlock);
bool step(int button, HitCallback callback);
2 changes: 2 additions & 0 deletions Jukcraft/src/core/util.h
Original file line number Diff line number Diff line change
@@ -51,6 +51,8 @@ namespace Jukcraft {
constexpr glm::vec3 SOUTH = glm::vec3(0.0f, 0.0f, 1.0f);
constexpr glm::vec3 NORTH = glm::vec3(0.0f, 0.0f, -1.0f);

constexpr float TICK_RATE = 60.0f;

constexpr glm::vec3 DIRECTIONS[6] = {
EAST, WEST, UP, DOWN, SOUTH, NORTH
};
30 changes: 30 additions & 0 deletions Jukcraft/src/entity/Entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

namespace Jukcraft {
class Entity {
public:
Entity() {

}
Entity(const glm::vec3& initialPos, const glm::vec3& initialVelocity,
const glm::vec3& initialAcceleration, float initialYaw, float initialPitch)
:position(initialPos), velocity(initialVelocity), acceleration(initialAcceleration),
yaw(initialYaw), pitch(initialPitch) {

}
virtual ~Entity() {}

virtual void tick(float delta_time) = 0;

constexpr const glm::vec3& getPos() const { return position; }
constexpr const glm::vec3& getVelocity() const { return position; }
constexpr const glm::vec3& getAccel() const { return position; }
constexpr float getYaw() const { return yaw; }
constexpr float getPitch() const { return pitch; }
protected:
glm::vec3 position;
glm::vec3 velocity;
glm::vec3 acceleration;
float yaw, pitch;
};
}
2 changes: 2 additions & 0 deletions Jukcraft/src/entity/Mob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "pch.h"
#include "models/Collider.h"
33 changes: 33 additions & 0 deletions Jukcraft/src/entity/Mob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include "entity/Entity.h"
#include "models/Collider.h"

namespace Jukcraft {
class Mob : public Entity {
public:
Mob(const glm::vec3& initialPos = glm::vec3(0.0f), const glm::vec3& initialVelocity = glm::vec3(0.0f),
const glm::vec3& initialAcceleration = glm::vec3(0.0f), float initialYaw = 0.0f, float initialPitch = 0.0f)
:Entity(initialPos, initialVelocity, initialAcceleration, initialYaw, initialPitch),
width(0.6f), height(1.8f), collider(){

}
virtual ~Mob() {

}
void updateCollider() {
collider.vx1 = position - glm::vec3(width / 2.0f, 0, width / 2.0f);
collider.vx2 = position + glm::vec3(width / 2.0f, height, width / 2.0f);
}

void tick(float delta_time) override {
updateCollider();

acceleration += velocity * delta_time;
position += velocity * delta_time;
}
protected:
float width, height;

Collider collider;
};
}
15 changes: 15 additions & 0 deletions Jukcraft/src/entity/player/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "pch.h"
#include "entity/player/Player.h"

namespace Jukcraft {
Player::Player(const glm::vec3& initialPos, const glm::vec3& initialVelocity,
const glm::vec3& initialAcceleration, float initialYaw, float initialPitch)
:Mob(initialPos, initialVelocity, initialAcceleration, initialYaw, initialPitch) {


}
Player::~Player() {

}

}
14 changes: 14 additions & 0 deletions Jukcraft/src/entity/player/Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "entity/Mob.h"

namespace Jukcraft {
class Player : public Mob {
public:
Player(const glm::vec3& initialPos = glm::vec3(0.0f), const glm::vec3& initialVelocity = glm::vec3(0.0f),
const glm::vec3& initialAcceleration = glm::vec3(0.0f), float initialYaw = 0.0f, float initialPitch = 0.0f);
virtual ~Player();


friend class Camera;
};
}
Loading

0 comments on commit bd1f759

Please sign in to comment.