Skip to content

Commit 87f74d1

Browse files
committed
✨ Add death animation
1 parent 1aac412 commit 87f74d1

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed

include/bezierAnimation.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <functional>
34
#include <glm/glm.hpp>
45
#include <vector>
56
#include "game/component.hpp"
@@ -26,9 +27,22 @@ class BezierAnimation final : public Component {
2627
if (currentTime > totalTime) {
2728
isForward = false;
2829
currentTime = totalTime;
30+
if (onEndCallback)
31+
onEndCallback();
2932
} else if (currentTime < 0) {
3033
isForward = true;
3134
currentTime = 0;
35+
if (onEndCallback)
36+
onEndCallback();
37+
}
38+
} else {
39+
currentTime += deltaTime;
40+
if (currentTime > totalTime) {
41+
currentTime = totalTime;
42+
if (onEndCallback) {
43+
onEndCallback();
44+
return;
45+
}
3246
}
3347
}
3448

@@ -37,10 +51,14 @@ class BezierAnimation final : public Component {
3751
transform->setPosition(initialPosition + position);
3852
}
3953

54+
void setOnEndCallback(std::function<void()> callback) { onEndCallback = callback; }
55+
4056
private:
4157
std::shared_ptr<Transform> transform;
4258
glm::vec3 initialPosition;
4359
std::vector<glm::vec3> controlPoints;
60+
std::function<void()> onEndCallback;
61+
4462
float totalTime;
4563
float currentTime;
4664
bool isLooping;

include/game/gameObject.hpp

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <algorithm>
34
#include <map>
45
#include <memory>
56
#include <vector>
@@ -10,6 +11,7 @@
1011
enum class ObjectType {
1112
Player,
1213
Platform,
14+
DeathBox,
1315
Light,
1416
Camera,
1517
};
@@ -33,6 +35,25 @@ class GameObject {
3335
}
3436
}
3537

