8 Commits

Author SHA1 Message Date
d3467418c7 Merge branch 'main' of github.com:Persson-dev/Blitz 2024-03-05 09:01:15 +01:00
d98939cb31 remove useless add_requires 2024-03-05 08:39:24 +01:00
0d039caa0e Merge branch 'weirdshoot' 2024-03-05 08:38:34 +01:00
617e1fa95e branchless 2024-01-28 19:09:30 +01:00
73b4539e8d add gun animation 2024-01-28 18:30:16 +01:00
de3ff4326a CooldownTimer to template 2024-01-28 18:29:55 +01:00
7cf93fa16f add Rotate function 2024-01-27 18:34:14 +01:00
d57195cef2 fix gun model 2024-01-27 17:42:49 +01:00
10 changed files with 221 additions and 170 deletions

View File

@@ -2,143 +2,143 @@
# www.blender.org # www.blender.org
mtllib fingergun.mtl mtllib fingergun.mtl
o Cube o Cube
v 0.285220 -0.306557 -0.369022 v 0.28522 -0.306557 -0.869022
v 0.322362 -0.317165 -0.261062 v 0.322362 -0.317165 -0.761062
v 0.343467 -0.302403 -0.385416 v 0.343467 -0.302403 -0.885416
v 0.368924 -0.313485 -0.273896 v 0.368924 -0.313485 -0.773896
v 0.290013 -0.112715 -0.288332 v 0.290013 -0.112715 -0.788332
v 0.323476 -0.173525 -0.215829 v 0.323476 -0.173525 -0.715829
v 0.352603 -0.107768 -0.305584 v 0.352603 -0.107768 -0.805584
v 0.370039 -0.169845 -0.228663 v 0.370039 -0.169845 -0.728663
v 0.313472 -0.282474 -0.480820 v 0.313472 -0.282474 -0.98082
v 0.305899 -0.094307 -0.466123 v 0.305899 -0.094307 -0.966123
v 0.250690 -0.288091 -0.463829 v 0.25069 -0.288091 -0.963829
v 0.248265 -0.098863 -0.450236 v 0.248265 -0.098863 -0.950236
v 0.315042 -0.255677 -0.241247 v 0.315042 -0.255677 -0.741247
v 0.353448 -0.222605 -0.366318 v 0.353448 -0.222605 -0.866318
v 0.376243 -0.250840 -0.258116 v 0.376243 -0.25084 -0.758116
v 0.275133 -0.228795 -0.344732 v 0.275133 -0.228795 -0.844732
v 0.306822 -0.188042 -0.497506 v 0.306822 -0.188042 -0.997506
v 0.242783 -0.193417 -0.479751 v 0.242783 -0.193417 -0.979751
v 0.328963 -0.094936 -0.387948 v 0.328963 -0.094936 -0.887948
v 0.266060 -0.099642 -0.370998 v 0.26606 -0.099642 -0.870998
v 0.256312 -0.294010 -0.429786 v 0.256312 -0.29401 -0.929786
v 0.325556 -0.290373 -0.451170 v 0.325556 -0.290373 -0.95117
v 0.331511 -0.201707 -0.445538 v 0.331511 -0.201707 -0.945538
v 0.249474 -0.208163 -0.425103 v 0.249474 -0.208163 -0.925103
v 0.315600 -0.212794 -0.226928 v 0.3156 -0.212794 -0.726928
v 0.356824 -0.163947 -0.337251 v 0.356824 -0.163947 -0.837251
v 0.306541 -0.136889 -0.488441 v 0.306541 -0.136889 -0.988441
v 0.376801 -0.207956 -0.243797 v 0.376801 -0.207956 -0.743797
v 0.278508 -0.170136 -0.315665 v 0.278508 -0.170136 -0.815665
v 0.243140 -0.141900 -0.470965 v 0.24314 -0.1419 -0.970965
v 0.334727 -0.146782 -0.418122 v 0.334727 -0.146782 -0.918122
v 0.253545 -0.153198 -0.395745 v 0.253545 -0.153198 -0.895745
v 0.376243 -0.285911 -0.268173 v 0.376243 -0.285911 -0.768173
v 0.274555 -0.274639 -0.358509 v 0.274555 -0.274639 -0.858509
v 0.244437 -0.249179 -0.468971 v 0.244437 -0.249179 -0.968971
v 0.315042 -0.290748 -0.251303 v 0.315042 -0.290748 -0.751303
v 0.353299 -0.268056 -0.379890 v 0.353299 -0.268056 -0.87989
v 0.312528 -0.243154 -0.486300 v 0.312528 -0.243154 -0.9863
v 0.246945 -0.259562 -0.429467 v 0.246945 -0.259562 -0.929467
v 0.333607 -0.251364 -0.452175 v 0.333607 -0.251364 -0.952175
v 0.285002 -0.111885 -0.547600 v 0.285002 -0.111885 -1.0476
v 0.284624 -0.075309 -0.538484 v 0.284624 -0.075309 -1.03848
v 0.240924 -0.078763 -0.526438 v 0.240924 -0.078763 -1.02644
v 0.241302 -0.115339 -0.535554 v 0.241302 -0.115339 -1.03555
v 0.265142 -0.095745 -0.599045 v 0.265142 -0.095745 -1.09905
v 0.264710 -0.065690 -0.591995 v 0.26471 -0.06569 -1.092
v 0.231536 -0.068311 -0.582851 v 0.231536 -0.068311 -1.08285
v 0.231968 -0.098367 -0.589901 v 0.231968 -0.098367 -1.0899
v 0.245138 -0.079879 -0.644839 v 0.245138 -0.079879 -1.14484
v 0.244853 -0.060082 -0.640196 v 0.244853 -0.060082 -1.1402
v 0.223002 -0.061809 -0.634173 v 0.223002 -0.061809 -1.13417
v 0.223287 -0.081606 -0.638816 v 0.223287 -0.081606 -1.13882
v 0.238589 -0.126121 -0.543036 v 0.238589 -0.126121 -1.04304
v 0.238302 -0.156446 -0.552771 v 0.238302 -0.156446 -1.05277
v 0.282403 -0.152961 -0.564927 v 0.282403 -0.152961 -1.06493
v 0.282689 -0.122635 -0.555192 v 0.282689 -0.122635 -1.05519
v 0.224842 -0.104653 -0.608086 v 0.224842 -0.104653 -1.10809
v 0.224605 -0.129737 -0.616139 v 0.224605 -0.129737 -1.11614
v 0.259885 -0.126949 -0.625863 v 0.259885 -0.126949 -1.12586
v 0.260122 -0.101865 -0.617810 v 0.260122 -0.101865 -1.11781
v 0.212474 -0.086380 -0.663496 v 0.212474 -0.08638 -1.1635
v 0.212291 -0.105749 -0.669714 v 0.212291 -0.105749 -1.16971
v 0.239533 -0.103596 -0.677223 v 0.239533 -0.103596 -1.17722
v 0.239716 -0.084227 -0.671005 v 0.239716 -0.084227 -1.17101
v 0.245255 -0.211794 -0.437667 v 0.245255 -0.211794 -0.937667
v 0.235640 -0.203772 -0.470252 v 0.23564 -0.203772 -0.970252
v 0.239644 -0.243118 -0.467006 v 0.239644 -0.243118 -0.967006
v 0.247582 -0.250332 -0.438814 v 0.247582 -0.250332 -0.938814
v 0.209040 -0.218297 -0.424812 v 0.20904 -0.218297 -0.924812
v 0.199926 -0.212656 -0.455540 v 0.199926 -0.212656 -0.95554
v 0.202827 -0.242012 -0.453432 v 0.202827 -0.242012 -0.953432
v 0.211233 -0.248682 -0.425568 v 0.211233 -0.248682 -0.925568
v 0.237970 -0.250875 -0.431151 v 0.23797 -0.250875 -0.931151
v 0.227883 -0.243486 -0.463280 v 0.227883 -0.243486 -0.96328
v 0.224030 -0.205538 -0.466375 v 0.22403 -0.205538 -0.966375
v 0.235306 -0.213245 -0.430024 v 0.235306 -0.213245 -0.930024
v 0.228892 -0.223387 -0.388623 v 0.228892 -0.223387 -0.888623
v 0.230462 -0.247719 -0.389906 v 0.230462 -0.247719 -0.889906
v 0.247651 -0.246870 -0.394579 v 0.247651 -0.24687 -0.894579
v 0.246456 -0.221472 -0.392860 v 0.246456 -0.221472 -0.89286
v 0.239437 -0.222641 -0.368515 v 0.239437 -0.222641 -0.868515
v 0.241043 -0.247531 -0.369827 v 0.241043 -0.247531 -0.869827
v 0.256136 -0.245131 -0.375942 v 0.256136 -0.245131 -0.875942
v 0.256357 -0.221304 -0.373179 v 0.256357 -0.221304 -0.873179
v 0.211074 -0.263639 -0.453702 v 0.211074 -0.263639 -0.953702
v 0.213693 -0.287969 -0.451236 v 0.213693 -0.287969 -0.951236
v 0.220098 -0.290426 -0.427654 v 0.220098 -0.290426 -0.927654
v 0.217767 -0.267981 -0.429448 v 0.217767 -0.267981 -0.929448
v 0.244401 -0.260805 -0.398326 v 0.244401 -0.260805 -0.898326
v 0.246352 -0.279833 -0.396941 v 0.246352 -0.279833 -0.896941
v 0.226050 -0.263629 -0.395257 v 0.22605 -0.263629 -0.895257
v 0.227699 -0.279716 -0.394086 v 0.227699 -0.279716 -0.894086
v 0.244023 -0.258082 -0.377504 v 0.244023 -0.258082 -0.877504
v 0.245361 -0.271127 -0.376555 v 0.245361 -0.271127 -0.876555
v 0.231216 -0.259837 -0.375381 v 0.231216 -0.259837 -0.875381
v 0.232387 -0.271258 -0.374550 v 0.232387 -0.271258 -0.87455
v 0.242235 -0.234909 -0.365645 v 0.242235 -0.234909 -0.865645
v 0.250770 -0.246182 -0.369083 v 0.25077 -0.246182 -0.869083
v 0.249798 -0.221958 -0.367300 v 0.249798 -0.221958 -0.8673
v 0.258332 -0.233231 -0.370738 v 0.258332 -0.233231 -0.870738
v 0.250591 -0.234249 -0.367948 v 0.250591 -0.234249 -0.867948
v 0.340336 -0.064822 -0.314251 v 0.340336 -0.064822 -0.814251
v 0.289619 -0.068389 -0.300918 v 0.289619 -0.068389 -0.800918
v 0.275750 -0.028073 -0.348984 v 0.27575 -0.028073 -0.848984
v 0.323192 -0.025853 -0.359801 v 0.323192 -0.025853 -0.859801
v 0.347259 -0.086137 -0.309994 v 0.347259 -0.086137 -0.809994
v 0.288925 -0.090417 -0.294400 v 0.288925 -0.090417 -0.7944
v 0.323713 -0.063998 -0.376764 v 0.323713 -0.063998 -0.876764
v 0.271488 -0.066639 -0.364561 v 0.271488 -0.066639 -0.864561
v 0.307790 -0.003466 -0.402060 v 0.30779 -0.003466 -0.90206
v 0.269355 -0.005186 -0.393491 v 0.269355 -0.005186 -0.893491
v 0.305922 -0.036948 -0.417187 v 0.305922 -0.036948 -0.917187
v 0.267487 -0.038668 -0.408618 v 0.267487 -0.038668 -0.908618
v 0.290951 0.010285 -0.445364 v 0.290951 0.010285 -0.945364
v 0.264889 0.009062 -0.439487 v 0.264889 0.009062 -0.939487
v 0.289769 -0.012563 -0.455362 v 0.289769 -0.012563 -0.955362
v 0.263707 -0.013786 -0.449485 v 0.263707 -0.013786 -0.949485
v 0.389423 -0.241181 -0.011183 v 0.389423 -0.241181 -0.511183
v 0.428240 -0.238113 -0.021883 v 0.42824 -0.238113 -0.521883
v 0.390542 -0.278589 -0.008273 v 0.390542 -0.278589 -0.508273
v 0.434238 -0.348579 -0.041377 v 0.434238 -0.348579 -0.541377
v 0.428240 -0.370001 -0.059701 v 0.42824 -0.370001 -0.559701
v 0.389423 -0.373069 -0.049002 v 0.389423 -0.373069 -0.549002
v 0.390542 -0.352032 -0.029332 v 0.390542 -0.352032 -0.529332
v 0.390542 -0.318275 -0.019653 v 0.390542 -0.318275 -0.519653
v 0.434238 -0.275136 -0.020317 v 0.434238 -0.275136 -0.520317
v 0.434238 -0.314821 -0.031697 v 0.434238 -0.314821 -0.531697
v 0.616718 -0.481384 0.744553 v 0.616718 -0.481384 0.244553
v 0.655534 -0.478316 0.733854 v 0.655534 -0.478316 0.233854
v 0.617836 -0.518792 0.747464 v 0.617836 -0.518792 0.247464
v 0.661532 -0.588782 0.714360 v 0.661532 -0.588782 0.21436
v 0.655534 -0.610204 0.696036 v 0.655534 -0.610204 0.196036
v 0.616718 -0.613272 0.706735 v 0.616718 -0.613272 0.206735
v 0.617836 -0.592235 0.726404 v 0.617836 -0.592235 0.226404
v 0.617836 -0.558478 0.736084 v 0.617836 -0.558478 0.236084
v 0.661532 -0.515339 0.735420 v 0.661532 -0.515339 0.23542
v 0.661532 -0.555025 0.724040 v 0.661532 -0.555025 0.22404
vn -0.0693 0.9972 0.0269 vn -0.0693 0.9972 0.0269
vn 0.9795 0.1687 -0.1100 vn 0.9795 0.1687 -0.1100
vn 0.1060 0.7854 0.6098 vn 0.1060 0.7854 0.6098

