summary refs log tree commit diff stats
path: root/src/systems/animating.cpp
blob: fcbfca5807968b9a2522dd026ce604348e522104 (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 "animating.h"
#include "game.h"
#include "components/animatable.h"

void AnimatingSystem::tick(double)
{
  std::set<id_type> spriteEntities =
    game_.getEntityManager().getEntitiesWithComponents<AnimatableComponent>();

  for (id_type entity : spriteEntities)
  {
    auto& sprite = game_.getEntityManager().
      getComponent<AnimatableComponent>(entity);

    sprite.setCountdown(sprite.getCountdown() + 1);

    const Animation& anim = sprite.getAnimation();
    if (sprite.getCountdown() >= anim.getDelay())
    {
      sprite.setFrame(sprite.getFrame() + 1);
      sprite.setCountdown(0);

      if (sprite.getFrame() >= anim.getFirstFrame() + anim.getNumFrames())
      {
        sprite.setFrame(anim.getFirstFrame());
      }
    }
  }
}

void AnimatingSystem::startAnimation(id_type entity, std::string animation)
{
  auto& sprite = game_.getEntityManager().
    getComponent<AnimatableComponent>(entity);

  sprite.setAnimation(animation);
  sprite.setFrame(sprite.getAnimation().getFirstFrame());
}