aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/AnimatorSystem.cpp
blob: 107b25d9bcfdc12821d82bc09c0f439086718a57 (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::update() {
	ComponentManager & mgr = this->mediator.component_manager;
	LoopTimerManager & timer = this->mediator.loop_timer;
	RefVector<Animator> animations = mgr.get_components_by_type<Animator>();

	float elapsed_time = duration_cast<duration<float>>(timer.get_elapsed_time()).count();

	for (Animator & a : animations) {
		if (!a.active) continue;
		if (a.data.fps == 0) continue;

		Animator::Data & ctx = a.data;
		float frame_duration = 1.0f / ctx.fps;

		int last_frame = ctx.row;

		int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end;
		int total_frames = cycle_end - ctx.cycle_start;

		int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames;

		ctx.row = ctx.cycle_start + curr_frame;
		a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w;
		a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h);

		if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) {
			a.active = false;
		}
	}
}