38+
template<typename T>
39+
void removeComponent() {
40+
const ComponentType type = getComponentType<T>();
41+
42+
auto it = components.find(type);
43+
if (it != components.end()) {
44+
if (isPhysicsComponent(type)) {
45+
auto physIt =
46+
std::find_if(physicsComponents.begin(), physicsComponents.end(),
47+
[&it](const std::shared_ptr<Component> &comp) { return comp == it->second; });
48+
if (physIt != physicsComponents.end()) {
49+
physicsComponents.erase(physIt);
50+
}
51+
}
52+
it->second->setGameObject(nullptr);
53+
components.erase(it);
54+
}
55+
}
56+
3657
template<typename T>
3758
[[nodiscard]] std::shared_ptr<T> getComponent() const {
3859
const ComponentType type = getComponentType<T>();

include/game/scene.hpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "graphics/renderManager.hpp"
88
#include "math/matrix.hpp"
99
#include "objects/camera.hpp"
10+
#include "objects/deathBox.hpp"
1011
#include "objects/light.hpp"
1112
#include "objects/platform.hpp"
1213
#include "objects/player.hpp"
@@ -19,7 +20,7 @@ class Scene {
1920
RenderManager::initializeShaders();
2021

2122
this->freeCam = std::make_unique<Camera>(glm::vec3(0.0f, 0.0f, 0.0f), 0.0f, 0.0f);
22-
this->playerCam = std::make_unique<Camera>(glm::vec3(0.0f, 0.0f, 0.0f), 0.0f, -30.0f);
23+
this->playerCam = std::make_unique<Camera>(glm::vec3(0.0f, 0.0f, 0.0f), -90.0f, -30.0f);
2324

2425
this->player = std::make_unique<Player>(*playerCam);
2526

@@ -30,6 +31,9 @@ class Scene {
3031

3132
addObject(std::make_shared<Light>(Transform{glm::vec3(0.0f, 10.0f, 10.0f)}, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)));
3233

34+
addObject(std::make_shared<DeathBox>(
35+
Transform{glm::vec3(0.0f, -40.0f, 0.0f), glm::vec3(1000.0f, 1.0f, 1000.0f)}));
36+
3337
initializePlatforms();
3438
}
3539

@@ -95,19 +99,19 @@ class Scene {
9599

96100
void initializePlatforms() {
97101
addObject(std::make_shared<Platform>(Transform{glm::vec3(0.0f, -10.0f, 0.0f), glm::vec3(10.0f, 1.0f, 5.0f)}));
98-
addObject(std::make_shared<Platform>(Transform{glm::vec3(20.0f, -10.0f, 0.0f), glm::vec3(10.0f, 1.0f, 5.0f)}));
102+
addObject(std::make_shared<Platform>(Transform{glm::vec3(20.0f, -10.0f, 0.0f), glm::vec3(10.0f, 1.0f, 4.98f)}));
99103
addObject(std::make_shared<IcePlatform>(Transform{
100104
glm::vec3(38.18f, -14.86f, 0.0f), glm::vec3(10.0f, 1.0f, 5.0f), glm::vec3(0.0f, 0.0f, -30.0f)}));
101-
addObject(std::make_shared<IcePlatform>(
102-
Transform{glm::vec3(56.33f, -20.0f, 0.0f), glm::vec3(10.0f, 1.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f)}));
105+
addObject(std::make_shared<IcePlatform>(Transform{glm::vec3(56.33f, -20.0f, 0.0f),
106+
glm::vec3(10.0f, 1.0f, 4.98f), glm::vec3(0.0f, 0.0f, 0.0f)}));
103107
addObject(std::make_shared<IcePlatform>(
104108
Transform{glm::vec3(74.0f, -15.0f, 0.0f), glm::vec3(10.0f, 1.0f, 5.0f), glm::vec3(0.0f, 0.0f, 30.0f)}));
105-
addObject(std::make_shared<Platform>(Transform{glm::vec3(60.0f, -20.0f, 15.0f), glm::vec3(10.0f, 1.0f, 5.0f),
109+
addObject(std::make_shared<Platform>(Transform{glm::vec3(60.0f, -20.0f, 15.0f), glm::vec3(10.0f, 1.0f, 4.98f),
106110
glm::vec3(0.0f, 90.0f, 0.0f)}));
107111
addObject(std::make_shared<Platform>(Transform{glm::vec3(60.0f, -17.0f, 33.0f), glm::vec3(10.0f, 1.0f, 5.0f),
108112
glm::vec3(90.0f, -70.0f, 90.0f)}));
109113
addObject(std::make_shared<JumpPlatform>(Transform{
110-
glm::vec3(60.0f, -13.66f, 52.1f), glm::vec3(10.0f, 1.0f, 5.0f), glm::vec3(0.0f, 90.0f, 00.0f)}));
114+
glm::vec3(60.0f, -13.66f, 52.1f), glm::vec3(10.0f, 1.0f, 4.98f), glm::vec3(0.0f, 90.0f, 00.0f)}));
111115

112116
auto movingPlatform = std::make_shared<Platform>(
113117
Transform{glm::vec3(80.0f, -12.0f, 55.0f), glm::vec3(10.0f, 1.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f)});

include/objects/deathBox.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include "bezierAnimation.hpp"
4+
#include "collision/aabbCollider.hpp"
5+
#include "collision/obbCollider.hpp"
6+
#include "game/gameObject.hpp"
7+
#include "graphics/materialManager.hpp"
8+
#include "graphics/meshManager.hpp"
9+
#include "graphics/renderer.hpp"
10+
#include "physics/physicsMaterial.hpp"
11+
12+
class DeathBox : public GameObject {
13+
public:
14+
virtual ~DeathBox() = default;
15+
16+
explicit DeathBox(const Transform &transform) {
17+
this->transform = std::make_shared<Transform>(transform);
18+
addComponent(this->transform);
19+
20+
if (this->transform->rotation == glm::quat()) {
21+
collider = std::make_shared<AABBCollider>(-this->transform->scale, this->transform->scale);
22+
addComponent(collider);
23+
} else {
24+
collider = std::make_shared<OBBCollider>(this->transform->scale);
25+
addComponent(collider);
26+
}
27+
}
28+
29+
[[nodiscard]] ObjectType getObjectType() const override { return ObjectType::DeathBox; }
30+
31+
private:
32+
std::shared_ptr<Transform> transform;
33+
std::shared_ptr<Collider> collider;
34+
};

include/objects/player.hpp

+35-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "camera.hpp"
44
#include "collision/sphereCollider.hpp"
5+
#include "debug.hpp"
56
#include "game/gameObject.hpp"
67
#include "graphics/materialManager.hpp"
78
#include "graphics/meshManager.hpp"
@@ -15,7 +16,7 @@
1516
class Player final : public GameObject, public InputObserver {
1617
public:
1718
explicit Player(Camera &camera) : camera(camera) {
18-
transform = std::make_shared<Transform>(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f));
19+
transform = std::make_shared<Transform>(playerSpawnPoint, glm::vec3(1.0f));
1920
addComponent(transform);
2021

2122
renderer = std::make_shared<Renderer>(
@@ -99,6 +100,15 @@ class Player final : public GameObject, public InputObserver {
99100

100101
void handleCollision(const GameObject &other, const glm::vec4 collisionNormal, const float penetrationDepth,
101102
const float deltaTime) {
103+
if (isOnDeathRoutine) {
104+
return;
105+
}
106+
107+
if (other.getObjectType() == ObjectType::DeathBox) {
108+
deathRoutine();
109+
return;
110+
}
111+
102112
const auto otherTransform = other.getComponent<Transform>();
103113
const auto physicsMat = other.getComponent<PhysicsMaterial>();
104114

@@ -160,8 +170,32 @@ class Player final : public GameObject, public InputObserver {
160170
std::shared_ptr<RigidBody> rigidBody;
161171
std::shared_ptr<SphereCollider> sphereCollider;
162172
std::shared_ptr<GravityComponent> gravity;
173+
bool isOnDeathRoutine = false;
174+
175+
constexpr static glm::vec3 playerSpawnPoint = glm::vec3(0.0f, 0.0f, 0.0f);
163176

164177
float movementSpeed = 2.0f;
165178

166179
[[nodiscard]] glm::vec4 getInterpolatedPosition() const { return renderer->interpolatedTransform->getPosition(); }
180+
181+
void deathRoutine() {
182+
isOnDeathRoutine = true;
183+
rigidBody->initValues();
184+
gravity->disable();
185+
inputEnabled = false;
186+
187+
const auto relativeEnd = playerSpawnPoint - glm::vec3(transform->getPosition());
188+
189+
std::vector<glm::vec3> points = {glm::vec3(0.0f, 0.0f, -0.0f), glm::vec3(0.0f, 10.0f, 0.0f),
190+
glm::vec3(0.0f, 30.0f, 0.0f), relativeEnd};
191+
192+
auto bezierAnimation = std::make_shared<BezierAnimation>(points, 5.0f, false);
193+
bezierAnimation->setOnEndCallback([this]() {
194+
isOnDeathRoutine = false;
195+
inputEnabled = true;
196+
removeComponent<BezierAnimation>();
197+
std::cout << "Player respawned\n";
198+
});
199+
addComponent(std::move(bezierAnimation));
200+
}
167201
};

include/physics/rigidBody.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class RigidBody final : public Component {
2929
}
3030
}
3131

32+
void initValues() {
33+
velocity = glm::vec4(0, 0, 0, 0);
34+
inputVelocity = glm::vec4(0, 0, 0, 0);
35+
acceleration = glm::vec4(0, 0, 0, 0);
36+
forceAccumulator = glm::vec4(0, 0, 0, 0);
37+
}
38+
3239
void update(float deltaTime) override {
3340
if (!transform) {
3441
return;

0 commit comments

Comments
 (0)