View File

@@ -131,5 +131,7 @@ Mat4f RotateZ(float angle);
Mat4f Rotate(const Vec3f& angles); Mat4f Rotate(const Vec3f& angles);
Mat4f Rotate(float angle, Vec3f axis);
} // namespace maths } // namespace maths
} // namespace blitz } // namespace blitz

View File

@@ -70,26 +70,36 @@ class Timer {
}; };
// utililty class to trigger update at regular period of time with a cooldown // utililty class to trigger update at regular period of time with a cooldown
template <typename T>
class CooldownTimer { class CooldownTimer {
private: private:
std::uint64_t m_Cooldown; // in millis T m_Cooldown;
std::uint64_t m_CooldownTime; T m_CooldownTime;
public: public:
CooldownTimer() : m_Cooldown(0), m_CooldownTime(0) {} CooldownTimer() : m_Cooldown(0), m_CooldownTime(0) {}
CooldownTimer(std::uint64_t cooldown) : m_Cooldown(0), m_CooldownTime(cooldown) {} CooldownTimer(T cooldown) : m_Cooldown(0), m_CooldownTime(cooldown) {}
bool Update(std::uint64_t delta); bool Update(T delta) {
if (m_Cooldown > 0) {
m_Cooldown = std::max<T>(static_cast<T>(0), static_cast<T>(m_Cooldown - delta));
}
return m_Cooldown == 0;
}
void ApplyCooldown(); void ApplyCooldown() {
m_Cooldown = m_CooldownTime;
}
void Reset(); void Reset() {
m_Cooldown = 0;
}
void SetCooldown(std::uint64_t newCooldown) { void SetCooldown(T newCooldown) {
m_CooldownTime = newCooldown; m_CooldownTime = newCooldown;
} }
std::uint64_t GetCooldown() const { T GetCooldown() const {
return m_CooldownTime; return m_CooldownTime;
} }
}; };

