forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.cpp
More file actions
73 lines (60 loc) · 2.16 KB
/
animation.cpp
File metadata and controls
73 lines (60 loc) · 2.16 KB
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
#include "stdafx.h"
#include "animation.h"
#include "view_object.h"
Animation::Animation(milliseconds d) : duration(d) {}
bool Animation::isDone(milliseconds time) const {
return begin && time > *begin + duration;
}
void Animation::setBegin(milliseconds time) {
CHECK(!begin);
begin = time;
}
void Animation::render(Renderer& r, Rectangle bounds, Vec2 origin, milliseconds time) {
CHECK(begin);
CHECK(time - *begin <= duration) << time << " " << *begin << " " << duration;
renderSpec(r, bounds, origin, (double)(time - *begin).count() / duration.count());
}
class ThrownObject : public Animation {
public:
ThrownObject(Vec2 dir, ViewObject obj, bool sprite, Vec2 sz)
: Animation(milliseconds{dir.length8()}), direction(dir), viewObject(obj), useSprite(sprite),
squareSize(sz) {}
virtual void renderSpec(Renderer& renderer, Rectangle bounds, Vec2 origin, double state) {
int x = origin.x + state * direction.x;
int y = origin.y + state * direction.y;
renderer.drawViewObject(Vec2(x, y), viewObject, useSprite, squareSize);
}
private:
Vec2 direction;
ViewObject viewObject;
bool useSprite;
Vec2 squareSize;
};
PAnimation Animation::thrownObject(Vec2 direction, ViewObject obj, bool useSprite, Vec2 squareSize) {
return PAnimation(new ThrownObject(direction, obj, useSprite, squareSize));
}
class SpriteAnim : public Animation {
public:
struct FrameInfo {
Vec2 origin;
Vec2 size;
Vec2 offset;
};
SpriteAnim(int frame, int tile, vector<FrameInfo> f)
: Animation(milliseconds{frame * f.size()}), frames(f), tileNum(tile) {}
virtual void renderSpec(Renderer& renderer, Rectangle bounds, Vec2 origin, double state) {
return;
FrameInfo current = frames[min<int>(frames.size() - 1, max(0, int(state * frames.size())))];
renderer.drawSprite(origin + current.offset, current.origin, current.size, renderer.tiles[tileNum]);
}
private:
vector<FrameInfo> frames;
int tileNum;
};
PAnimation Animation::fromId(AnimationId id) {
return PAnimation(new SpriteAnim(50, 6, {
{{510, 628}, {36, 36}, {0, 0}},
{{683, 611}, {70, 70}, {-17, -17}},
{{577, 598}, {94, 94}, {-29, -29}},
}));
}