aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/AnimatorSystem.cpp
blob: 6c933729156fa784622abecb3f6ce98d7701ceb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "../api/Animator.h"
#include "../manager/ComponentManager.h"
#include "../manager/LoopTimerManager.h"
#include <chrono>

#include "AnimatorSystem.h"

using namespace crepe;
using namespace std::chrono;

void AnimatorSystem::frame_update() {
	ComponentManager & mgr = this->mediator.component_manager;
	LoopTimerManager & timer = this->mediator.loop_timer;
	RefVector<Animator> animations = mgr.get_components_by_type<Animator>();

	duration_t elapsed = timer.get_delta_time();

	for (Animator & animator : animations) {
		if (!animator.active) continue;

		Animator::Data & data = animator.data;
		if (animator.data.fps == 0) continue;

		if (animator.data.cycle_end == -1)
			animator.data.cycle_end = animator.grid_size.x * animator.grid_size.y;

		animator.elapsed += elapsed;
		duration_t frame_duration = 1000ms / animator.data.fps;

		if (animator.elapsed > frame_duration) {
			animator.elapsed = 0ms;
			animator.data.frame++;

			if (animator.data.looping && animator.data.frame >= animator.data.cycle_end)
				animator.data.frame = animator.data.cycle_start;
		}

		Sprite::Mask & mask = animator.spritesheet.mask;
		mask.x = animator.data.frame % animator.grid_size.x * mask.w;
		mask.y = animator.data.frame / animator.grid_size.x * mask.h;
	}
}