Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
AnimationComponent.cpp
Go to the documentation of this file.
3 #include "Engine/engine.hpp"
4 
5 namespace Engine {
6 
8  {
9  }
10 
11  eastl::vector<eastl::weak_ptr<ModelComponent>> AnimationComponent::GetEntityModels() const
12  {
13  return entityModels;
14  }
15 
16  eastl::vector<eastl::string> AnimationComponent::GetModelAnimations(eastl::weak_ptr<ModelComponent> modelComponent)
17  {
18  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
19  if (entityModels[i].lock() == modelComponent.lock()) {
20  return GetModelAnimations(i);
21  }
22  }
23  return eastl::vector<eastl::string>();
24  }
25 
26  eastl::vector<eastl::string> AnimationComponent::GetModelAnimations(size_t index)
27  {
28  if (entityModels.size()>index) {
29  if (!entityModels[index].expired())
30  return entityModels[index].lock()->GetModel().lock()->GetAnimations();
31  }
32  return eastl::vector<eastl::string>();
33  }
34 
35  void AnimationComponent::SetModelAnimation(eastl::weak_ptr<ModelComponent> modelComponent, eastl::string name, bool resetTime)
36  {
37  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
38  if (entityModels[i].lock() == modelComponent.lock()) {
39  SetModelAnimation(i, name, resetTime);
40  }
41  }
42  }
43 
44  void AnimationComponent::SetModelAnimation(size_t index, eastl::string name, bool resetTime)
45  {
46  if (index < entityModels.size())
47  if (!entityModels[index].expired())
48  entityModels[index].lock()->GetModel().lock()->SetAnimation(name, resetTime);
49  }
50 
51  eastl::string AnimationComponent::GetModelCurrentAnimation(eastl::weak_ptr<ModelComponent> modelComponent)
52  {
53  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
54  if (entityModels[i].lock() == modelComponent.lock()) {
56  }
57  }
58  return "";
59  }
60 
62  {
63  if (index < entityModels.size())
64  if (!entityModels[index].expired())
65  return entityModels[index].lock()->GetModel().lock()->GetCurrentAnimationName();
66  return "";
67  }
68 
69  void AnimationComponent::ResetModelAnimation(eastl::weak_ptr<ModelComponent> modelComponent)
70  {
71  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
72  if (entityModels[i].lock() == modelComponent.lock()) {
74  }
75  }
76  }
77 
79  {
80  if (index < entityModels.size())
81  if (!entityModels[index].expired())
82  entityModels[index].lock()->GetModel().lock()->ResetAnimation();
83  }
84 
85  void AnimationComponent::SetModelAnimationTime(eastl::weak_ptr<ModelComponent> modelComponent, float time)
86  {
87  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
88  if (entityModels[i].lock() == modelComponent.lock()) {
89  SetModelAnimationTime(i, time);
90  }
91  }
92  }
93 
94  void AnimationComponent::SetModelAnimationTime(size_t index, float time)
95  {
96  if (index < entityModels.size())
97  if (!entityModels[index].expired())
98  entityModels[index].lock()->GetModel().lock()->SetAnimationTime(time);
99  }
100 
101  float AnimationComponent::GetModelAnimationTime(eastl::weak_ptr<ModelComponent> modelComponent)
102  {
103  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
104  if (entityModels[i].lock() == modelComponent.lock()) {
105  return GetModelAnimationTime(i);
106  }
107  }
108  return 0.0f;
109  }
110 
112  {
113  if (index < entityModels.size())
114  if (!entityModels[index].expired())
115  return entityModels[index].lock()->GetModel().lock()->GetAnimationTime();
116  return 0.0f;
117  }
118 
119  float AnimationComponent::GetModelAnimationDuration(eastl::weak_ptr<ModelComponent> modelComponent)
120  {
121  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
122  if (entityModels[i].lock() == modelComponent.lock()) {
123  return GetModelAnimationDuration(i);
124  }
125  }
126  return 0.0f;
127  }
128 
130  {
131  if (index < entityModels.size())
132  if (!entityModels[index].expired())
133  return entityModels[index].lock()->GetModel().lock()->GetAnimationDuration();
134  return 0.0f;
135  }
136 
137  void AnimationComponent::SetModelAnimationPaused(eastl::weak_ptr<ModelComponent> modelComponent, bool paused)
138  {
139  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
140  if (entityModels[i].lock() == modelComponent.lock()) {
141  SetModelAnimationPaused(i, paused);
142  }
143  }
144  }
145 
146  void AnimationComponent::SetModelAnimationPaused(size_t index, bool paused)
147  {
148  if (index < entityModels.size())
149  if (!entityModels[index].expired())
150  entityModels[index].lock()->GetModel().lock()->SetPaused(paused);
151  }
152 
153  bool AnimationComponent::isModelAnimationPaused(eastl::weak_ptr<ModelComponent> modelComponent)
154  {
155  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
156  if (entityModels[i].lock() == modelComponent.lock()) {
157  return isModelAnimationPaused(i);
158  }
159  }
160  return false;
161  }
162 
164  {
165  if (index < entityModels.size())
166  if (!entityModels[index].expired())
167  return entityModels[index].lock()->GetModel().lock()->IsPaused();
168  return true;
169  }
170 
171  void AnimationComponent::SetModelAnimationLooping(eastl::weak_ptr<ModelComponent> modelComponent, bool looping)
172  {
173  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
174  if (entityModels[i].lock() == modelComponent.lock()) {
175  SetModelAnimationLooping(i, looping);
176  }
177  }
178  }
179 
180  void AnimationComponent::SetModelAnimationLooping(size_t index, bool looping)
181  {
182  if (index < entityModels.size())
183  if (!entityModels[index].expired())
184  entityModels[index].lock()->GetModel().lock()->SetLooping(looping);
185  }
186 
187  bool AnimationComponent::IsModelAnimationLooping(eastl::weak_ptr<ModelComponent> modelComponent)
188  {
189  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
190  if (entityModels[i].lock() == modelComponent.lock()) {
191  return IsModelAnimationLooping(i);
192  }
193  }
194  return false;
195  }
196 
198  {
199  if (index < entityModels.size())
200  if (!entityModels[index].expired())
201  return entityModels[index].lock()->GetModel().lock()->IsLooping();
202  return false;
203  }
204 
205  void AnimationComponent::SetModelAnimationSpeed(eastl::weak_ptr<ModelComponent> modelComponent, float speed)
206  {
207  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
208  if (entityModels[i].lock() == modelComponent.lock()) {
209  SetModelAnimationSpeed(i, speed);
210  }
211  }
212  }
213 
214  void AnimationComponent::SetModelAnimationSpeed(size_t index, float speed)
215  {
216  if (index < entityModels.size())
217  if (!entityModels[index].expired())
218  entityModels[index].lock()->GetModel().lock()->SetSpeed(speed);
219  }
220 
221  float AnimationComponent::GetModelAnimationSpeed(eastl::weak_ptr<ModelComponent> modelComponent)
222  {
223  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
224  if (entityModels[i].lock() == modelComponent.lock()) {
225  return GetModelAnimationSpeed(i);
226  }
227  }
228  return 0.0f;
229  }
230 
232  {
233  if (index < entityModels.size())
234  if (!entityModels[index].expired())
235  return entityModels[index].lock()->GetModel().lock()->GetSpeed();
236  return 0.0f;
237  }
238 
239  AnimationComponent::AnimationComponent() noexcept
240  {
241  }
242 
243  void AnimationComponent::InitializeComponent()
244  {
245  isEnabled = true;
246  }
247 
248  void AnimationComponent::Update()
249  {
250  for (size_t i = 0, size = entityModels.size(); i < size; ++i) {
251  if (entityModels[i].expired())
252  continue;
253 
254  if (!entityModels[i].lock()->isEnabled)
255  continue;
256 
257  entityModels[i].lock()->GetModel().lock()->UpdateAnimation(
258  Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime());
259  }
260  }
261 
262  void AnimationComponent::OnComponentAdded(eastl::weak_ptr<Component> addedComponent)
263  {
264  if (eastl::dynamic_pointer_cast<ModelComponent, Component>(addedComponent.lock())) {
265  if (eastl::dynamic_pointer_cast<ModelComponent, Component>(addedComponent.lock())->GetModel().lock()->HasAnimations())
266  entityModels.push_back(eastl::dynamic_pointer_cast<ModelComponent, Component>(addedComponent.lock()));
267  }
268  }
269 
270  void AnimationComponent::OnComponentRemoved(eastl::weak_ptr<Component> removedComponent)
271  {
272  if (eastl::dynamic_pointer_cast<ModelComponent, Component>(removedComponent.lock())) {
273  eastl::vector<eastl::weak_ptr<ModelComponent>>::iterator it;
274  for (it = entityModels.begin(); it != entityModels.end(); ++it) {
275  if (it->lock() == removedComponent.lock())
276  break;
277  }
278  if (it != entityModels.end()) {
279  entityModels.erase(it);
280  }
281  }
282  }
283 
284 }
void SetModelAnimationLooping(eastl::weak_ptr< ModelComponent > modelComponent, bool looping)
Sets whether or not the model&#39;s animation should loop.
float GetModelAnimationTime(eastl::weak_ptr< ModelComponent > modelComponent)
Returns the progress of the model&#39;s current animation.
void SetModelAnimationSpeed(eastl::weak_ptr< ModelComponent > modelComponent, float speed)
Sets the speed modifier of the model&#39;s animation. The default is 1.0f.
float GetModelAnimationSpeed(eastl::weak_ptr< ModelComponent > modelComponent)
Returns the speed modifier of the model&#39;s animation.
float GetModelAnimationDuration(eastl::weak_ptr< ModelComponent > modelComponent)
Returns the duration of the model&#39;s current animation.
eastl::vector< eastl::weak_ptr< ModelComponent > > GetEntityModels() const
Returns all model components owned by the entity.
bool isModelAnimationPaused(eastl::weak_ptr< ModelComponent > modelComponent)
Returns whether or not the model&#39;s animation is paused.
IMGUI_API float GetTime()
Definition: imgui.cpp:2160
void SetModelAnimationTime(eastl::weak_ptr< ModelComponent > modelComponent, float time)
Sets the progress of the model&#39;s animation to the specified time.
void SetModelAnimation(eastl::weak_ptr< ModelComponent > modelComponent, eastl::string name, bool resetTime)
Sets the animation of the specified model to the animation with the specified name.
eastl::vector< eastl::string > GetModelAnimations(eastl::weak_ptr< ModelComponent > modelComponent)
Returns the names of all animations of the given model component.
void ResetModelAnimation(eastl::weak_ptr< ModelComponent > modelComponent)
Resets the animation of the specified model component.
void SetModelAnimationPaused(eastl::weak_ptr< ModelComponent > modelComponent, bool paused)
Sets whether or not the model&#39;s animation should be paused.
eastl::string GetModelCurrentAnimation(eastl::weak_ptr< ModelComponent > modelComponent)
Returns the name of the model&#39;s current animation.
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
Definition: engine.cpp:130
bool IsModelAnimationLooping(eastl::weak_ptr< ModelComponent > modelComponent)
Returns whether or not the model&#39;s animation is looping.