Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Dec 24, 2023
1 parent 04a7568 commit 42c1d9e
Show file tree
Hide file tree
Showing 17 changed files with 4,095 additions and 12 deletions.
3,852 changes: 3,852 additions & 0 deletions AimGL/resources/Models/button_stand/button-stand.obj

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Sounds/button-click.wav
Binary file not shown.
3 changes: 3 additions & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ set(PROJECT_SOURCES
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/Scene/Nodes/Node.cpp
World/Scene/GameObjects/Rifle.cpp
World/Scene/GameObjects/PreviewTarget.cpp
World/Scene/GameObjects/ButtonStand.cpp
World/Physics/Drawable/AABB.cpp
World/Physics/Drawable/Ray.cpp
World/Physics/Drawable/DrawableCollider.cpp
Expand All @@ -37,5 +39,6 @@ set(PROJECT_SOURCES
World/Physics/RectangleCollider.cpp
World/Physics/SphereCollider.cpp
World/Physics/Collisions.cpp
Utils/Transform.cpp
Utils/Mouse.cpp
)
19 changes: 9 additions & 10 deletions AimGL/src/Renderer/Graphics/3D/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Model::Model(const std::string& objFilePath, const std::vector<TextureType>& tex

void Model::draw(const Renderer& target, const Camera& camera) const
{
mShader.bind();
mShader.setUniform("model", mLastCalculatedModel);
mShader.unbind();
mMesh->draw(target, camera, mShader);
}

Expand Down Expand Up @@ -86,14 +89,10 @@ glm::vec3 Model::dimensions() const

void Model::updateModel()
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(mPosition));
model = glm::translate(model, -mRotationOrigin);
model = mRotation.rotate(model);
model = glm::translate(model, mRotationOrigin);
model = glm::scale(model, glm::vec3(mScale, mScale, mScale));

mShader.bind();
mShader.setUniform("model", model);
mShader.unbind();
mLastCalculatedModel = glm::mat4(1.0f);
mLastCalculatedModel = glm::translate(mLastCalculatedModel, glm::vec3(mPosition));
mLastCalculatedModel = glm::translate(mLastCalculatedModel, -mRotationOrigin);
mLastCalculatedModel = mRotation.rotate(mLastCalculatedModel);
mLastCalculatedModel = glm::translate(mLastCalculatedModel, mRotationOrigin);
mLastCalculatedModel = glm::scale(mLastCalculatedModel, glm::vec3(mScale, mScale, mScale));
}
2 changes: 2 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Renderer/Renderer.h>
#include <Resources/ObjLoader.h>

class Transform;
/**
* \brief Texture along with the type of texture it represents
*/
Expand Down Expand Up @@ -114,4 +115,5 @@ class Model
glm::vec3 mRotationOrigin{0, 0, 0};
float mScale{1};
Rotation3D mRotation;
glm::mat4 mLastCalculatedModel;
};
2 changes: 1 addition & 1 deletion AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Rotation3D& Rotation3D::operator*=(const Rotation3D& rhs)
return *this;
}

Rotation3D::operator glm::vec<3, float>()
Rotation3D::operator glm::vec<3, float>() const
{
return mRotation;
}
Expand Down
2 changes: 1 addition & 1 deletion AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Rotation3D
Rotation3D& operator-=(const Rotation3D&);
Rotation3D& operator*=(const Rotation3D&);
friend Rotation3D operator*(const Rotation3D& lhs, float rhs);
explicit operator glm::vec3();
explicit operator glm::vec3() const;

