Skip to content

Commit c116c39

Browse files
committed
feat(weapon): add WeaponComponent with deserialization correct rendering
1 parent e88eaf6 commit c116c39

7 files changed

Lines changed: 70 additions & 4 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ add_executable(${PROJECT_NAME}
262262
src/game/components/enemy.cpp
263263
src/game/components/health.cpp
264264
src/game/components/player.cpp
265+
src/game/components/weapon.cpp
265266
src/game/game-registration.cpp
266267
src/game/systems/collision-system.cpp
267268
src/game/systems/collision-debug-drawer.cpp

config/playgrounds/model.jsonc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@
288288
{
289289
"type": "Model Renderer",
290290
"model": "gun"
291+
},
292+
{
293+
"type": "Weapon",
294+
"damage": 25,
295+
"range": 50.0,
296+
"fireRate": 0.5
291297
}
292298
],
293299
"children": [

src/common/systems/forward-renderer.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../components/model-renderer.hpp"
44
#include "../mesh/mesh-utils.hpp"
55
#include "../texture/texture-utils.hpp"
6+
#include "components/weapon.hpp"
67

78
namespace our {
89

@@ -132,9 +133,12 @@ namespace our {
132133
opaqueCommands.clear();
133134
transparentCommands.clear();
134135
sceneLights.clear();
136+
weaponCommands.clear();
135137
for (auto entity : world->getEntities()) {
136138
// If we hadn't found a camera yet, we look for a camera in this entity
137139
if (!camera) camera = entity->getComponent<CameraComponent>();
140+
141+
bool isWeapon = entity->getComponent<gameplay::WeaponComponent>() != nullptr;
138142
// If this entity has a mesh renderer component
139143
if (auto meshRenderer = entity->getComponent<MeshRendererComponent>(); meshRenderer) {
140144
// We construct a command from it
@@ -143,8 +147,9 @@ namespace our {
143147
command.center = glm::vec3(command.localToWorld * glm::vec4(0, 0, 0, 1));
144148
command.mesh = meshRenderer->mesh;
145149
command.material = meshRenderer->material;
146-
// if it is transparent, we add it to the transparent commands list
147-
if (command.material->transparent) {
150+
if (isWeapon) {
151+
weaponCommands.push_back(command);
152+
} else if (command.material->transparent) {
148153
transparentCommands.push_back(command);
149154
} else {
150155
// Otherwise, we add it to the opaque command list
@@ -176,7 +181,10 @@ namespace our {
176181
command.center = glm::vec3(command.localToWorld * glm::vec4(0, 0, 0, 1));
177182
command.mesh = submesh->mesh;
178183
command.material = submesh->material;
179-
if (command.material->transparent) {
184+
185+
if (isWeapon) {
186+
weaponCommands.push_back(command);
187+
} else if (command.material->transparent) {
180188
transparentCommands.push_back(command);
181189
} else {
182190
opaqueCommands.push_back(command);
@@ -263,6 +271,22 @@ namespace our {
263271
command.mesh->draw();
264272
}
265273

274+
// render weapon at the end with depth testing cleared
275+
glClear(GL_DEPTH_BUFFER_BIT);
276+
for (const RenderCommand& command : weaponCommands) {
277+
command.material->pipelineState.depthMask = false;
278+
command.material->setup();
279+
glm::mat4 MVP = VP * command.localToWorld;
280+
command.material->shader->set("transform", MVP);
281+
if (LitMaterial* litMaterial = dynamic_cast<LitMaterial*>(command.material); litMaterial) {
282+
// if the material is a lit material, we need to set the light uniforms
283+
litMaterial->setLightUniforms(sceneLights);
284+
litMaterial->shader->set("cameraPos", cameraPosition);
285+
litMaterial->shader->set("model", command.localToWorld);
286+
}
287+
command.mesh->draw();
288+
}
289+
266290
// If there is a postprocess material, apply postprocessing
267291
if (postprocessMaterial) {
268292
glBindFramebuffer(GL_FRAMEBUFFER, 0);

src/common/systems/forward-renderer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace our {
3434
// reallocating them every frame
3535
std::vector<RenderCommand> opaqueCommands;
3636
std::vector<RenderCommand> transparentCommands;
37+
std::vector<RenderCommand> weaponCommands;
3738
// Objects used for rendering a skybox
3839
Mesh* skySphere;
3940
TexturedMaterial* skyMaterial;

src/game/components/weapon.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "weapon.hpp"
2+
3+
namespace gameplay {
4+
5+
void WeaponComponent::deserialize(const nlohmann::json& data) {
6+
if (!data.is_object()) {
7+
return;
8+
}
9+
damage = data.value("damage", 0);
10+
range = data.value("range", 0.0f);
11+
fireRate = data.value("fireRate", 0.0f);
12+
}
13+
14+
} // namespace gameplay

src/game/components/weapon.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <ecs/component.hpp>
4+
5+
namespace gameplay {
6+
7+
class WeaponComponent : public our::Component {
8+
public:
9+
int damage;
10+
float range;
11+
float fireRate;
12+
static std::string getID() {
13+
return "Weapon";
14+
}
15+
16+
void deserialize(const nlohmann::json& data) override;
17+
};
18+
19+
} // namespace gameplay

src/game/game-registration.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "components/enemy.hpp"
77
#include "components/health.hpp"
88
#include "components/player.hpp"
9-
9+
#include "components/weapon.hpp"
1010
namespace gameplay {
1111

1212
void registerGameComponents() {
@@ -17,6 +17,7 @@ namespace gameplay {
1717
our::ComponentRegistry::registerType<EnemyComponent>();
1818
our::ComponentRegistry::registerType<HealthComponent>();
1919
our::ComponentRegistry::registerType<PlayerComponent>();
20+
our::ComponentRegistry::registerType<WeaponComponent>();
2021

2122
initialized = true;
2223
}

0 commit comments

Comments
 (0)