22 Commits

Author SHA1 Message Date
920c2e20b3 Merge pull request 'Merge new features' (#23) from dev into main
All checks were successful
Linux arm64 / Build (push) Successful in 4m46s
Reviewed-on: #23
2024-03-26 12:07:09 +01:00
1e1880376d fix merge
All checks were successful
Linux arm64 / Build (push) Successful in 4m52s
2024-03-26 11:47:47 +01:00
de22c89975 Squashed commit of the following:
commit 0adeba26e4
Author: = <=>
Date:   Tue Mar 26 11:35:37 2024 +0100

    minor fix

commit 011d8a573c
Author: = <=>
Date:   Tue Mar 26 11:33:56 2024 +0100

    fix stupid build error

commit 22406ad020
Author: = <=>
Date:   Tue Mar 26 11:25:35 2024 +0100

    yes

commit 4db55a372b
Author: = <=>
Date:   Tue Mar 26 11:13:22 2024 +0100

    csavgfdslgakdf;g

commit 20d176ccb5
Author: AquaEBM <AquaEBM@gmail.com>
Date:   Tue Mar 26 11:07:27 2024 +0100

    add range overlapping check utilities

commit 3863e7907f
Author: AquaEBM <AquaEBM@gmail.com>
Date:   Tue Mar 26 10:24:35 2024 +0100

    better edge case handling

commit 35b7d7bab0
Author: = <=>
Date:   Tue Mar 26 10:19:21 2024 +0100

    I am fucking retarded what the fuck is this stupid brain smoking

commit 6aca413b4f
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Sun Mar 24 18:14:02 2024 +0100

    small refactor

commit ee322e3e7b
Author: AquaEBM@gmail.com <AquaEBM@gmail.com>
Date:   Sun Mar 24 17:40:39 2024 +0100

    improve tests, fix some bugs in Distance, (not working yet)

commit 864a15e4c8
Merge: 7efd821 c1ded40
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Fri Mar 22 19:33:45 2024 +0100

    Squashed commit of the following:

    commit c1ded40cc4
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 17:24:05 2024 +0100

        improve network test

    commit 285bf880ee
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 17:14:43 2024 +0100

        fix warning

    commit 06ff76607d
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 15:45:33 2024 +0100

        add bots

    commit 19229bbe8a
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 14:55:57 2024 +0100

        fix warnings

    commit 2b0930a734
    Author: Morph01 <thibaut6969delastreet@gmail.com>
    Date:   Tue Mar 19 10:43:55 2024 +0100

        hud of blitz (#16)

        Résoudre partiellement #12

        Co-authored-by: Persson-dev <sim16.prib@gmail.com>
        Co-authored-by: Morph01 <145839520+Morph01@users.noreply.github.com>
        Co-authored-by: Simon Pribylski <sim16.prib@gmail.com>
        Reviewed-on: #16

    commit 84b6acad4c
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Tue Mar 19 10:15:54 2024 +0100

        less spooky light

    commit 9f94d51fc4
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 14 16:36:15 2024 +0100

        Ajout de tests d'intégration (#17)

        Le paquet fournissant OpenGL a été temporairement remplacé par glew

        Reviewed-on: #17
        Co-authored-by: Persson-dev <sim16.prib@gmail.com>
        Co-committed-by: Persson-dev <sim16.prib@gmail.com>

    commit 9951256881
    Author: Simon Pribylski <sim16.prib@gmail.com>
    Date:   Wed Mar 13 20:19:37 2024 +0100

        action: install libsdl via apt

    commit 4a02054648
    Author: Morph01 <145839520+Morph01@users.noreply.github.com>
    Date:   Wed Mar 13 13:53:20 2024 +0100

        compression tests

    commit 19c39312bf
    Author: Morph01 <145839520+Morph01@users.noreply.github.com>
    Date:   Wed Mar 13 13:51:57 2024 +0100

        setup action

    commit d5014b1e8a
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Tue Mar 12 10:26:47 2024 +0100

        Revert "extend Vec3"

        This reverts commit dccfa9c936.

    commit dccfa9c936
    Author: = <=>
    Date:   Tue Mar 12 10:01:00 2024 +0100

        extend Vec3
    Merge branch 'main' into physics

commit 7efd8218ea
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Fri Mar 22 19:28:46 2024 +0100

    moved functions in Maths.h

commit 08db7f84b9
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Fri Mar 22 19:16:48 2024 +0100

    add missing include

commit 7119dea783
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Fri Mar 22 19:15:01 2024 +0100

    merge 'main' branch

    commit c1ded40cc4
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 17:24:05 2024 +0100

        improve network test

    commit 285bf880ee
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 17:14:43 2024 +0100

        fix warning

    commit 06ff76607d
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 15:45:33 2024 +0100

        add bots

    commit 19229bbe8a
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 21 14:55:57 2024 +0100

        fix warnings

    commit 2b0930a734
    Author: Morph01 <thibaut6969delastreet@gmail.com>
    Date:   Tue Mar 19 10:43:55 2024 +0100

        hud of blitz (#16)

        Résoudre partiellement #12

        Co-authored-by: Persson-dev <sim16.prib@gmail.com>
        Co-authored-by: Morph01 <145839520+Morph01@users.noreply.github.com>
        Co-authored-by: Simon Pribylski <sim16.prib@gmail.com>
        Reviewed-on: #16

    commit 84b6acad4c
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Tue Mar 19 10:15:54 2024 +0100

        less spooky light

    commit 9f94d51fc4
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Thu Mar 14 16:36:15 2024 +0100

        Ajout de tests d'intégration (#17)

        Le paquet fournissant OpenGL a été temporairement remplacé par glew

        Reviewed-on: #17
        Co-authored-by: Persson-dev <sim16.prib@gmail.com>
        Co-committed-by: Persson-dev <sim16.prib@gmail.com>

    commit 9951256881
    Author: Simon Pribylski <sim16.prib@gmail.com>
    Date:   Wed Mar 13 20:19:37 2024 +0100

        action: install libsdl via apt

    commit 4a02054648
    Author: Morph01 <145839520+Morph01@users.noreply.github.com>
    Date:   Wed Mar 13 13:53:20 2024 +0100

        compression tests

    commit 19c39312bf
    Author: Morph01 <145839520+Morph01@users.noreply.github.com>
    Date:   Wed Mar 13 13:51:57 2024 +0100

        setup action

    commit d5014b1e8a
    Author: Persson-dev <sim16.prib@gmail.com>
    Date:   Tue Mar 12 10:26:47 2024 +0100

        Revert "extend Vec3"

        This reverts commit dccfa9c936.

    commit dccfa9c936
    Author: = <=>
    Date:   Tue Mar 12 10:01:00 2024 +0100

        extend Vec3

commit 6e998fc368
Author: AquaEBM@gmail.com <AquaEBM@gmail.com>
Date:   Fri Mar 22 16:41:52 2024 +0100

    change tests

commit 441131a2f5
Author: AquaEBM@gmail.com <AquaEBM@gmail.com>
Date:   Fri Mar 22 16:14:56 2024 +0100

    rename Intersects to distance

commit c875fa1dee
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Fri Mar 22 15:50:32 2024 +0100

    more template

commit 5e4b318d67
Author: AquaEBM@gmail.com <AquaEBM@gmail.com>
Date:   Fri Mar 22 15:26:07 2024 +0100

    inline Reduce_<> functions

commit 076fa7badc
Author: AquaEBM@gmail.com <AquaEBM@gmail.com>
Date:   Fri Mar 22 15:18:39 2024 +0100

    fix compilation error

commit 1091abd034
Author: AquaEBM <AquaEBM@gmail.com>
Date:   Fri Mar 22 14:58:26 2024 +0100

    fix some compilation errors

commit dd9ea3ece8
Author: AquaEBM <AquaEBM@gmail.com>
Date:   Fri Mar 22 12:02:39 2024 +0100

    update intersects, document changes

commit 6226161e31
Author: AquaEBM <AquaEBM@gmail.com>
Date:   Fri Mar 22 09:22:06 2024 +0100

    fix compilation error, add min/max functions for Vec3d

commit 6b32e8878e
Author: AquaEBM <AquaEBM@gmail.com>
Date:   Fri Mar 22 09:18:34 2024 +0100

    fix min/max functions
2024-03-26 11:44:37 +01:00
d1bb086daf temp intersect
Some checks failed
Linux arm64 / Build (push) Failing after 4m42s
2024-03-26 09:00:47 +01:00
7f5aee9e16 fix merge 2024-03-26 09:00:30 +01:00
1bdf35a4e6 Merge branch 'death' into dev
Some checks failed
Linux arm64 / Build (push) Failing after 2m17s
2024-03-26 08:39:18 +01:00
2bef8801e8 Physique presque finie (#21)
Some checks failed
Linux arm64 / Build (push) Failing after 4m47s
Co-authored-by: = <=>
Co-authored-by: AquaEBM <AquaEBM@gmail.com>
Co-authored-by: AquaEBM@gmail.com <AquaEBM@gmail.com>
Reviewed-on: #21
Co-authored-by: Persson-dev <sim16.prib@gmail.com>
Co-committed-by: Persson-dev <sim16.prib@gmail.com>
2024-03-26 08:31:02 +01:00
5492f52806 fix warning
Some checks failed
Linux arm64 / Build (push) Failing after 4m53s
2024-03-21 17:14:43 +01:00
2cbe3f1219 move player on death
Some checks failed
Linux arm64 / Build (push) Failing after 3m53s
2024-03-21 16:46:38 +01:00
fe68b98ba1 update health to client 2024-03-21 16:43:45 +01:00
860ec70829 Squashed commit of the following:
commit 06ff76607d
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Thu Mar 21 15:45:33 2024 +0100

    add bots

commit 19229bbe8a
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Thu Mar 21 14:55:57 2024 +0100

    fix warnings

commit 2b0930a734
Author: Morph01 <thibaut6969delastreet@gmail.com>
Date:   Tue Mar 19 10:43:55 2024 +0100

    hud of blitz (#16)

    Résoudre partiellement #12

    Co-authored-by: Persson-dev <sim16.prib@gmail.com>
    Co-authored-by: Morph01 <145839520+Morph01@users.noreply.github.com>
    Co-authored-by: Simon Pribylski <sim16.prib@gmail.com>
    Reviewed-on: #16

commit 84b6acad4c
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Tue Mar 19 10:15:54 2024 +0100

    less spooky light

commit 9f94d51fc4
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Thu Mar 14 16:36:15 2024 +0100

    Ajout de tests d'intégration (#17)

    Le paquet fournissant OpenGL a été temporairement remplacé par glew

    Reviewed-on: #17
    Co-authored-by: Persson-dev <sim16.prib@gmail.com>
    Co-committed-by: Persson-dev <sim16.prib@gmail.com>

commit 9951256881
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Wed Mar 13 20:19:37 2024 +0100

    action: install libsdl via apt

commit 4a02054648
Author: Morph01 <145839520+Morph01@users.noreply.github.com>
Date:   Wed Mar 13 13:53:20 2024 +0100

    compression tests

commit 19c39312bf
Author: Morph01 <145839520+Morph01@users.noreply.github.com>
Date:   Wed Mar 13 13:51:57 2024 +0100

    setup action

commit d5014b1e8a
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Tue Mar 12 10:26:47 2024 +0100

    Revert "extend Vec3"

    This reverts commit dccfa9c936.

commit dccfa9c936
Author: = <=>
Date:   Tue Mar 12 10:01:00 2024 +0100

    extend Vec3
2024-03-21 15:51:57 +01:00
438bc4a968 remove unused variable
Some checks failed
Linux arm64 / Build (push) Failing after 4m32s
2024-03-21 14:39:18 +01:00
9aa546881a better shoot logging
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-03-21 14:35:41 +01:00
4db03f2b83 working shoot
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-03-21 14:32:03 +01:00
0119d36b5c should work
All checks were successful
Linux arm64 / Build (push) Successful in 5m5s
2024-03-19 13:01:17 +01:00
249d7534a4 Squashed commit of the following:
All checks were successful
Linux arm64 / Build (push) Successful in 4m20s
commit 2b0930a734
Author: Morph01 <thibaut6969delastreet@gmail.com>
Date:   Tue Mar 19 10:43:55 2024 +0100

    hud of blitz (#16)

    Résoudre partiellement #12

    Co-authored-by: Persson-dev <sim16.prib@gmail.com>
    Co-authored-by: Morph01 <145839520+Morph01@users.noreply.github.com>
    Co-authored-by: Simon Pribylski <sim16.prib@gmail.com>
    Reviewed-on: #16

commit 84b6acad4c
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Tue Mar 19 10:15:54 2024 +0100

    less spooky light

commit 9f94d51fc4
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Thu Mar 14 16:36:15 2024 +0100

    Ajout de tests d'intégration (#17)

    Le paquet fournissant OpenGL a été temporairement remplacé par glew

    Reviewed-on: #17
    Co-authored-by: Persson-dev <sim16.prib@gmail.com>
    Co-committed-by: Persson-dev <sim16.prib@gmail.com>

commit 9951256881
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Wed Mar 13 20:19:37 2024 +0100

    action: install libsdl via apt

commit 4a02054648
Author: Morph01 <145839520+Morph01@users.noreply.github.com>
Date:   Wed Mar 13 13:53:20 2024 +0100

    compression tests

commit 19c39312bf
Author: Morph01 <145839520+Morph01@users.noreply.github.com>
Date:   Wed Mar 13 13:51:57 2024 +0100

    setup action

commit d5014b1e8a
Author: Persson-dev <sim16.prib@gmail.com>
Date:   Tue Mar 12 10:26:47 2024 +0100

    Revert "extend Vec3"

    This reverts commit dccfa9c936.

commit dccfa9c936
Author: = <=>
Date:   Tue Mar 12 10:01:00 2024 +0100

    extend Vec3
2024-03-19 12:16:23 +01:00
=
abe89d2089 remove debug prints 2024-03-19 12:10:32 +01:00
f070b28d8b revert c_cpp_properties 2024-03-19 12:03:12 +01:00
=
a7f734d22e fix Intersects, add tests 2024-03-19 11:56:29 +01:00
=
abbc4419fa update intersects function 2024-03-19 10:36:43 +01:00
86e47601d7 idk 2024-03-12 11:31:04 +01:00
=
a3b6d2488f extend Vec3, implement AABB intersection 2024-03-12 10:25:27 +01:00
39 changed files with 565 additions and 75 deletions

View File

@@ -1,11 +1,13 @@
{ {
"configurations": [ "configurations": [
{ {
"name": "Blitz", "name": "Blitz",
"cppStandard": "c++17", "cppStandard": "c++17",
"includePath": ["include"], "includePath": [
"compileCommands": ".vscode/compile_commands.json" "include"
} ],
], "compileCommands": ".vscode/compile_commands.json"
"version": 4 }
],
"version": 4
} }

7
.vscode/launch.json vendored
View File

@@ -2,6 +2,13 @@
// Xmake debug // Xmake debug
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
"type": "xmake",
"request": "launch",
"name": "debug intersects test",
"target": "test_intersects",
"cwd": "",
},
{ {
"type": "xmake", "type": "xmake",
"request": "launch", "request": "launch",

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
namespace blitz { namespace blitz {
namespace game { namespace game {

View File

@@ -1,13 +1,20 @@
#pragma once #pragma once
#include "blitz/common/Defines.h" #include "blitz/common/Defines.h"
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include <cstdint> #include <cstdint>
#include <string> #include <string>
namespace blitz { namespace blitz {
namespace game { namespace game {
struct PlayerStats {
std::uint16_t m_Deaths;
std::uint16_t m_Kills;
std::uint32_t m_ShootCount;
std::uint32_t m_ShootSuccessCount;
};
class Player { class Player {
private: private:
PlayerID m_ID; PlayerID m_ID;
@@ -16,9 +23,12 @@ class Player {
Vec3f m_Velocity; Vec3f m_Velocity;
float m_Yaw; float m_Yaw;
float m_Pitch; float m_Pitch;
float m_HP;
bool m_IsBot;
PlayerStats m_Stats{};
public: public:
Player(PlayerID id) : m_ID(id), m_Yaw(0), m_Pitch(0) {} Player(PlayerID id) : m_ID(id), m_Yaw(0), m_Pitch(0), m_IsBot(false) {}
PlayerID GetID() const { PlayerID GetID() const {
return m_ID; return m_ID;
@@ -75,6 +85,26 @@ class Player {
void AddPitch(float dPitch) { void AddPitch(float dPitch) {
m_Pitch += dPitch; m_Pitch += dPitch;
} }
float GetHP() const {
return m_HP;
}
void SetHP(float hp) {
m_HP = hp;
}
bool IsBot() const {
return m_IsBot;
}
void SetBot() {
m_IsBot = true;
}
PlayerStats& GetStats() {
return m_Stats;
}
}; };

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include <cmath> #include <cmath>
namespace blitz { namespace blitz {
@@ -8,6 +8,22 @@ namespace maths {
static constexpr float PI = 3.141592653f; static constexpr float PI = 3.141592653f;
template <typename T>
/**
* @brief returns the amount of overlap between the ranges [a1 ; b1] and [a2 ; b2]
*/
T RangesOverlap(T& a1, T& b1, T& a2, T& b2) {
return std::min(std::max(a1, b1), std::max(a2, b2)) - std::max(std::min(a1, b1), std::min(a2, b2));
}
template <typename T>
/**
* @brief returns whether the ranges [a1 ; b1] and [a2 ; b2] overlap
*/
bool RangesOverlapping(T& a1, T& b1, T& a2, T& b2) {
return RangesOverlap(a1, a2, b1, b2) >= 0;
}
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Vectors // // Vectors //
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
@@ -55,6 +71,100 @@ T Distance(const Vec3<T>& vect, const Vec3<T>& other) {
return Length(vect - other); return Length(vect - other);
} }
// it seems that `std::{min, max}`'s behavior conflicts with that of `cmath`'s `f{min, max}[f]`
// Why? Like I fucking know dude
template <typename T>
T ReduceMin(const Vec3<T>& vect) {
return std::min(std::min(vect.x, vect.y), vect.z);
}
template <typename T>
T ReduceMax(const Vec3<T>& vect) {
return std::max(std::max(vect.x, vect.y), vect.z);
}
/**
* @brief returns the (signed) minimal coordinate of the vector
*
* @param v
* @return constexpr T
*/
template <>
inline float ReduceMin<float>(const Vec3f& v) {
return std::fminf(std::fminf(v.x, v.y), v.z);
}
/**
* @brief returns the (signed) maximal coordinate of the vector
*
* @param v
* @return constexpr T
*/
template <>
inline float ReduceMax<float>(const Vec3f& v) {
return std::fmaxf(std::fmaxf(v.x, v.y), v.z);
}
/**
* @brief returns the (signed) minimal coordinate of the vector
*
* @param v
* @return constexpr T
*/
template <>
inline double ReduceMin<double>(const Vec3d& v) {
return std::fmin(std::fmin(v.x, v.y), v.z);
}
/**
* @brief returns the (signed) maximal coordinate of the vector
*
* @param v
* @return constexpr T
*/
template <>
inline double ReduceMax<double>(const Vec3d& v) {
return std::fmax(std::fmax(v.x, v.y), v.z);
}
/**
* @brief returns the coordinate-wise minimum of the given vectors,
* where a coordinate in the returned vector is NAN iff any of the two compared ones are NAN
*
* @tparam T
* @param self
* @param other
* @return constexpr Vec3f
*/
template <typename T>
constexpr Vec3<T> Min(const Vec3<T>& self, const Vec3<T>& other) {
return {
std::min(self.x, other.x),
std::min(self.y, other.y),
std::min(self.z, other.z),
};
}
/**
* @brief returns the coordinate-wise maximum of the given vectors,
* where a coordinate in the returned vector is NAN iff any of the two compared ones are NAN
*
* @tparam T
* @param self
* @param other
* @return constexpr Vec3f
*/
template <typename T>
constexpr Vec3<T> Max(const Vec3<T>& self, const Vec3<T>& other) {
return {
std::max(self.x, other.x),
std::max(self.y, other.y),
std::max(self.z, other.z),
};
}
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Matricies // // Matricies //
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////

View File

@@ -0,0 +1,30 @@
#pragma once
#include "blitz/maths/Vector.h"
namespace blitz {
namespace maths {
struct Ray {
Vec3f origin;
Vec3f direction;
};
struct AABB {
Vec3f from;
Vec3f to;
};
inline AABB operator+(const AABB& aabb, const Vec3f& offset) {
AABB result = aabb;
result.from += offset;
result.to += offset;
return result;
}
float Distance(const Ray& ray, const AABB& aabb);
bool Intersects(const Ray& ray, const AABB& aabb);
} // namespace maths
} // namespace blitz

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <algorithm>
#include <cmath>
#include <cstddef> #include <cstddef>
namespace blitz { namespace blitz {
@@ -23,10 +25,6 @@ struct Vec2 {
constexpr Vec2(T X = 0, T Y = 0) : x(X), y(Y) {} constexpr Vec2(T X = 0, T Y = 0) : x(X), y(Y) {}
}; };
template <typename T> template <typename T>
struct Vec3 { struct Vec3 {
union { union {
@@ -47,10 +45,6 @@ struct Vec3 {
constexpr Vec3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {} constexpr Vec3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {}
}; };
template <typename T> template <typename T>
struct Vec4 { struct Vec4 {
union { union {
@@ -77,10 +71,6 @@ struct Vec4 {
constexpr Vec4(T X = 0, T Y = 0, T Z = 0, T W = 0) : x(X), y(Y), z(Z), w(W) {} constexpr Vec4(T X = 0, T Y = 0, T Z = 0, T W = 0) : x(X), y(Y), z(Z), w(W) {}
}; };
using Vec2uc = Vec2<unsigned char>; using Vec2uc = Vec2<unsigned char>;
using Vec2i = Vec2<int>; using Vec2i = Vec2<int>;
using Vec2u = Vec2<unsigned int>; using Vec2u = Vec2<unsigned int>;
@@ -178,7 +168,7 @@ Vec3<T>& operator+=(Vec3<T>& vect, const Vec3<T>& other) {
template <typename T> template <typename T>
constexpr Vec3<T> operator-(const Vec3<T>& vect, const Vec3<T>& other) { constexpr Vec3<T> operator-(const Vec3<T>& vect, const Vec3<T>& other) {
return vect + (-other); return {vect.x - other.x, vect.y - other.y, vect.z - other.z};
} }
template <typename T> template <typename T>
@@ -197,9 +187,44 @@ constexpr Vec3<T> operator*(T mult, const Vec3<T>& vect) {
return vect * mult; return vect * mult;
} }
template <typename T>
constexpr Vec3<T> operator*=(const Vec3<T>& vect, T mult) {
vect = vect * mult;
return vect;
}
template <typename T>
constexpr Vec3<T> operator*=(T mult, const Vec3<T>& vect) {
vect = vect * mult;
return vect;
}
template <typename T>
constexpr Vec3<T> operator*(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x * other.x, vect.y * other.y, vect.z * other.z};
}
template <typename T>
Vec3<T>& operator*=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect * other;
return vect;
}
template <typename T>
constexpr Vec3<T> operator/(const Vec3<T>& vect, const Vec3<T>& other) {
return {vect.x / other.x, vect.y / other.y, vect.z / other.z};
}
template <typename T>
Vec3<T>& operator/=(Vec3<T>& vect, const Vec3<T>& other) {
vect = vect / other;
return vect;
}
template <typename T>
constexpr bool operator<(const Vec3<T>& vec3, const Vec3<T>& other) {
return vec3.x < other.x && vec3.y < other.y && vec3.z < other.z;
}
// Vec4 // Vec4
@@ -245,10 +270,6 @@ constexpr Vec4<T> operator*(T mult, const Vec4<T>& vect) {
return vect * mult; return vect * mult;
} }
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Matrix // // Matrix //
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
@@ -279,26 +300,18 @@ struct Mat4 {
} }
T at(std::size_t row, std::size_t column) const { T at(std::size_t row, std::size_t column) const {
return operator[](row* MATRIX_SIZE + column); return operator[](row * MATRIX_SIZE + column);
} }
T& at(std::size_t row, std::size_t column) { T& at(std::size_t row, std::size_t column) {
return operator[](row* MATRIX_SIZE + column); return operator[](row * MATRIX_SIZE + column);
} }
}; };
typedef Mat4<float> Mat4f; typedef Mat4<float> Mat4f;
typedef Mat4<int> Mat4i; typedef Mat4<int> Mat4i;
typedef Mat4<double> Mat4d; typedef Mat4<double> Mat4d;
template <typename T> template <typename T>
bool operator==(const Mat4<T>& mat, const Mat4<T>& other) { bool operator==(const Mat4<T>& mat, const Mat4<T>& other) {
return mat.x0 == other.x0 && mat.y0 == other.y0 && mat.z0 == other.z0 && mat.w0 == other.w0 && mat.x1 == other.x1 && return mat.x0 == other.x0 && mat.y0 == other.y0 && mat.z0 == other.z0 && mat.w0 == other.w0 && mat.x1 == other.x1 &&
@@ -306,5 +319,4 @@ bool operator==(const Mat4<T>& mat, const Mat4<T>& other) {
mat.z2 == other.z2 && mat.w2 == other.w2 && mat.x3 == other.x3 && mat.y3 == other.y3 && mat.z3 == other.z3 && mat.z2 == other.z2 && mat.w2 == other.w2 && mat.x3 == other.x3 && mat.y3 == other.y3 && mat.z3 == other.z3 &&
mat.w3 == other.w3; mat.w3 == other.w3;
} }
} // namespace blitz } // namespace blitz

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include <string> #include <string>
namespace blitz { namespace blitz {

View File

@@ -31,6 +31,7 @@ class PacketHandler {
virtual void HandlePacket(const PlayerPositionAndRotationPacket* packet) {} virtual void HandlePacket(const PlayerPositionAndRotationPacket* packet) {}
virtual void HandlePacket(const PlayerShootPacket* packet) {} virtual void HandlePacket(const PlayerShootPacket* packet) {}
virtual void HandlePacket(const ServerTpsPacket* packet) {} virtual void HandlePacket(const ServerTpsPacket* packet) {}
virtual void HandlePacket(const UpdateHealthPacket* packet) {}
}; };
} // namespace protocol } // namespace protocol

View File

@@ -8,4 +8,5 @@
#include "packets/PlayerLoginPacket.h" #include "packets/PlayerLoginPacket.h"
#include "packets/PlayerPositionAndRotationPacket.h" #include "packets/PlayerPositionAndRotationPacket.h"
#include "packets/PlayerShootPacket.h" #include "packets/PlayerShootPacket.h"
#include "packets/ServerTpsPacket.h" #include "packets/ServerTpsPacket.h"
#include "packets/UpdateHealthPacket.h"

View File

@@ -14,6 +14,7 @@ class PlayerLoginPacket;
class PlayerPositionAndRotationPacket; class PlayerPositionAndRotationPacket;
class PlayerShootPacket; class PlayerShootPacket;
class ServerTpsPacket; class ServerTpsPacket;
class UpdateHealthPacket;
} // namespace protocol } // namespace protocol
} // namespace blitz } // namespace blitz

View File

@@ -20,6 +20,7 @@ enum class PacketType : std::uint8_t {
// client --> server // client --> server
PlayerLogin = 0, /**< Corresponds to PlayerLoginPacket */ PlayerLogin = 0, /**< Corresponds to PlayerLoginPacket */
UpdateHealth, /**< Corresponds to UpdateHealthPacket */
// client <-- server // client <-- server

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include "blitz/protocol/Protocol.h" #include "blitz/protocol/Protocol.h"
#include <vector> #include <vector>

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include "blitz/common/Defines.h" #include "blitz/common/Defines.h"
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include "blitz/protocol/Protocol.h" #include "blitz/protocol/Protocol.h"
namespace blitz { namespace blitz {

View File

@@ -0,0 +1,31 @@
#pragma once
#include "blitz/protocol/Protocol.h"
namespace blitz {
namespace protocol {
class UpdateHealthPacket : public Packet {
private:
float m_NewHealth;
public:
UpdateHealthPacket() {}
UpdateHealthPacket(float newHealth) : m_NewHealth(newHealth) {}
virtual ~UpdateHealthPacket() {}
virtual DataBuffer Serialize(bool packetID = true) const;
virtual void Deserialize(DataBuffer& data);
virtual void Dispatch(PacketHandler* handler) const;
std::uint8_t GetNewHealth() const {
return m_NewHealth;
}
virtual PacketType GetType() const {
return PacketType::UpdateHealth;
}
};
} // namespace protocol
} // namespace blitz

View File

@@ -24,6 +24,7 @@ class ClientGame : public game::Game, public protocol::PacketHandler {
virtual void HandlePacket(const protocol::PlayerListPacket* packet) override; virtual void HandlePacket(const protocol::PlayerListPacket* packet) override;
virtual void HandlePacket(const protocol::PlayerPositionAndRotationPacket* packet) override; virtual void HandlePacket(const protocol::PlayerPositionAndRotationPacket* packet) override;
virtual void HandlePacket(const protocol::PlayerShootPacket* packet) override; virtual void HandlePacket(const protocol::PlayerShootPacket* packet) override;
virtual void HandlePacket(const protocol::UpdateHealthPacket* packet) override;
private: private:
void RegisterHandlers(); void RegisterHandlers();

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include "client/render/loader/ModelLoader.h" #include "client/render/loader/ModelLoader.h"
#include <memory> #include <memory>
#include <vector> #include <vector>

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include "blitz/common/Vector.h"
#include "blitz/game/Player.h" #include "blitz/game/Player.h"
#include "blitz/maths/Vector.h"
#include <cstdint> #include <cstdint>
namespace blitz { namespace blitz {

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include "client/render/OpenGL.h" #include "client/render/OpenGL.h"
#include <string> #include <string>

View File

@@ -17,6 +17,8 @@ class ServerGame : public game::Game {
ServerGame(Server* server); ServerGame(Server* server);
virtual ~ServerGame(); virtual ~ServerGame();
void CheckShoot(game::PlayerID player, Vec3f position, float yaw, float pitch);
void AddPlayer(game::PlayerID player, const std::string& name) override; void AddPlayer(game::PlayerID player, const std::string& name) override;
void RemovePlayer(game::PlayerID player) override; void RemovePlayer(game::PlayerID player) override;
@@ -24,6 +26,8 @@ class ServerGame : public game::Game {
private: private:
void SendPlayerPositions(); void SendPlayerPositions();
void DamagePlayer(game::Player& player, game::Player& shooter);
void UpdateHP(game::Player& player, float newHP);
}; };
} // namespace server } // namespace server

View File

@@ -22,8 +22,11 @@ const Player* Game::GetPlayerById(PlayerID id) const {
} }
void Game::AddPlayer(PlayerID player, const std::string& name) { void Game::AddPlayer(PlayerID player, const std::string& name) {
static float MAX_HP = 100;
game::Player newPlayer{player}; game::Player newPlayer{player};
newPlayer.SetName(name); newPlayer.SetName(name);
newPlayer.SetHP(MAX_HP);
GetPlayers().insert({player, newPlayer}); GetPlayers().insert({player, newPlayer});
} }

View File

@@ -1,4 +1,4 @@
#include "blitz/misc/Maths.h" #include "blitz/maths/Maths.h"
#include <cassert> #include <cassert>

68
src/blitz/maths/Physics.cpp Executable file
View File

@@ -0,0 +1,68 @@
#include "blitz/maths/Physics.h"
#include "blitz/maths/Maths.h"
#include <cmath>
namespace blitz {
namespace maths {
/**
* @brief Returns `true` if the half-line `ray` _strictly_ intersects with `aabb`,
* and `false` if it _strictly_ doesn't intersect.
* Note that if it only intersects with corners, edges, or sides of the box,
* or if any coordinate in `ray` is `NAN` or `inf` or if any coordinate in
* `aabb` is `NAN` the result is unspecified.
* */
float Distance(const Ray& ray, const AABB& aabb) {
// This function calculates smallest interval I = ] a, b [, for strictly positive a and b
// such that for all t in I, for all l, r, o, d corresponding
// coordinates in aabb.from, aabb.to, ray.origin, ray.direction, respectively
//
// min(l, r) < o + t * d < max(l, r)
//
// and returns true if it's non-empty i.e. a < b
// m = min(l, r), M = max(l, r)
// m < o + t * d < M
Vec3f l = Min(aabb.from, aabb.to);
Vec3f r = Max(aabb.to, aabb.from);
// m - o < t * d < M - o
l -= ray.origin;
r -= ray.origin;
// (m - o) / d < t < (M - o) / d
l /= ray.direction;
r /= ray.direction;
// but if d is negative the inequality is flipped
Vec3f u = Min(l, r);
r = Max(l, r);
l = u;
float tmin = ReduceMax(l);
float tmax = ReduceMin(r);
// Since Min propagates NANs and ReduceMin doesn't, and since NAN !< <any float>
// the inequality becomes ignored for coordinates where a NAN is involved
// (as a result of 0.0 / 0.0). If all coordinates are NAN, this means
// that the box is reduced to a point and the ray has direction 0,
// in which case this returns -1
if (tmax >= 0.0f && tmin <= tmax) {
return std::fmaxf(tmin, 0.0f);
}
return -1.0f;
}
bool Intersects(const AABB& aabb1, const AABB& aabb2) {
return false;
}
bool Intersects(const Ray& ray, const AABB& aabb) {
return Distance(ray, aabb) >= 0.0f;
}
} // namespace maths
} // namespace blitz

View File

@@ -1,6 +1,6 @@
#include "blitz/misc/Easing.h" #include "blitz/misc/Easing.h"
#include "blitz/misc/Maths.h" #include "blitz/maths/Maths.h"
namespace blitz { namespace blitz {
namespace utils { namespace utils {

View File

@@ -14,6 +14,7 @@ typedef std::unique_ptr<Packet> PacketPtr;
static std::array<PacketPtr, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets = { static std::array<PacketPtr, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets = {
std::make_unique<PlayerLoginPacket>(), std::make_unique<PlayerLoginPacket>(),
std::make_unique<UpdateHealthPacket>(),
std::make_unique<ConnexionInfoPacket>(), std::make_unique<ConnexionInfoPacket>(),
std::make_unique<PlayerJoinPacket>(), std::make_unique<PlayerJoinPacket>(),
std::make_unique<PlayerLeavePacket>(), std::make_unique<PlayerLeavePacket>(),

View File

@@ -25,6 +25,7 @@ REGISTER_DISPATCH_CLASS(ServerTpsPacket)
REGISTER_DISPATCH_CLASS(ChatPacket) REGISTER_DISPATCH_CLASS(ChatPacket)
REGISTER_DISPATCH_CLASS(PlayerPositionAndRotationPacket) REGISTER_DISPATCH_CLASS(PlayerPositionAndRotationPacket)
REGISTER_DISPATCH_CLASS(PlayerShootPacket); REGISTER_DISPATCH_CLASS(PlayerShootPacket);
REGISTER_DISPATCH_CLASS(UpdateHealthPacket);
} // namespace protocol } // namespace protocol
} // namespace blitz } // namespace blitz

View File

@@ -1,6 +1,6 @@
#include "blitz/protocol/packets/ChatPacket.h" #include "blitz/protocol/packets/ChatPacket.h"
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
#include "blitz/misc/Log.h" #include "blitz/misc/Log.h"
#include <map> #include <map>
#include <sstream> #include <sstream>

View File

@@ -0,0 +1,19 @@
#include "blitz/protocol/packets/UpdateHealthPacket.h"
namespace blitz {
namespace protocol {
DataBuffer UpdateHealthPacket::Serialize(bool packetID) const {
DataBuffer data;
WritePacketID(data, packetID);
data << m_NewHealth;
return data;
}
void UpdateHealthPacket::Deserialize(DataBuffer& data) {
data >> m_NewHealth;
}
} // namespace protocol
} // namespace blitz

View File

@@ -1,8 +1,8 @@
#include "client/display/PlayerController.h" #include "client/display/PlayerController.h"
#include "blitz/game/Player.h" #include "blitz/game/Player.h"
#include "blitz/maths/Maths.h"
#include "blitz/misc/Log.h" #include "blitz/misc/Log.h"
#include "blitz/misc/Maths.h"
#include "client/Client.h" #include "client/Client.h"
#include "client/display/InputManager.h" #include "client/display/InputManager.h"
#include "imgui.h" #include "imgui.h"

View File

@@ -1,12 +1,14 @@
#include "client/game/ClientGame.h" #include "client/game/ClientGame.h"
#include "blitz/misc/Log.h" #include "blitz/misc/Log.h"
#include "blitz/misc/Random.h"
#include "blitz/protocol/PacketDispatcher.h" #include "blitz/protocol/PacketDispatcher.h"
#include "blitz/protocol/packets/PlayerJoinPacket.h" #include "blitz/protocol/packets/PlayerJoinPacket.h"
#include "blitz/protocol/packets/PlayerLeavePacket.h" #include "blitz/protocol/packets/PlayerLeavePacket.h"
#include "blitz/protocol/packets/PlayerListPacket.h" #include "blitz/protocol/packets/PlayerListPacket.h"
#include "blitz/protocol/packets/PlayerPositionAndRotationPacket.h" #include "blitz/protocol/packets/PlayerPositionAndRotationPacket.h"
#include "blitz/protocol/packets/PlayerShootPacket.h" #include "blitz/protocol/packets/PlayerShootPacket.h"
#include "blitz/protocol/packets/UpdateHealthPacket.h"
#include "client/Client.h" #include "client/Client.h"
namespace blitz { namespace blitz {
@@ -27,6 +29,7 @@ void ClientGame::RegisterHandlers() {
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerList, this); GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerList, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerPositionAndRotation, this); GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerPositionAndRotation, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerShoot, this); GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerShoot, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateHealth, this);
} }
void ClientGame::HandlePacket(const protocol::PlayerJoinPacket* packet) { void ClientGame::HandlePacket(const protocol::PlayerJoinPacket* packet) {
@@ -48,6 +51,17 @@ void ClientGame::HandlePacket(const protocol::PlayerListPacket* packet) {
} }
} }
void ClientGame::HandlePacket(const protocol::UpdateHealthPacket* packet) {
game::Player* player = m_Client->GetGame()->GetPlayerById(m_Client->GetPlayerID());
player->SetHP(packet->GetNewHealth());
// we are dead
if (player->GetHP() <= 0.0f) {
player->SetPosition({utils::GetRandomReal(-10.0f, 10.0f), 0, utils::GetRandomReal(-10.0f, 10.0f)});
}
}
void ClientGame::HandlePacket(const protocol::PlayerShootPacket* packet) { void ClientGame::HandlePacket(const protocol::PlayerShootPacket* packet) {
m_Client->NotifyListeners( m_Client->NotifyListeners(
&GuiListener::OnPlayerShoot, packet->GetPlayer(), packet->GetPosition(), packet->GetYaw(), packet->GetPitch()); &GuiListener::OnPlayerShoot, packet->GetPlayer(), packet->GetPosition(), packet->GetYaw(), packet->GetPitch());

View File

@@ -1,8 +1,9 @@
#include "client/gui/Hud.h" #include "client/gui/Hud.h"
#include "client/Client.h"
#include "client/game/ClientGame.h"
#include "client/gui/GuiWidget.h" #include "client/gui/GuiWidget.h"
#include "client/render/loader/TextureLoader.h" #include "client/render/loader/TextureLoader.h"
#include <client/Client.h>
#include <imgui.h> #include <imgui.h>
namespace blitz { namespace blitz {
@@ -37,28 +38,16 @@ void Hud::Draw(const char* title, bool* p_open) {
ImGui::Image(reinterpret_cast<ImTextureID>(m_JP), jpSize); ImGui::Image(reinterpret_cast<ImTextureID>(m_JP), jpSize);
ImGui::SameLine(0.0f, paddingHeight); ImGui::SameLine(0.0f, paddingHeight);
// ImGui::EndGroup();
// ImGui::SetCursorPosX(pvBarPos.x);
// ImGui::SetCursorPosY(pvBarPos.y);
ImGui::BeginGroup(); ImGui::BeginGroup();
{ {
// Animate a simple progress bar game::Player* player = m_Client->GetGame()->GetPlayerById(m_Client->GetPlayerID());
static float progress = 0.0f, progress_dir = 1.0f;
progress += progress_dir * 0.4f * ImGui::GetIO().DeltaTime;
if (progress >= +1.0f) {
progress = +1.0f;
progress_dir *= -1.0f;
}
if (progress <= 0.0f) {
progress = 0.0f;
progress_dir *= -1.0f;
}
int pv = static_cast<int>(progress * 100);
ImGui::Text("PV : %i", pv); if (player) {
// ImGui::Dummy(); ImGui::Text("PV : %.0f", player->GetHP());
ImGui::ProgressBar(progress, progressSize, "");
ImGui::ProgressBar(static_cast<float>(player->GetHP() / 100.0f), progressSize, "");
}
} }
ImGui::EndGroup(); ImGui::EndGroup();
ImGui::EndGroup(); ImGui::EndGroup();

View File

@@ -1,7 +1,7 @@
#include "client/render/BulletRenderer.h" #include "client/render/BulletRenderer.h"
#include "blitz/maths/Maths.h"
#include "blitz/misc/Log.h" #include "blitz/misc/Log.h"
#include "blitz/misc/Maths.h"
#include "blitz/misc/Test.h" #include "blitz/misc/Test.h"
#include "client/render/Camera.h" #include "client/render/Camera.h"
#include "client/render/OpenGL.h" #include "client/render/OpenGL.h"

View File

@@ -1,7 +1,7 @@
#include "client/render/Camera.h" #include "client/render/Camera.h"
#include "blitz/game/Player.h" #include "blitz/game/Player.h"
#include "blitz/misc/Maths.h" #include "blitz/maths/Maths.h"
#include "imgui.h" #include "imgui.h"
namespace blitz { namespace blitz {

View File

@@ -1,9 +1,9 @@
#include "client/render/MainRenderer.h" #include "client/render/MainRenderer.h"
#include "blitz/maths/Maths.h"
#include "blitz/misc/Easing.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/Test.h" #include "blitz/misc/Test.h"
#include "blitz/misc/Time.h" #include "blitz/misc/Time.h"
#include "client/Client.h" #include "client/Client.h"

View File

@@ -170,6 +170,7 @@ void Server::AddBot() {
// set the bot at a random location to be able to see several of them // set the bot at a random location to be able to see several of them
game::Player* botPlayer = m_Game.GetPlayerById(botID); game::Player* botPlayer = m_Game.GetPlayerById(botID);
botPlayer->SetPosition({utils::GetRandomReal(-10.0f, 10.0f), 0.0f, utils::GetRandomReal(-10.0f, 10.0f)}); botPlayer->SetPosition({utils::GetRandomReal(-10.0f, 10.0f), 0.0f, utils::GetRandomReal(-10.0f, 10.0f)});
botPlayer->SetBot();
} }
} // namespace server } // namespace server

View File

@@ -134,6 +134,8 @@ void ServerConnexion::HandlePacket(const protocol::PlayerPositionAndRotationPack
void ServerConnexion::HandlePacket(const protocol::PlayerShootPacket* packet) { void ServerConnexion::HandlePacket(const protocol::PlayerShootPacket* packet) {
protocol::PlayerShootPacket broadcastShoot(packet->GetPosition(), packet->GetYaw(), packet->GetPitch(), m_Player->GetID()); protocol::PlayerShootPacket broadcastShoot(packet->GetPosition(), packet->GetYaw(), packet->GetPitch(), m_Player->GetID());
m_Server->BroadcastPacket(&broadcastShoot); m_Server->BroadcastPacket(&broadcastShoot);
m_Server->GetGame().CheckShoot(m_Player->GetID(), packet->GetPosition(), packet->GetYaw(), packet->GetPitch());
} }
void ServerConnexion::Start() { void ServerConnexion::Start() {

View File

@@ -1,11 +1,15 @@
#include "server/game/ServerGame.h" #include "server/game/ServerGame.h"
#include "blitz/maths/Physics.h"
#include "blitz/misc/Format.h" #include "blitz/misc/Format.h"
#include "blitz/misc/Log.h" #include "blitz/misc/Log.h"
#include "blitz/misc/Random.h"
#include "blitz/protocol/packets/ChatPacket.h" #include "blitz/protocol/packets/ChatPacket.h"
#include "blitz/protocol/packets/PlayerJoinPacket.h" #include "blitz/protocol/packets/PlayerJoinPacket.h"
#include "blitz/protocol/packets/PlayerPositionAndRotationPacket.h" #include "blitz/protocol/packets/PlayerPositionAndRotationPacket.h"
#include "blitz/protocol/packets/UpdateHealthPacket.h"
#include "server/Server.h" #include "server/Server.h"
#include <cmath>
namespace blitz { namespace blitz {
namespace server { namespace server {
@@ -27,6 +31,33 @@ void ServerGame::SendPlayerPositions() {
} }
} }
void ServerGame::CheckShoot(game::PlayerID shooter, Vec3f position, float yaw, float pitch) {
maths::Ray shootRay;
shootRay.origin = position + Vec3f{0.0, 1.75, 0.0};
shootRay.direction = {
std::cos(yaw) * std::cos(pitch),
std::sin(pitch),
std::sin(yaw) * std::cos(pitch),
};
static const maths::AABB playerStaticAABB = {Vec3f{-0.5f, 0.0f, -0.5f}, Vec3f{0.5f, 1.8f, 0.5f}};
bool shootLanded = false;
game::Player* shooterPlayer = GetPlayerById(shooter);
for (auto& [playerId, player] : GetPlayers()) {
if (playerId != shooter && maths::Intersects(shootRay, playerStaticAABB + player.GetPosition())) {
DamagePlayer(player, *shooterPlayer);
shootLanded = true;
}
}
shooterPlayer->GetStats().m_ShootCount++;
if (shootLanded)
shooterPlayer->GetStats().m_ShootSuccessCount++;
}
void ServerGame::AddPlayer(game::PlayerID player, const std::string& name) { void ServerGame::AddPlayer(game::PlayerID player, const std::string& name) {
Game::AddPlayer(player, name); Game::AddPlayer(player, name);
@@ -43,5 +74,30 @@ void ServerGame::RemovePlayer(game::PlayerID player) {
Game::RemovePlayer(player); Game::RemovePlayer(player);
} }
void ServerGame::DamagePlayer(game::Player& player, game::Player& shooter) {
static float MAX_HP = 100;
static float GUN_DAMAGE = 50;
UpdateHP(player, player.GetHP() - GUN_DAMAGE);
// player is dead
if (player.GetHP() <= 0.0f) {
UpdateHP(player, MAX_HP);
player.GetStats().m_Deaths++;
shooter.GetStats().m_Kills++;
player.SetPosition({utils::GetRandomReal(-10.0f, 10.0f), 0.0f, utils::GetRandomReal(-10.0f, 10.0f)});
}
}
void ServerGame::UpdateHP(game::Player& player, float newHP) {
player.SetHP(newHP);
if (player.IsBot())
return;
protocol::UpdateHealthPacket packet(player.GetHP());
m_Server->GetConnexions().at(player.GetID())->SendPacket(&packet);
}
} // namespace server } // namespace server
} // namespace blitz } // namespace blitz

105
test/test_intersects.cpp Executable file
View File

@@ -0,0 +1,105 @@
#include "blitz/misc/Test.h"
#include "blitz/maths/Physics.h"
#include "blitz/misc/Log.h"
#include "blitz/misc/Format.h"
#include <iostream>
#include <ostream>
#include <limits>
using namespace blitz;
using namespace maths;
#define let auto // sexy boiiii
static void test_left() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {-2.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_right() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {2.0f, 0.0f, 0.0f}, {-1.0f, 0.0f, 0.0f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_forward() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.0f, 2.0f, 0.0f}, {0.0f, -1.0f, 0.0f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_backward() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.0f, -2.0f, 0.0f}, {0.0f, 1.0f, 0.0f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_above() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.0f, 0.0f, 2.0f}, {0.0f, 0.0f, -1.0f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_below() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.0f, 0.0f, -2.0f}, {0.0f, 0.0f, 1.0f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_shifted() {
let box = AABB { {-1.0f, -1.0f, -1.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {-3.0f, 0.0f, 100.0f}, {1.0f, 0.0f, 0.0f} };
blitz_test_assert(!Intersects(ray, box));
}
static void test_corner_inside() {
let box = AABB { {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.0f, 0.0f, 0.0f}, {0.5f, 0.5f, 0.5f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_corner_inside_weird() {
let box = AABB { {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_corner_inside_opposite_big() {
let box = AABB { {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.0f, 0.0f, 0.0f}, {1.5f, 1.5f, 1.5f} };
blitz_test_assert(Intersects(ray, box));
}
static void test_corner_inside_opposite_weird() {
let box = AABB { {0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f} };
let ray = Ray { {0.4f, 0.4f, 0.4f}, {-1.0f, -1.0f, -1.0f} };
blitz_test_assert(Intersects(ray, box));
}
int main(int argc, const char* args[]) {
test_left();
test_right();
test_forward();
test_backward();
test_below();
test_above();
test_shifted();
test_corner_inside();
test_corner_inside_weird();
test_corner_inside_opposite_big();
test_corner_inside_opposite_weird();
return BLITZ_TEST_SUCCESSFUL;
}

View File

@@ -1,6 +1,6 @@
#include "blitz/misc/Test.h" #include "blitz/misc/Test.h"
#include "blitz/common/Vector.h" #include "blitz/maths/Vector.h"
using namespace blitz; using namespace blitz;