Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ add_executable(${PROJECT_NAME}
src/game/components/enemy.cpp
src/game/components/health.cpp
src/game/components/player.cpp
src/game/components/weapon.cpp
src/game/game-registration.cpp
src/game/systems/collision-system.cpp
src/game/systems/collision-debug-drawer.cpp
Expand Down
16 changes: 12 additions & 4 deletions config/app.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@
"radius": 0.45,
"height": 1.8,
"shape": "Capsule"
}
],
"children": [
},
{
"type": "Health",
"maxHealth": 100.0
},
}
],
"children": [
{
"position": [1, -1, -2],
"rotation": [0, 0, 0],
Expand All @@ -337,6 +337,14 @@
{
"type": "Model Renderer",
"model": "gun"
},
{
"type": "Weapon",
"damage": 25.0,
"range": 100.0,
"fireRate": 0.15,
"maxAmmo": 30,
"reloadTime": 1.5
}
],
"children": [
Expand Down
6 changes: 6 additions & 0 deletions config/playgrounds/model.jsonc
Comment thread
AhmedAmrNabil marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@
{
"type": "Model Renderer",
"model": "gun"
},
{
"type": "Weapon",
"damage": 25,
"range": 50.0,
"fireRate": 0.5
}
],
"children": [
Expand Down
33 changes: 30 additions & 3 deletions src/common/systems/forward-renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../components/post-process-effects.hpp"
#include "../mesh/mesh-utils.hpp"
#include "../texture/texture-utils.hpp"
#include "components/weapon.hpp"

namespace our {

Expand Down Expand Up @@ -146,9 +147,12 @@ namespace our {
opaqueCommands.clear();
transparentCommands.clear();
sceneLights.clear();
weaponCommands.clear();
for (auto entity : world->getEntities()) {
// If we hadn't found a camera yet, we look for a camera in this entity
if (!camera) camera = entity->getComponent<CameraComponent>();

bool isWeapon = entity->getComponent<gameplay::WeaponComponent>() != nullptr;
// If this entity has a mesh renderer component
if (auto meshRenderer = entity->getComponent<MeshRendererComponent>(); meshRenderer) {
// We construct a command from it
Expand All @@ -157,8 +161,9 @@ namespace our {
command.center = glm::vec3(command.localToWorld * glm::vec4(0, 0, 0, 1));
command.mesh = meshRenderer->mesh;
command.material = meshRenderer->material;
// if it is transparent, we add it to the transparent commands list
if (command.material->transparent) {
if (isWeapon) {
weaponCommands.push_back(command);
} else if (command.material->transparent) {
transparentCommands.push_back(command);
} else {
// Otherwise, we add it to the opaque command list
Expand Down Expand Up @@ -190,7 +195,10 @@ namespace our {
command.center = glm::vec3(command.localToWorld * glm::vec4(0, 0, 0, 1));
command.mesh = submesh->mesh;
command.material = submesh->material;
if (command.material->transparent) {

if (isWeapon) {
weaponCommands.push_back(command);
} else if (command.material->transparent) {
transparentCommands.push_back(command);
} else {
opaqueCommands.push_back(command);
Expand Down Expand Up @@ -277,6 +285,25 @@ namespace our {
command.mesh->draw();
}

// render weapon at the end with depth testing cleared
// enable depth mask again so it actually get cleared
glDepthMask(GL_TRUE);
glClear(GL_DEPTH_BUFFER_BIT);

// render weapon
for (const RenderCommand& command : weaponCommands) {
command.material->setup();
glm::mat4 MVP = VP * command.localToWorld;
command.material->shader->set("transform", MVP);
if (LitMaterial* litMaterial = dynamic_cast<LitMaterial*>(command.material); litMaterial) {
// if the material is a lit material, we need to set the light uniforms
litMaterial->setLightUniforms(sceneLights);
litMaterial->shader->set("cameraPos", cameraPosition);
litMaterial->shader->set("model", command.localToWorld);
}
command.mesh->draw();
}

// If there is a postprocess material, apply postprocessing
if (postprocessMaterial) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Expand Down
1 change: 1 addition & 0 deletions src/common/systems/forward-renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace our {
// reallocating them every frame
std::vector<RenderCommand> opaqueCommands;
std::vector<RenderCommand> transparentCommands;
std::vector<RenderCommand> weaponCommands;
// Objects used for rendering a skybox
Mesh* skySphere;
TexturedMaterial* skyMaterial;
Expand Down
17 changes: 17 additions & 0 deletions src/game/components/weapon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "weapon.hpp"

namespace gameplay {

void WeaponComponent::deserialize(const nlohmann::json& data) {
if (!data.is_object()) {
return;
}
damage = data.value("damage", 0);
range = data.value("range", 0.0f);
fireRate = data.value("fireRate", 0.0f);
maxAmmo = data.value("maxAmmo", 0);
reloadTime = data.value("reloadTime", 0.0f);
currentAmmo = maxAmmo; // Initialize current ammo to max ammo when deserialized
}

} // namespace gameplay
22 changes: 22 additions & 0 deletions src/game/components/weapon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <ecs/component.hpp>

namespace gameplay {

class WeaponComponent : public our::Component {
public:
int damage;
float range;
float fireRate;
int maxAmmo;
float reloadTime;
int currentAmmo;
static std::string getID() {
return "Weapon";
}

void deserialize(const nlohmann::json& data) override;
};

} // namespace gameplay
3 changes: 2 additions & 1 deletion src/game/game-registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "components/player-movement.hpp"
#include "components/player.hpp"
#include "components/post-process-effects.hpp"

#include "components/weapon.hpp"
namespace gameplay {

void registerGameComponents() {
Expand All @@ -21,6 +21,7 @@ namespace gameplay {
our::ComponentRegistry::registerType<PlayerComponent>();
our::ComponentRegistry::registerType<PlayerMovementComponent>();
our::ComponentRegistry::registerType<our::PostProcessEffectsComponent>();
our::ComponentRegistry::registerType<WeaponComponent>();

initialized = true;
}
Expand Down
Loading