/**
* \brief Angles of Euler rotation.
Expand Down
3 changes: 3 additions & 0 deletions AimGL/src/States/CustomStates/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GameState::GameState(StateStack& stack, WindowToRender& window)
, mPhaseInLogoColor({window.getSize().x, window.getSize().y}, {0.067f, 0.11f, 0.18f, 1.1f})
, mTree("resources/Models/tree/tree.obj",
{{"resources/Models/tree/tree_combined.png", Texture::Type::Diffuse}})
, mButtonStand(mColliderRegister, {0, 0, 0})
{
Mouse::lockMouseAtCenter(mWindow);
mTree.setScale(0.2f);
Expand Down Expand Up @@ -47,6 +48,7 @@ void GameState::draw(sf::Window& target) const
mTree.draw(mRenderer, mPlayer.camera());
mLogo.draw(mRenderer, mPlayer.camera());
mPhaseInLogoColor.draw(mRenderer);
mButtonStand.draw(mRenderer, mPlayer.camera());

for (auto& previewTarget: mPreviewTargets)
{
Expand All @@ -70,6 +72,7 @@ bool GameState::update(const float& deltaTime)
{
previewTarget->update(deltaTime);
}
mButtonStand.update(deltaTime);
mPlayer.update(deltaTime);

if (mPhaseInLogoColor.opacity() > 0)
Expand Down
2 changes: 2 additions & 0 deletions AimGL/src/States/CustomStates/GameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <Player/Player.h>
#include <Renderer/Graphics/3D/Model.h>
#include <World/Physics/ColliderRegister.h>
#include <World/Scene/GameObjects/ButtonStand.h>
#include <World/Scene/GameObjects/PreviewTarget.h>

class StateStack;
Expand Down Expand Up @@ -66,4 +67,5 @@ class GameState : public State
InfiniteGridFloor mInfiniteGridFloor;
Model mTree;
std::vector<std::unique_ptr<PreviewTarget>> mPreviewTargets;
ButtonStand mButtonStand;
};
27 changes: 27 additions & 0 deletions AimGL/src/Utils/Transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Transform.h"
#include "pch.h"

#include <Renderer/Graphics/3D/Utils/Rotation3D.h>

glm::mat4 Transform::matrix() const
{
return mTransform;
}

Transform& Transform::translate(const glm::vec3& vec)
{
glm::translate(mTransform, vec);
return *this;
}

Transform& Transform::rotate(const Rotation3D& rotation)
{
mTransform = rotation.rotate(mTransform);
return *this;
}

Transform& Transform::operator*=(const Transform& rhs)
{
mTransform *= rhs.mTransform;
return *this;
}
17 changes: 17 additions & 0 deletions AimGL/src/Utils/Transform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#pragma once

class Rotation3D;

class Transform
{
public:
[[nodiscard]] glm::mat4 matrix() const;
Transform& translate(const glm::vec3& vec);
Transform& rotate(const Rotation3D& rotation);
Transform& operator*=(const Transform& rhs);


private:
glm::mat4 mTransform{1};
};
42 changes: 42 additions & 0 deletions AimGL/src/World/Scene/GameObjects/ButtonStand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "ButtonStand.h"
#include "pch.h"

ButtonStand::ButtonStand(ColliderRegister& colliderRegister, const glm::vec3& position)
: mButtonStand("resources/Models/button_stand/button-stand.obj",
{{"resources/Models/button_stand/button-stand.png", Texture::Type::Diffuse}})
, mColliderRegister(colliderRegister)
, mCollisionBox(mColliderRegister, {0, 0, 0}, mButtonStand.dimensions())
, mButtonShotTriggerBox(
colliderRegister, {0, 0, 0},
{mButtonStand.dimensions().x / 2.f, 0.1, mButtonStand.dimensions().z / 2.f})
{
mButtonStand.setPosition(position);
mCollisionBox.setPosition(mButtonStand.position() - mButtonStand.dimensions() / 2.f);

auto shotTriggerBoxPosition = mButtonStand.position();
shotTriggerBoxPosition.x -= mButtonStand.dimensions().x / 4.f;
shotTriggerBoxPosition.z -= mButtonStand.dimensions().z / 4.f;
shotTriggerBoxPosition.y += (mButtonStand.dimensions().y / 2.f) - 0.1;
mButtonShotTriggerBox.setPosition(shotTriggerBoxPosition);
}

void ButtonStand::draw(const Renderer& target, const Camera& camera) const
{
mButtonStand.draw(target, camera);
mCollisionBox.draw(target, camera);
mButtonShotTriggerBox.draw(target, camera);
}

void ButtonStand::update(const float& deltaTime)
{
// Nothing I guess?
}

void ButtonStand::fixedUpdate(const float& deltaTime)
{
}

void ButtonStand::handleEvent(const sf::Event& event)
{
// Nothing I guess?
}
45 changes: 45 additions & 0 deletions AimGL/src/World/Scene/GameObjects/ButtonStand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#include <Renderer/Graphics/3D/Model.h>
#include <World/Physics/Drawable/AABB.h>

class ColliderRegister;

class ButtonStand
{
public:
/**
* \brief Constructor of Button Stand class
* \param colliderRegister Register in which all collisions on the scene should be located
* \param position
*/
ButtonStand(ColliderRegister& colliderRegister, const glm::vec3& position);

