Brunot
Loading...
Searching...
No Matches
Action.h
Go to the documentation of this file.
1// File: Action.h
2// Description: A class that sets a variable to a value over a time interval for a game object
3// Author(s): Ori Balashov (ori.balashov@digipen.edu): Filled out most of the functionality
4// Aidan Hartman (aidan.hartman@digipen.edu): Helped Ori with type erasure
5// 2025 / 09 / 11
6// (C) Digipen 2025
7// ____ __ __ __
8// /\__ _\/\ \ /\ \/\ \
9// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
10// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
11// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
12// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
13// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
14#pragma once
15
16#include <algorithm>
17#include <numbers>
18
19#include "Entity.h"
20#include "Component/Sprite.h"
21#include "Component/Transform.h"
22
23// Early declaration for the basic ease so the constructor knows what to use
24namespace ac
25{
26inline static const std::function LinearEase = [](float current, float total)
27{
28 return current / total;
29};
30
31}
32
33template <typename T>
34class Action
35{
36public:
37 Action(T start, T end, float totalTime, GameObject* actionOwner,
38 std::function<void(GameObject*, const T&)> funcToCall,
39 std::function<float(float, float)> EasingFunction = ac::LinearEase)
40 : startValue(std::move(start))
41 , endValue(std::move(end))
42 , totalDuration(totalTime)
43 , currentTime(0)
44 , changingObject(actionOwner)
45 , dataUpdate(funcToCall)
46 , easingFunction(EasingFunction)
47 {
48 }
49
50 auto update(float dt) -> void
51 {
52 // Increase how far we are in doing the action, clamped to our total duration so we don't go over
53 currentTime = std::min<float>(currentTime + dt, totalDuration);
54 // Get a percentage of our progress, clamped to 1 so we don't go over
57 if (changingObject->destroyed() || changingObject->getInternalParent() == nullptr || changingObject->
58 getInternalParent()->destroyed())
59 {
60 return;
61 }
62 // Increase the value we want to change
64 }
65
66 auto shouldRemove() const -> bool
67 {
68 return currentTime >= totalDuration - 0.01
69 || changingObject->destroyed()
70 || changingObject->getInternalParent() == nullptr
71 || changingObject->getInternalParent()->destroyed();
72 }
73
74 auto render() const -> void
75 {
77 }
78
79 auto setOwner(GameObject* newObject) -> void
80 {
81 changingObject = newObject;
82 }
83
84private:
90
91 std::function<void(GameObject*, const T&)> dataUpdate;
92 std::function<float(float, float)> easingFunction;
93
94};
95
96namespace ac
97{
98
100
101#pragma region actions
102inline static const std::function TranslateAction = [](GameObject* parent, const Point2D& newTranslation)
103{
104 assert(parent && "TranslateAction given a nullptr");
105 if (auto parentAsEntity = dynamic_cast<Entity*>(parent))
106 {
107 if (auto parentTransform = parentAsEntity->GetComponent<Transform>(Component::cTransform))
108 {
109 parentTransform->translation(newTranslation);
110 }
111 }
112
113};
114
115inline static const std::function ScaleAction = [](GameObject* parent, const Vector4D& newScale)
116{
117 assert(parent && "TranslateAction given a nullptr");
118 if (auto parentAsEntity = dynamic_cast<Entity*>(parent))
119 {
120 if (auto parentTransform = parentAsEntity->GetComponent<Transform>(Component::cTransform))
121 {
122 parentTransform->scale(newScale);
123 }
124 }
125
126};
127
128inline static const std::function SetColorAction = [](GameObject* parent, const Vector4D& newColor)
129{
130 assert(parent && "SetColorAction given a nullptr");
131 if (auto parentAsEntity = dynamic_cast<Entity*>(parent))
132 {
133 if (auto parentSprite = parentAsEntity->GetComponent<Sprite>(Component::cSprite))
134 {
135 parentSprite->color() = newColor;
136 }
137 }
138
139};
140#pragma endregion
141
142#pragma region ease_functions
143
144// Note: the code for these functions is taken from https://easings.net/
145
147inline static const std::function easeInOutBack = [](float current, float total)
148{
149 constexpr auto c1 = 1.70158f;
150 constexpr auto c2 = c1 * 1.525f;
151
152 auto progress = current / total;
153
154 if (progress < 0.5f)
155 {
156 return (pow(2 * total, 2) * ((c2 + 1) * 2 * total - c2)) / 2;
157 }
158
159 return (pow(2 * total - 2, 2) * ((c2 + 1) * (total * 2 - 2) + c2) + 2) / 2;
160
161
162};
163
164inline static const std::function easeOutElastic = [](float current, float total)
165{
166
167 auto progress = current / total;
168
169 if (progress == 0 || progress == 1)
170 {
171 return progress;
172 }
173 return static_cast<float>(pow(2, -10 * progress) * sin((progress * 10 - 0.75f) * (2 * std::numbers::pi / 3.f)) + 1);
174
175};
176
178inline static const std::function easeOutBounce = [](float current, float total)
179{
180 constexpr auto n1 = 7.5625f;
181
182 if (current < 1 / total)
183 {
184 return n1 * current * current;
185 }
186
187 if (current < 2 / total)
188 {
189 return n1 * (current -= 1.5f / total) * current + 0.75f;
190 }
191
192 if (current < 2.5f / total)
193 {
194 return n1 * (current -= 2.25f / total) * current + 0.9375f;
195 }
196 return n1 * (current -= 2.625f / total) * current + 0.984375f;
197};
198
199inline static const std::function EaseInOutBounce = [](float current, float total)
200{
201 return current < 0.5f
202 ? (1 - easeOutBounce(total, 1 - 2 * current)) / 2
203 : (1 + easeOutBounce(total, 2 * current - 1)) / 2;
204};
205#pragma endregion
206
207}
The class containing all entity elements.
Vector4D Point2D
Definition Transform.h:21
Action(T start, T end, float totalTime, GameObject *actionOwner, std::function< void(GameObject *, const T &)> funcToCall, std::function< float(float, float)> EasingFunction=ac::LinearEase)
Definition Action.h:37
std::function< float(float, float)> easingFunction
Definition Action.h:92
std::function< void(GameObject *, const T &)> dataUpdate
Definition Action.h:91
T endValue
Definition Action.h:86
T startValue
Definition Action.h:85
float currentTime
Definition Action.h:88
auto shouldRemove() const -> bool
Definition Action.h:66
float totalDuration
Definition Action.h:87
auto update(float dt) -> void
Definition Action.h:50
auto render() const -> void
Definition Action.h:74
GameObject * changingObject
Definition Action.h:89
auto setOwner(GameObject *newObject) -> void
Definition Action.h:79
@ cTransform
Definition Component.h:39
@ cSprite
Definition Component.h:41
Definition Entity.h:33
the base class for the engine, most things inherit from this.
Definition GameObject.h:77
A component that renders an image on an entity, or text.
Definition Sprite.h:34
Definition Transform.h:27
Definition Action.h:25
static const std::function ScaleAction
Definition Action.h:115
static const std::function LinearEase
Definition Action.h:26
static const std::function EaseInOutBounce
Definition Action.h:199
static const std::function TranslateAction
Definition Action.h:102
static const std::function easeOutElastic
Definition Action.h:164
static const std::function SetColorAction
Definition Action.h:128
static const std::function easeInOutBack
Definition Action.h:147
static const std::function easeOutBounce
Definition Action.h:178
Definition Vector4D.h:23