-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathanimation.cpp
65 lines (51 loc) · 2.02 KB
/
animation.cpp
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
#include "stdafx.h"
#include "animation.h"
#include "animation_id.h"
#include "renderer.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, Vec2 squareSize, milliseconds time, Color color) {
CHECK(begin);
CHECK(time - *begin <= duration) << time << " " << *begin << " " << duration;
renderSpec(r, bounds, origin, squareSize, (double)(time - *begin).count() / duration.count(), color);
}
class ThrownObject : public Animation {
public:
ThrownObject(Vec2 dir, ViewId obj, bool sprite)
: Animation(milliseconds{dir.length8()}), direction(dir), viewObject(obj), useSprite(sprite) {}
virtual void renderSpec(Renderer& renderer, Rectangle bounds, Vec2 origin, Vec2 squareSize, double state,
Color color) override {
int x = origin.x + state * direction.x - squareSize.x / 2;
int y = origin.y + state * direction.y - squareSize.y / 2;
renderer.drawViewObject(Vec2(x, y), viewObject, useSprite, squareSize, color,
Renderer::SpriteOrientation(direction, false));
}
private:
Vec2 direction;
ViewId viewObject;
bool useSprite;
};
PAnimation Animation::thrownObject(Vec2 direction, ViewId obj, bool useSprite) {
return PAnimation(new ThrownObject(direction, obj, useSprite));
}
class SpriteAnim : public Animation {
public:
SpriteAnim(AnimationId id, milliseconds duration, Dir o) : Animation(duration), id(id), orientation(o) {}
virtual void renderSpec(Renderer& renderer, Rectangle bounds, Vec2 origin, Vec2 squareSize, double state,
Color color) override {
renderer.drawAnimation(id, origin, state, squareSize, orientation, color);
}
private:
AnimationId id;
Dir orientation;
};
PAnimation Animation::fromId(AnimationId id, Dir orientation) {
return make_unique<SpriteAnim>(id, getDuration(id), orientation);
}