/**
* \brief Draws a Button Stand to a given target
* \param target The target to which the sprite is drawn
* \param camera TODO: THIS
*/
void draw(const Renderer& target, const Camera& camera) const;

/**
* Updates the Button Stand 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);

void fixedUpdate(const float& deltaTime);

/**
* \brief It takes input (event) from the user and interprets it
* \param event user input
*/
void handleEvent(const sf::Event& event);

private:
Model mButtonStand;
sf::SoundBuffer mSoundBuffer;
sf::Sound mClickSound;
ColliderRegister& mColliderRegister;
AABB mCollisionBox;
AABB mButtonShotTriggerBox;
};
52 changes: 52 additions & 0 deletions AimGL/src/World/Scene/Nodes/Node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "Node.h"
#include "pch.h"

void Node::pinNode(NodePtr node)
{
node->mParent = this;
mPinnedNodes.emplace_back(std::move(node));
}

cpp::result<Node::NodePtr, Node::Result> Node::unpinNode(const Node& node)
{
auto foundNode = std::find_if(mPinnedNodes.begin(), mPinnedNodes.end(),
[&node](const NodePtr& containerNode)
{
return containerNode.get() == &node;
});
if (foundNode == mPinnedNodes.end())
{
spdlog::critical("Trying to unpin a node that does not exist in Node container!");
return cpp::fail(Result::NodeNotFound);
}

auto nodePtr = std::move(*foundNode);
mPinnedNodes.erase(foundNode);
nodePtr->mParent = nullptr;
return nodePtr;
}

void Node::draw(const Renderer& target) const
{
drawThis(target, mTransform);

for (const auto& pinnedNode: mPinnedNodes)
{
pinnedNode->draw(target, mTransform);
}
}

void Node::drawThis(const Renderer& target, const Transform& transform) const
{
// Should be implemented
}

void Node::draw(const Renderer& target, Transform transform) const
{
transform *= mTransform;
drawThis(target, transform);
for (const auto& pinnedNode: mPinnedNodes)
{
pinnedNode->draw(target, transform);
}
}
36 changes: 36 additions & 0 deletions AimGL/src/World/Scene/Nodes/Node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <Utils/Transform.h>
#include <result.hpp>

class Renderer;

class Node
{
public:
enum class Result
{
NodeNotFound
};

using NodePtr = std::unique_ptr<Node>;
virtual ~Node() = default;

void pinNode(NodePtr node);
cpp::result<NodePtr, Result> unpinNode(const Node& node);
void draw(const Renderer& target) const;
void update(const float& deltaTime);
void handleEvent(const sf::Event& event);

protected:
virtual void drawThis(const Renderer& target, const Transform& transform) const;
virtual void updateThis(const float& deltaTime);
virtual void handleEventThis(const sf::Event& event);

private:
void draw(const Renderer& target, Transform transform) const;

std::list<NodePtr> mPinnedNodes;
Node* mParent{nullptr};
glm::vec3 mPosition{0, 0, 0};
Transform mTransform;
};
3 changes: 3 additions & 0 deletions AimGL/src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@
// Logging
#include "minitrace.h"
#include "spdlog/spdlog.h"

// Other
#include <result.hpp>

0 comments on commit 42c1d9e

Please sign in to comment.