Skip to content

Commit

Permalink
Add basic Player class
Browse files Browse the repository at this point in the history
Adds a basic player class to move around the map.
A rifle is attached to the player.
  • Loading branch information
Notiooo committed Nov 30, 2023
1 parent 1143953 commit a030091
Show file tree
Hide file tree
Showing 24 changed files with 11,540 additions and 48 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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/Models/ak47/ak47-alternative.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10,967 changes: 10,967 additions & 0 deletions AimGL/resources/Models/ak47/ak47.obj

Large diffs are not rendered by default.

Binary file added AimGL/resources/Models/ak47/ak47.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions AimGL/resources/Models/ak47/credits.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://sketchfab.com/3d-models/lowpoly-ak47-9d2d20e53cf7428c8c993f6aee04dc88
Model made by Firewarden
44 changes: 29 additions & 15 deletions AimGL/resources/Shaders/Graphics/Model/ModelTextured.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,58 @@ in vec3 FragPos;
in vec2 TexCoords;

uniform vec3 lightPos = vec3(3, 3, 3);
uniform vec3 cameraPosition;
vec3 lightAmbient = vec3(0.3f, 0.3f, 0.3f);
vec3 lightDiffuse = vec3(0.6f, 0.6f, 0.6f);
vec3 lightSpecular = vec3(1.0f, 1.0f, 1.0f);
uniform vec3 lightColor = vec3(1,1,1);
uniform vec3 cameraPosition;

struct Light {
vec3 position;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};

struct Material {
bool isDiffusePresent;
bool isSpecularPresent;
sampler2D diffuse;
sampler2D specular; // not used yet
sampler2D specular;
};

uniform Material material;

void main()
{
// ambient
float ambientStrength = 0.1;
vec3 ambientColor = vec3(1.0, 1.0, 1.0);
vec3 ambient = ambientStrength * ambientColor;
vec3 ambient = lightAmbient;
if(material.isDiffusePresent)
{
ambient = ambient * texture(material.diffuse, TexCoords).rgb;
}

// diffuse
vec3 norm = normalize(FragmentNormal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuseLight = diff * lightColor;
vec3 diffuse = lightDiffuse * diff;
if(material.isDiffusePresent)
{
diffuse = diffuse * texture(material.diffuse, TexCoords).rgb;
}

// specular
float specularStrength = 0.5;
vec3 viewDir = normalize(cameraPosition - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 16);
vec3 specular = specularStrength * spec * lightColor;

// Combine texture color with lighting
vec3 textureColor = vec3(1,1,1);
if(material.isDiffusePresent)
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 64.f);
vec3 specular = lightSpecular * spec;
if(material.isSpecularPresent)
{
textureColor = vec3(texture(material.diffuse, TexCoords));
specular = specular * texture(material.specular, TexCoords).rgb;
}
vec3 result = textureColor * (ambient + diffuseLight) + specular;

vec3 result = ambient + diffuse + specular;
FragColor = vec4(result, 1.0);
}
Binary file added AimGL/resources/Textures/crosshair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ set(PROJECT_SOURCES
Renderer/Graphics/3D/Sprite3D.cpp
Renderer/Graphics/3D/Core/Mesh.cpp
Renderer/Graphics/3D/Model.cpp
Renderer/Graphics/3D/Utils/Rotation3D.cpp
Renderer/Renderer.cpp
Resources/ObjLoader.cpp
States/State.cpp
States/StateStack.cpp
States/CustomStates/LogoState.cpp
States/CustomStates/ExitGameState.cpp
States/CustomStates/GameState.cpp
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
Utils/Mouse.cpp
Expand Down
2 changes: 1 addition & 1 deletion AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Game::Game()
mAppStack.saveState<GameState>(State_ID::GameState, *mGameWindow);

// Initial state of the statestack is TitleState
mAppStack.push(State_ID::LogoState);
mAppStack.push(State_ID::GameState);
}

void Game::run()
Expand Down
168 changes: 168 additions & 0 deletions AimGL/src/Player/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include "Player.h"
#include "Utils/Lerp.h"
#include "pch.h"

Player::Player(WindowToRender& window)
: mCamera(window)
, 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}})
, mCrosshairTexture("resources/Textures/crosshair.png")
, mCrosshair(mCrosshairTexture)
{
mCrosshair.setPosition({window.getSize().x / 2.f, window.getSize().y / 2.f},
Sprite2D::Origin::Center);
mCrosshair.setOpacity(0.8f);
mCamera.cameraPosition({mPosition.x, mPosition.y + PLAYER_HEGIHT, mPosition.z});
mGun.setPosition(mCamera.cameraPosition(), Model::Origin::Center);
}

