TazGraph Project v0.1.0
Loading...
Searching...
No Matches
AnimatorComponent.h
1#pragma once
2
3#include "../Components.h"
4#include <map>
5#include "Animation.h"
6#include "AnimatorManager.h"
7#include <functional>
8
9typedef uint32_t timestamp;
10
11
12class AnimatorComponent : public Component //Animator -> Sprite -> Transform
13{
14public:
15
16 // onAction is the same thing as Play()
17 // onFinish and onStart are for when we free Animator and when we initialize it
18 // difference between this and from the lectures is that in lectures it uses seperate animator for each animation
19
20 SpriteComponent* sprite = nullptr;
21 std::string textureid = "";
22 std::string animationName = "";
23 timestamp resumeTime = 0;
24
25 //std::map<const char*, Animation> animations; //Animator Manager
26
28 {
29
30 }
31
32 AnimatorComponent(std::string id)
33 {
34 textureid = id;
35 }
36
38 {
39
40 }
41
42 void init() override
43 {
44 if (!entity->hasComponent<SpriteComponent>())
45 {
46 textureid.empty() ?
47 entity->addComponent<SpriteComponent>() :
48 entity->addComponent<SpriteComponent>(textureid);
49 }
50 sprite = &entity->GetComponent<SpriteComponent>();
51
52 Play("P1Idle"); //onStart
53 }
54
55 void update(float deltaTime) override //onAction
56 {
57 if (animationName == "Default") return;
58
59 if (sprite->animation.hasFinished()) { // playing again animation
60 sprite->animation.finished = false;
61 sprite->animation.times_played = 0;
62 resetAnimation();
63 }
64
65 sprite->animation.advanceFrame(deltaTime);
66 sprite->setCurrFrame();
67 }
68
69 void draw(size_t e_index, PlaneModelRenderer& batch, TazGraphEngine::Window& window) override
70 {
71 //sprite->draw(batch);
72 }
73
74 void Play(std::string animName, int reps = 0)
75 {
76 AnimatorManager& animManager = AnimatorManager::getInstance();
77 animationName = animName;
78 sprite->SetAnimation(animManager.animations[animName].indexX, animManager.animations[animName].indexY,
79 animManager.animations[animName].total_frames, animManager.animations[animName].speed,
80 animManager.animations[animName].type,
81 reps ? reps : animManager.animations[animName].reps);
82 }
83
84 void resetAnimation() {
85 sprite->flash_animation.resetFrameIndex();
86
87 AnimatorManager& animManager = AnimatorManager::getInstance();
88 animationName = "P1Idle";
89 sprite->SetAnimation(
90 animManager.animations[animationName].indexX, animManager.animations[animationName].indexY,
91 animManager.animations[animationName].total_frames, animManager.animations[animationName].speed,
92 animManager.animations[animationName].type
93 );
94 }
95
96 std::string getPlayName()
97 {
98 return animationName;
99 }
100
101 void DestroyTex()
102 {
103 sprite->DestroyTex();
104 }
105
106};
Definition AnimatorComponent.h:13
Definition GECS.h:131
T & addComponent(TArgs &&... mArgs)
have addScript function
Definition GECS.h:345
Definition PlaneModelRenderer.h:15
Definition SpriteComponent.h:15
Definition Window.h:12
Definition AnimatorManager.h:10