summary refs log tree commit diff stats
path: root/src/animation_system.cpp
blob: 73d30d5f6d8e9a2d4f2ce7acf8f9036f75e3eac6 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "animation_system.h"
#include <fstream>
#include <string>
#include <list>
#include "game.h"
#include "vector.h"
#include "util.h"

void AnimationSystem::initSprite(int spriteId, std::string_view filename) {
  std::ifstream datafile(filename.data());
  if (!datafile.is_open()) {
    throw std::invalid_argument(std::string("Could not find sprite datafile: ") + std::string(filename));
  }

  Sprite& sprite = game_.getSprite(spriteId);
  sprite.isAnimated = true;

  char ch;
  std::string line;

  datafile >> sprite.spritesheet;

  std::string framefilename;
  datafile >> framefilename;

  std::ifstream framefile(framefilename);
  if (!framefile.is_open()) {
    throw std::invalid_argument("Could not find frame datafile: " + framefilename);
  }

  vec2i cellSize;
  framefile >> cellSize.w();
  framefile >> ch; //,
  framefile >> cellSize.h();
  std::getline(framefile, line); // cell size

  int framesPerRow;
  framefile >> framesPerRow;
  std::getline(framefile, line); // frames per row

  int numFrames;
  framefile >> numFrames;
  std::getline(framefile, line); // frames
  std::getline(framefile, line); // blank

  sprite.frames.clear();
  for (int i=0; i<numFrames; i++) {
    SpriteFrame f;
    framefile >> f.size.w();
    framefile >> ch; //,
    framefile >> f.size.h();
    framefile >> ch; //,
    framefile >> f.center.x();
    framefile >> ch; //,
    framefile >> f.center.y();
    std::getline(framefile, line); // blank

    f.srcRect.x = (i % framesPerRow) * cellSize.w();
    f.srcRect.y = (i / framesPerRow) * cellSize.h();
    f.srcRect.w = f.size.w();
    f.srcRect.h = f.size.h();

    sprite.frames.push_back(std::move(f));
  }

  std::string animLine;
  std::getline(datafile, animLine); // blank
  sprite.animations.clear();
  sprite.nameDirToAnim.clear();
  while (std::getline(datafile, animLine)) {
    std::regex re(R"(([a-z!._]+)\[([a-z_]+)\](%[0-9]+)?: ([0-9,#]+))");
    std::smatch m;
    std::regex_match(animLine, m, re);

    std::string animName = m[1];
    Animation anim;
    auto framestrs = splitStr<std::list<std::string>>(m[4], ",");
    for (const std::string& f : framestrs) {
      int times = 1;
      size_t repeat_it = f.find("#");
      if (repeat_it != std::string::npos) {
        times = std::stoi(f.substr(repeat_it + 1));
      }

      for (int i=0; i<times; i++) {
        anim.frameIndices.push_back(std::stoi(f));
      }
    }

    if (animName.back() == '!') {
      anim.looping = false;
    }

    if (m[3] != "") {
      int frameRate = std::stoi(std::string(m[3]).substr(1));
      switch (frameRate) {
        case 5: anim.timerNum = 0; break;
        case 60: anim.timerNum = 1; break;
        default: throw std::invalid_argument("Invalid frame rate");
      }
    }

    int animId = sprite.animations.size();
    sprite.animations.push_back(std::move(anim));

    Direction dir = directionFromString(std::string(m[2]));
    sprite.nameDirToAnim[animName][dir] = animId;
  }

  updateAnimation(spriteId);
}

void AnimationSystem::tick(double dt) {
  if (game_.isGameplayPaused()) return;

  for (int timerNum = 0; timerNum < animTimers_.size(); timerNum++) {
    Timer& animTimer = animTimers_[timerNum];

    animTimer.accumulate(dt);
    while (animTimer.step()) {
      for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
        if (sprite.isAnimated &&
            sprite.animations[sprite.animationId].timerNum == timerNum &&
            !sprite.animFinished &&
            !sprite.animPaused) {
          if (sprite.animSlowdown > 1) {
            sprite.animSlowdownProgress++;

            if (sprite.animSlowdownProgress == sprite.animSlowdown) {
              sprite.animSlowdownProgress = 0;
            } else {
              continue;
            }
          }

          sprite.animationFrame++;

          if (sprite.animationFrame >= sprite.animations[sprite.animationId].frameIndices.size()) {
            if (sprite.animations[sprite.animationId].looping) {
              sprite.animationFrame = 0;
            } else {
              sprite.animFinished = true;
              sprite.animationFrame = sprite.animations[sprite.animationId].frameIndices.size() - 1;
            }
          }
        }
      }
    }
  }

  bobbingTimer_.accumulate(dt);
  while (bobbingTimer_.step()) {
    for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
      if (sprite.isAnimated && sprite.bobbing) {
        if (sprite.bobbingDown) {
          sprite.bobAmount--;
          if (sprite.bobAmount == 0) {
            sprite.bobbingDown = false;
          }
        } else {
          sprite.bobAmount++;
          if (sprite.bobAmount == 4) {
            sprite.bobbingDown = true;
          }
        }
      }
    }
  }

  fadingTimer_.accumulate(dt);
  while (fadingTimer_.step()) {
    for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
      if (sprite.isAnimated) {
        if (sprite.shouldBeFadedIn && sprite.opacity < 1.0) {
          sprite.opacity += 0.05;
          if (sprite.opacity > 1.0) {
            sprite.opacity = 1.0;
          }
        } else if (!sprite.shouldBeFadedIn && sprite.opacity > 0.0) {
          sprite.opacity -= 0.05;
          if (sprite.opacity < 0.0) {
            sprite.opacity = 0.0;
          }
        }
      }
    }
  }
}

void AnimationSystem::setSpriteDirection(int spriteId, Direction dir) {
  Sprite& sprite = game_.getSprite(spriteId);
  if (sprite.dir != dir) {
    sprite.dir = dir;
    updateAnimation(spriteId);
  }
}

void AnimationSystem::setSpriteAnimation(int spriteId, std::string_view name) {
  Sprite& sprite = game_.getSprite(spriteId);
  if (sprite.animationName != name) {
    sprite.animationName = name;
    updateAnimation(spriteId);
  }
}

void AnimationSystem::updateAnimation(int spriteId) {
  Sprite& sprite = game_.getSprite(spriteId);
  sprite.animationId = sprite.nameDirToAnim[sprite.animationName][sprite.dir];
  sprite.animationFrame = 0;
  sprite.animFinished = false;
  sprite.animPaused = false;
  sprite.animSlowdownProgress = 0;
}