View File

@@ -1,5 +1,8 @@
#pragma once #pragma once
#include "blitz/common/Smoothing.h" #include "blitz/common/Smoothing.h"
#include "blitz/misc/ObjectNotifier.h"
#include "blitz/misc/Time.h"
namespace blitz { namespace blitz {
namespace game { namespace game {
@@ -10,9 +13,16 @@ class Player;
namespace input { namespace input {
class PlayerController { class PlayerListener {
public:
virtual void OnPlayerJump() {}
virtual void OnPlayerShoot() {}
};
class PlayerController : public utils::ObjectNotifier<PlayerListener> {
private: private:
game::Player* m_Player; game::Player* m_Player;
utils::CooldownTimer<float> m_ShootTimer{1.0f};
EMASmoother m_DxSmoother; EMASmoother m_DxSmoother;
/// maximum x-axis velocity /// maximum x-axis velocity
float m_MaxDx; float m_MaxDx;

View File

@@ -19,7 +19,7 @@ class GunShader;
namespace render { namespace render {
class MainRenderer : public GuiListener { class MainRenderer : public GuiListener, public input::PlayerListener {
private: private:
Client* m_Client; Client* m_Client;
ModelLoader::Model m_PlayerModel; ModelLoader::Model m_PlayerModel;
@@ -30,6 +30,7 @@ class MainRenderer : public GuiListener {
std::unique_ptr<shader::GunShader> m_GunShader; std::unique_ptr<shader::GunShader> m_GunShader;
input::PlayerController m_PlayerController; input::PlayerController m_PlayerController;
unsigned int m_Texture; unsigned int m_Texture;
float m_ShootTime;
Camera m_Camera; Camera m_Camera;
public: public:
@@ -38,6 +39,8 @@ class MainRenderer : public GuiListener {
virtual void OnSpectatorChange(const game::PlayerID player) override; virtual void OnSpectatorChange(const game::PlayerID player) override;
virtual void OnPlayerShoot() override;
void Update(); void Update();
void Render(); void Render();
void RenderEntity(const GL::VertexArray& vao, const Vec3f& position, float yaw); void RenderEntity(const GL::VertexArray& vao, const Vec3f& position, float yaw);

View File

@@ -133,5 +133,31 @@ Mat4f Rotate(const Vec3f& angle) {
return maths::Dot(maths::Dot(RotateX(angle.x), RotateY(angle.y)), RotateZ(angle.z)); return maths::Dot(maths::Dot(RotateX(angle.x), RotateY(angle.y)), RotateZ(angle.z));
} }
Mat4f Rotate(float angle, Vec3f axis) {
Mat4f mat{};
axis = Normalize(axis);
float cosine = std::cos(angle);
float sine = std::sin(angle);
float oneminuscos = (1.0f - cosine);
mat.at(0, 0) = cosine + axis.x * axis.x * oneminuscos;
mat.at(0, 1) = axis.x * axis.y * oneminuscos - axis.z * sine;
mat.at(0, 2) = axis.x * axis.z * oneminuscos + axis.y * sine;
mat.at(1, 0) = axis.x * axis.y * oneminuscos + axis.z * sine;
mat.at(1, 1) = cosine + axis.y * axis.y * oneminuscos;
mat.at(1, 2) = axis.y * axis.z * oneminuscos - axis.x * sine;
mat.at(2, 0) = axis.x * axis.z * oneminuscos - axis.y * sine;
mat.at(2, 1) = axis.y * axis.z * oneminuscos + axis.x * sine;
mat.at(2, 2) = cosine + axis.z * axis.z * oneminuscos;
mat.at(3, 3) = 1.0f;
return mat;
}
} // namespace maths } // namespace maths
} // namespace blitz } // namespace blitz

View File

@@ -44,21 +44,6 @@ void Timer::Reset() {
m_InternalTime = 0; // let the timer active once at the beginning m_InternalTime = 0; // let the timer active once at the beginning
} }
bool CooldownTimer::Update(std::uint64_t delta) {
if (m_Cooldown > 0) {
m_Cooldown = static_cast<std::uint64_t>(std::max(static_cast<std::int64_t>(0), static_cast<std::int64_t>(m_Cooldown - delta)));
}
return m_Cooldown == 0;
}
void CooldownTimer::Reset() {
m_Cooldown = 0; // let the timer active once at the beginning
}
void CooldownTimer::ApplyCooldown() {
m_Cooldown = m_CooldownTime;
}
std::uint64_t GetTime() { std::uint64_t GetTime() {
return static_cast<std::uint64_t>( return static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count()); std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());

View File

@@ -64,6 +64,13 @@ void PlayerController::Update(float delta) {
if (ImGui::IsKeyDown(ImGuiKey::ImGuiKey_Space) && m_OnGround) { if (ImGui::IsKeyDown(ImGuiKey::ImGuiKey_Space) && m_OnGround) {
m_Dz = m_MaxDz; m_Dz = m_MaxDz;
NotifyListeners(&PlayerListener::OnPlayerJump);
}
bool canShoot = m_ShootTimer.Update(delta);
if (ImGui::IsMouseDown(ImGuiMouseButton_Left) && canShoot) {
NotifyListeners(&PlayerListener::OnPlayerShoot);
m_ShootTimer.ApplyCooldown();
} }
} }

View File

@@ -1,5 +1,6 @@
#include "client/render/MainRenderer.h" #include "client/render/MainRenderer.h"
#include "blitz/misc/Easing.h"
#include "blitz/misc/Format.h" #include "blitz/misc/Format.h"
#include "blitz/misc/Log.h" #include "blitz/misc/Log.h"
#include "blitz/misc/Maths.h" #include "blitz/misc/Maths.h"
@@ -24,11 +25,12 @@ namespace render {
static const Vec4f SkyColor = {0.6, 0.8, 1, 1}; static const Vec4f SkyColor = {0.6, 0.8, 1, 1};
static const Vec4f MenuColor = {0, 0, 0, 0}; static const Vec4f MenuColor = {0, 0, 0, 0};
MainRenderer::MainRenderer(Client* client) : m_Client(client) { MainRenderer::MainRenderer(Client* client) : m_Client(client), m_ShootTime(0) {
LoadModels(); LoadModels();
client->BindListener(this); client->BindListener(this);
m_PlayerController.BindListener(this);
m_EntityShader = std::make_unique<shader::EntityShader>(); m_EntityShader = std::make_unique<shader::EntityShader>();
blitz_debug_assert(m_EntityShader->LoadShader()); blitz_debug_assert(m_EntityShader->LoadShader());
@@ -98,14 +100,22 @@ void MainRenderer::RenderPlayers() {
} }
} }
void MainRenderer::OnPlayerShoot() {
m_ShootTime = 1.0f;
}
void MainRenderer::RenderGun() { void MainRenderer::RenderGun() {
if (!m_Camera.GetAttachedPlayer()) if (!m_Camera.GetAttachedPlayer())
return; return;
m_GunShader->Start(); m_GunShader->Start();
Mat4f modelTransform = maths::Translate({0.0, 0.0, -0.5}); float progression = 1.0f - m_ShootTime;
m_GunShader->SetModelTransform(modelTransform);
float angle = progression * progression * progression * 7 - progression * progression * 11 + progression * 4;
Mat4f transform = maths::RotateX(-angle);
m_GunShader->SetModelTransform(transform);
for (auto& Vao : m_GunModel.mVaos) { for (auto& Vao : m_GunModel.mVaos) {
Vao->Bind(); Vao->Bind();
@@ -145,6 +155,8 @@ void MainRenderer::Update() {
m_GunShader->Start(); m_GunShader->Start();
m_GunShader->SetProjectionMatrix(m_Camera.GetPerspectiveMatrix()); m_GunShader->SetProjectionMatrix(m_Camera.GetPerspectiveMatrix());
m_ShootTime = std::max(0.0f, m_ShootTime - delta);
} }
void MainRenderer::OnSpectatorChange(const game::PlayerID player) { void MainRenderer::OnSpectatorChange(const game::PlayerID player) {

View File

@@ -5,10 +5,6 @@ set_policy("package.install_locally", true)
add_rules("mode.debug", "mode.release", "mode.valgrind") add_rules("mode.debug", "mode.release", "mode.valgrind")
if is_mode("valgrind") then
add_requires("valgrind")
end
add_requires("libsdl 2.28.3", {configs = {sdlmain = false}}) add_requires("libsdl 2.28.3", {configs = {sdlmain = false}})
add_requires("glbinding >= 3", "zlib", "assimp", "nlohmann_json") add_requires("glbinding >= 3", "zlib", "assimp", "nlohmann_json")