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 entity->addComponent<SpriteComponent>(textureid);
47 }
48 sprite = &entity->GetComponent<SpriteComponent>();
49
50 Play("P1Idle"); //onStart
51 sprite->setTex(textureid);
52 }
53
54 void update(float deltaTime) override //onAction
55 {
56 if (sprite->animation.hasFinished()) { // playing again animation
57 sprite->animation.finished = false;
58 sprite->animation.times_played = 0;
59 resetAnimation();
60 }
61
62 sprite->animation.advanceFrame(deltaTime);
63 sprite->setCurrFrame();
64 }
65
66 void draw(size_t e_index, PlaneModelRenderer& batch, TazGraphEngine::Window& window) override
67 {
68 //sprite->draw(batch);
69 }
70
71 void Play(std::string animName, int reps = 0)
72 {
73 AnimatorManager& animManager = AnimatorManager::getInstance();
74 animationName = animName;
75 sprite->SetAnimation(animManager.animations[animName].indexX, animManager.animations[animName].indexY,
76 animManager.animations[animName].total_frames, animManager.animations[animName].speed,
77 animManager.animations[animName].type,
78 reps ? reps : animManager.animations[animName].reps );
79 }
80
81 void resetAnimation() {
82 AnimatorManager& animManager = AnimatorManager::getInstance();
83 animationName = "P1Idle";
84 sprite->SetAnimation(
85 animManager.animations[animationName].indexX, animManager.animations[animationName].indexY,
86 animManager.animations[animationName].total_frames, animManager.animations[animationName].speed,
87 animManager.animations[animationName].type
88 );
89 }
90
91 std::string getPlayName()
92 {
93 return animationName;
94 }
95
96 void DestroyTex()
97 {
98 sprite->DestroyTex();
99 }
100
101};
Definition AnimatorComponent.h:13
Definition GECS.h:123
T & addComponent(TArgs &&... mArgs)
have addScript function
Definition GECS.h:239
Definition PlaneModelRenderer.h:21
Definition SpriteComponent.h:17
Definition Window.h:18
Definition AnimatorManager.h:10