Skip to content

Commit

Permalink
Kyoto update
Browse files Browse the repository at this point in the history
HAH look at this monke
  • Loading branch information
Jukitsu committed Aug 5, 2024
1 parent c119288 commit e0b6da6
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 79 deletions.
9 changes: 7 additions & 2 deletions Jukcraft/assets/shaders/entity/frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ layout(early_fragment_tests) in;

layout(location = 0) in VS_OUT {
vec2 v_TexCoords;
float v_Shading;
} fs_In;


layout(location = 0) uniform vec4 u_Overlay;
layout(binding = 1) uniform sampler2D u_TextureSampler;

layout(location = 0) out vec4 fragColor;

void main(void) {
vec4 texel = texture(u_TextureSampler, fs_In.v_TexCoords);
fragColor = texel;
vec4 color = texture(u_TextureSampler, fs_In.v_TexCoords);
color.rgb = mix(u_Overlay.rgb, color.rgb, u_Overlay.a);
color.rgb *= fs_In.v_Shading;
fragColor = color;
}
8 changes: 6 additions & 2 deletions Jukcraft/assets/shaders/entity/vert.glsl
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#version 460 core

layout(location = 0) in vec3 a_VertexPos;
layout(location = 0) in vec3 a_Pos;
layout(location = 1) in vec2 a_TexUV;
layout(location = 2) in vec3 a_Normal;

layout(location = 0) out VS_OUT {
vec2 v_TexCoords;
float v_Shading;
} vs_Out;

layout(std140, binding = 0) uniform u_Camera {
mat4 u_CameraTransforms;
vec4 u_CameraPos;
};

const vec3 c_Sunlight = normalize(vec3(0.0, 2.0, 1.0));

void main(void) {
gl_Position = u_CameraTransforms * vec4(a_VertexPos, 1.0f);
gl_Position = u_CameraTransforms * vec4(a_Pos, 1.0f);
vs_Out.v_TexCoords = a_TexUV;
vs_Out.v_Shading = max(0.4f, (1. + dot(abs(a_Normal), c_Sunlight)) / 2.0f);
}
22 changes: 14 additions & 8 deletions Jukcraft/src/core/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ namespace Jukcraft {
0.1f,
500.0f
);
#ifndef JUK_DEBUG
glm::mat4 view = glm::rotate(glm::mat4(1.0f), player->getPitch(), -glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, player->getYaw() + glm::pi<float>() / 2, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -interpolatedPos - glm::vec3(0, player->getEyeLevel(), 0));
#else


glm::mat4 view = glm::mat4(1.0f);
view = glm::rotate(view, glm::pi<float>() / 2 , glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -glm::vec3(0.0f, 65.0f, 32.0f) - glm::vec3(0, player->getEyeLevel(), 0));
#endif

