GameKit  0.0.1a
C++ gamedev tools
TilemapAnimator.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: TilemapAnimator.cpp
5  *
6  * Description:
7  *
8  * Created: 15/02/2019 19:20:46
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
15 #include "gk/graphics/Tilemap.hpp"
16 
17 namespace gk {
18 
20  m_tileAnimations.assign(map.width() * map.height(), {});
21 }
22 
24  for (size_t x = 0 ; x < map.width() ; ++x) {
25  for (size_t y = 0 ; y < map.height() ; ++y) {
26  const Tile &tile = map.tileset().getTile(map.getTile(x, y));
27  if (tile.getFrameCount()) {
28  TileAnimation &tileAnimation = m_tileAnimations.at(x + y * map.width());
29  if (!tileAnimation.timer.isStarted())
30  tileAnimation.timer.start();
31 
32  if (tileAnimation.timer.time() > tile.getFrame(tileAnimation.currentFrame).duration) {
33  tileAnimation.currentFrame++;
34  tileAnimation.currentFrame %= tile.getFrameCount();
35 
36  map.setTile(x, y, tile.getFrame(tileAnimation.currentFrame).tileID, false);
37 
38  tileAnimation.timer.reset();
39  tileAnimation.timer.start();
40  }
41  }
42  }
43  }
44 }
45 
46 } // namespace gk
47 
u16 getTile(u16 tileX, u16 tileY, u8 layer=0)
Definition: Tilemap.cpp:60
std::vector< TileAnimation > m_tileAnimations
void start()
Start the timer.
Definition: Timer.cpp:31
u16 width() const
Definition: Tilemap.hpp:39
const Tile & getTile(u16 id) const
Definition: Tileset.hpp:55
Tileset & tileset()
Definition: Tilemap.hpp:44
void reset()
Reset the timer.
Definition: Timer.cpp:38
void init(Tilemap &map)
const AnimationFrame & getFrame(u16 id) const
Definition: Tileset.hpp:33
u16 getFrameCount() const
Definition: Tileset.hpp:32
void setTile(u16 tileX, u16 tileY, u16 id, bool write=true, bool persistent=false)
Definition: Tilemap.cpp:68
u32 time() const
Get time.
Definition: Timer.cpp:44
bool isStarted() const
Check if the timer is started.
Definition: Timer.hpp:75
u16 height() const
Definition: Tilemap.hpp:40
void animateTiles(Tilemap &map)