From 23f8d2660aa186548a2eb0e71622e56891fa24f9 Mon Sep 17 00:00:00 2001 From: Notiooo Date: Fri, 15 Dec 2023 00:01:09 +0100 Subject: [PATCH] Replace currently used Ray with the new one The rifle used the old implementation of the ray all the time. Now it is removed and the new implementation of the drawable ray is used instead. --- AimGL/src/Player/Player.cpp | 4 +- AimGL/src/Player/Player.h | 3 +- AimGL/src/States/CustomStates/GameState.cpp | 3 +- AimGL/src/States/CustomStates/GameState.h | 2 + AimGL/src/World/Physics/Ray.cpp | 27 ------------ AimGL/src/World/Physics/Ray.h | 47 --------------------- AimGL/src/World/Scene/GameObjects/Rifle.cpp | 6 ++- AimGL/src/World/Scene/GameObjects/Rifle.h | 8 ++-- 8 files changed, 17 insertions(+), 83 deletions(-) delete mode 100644 AimGL/src/World/Physics/Ray.cpp delete mode 100644 AimGL/src/World/Physics/Ray.h diff --git a/AimGL/src/Player/Player.cpp b/AimGL/src/Player/Player.cpp index 52a8e46..0380b01 100644 --- a/AimGL/src/Player/Player.cpp +++ b/AimGL/src/Player/Player.cpp @@ -2,11 +2,11 @@ #include "Utils/Lerp.h" #include "pch.h" -Player::Player(WindowToRender& window) +Player::Player(WindowToRender& window, ColliderRegister& colliderRegister) : mCamera(window) , mCrosshairTexture("resources/Textures/crosshair.png") , mCrosshair(mCrosshairTexture) - , mRifle(mCamera) + , mRifle(mCamera, colliderRegister) { mCrosshair.setPosition({window.getSize().x / 2.f, window.getSize().y / 2.f}, Sprite2D::Origin::Center); diff --git a/AimGL/src/Player/Player.h b/AimGL/src/Player/Player.h index 1edb636..c1c3113 100644 --- a/AimGL/src/Player/Player.h +++ b/AimGL/src/Player/Player.h @@ -20,8 +20,9 @@ class Player /** * \brief Player class constructor * \param window The window into which the player's eye view is rendered + * \param colliderRegister Register in which all collisions on the scene should be located */ - explicit Player(WindowToRender& window); + Player(WindowToRender& window, ColliderRegister& colliderRegister); static constexpr auto PLAYER_HEGIHT = 0.8f; static constexpr auto PLAYER_MAX_HORIZONTAL_SPEED = 5.f; diff --git a/AimGL/src/States/CustomStates/GameState.cpp b/AimGL/src/States/CustomStates/GameState.cpp index 780a9b3..e24b480 100644 --- a/AimGL/src/States/CustomStates/GameState.cpp +++ b/AimGL/src/States/CustomStates/GameState.cpp @@ -9,7 +9,7 @@ GameState::GameState(StateStack& stack, WindowToRender& window) : State(stack) , mWindow(window) - , mPlayer(window) + , mPlayer(window, mColliderRegister) , mRenderer(mWindow) , mLogoTexture("resources/Textures/logo_background.png") , mLogo(mLogoTexture) @@ -45,6 +45,7 @@ bool GameState::fixedUpdate(const float& deltaTime) { MTR_SCOPE("GameState", "GameState::fixedUpdate"); mPlayer.fixedUpdate(deltaTime); + mColliderRegister.updateAllCollisions(); return true; } diff --git a/AimGL/src/States/CustomStates/GameState.h b/AimGL/src/States/CustomStates/GameState.h index dd21974..7bb5a0b 100644 --- a/AimGL/src/States/CustomStates/GameState.h +++ b/AimGL/src/States/CustomStates/GameState.h @@ -9,6 +9,7 @@ #include #include +#include class StateStack; @@ -53,6 +54,7 @@ class GameState : public State private: WindowToRender& mWindow; + ColliderRegister mColliderRegister; Player mPlayer; Renderer mRenderer; Texture mLogoTexture; diff --git a/AimGL/src/World/Physics/Ray.cpp b/AimGL/src/World/Physics/Ray.cpp deleted file mode 100644 index 945c31e..0000000 --- a/AimGL/src/World/Physics/Ray.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Ray.h" -#include "pch.h" - -#include - -Ray::Ray(const glm::vec3& origin, const glm::vec3& direction, float length) - : mOrigin(origin) - , mDirection(glm::normalize(direction)) - , mLength(length) - , mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Physics/Ray.vs"}, - {ShaderType::FragmentShader, "resources/Shaders/Graphics/Physics/Ray.fs"}} -{ - glm::vec3 endPoint = mOrigin + mDirection * mLength; - - std::vector vertices = {mOrigin.x, mOrigin.y, mOrigin.z, - endPoint.x, endPoint.y, endPoint.z}; - mBufferLayout.push(3); - - mVBO.setBuffer(vertices); - mVAO.setBuffer(mVBO, mBufferLayout); -} - -void Ray::draw(const Renderer& target, const Camera& camera) const -{ - constexpr auto numberOfVertices = 6; - target.draw3D(mVAO, numberOfVertices, mShader, camera, Renderer::DrawMode::Lines); -} diff --git a/AimGL/src/World/Physics/Ray.h b/AimGL/src/World/Physics/Ray.h deleted file mode 100644 index 193bb05..0000000 --- a/AimGL/src/World/Physics/Ray.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include "Renderer/Core/Buffers/BufferLayout.h" -#include "Renderer/Core/Buffers/VertexBuffer.h" -#include "Renderer/Core/VertexArray.h" - -#include - -class Camera; -class Renderer; - -/** - * \brief Represents a ray in 3D space, defined by an origin point, direction vector, and length. - */ -class Ray -{ -public: - /** - * \brief Constructs a Ray object. - * \param origin The starting point of the ray in 3D space. - * \param direction The direction vector of the ray. This vector should be normalized. - * \param length (Optional) The length of the ray. Default is 100 units. - */ - Ray(const glm::vec3& origin, const glm::vec3& direction, float length = 100); - Ray(Ray&&) noexcept = default; - - /** - * \brief Draws a Ray for a given target - * \param target The target to which the model is drawn - * \param camera A camera in 3D space that looks at this object - */ - void draw(const Renderer& target, const Camera& camera) const; - - /** - * Updates the Ray logic dependent, or independent of time, every rendered frame. - * \param deltaTime the time that has passed since the game was last updated. - */ - void update(const float& deltaTime); - -private: - VertexArray mVAO; - VertexBuffer mVBO; - BufferLayout mBufferLayout; - glm::vec3 mOrigin; - glm::vec3 mDirection; - float mLength; - Shader mShader; -}; diff --git a/AimGL/src/World/Scene/GameObjects/Rifle.cpp b/AimGL/src/World/Scene/GameObjects/Rifle.cpp index ffa3e3a..ba0d474 100644 --- a/AimGL/src/World/Scene/GameObjects/Rifle.cpp +++ b/AimGL/src/World/Scene/GameObjects/Rifle.cpp @@ -4,11 +4,12 @@ #include #include -Rifle::Rifle(Camera& camera) +Rifle::Rifle(Camera& camera, ColliderRegister& colliderRegister) : mCamera(camera) , mGun("resources/Models/ak47/ak47.obj", {{"resources/Models/ak47/ak47-alternative.png", Texture::Type::Diffuse}, {"resources/Models/ak47/ak47-alternative-specular.png", Texture::Type::Specular}}) + , mColliderRegister(colliderRegister) { mSoundBuffer.loadFromFile("resources/Sounds/gunshot.wav"); mGunShotSound.setBuffer(mSoundBuffer); @@ -64,6 +65,7 @@ void Rifle::handleEvent(const sf::Event& event) mCurrentRecoil = std::min(mCurrentRecoil, RECOIL_OFFSET_MAX); mCamera.shake(); mGunShotSound.play(); - mLatelyShotRay.emplace(mCamera.cameraPosition(), mCamera.direction()); + mLatelyShotRay.emplace(mColliderRegister, mCamera.cameraPosition(), mCamera.direction()); + mLatelyShotRay.value().colliderTag(ColliderTag::GunShot); } } \ No newline at end of file diff --git a/AimGL/src/World/Scene/GameObjects/Rifle.h b/AimGL/src/World/Scene/GameObjects/Rifle.h index d5e6e37..849cedb 100644 --- a/AimGL/src/World/Scene/GameObjects/Rifle.h +++ b/AimGL/src/World/Scene/GameObjects/Rifle.h @@ -1,6 +1,6 @@ #pragma once -#include "World/Physics/Ray.h" -#include +#include "Renderer/Graphics/3D/Model.h" +#include "World/Physics/Drawable/Ray.h" #include #include @@ -16,8 +16,9 @@ class Rifle /** * \brief Constructor of Rifle class * \param camera The camera to which the weapon should be attached + * \param colliderRegister Register in which all collisions on the scene should be located */ - explicit Rifle(Camera& camera); + Rifle(Camera& camera, ColliderRegister& colliderRegister); /** * \brief Draws a Rifle to a given target @@ -55,4 +56,5 @@ class Rifle sf::Sound mGunShotSound; std::optional mLatelyShotRay; std::vector> mRaySubscribers; + ColliderRegister& mColliderRegister; }; \ No newline at end of file