if (isFirstPerson) {
view = glm::rotate(view, player->getPitch(), -glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, player->getYaw() + glm::pi<float>() / 2, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -interpolatedPos - glm::vec3(0, player->getEyeLevel(), 0));
}
else {
view = glm::rotate(view, glm::pi<float>() / 2, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::translate(view, -glm::vec3(0.0f, 65.0f, 32.0f) - glm::vec3(0, player->getEyeLevel(), 0));
}


mappedUbo->transform = proj * view;

mappedUbo->pos = glm::vec4(player->position, 1.0f);
Expand Down
2 changes: 2 additions & 0 deletions Jukcraft/src/core/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Jukcraft {
public:
Camera(gfx::Shader& shader, Auto<Player>& player);
void update(const float delta_time);
bool isFirstPerson = true;
private:
gfx::Buffer ubo;
gfx::Shader& shader;
Expand All @@ -18,6 +19,7 @@ namespace Jukcraft {

ShaderCameraData *mappedUbo;


FORBID_COPY(Camera);
FORBID_MOVE(Camera);
};
Expand Down
12 changes: 9 additions & 3 deletions Jukcraft/src/core/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ namespace Jukcraft {
case GLFW_KEY_LEFT_CONTROL:
player->dash();
break;
case GLFW_KEY_F5:
camera.isFirstPerson = !camera.isFirstPerson;
break;
}
}
void Game::hitCallback(int button, const BlockPos& currentBlock, const BlockPos& nextBlock) {
Expand Down Expand Up @@ -79,9 +82,12 @@ namespace Jukcraft {

Renderer::Begin(world->getSkyColor());
world->render(partialTicks);
#ifdef JUK_DEBUG
world->mobRenderer.render(*player, partialTicks);
#endif

if (!camera.isFirstPerson) {
world->mobRenderer.render(*player, partialTicks);
}


Renderer::End();
}
}
4 changes: 3 additions & 1 deletion Jukcraft/src/entity/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ namespace Jukcraft {
virtual void tick() = 0;
virtual void applyPhysics() = 0;
virtual void aiStep() = 0;
virtual void handleRotation() {}
virtual void tickRotations() {}
virtual void move(const glm::vec3& motion) = 0;
virtual void push(const glm::vec3& motion) = 0;
virtual void render(float partialTicks) {}
virtual void hurt(float amount, const glm::vec3& knockback) = 0;


constexpr const glm::vec3& getPos() const { return position; }
constexpr const glm::vec3& getVelocity() const { return velocity; }
Expand Down
49 changes: 32 additions & 17 deletions Jukcraft/src/entity/LivingEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,11 @@ namespace Jukcraft {
velocity.y = glm::sqrt(-2 * g.y * hmax);
}

void LivingEntity::handleRotation() {
void LivingEntity::tickRotations() {
glm::vec3 ds = position - old.position;
float g = 0.0f;
float nextBodyYaw = headRot.x;
float ortho2 = ds.x * ds.x + ds.z * ds.z;
if (ortho2 > 0.0f) {
g = glm::sqrt(ortho2);
nextBodyYaw = glm::atan<float>(ds.z, ds.x);
}
if (animationTicks.attack > 0.0F) {
Expand All @@ -137,12 +135,6 @@ namespace Jukcraft {
bodyRot.x += relativeAngle - glm::sign(relativeAngle) * glm::pi<float>() / 3.0f;
}

if (glm::abs(relativeAngle) > glm::pi<float>() / 2) {
g *= -1.0F;
}

animationTicks.step = g;

while (bodyRot.x - old.bodyRot.x < -glm::pi<float>()) {
old.bodyRot.x -= 2.0f * glm::pi<float>();
}
Expand All @@ -167,24 +159,47 @@ namespace Jukcraft {
old.velocity = velocity;

age++;

iframes = glm::max(0, iframes - 1);

aiStep();
handleRotation();
tickRotations();

walkAnimation.update(glm::min(4.0f * glm::length(position - old.position), 1.0f), 0.4f);
walkAnimation.update(glm::min(glm::length(position - old.position) * 4.0f,
1.0f), //TICK_RATE / 20.0f),
0.4f);

if (health.hp <= 0.0f || deathTime > 0) {
die();
}
}

void LivingEntity::die() {
deathTime++;
setInput(glm::vec3(0.0f));
}

void LivingEntity::hurt(float amount, const glm::vec3& knockback) {
bool success = health.hurt(amount, iframes > 0);
velocity += (float)(success) * knockback / (iframes > 0 ? 2.0f : 1.0f);
iframes = iframes > 0 ? iframes : glm::floor(TICK_RATE / 2);
if (health.hp <= 0.0f) {
die();
}
}

void LivingEntity::aiStep() {
applyPhysics();
checkInjury();
health.healNatural();
stamina = glm::min(stamina + 1.0f / TICK_RATE, 1.0f);
}

void LivingEntity::checkInjury() {
float unabsorbedEnergy = getKineticEnergy() - getMaxKineticEnergy();
if (unabsorbedEnergy > 0.0f) {
health = glm::max(0.0f, health - unabsorbedEnergy * TICK_RATE * TICK_RATE);
hurt(health.hp - unabsorbedEnergy * TICK_RATE * TICK_RATE, glm::vec3(0.0f));
consumeInertia();
}
health = glm::min(health + 0.25f / TICK_RATE, 20.0f);
stamina = glm::min(stamina + 1.0f / TICK_RATE, 1.0f);


}

void LivingEntity::applyPhysics() {
Expand Down
41 changes: 38 additions & 3 deletions Jukcraft/src/entity/LivingEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ namespace Jukcraft {

class World;

struct Health {
float oldHp = 20.0f;
float hp = 20.0f;
float energy = 1.0f;

void healNatural() {
// hp = glm::min(hp + 0.25f / TICK_RATE, 20.0f);

}

bool hurt(float amount, bool isIframe) {
if (!isIframe) {
oldHp = hp;
hp -= amount;
}
else if (amount > oldHp - hp) {
hp = oldHp - amount;
}
else {
return false;
}
return true;

}
};

class LivingEntity : public Entity {
public:
LivingEntity(World& world, const glm::vec3& initialPos = glm::vec3(0.0f),
Expand All @@ -23,7 +49,9 @@ namespace Jukcraft {
void aiStep() override;
void tick() override;
void applyPhysics() override;
void handleRotation() override;
void tickRotations() override;
void hurt(float amount, const glm::vec3& knockback) override;

void move(const glm::vec3& motion) override;
void push(const glm::vec3& motion) override;

Expand All @@ -40,13 +68,17 @@ namespace Jukcraft {
constexpr const auto& getOld() const { return old; }
constexpr const auto& getAnimationTicks() const { return animationTicks; }
constexpr const auto& getWalkAnimation() const { return walkAnimation; }
constexpr bool isHurt() const { return iframes > 0 || deathTime > 0; }
constexpr int getDeathTime() const { return deathTime; }

void setHeadYaw(float theta) { headRot.x = wrapRadians(theta); }
void setHeadPitch(float phi) { headRot.y = glm::clamp(phi, -glm::pi<float>() / 2, glm::pi<float>() / 2); }
void setBodyYaw(float theta) { bodyRot.x = wrapRadians(theta);}

void consumeInertia();
void die();
protected:
void checkInjury();
constexpr const glm::vec3& getFriction() const {
if (onGround)
return FRICTION;
Expand All @@ -61,10 +93,13 @@ namespace Jukcraft {
glm::vec2 bodyRot;
glm::vec2 headRot;

int iframes = 0;
int deathTime = 0;

float width, height;

float health = 20.0f;
float stamina = 0.0f;
Health health;
float stamina = 1.0f;
glm::vec3 inertia = glm::vec3(0.0f);

size_t age = 0;
Expand Down
50 changes: 28 additions & 22 deletions Jukcraft/src/entity/Mob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,40 @@ namespace Jukcraft {
}

void Mob::aiStep() {
std::uniform_int_distribution<std::mt19937::result_type> uniformDist2(0, 100);
if (uniformDist2(rng) > 99) {
std::uniform_int_distribution<std::mt19937::result_type> uniformDist(0, 12);
float dx = (int)uniformDist(rng) - 6;
float dz = (int)uniformDist(rng) - 6;

if (dx != 0 && dz != 0) {
setYaw(wrapRadians(glm::atan(dz, dx) - glm::pi<float>() / 2));

if (deathTime <= 0) {
std::uniform_int_distribution<std::mt19937::result_type> uniformDist2(0, 100);
if (uniformDist2(rng) > 99) {
std::uniform_int_distribution<std::mt19937::result_type> uniformDist(0, 12);
float dx = (int)uniformDist(rng) - 6;
float dz = (int)uniformDist(rng) - 6;

if (dx != 0 && dz != 0) {
setYaw(wrapRadians(glm::atan(dz, dx) - glm::pi<float>() / 2));
}
}
}

float relativeAngle = wrapRadians(rotation.x - headRot.x);
float relativeAngle = wrapRadians(rotation.x - headRot.x);

if (glm::abs(relativeAngle) > 0) {
headRot.x += relativeAngle * 2.0f / TICK_RATE;
}
if (glm::abs(relativeAngle) > 0) {
headRot.x += relativeAngle * 2.0f / TICK_RATE;
}

if (1) {
setInput(glm::vec3(0.0f, 0.0f, 1.0f));
if (1) {
setInput(glm::vec3(0.0f, 0.0f, 1.0f));
}

if (App::GetWindow().isKeyPressed(GLFW_KEY_LEFT_SHIFT) && onGround) {
hurt(19.9f, glm::vec3(
-20 / TICK_RATE * glm::cos(rotation.x),
10 / TICK_RATE,
-20 / TICK_RATE * glm::sin(rotation.x)
));
}

// LOG_INFO("{}", health.hp);
}

if (App::GetWindow().isKeyPressed(GLFW_KEY_LEFT_SHIFT) && onGround) {
velocity += glm::vec3(
-20 / TICK_RATE * glm::cos(rotation.x),
10 / TICK_RATE,
-20 / TICK_RATE * glm::sin(rotation.x)
);
}



Expand Down
2 changes: 0 additions & 2 deletions Jukcraft/src/entity/player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace Jukcraft {
if (dashing && onGround) {
dashing = false;
}

fov = glm::radians(70.0f + glm::length(velocity) * TICK_RATE);

}

Expand Down
5 changes: 5 additions & 0 deletions Jukcraft/src/models/entity/EntityModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ namespace Jukcraft {
};

for (int i = 0; i < 6; ++i) {
glm::vec3 normal = glm::normalize(glm::cross(
transformedVertices[i * 4] - transformedVertices[i * 4 + 1],
transformedVertices[i * 4] - transformedVertices[i * 4 + 2]
));
for (int j = 0; j < 4; ++j) {
quads[i].vertices[j].pos = transformedVertices[i * 4 + j];
quads[i].vertices[j].texUV = texCoords[i][j];
quads[i].vertices[j].normal = normal;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Jukcraft/src/models/entity/EntityModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Jukcraft {
struct Vertex {
glm::vec3 pos;
glm::vec2 texUV;
glm::vec3 normal;
};

struct Quad {
Expand Down
Loading

0 comments on commit e0b6da6

Please sign in to comment.