aboutsummaryrefslogtreecommitdiff
path: root/animation_system.c
blob: 8411e643be9e239e64da833b03bdac6ad85eb244 (plain) (blame)
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
#include "animation.h"
#include "components.h"
#include "world.h"

void animation_system(World* world) {
	int i;
	unsigned bits;
	CSprite* sprite;
	CAnimated* animated;
	const Animation* animation;

	for (i = 0; i < world->entity_count; i++) {
		bits = world->bitmask[i];
		if ((bits & ctype_sprite) && (bits & ctype_animated)) {
			sprite = &world->sprites[i];
			animated = &world->animateds[i];
			animation = get_animation(animated->id);

			animated->timer++;
			if (animated->timer > animation->slowness) {
				animated->frame++;
				animated->timer = 0;
			}

			if (animated->frame >= animation->frame_count) {
				if (bits & ctype_destroy_on_anim_done) {
					destroy_entity(world, i);
					continue;
				}

				animated->frame = 0;
				animated->timer = 0;
			}

			sprite->rect = animation->frames[animated->frame];
		}
	}
}