void Player::draw(const Renderer& target) const
{
glDepthRange(0.0, 0.01);
mGun.draw(target, mCamera);
glDepthRange(0.0, 1.0);

mCrosshair.draw(target);
}

void Player::updateGunPosition(const float& deltaTime)
{
const glm::vec3 rotationOrigin = {0.3f, -0.2f, -0.35f};
const glm::vec3 targetPosition = {mPosition.x + rotationOrigin.x,
mPosition.y + PLAYER_HEGIHT * 5 / 8,
mPosition.z + rotationOrigin.z};

const auto gunPosition = lerp(mGun.position(), targetPosition, deltaTime * 30.f);
mGun.setPosition(gunPosition, Model::Origin::Center);
mGun.setRotationOrigin(rotationOrigin);

const auto gunTargetPitch = lerp(mGun.rotation().pitch, mCamera.pitch(), deltaTime * 20.f);
const auto gunTargetYaw = lerp(mGun.rotation().yaw, -(mCamera.yaw() + 90), deltaTime * 20.f);
mGun.setRotation({gunTargetYaw, gunTargetPitch, 0});
}

void Player::update(const float& deltaTime)
{
mCamera.cameraPosition({mPosition.x, mPosition.y + PLAYER_HEGIHT, mPosition.z});
mCamera.update(deltaTime);
updateGunPosition(deltaTime);
}

void Player::updatePhysics(float deltaTime)
{
handleMovementKeyboardInputs(deltaTime);
decelerateVelocity(deltaTime);
manageVerticalVelocity(deltaTime);
limitVelocity(deltaTime);

mPosition += mVelocity;
if (mPosition.y < 0)
{
mPosition.y = 0;
}
}

void Player::decelerateVelocity(const float& deltaTime)
{
float decayFactor = exp(-PLAYER_WALKING_DECELERATE_RATIO * deltaTime);
mVelocity.x *= decayFactor;
mVelocity.z *= decayFactor;
}

void Player::manageVerticalVelocity(const float& deltaTime)
{
constexpr auto G = -9.81f * 0.01f;
mVelocity.y += G * deltaTime;
}

void Player::limitVelocity(const float& deltaTime)
{
static auto limitVelocitySpeed = [](auto& velocity, const auto& maxVelocity)
{
if (velocity > maxVelocity)
{
velocity = maxVelocity;
}
else if (-velocity > maxVelocity)
{
velocity = -maxVelocity;
}
};

auto playerVelocity = PLAYER_MAX_HORIZONTAL_SPEED * deltaTime;
limitVelocitySpeed(mVelocity.x, playerVelocity);
limitVelocitySpeed(mVelocity.z, playerVelocity);


if (-mVelocity.y > PLAYER_MAX_FALLING_SPEED)
{
mVelocity.y = -PLAYER_MAX_FALLING_SPEED;
}
}

void Player::fixedUpdate(const float& deltaTime)
{
updatePhysics(deltaTime);
}

void Player::handleMovementKeyboardInputs(const float& deltaTime)
{
auto ACCELERATION_RATIO = 0.1f;
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f);

if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
direction += mCamera.directionWithoutPitch();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
direction -= mCamera.directionWithoutPitch();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
direction += mCamera.rightDirectionWithoutPitch();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
direction -= mCamera.rightDirectionWithoutPitch();
}

if (glm::length(direction) > 0.0f)
{
direction = glm::normalize(direction);// Normalize the combined direction
const auto finalSpeed = PLAYER_WALKING_SPEED * ACCELERATION_RATIO * deltaTime;
mVelocity += finalSpeed * direction;
}
}

void Player::tryJump()
{
if (isOnGround())
{
mVelocity.y = PLAYER_JUMP_FORCE;
}
}

void Player::handleEvent(const sf::Event& event)
{
switch (event.key.code)
{
case sf::Keyboard::Space: tryJump(); break;
}
}

Camera& Player::camera()
{
return mCamera;
}

const Camera& Player::camera() const
{
return mCamera;
}

bool Player::isOnGround() const
{
return mPosition.y <= 0;
}
Loading

0 comments on commit a030091

Please sign in to comment.