]> git.localhorst.tv Git - l2e.git/blob - src/battle/NumberAnimation.cpp
added fallback for string resources
[l2e.git] / src / battle / NumberAnimation.cpp
1 #include "NumberAnimation.h"
2
3 #include "../math/Vector.h"
4 #include "../graphics/Sprite.h"
5
6 using app::State;
7 using math::Vector;
8 using graphics::Animation;
9 using graphics::AnimationRunner;
10 using graphics::Sprite;
11
12 namespace battle {
13
14 NumberAnimation::NumberAnimation(int number, const Animation *a, const Sprite *numbers)
15 : number(number) {
16         animation[0] = AnimationRunner(a);
17         animation[1] = AnimationRunner(a);
18         animation[2] = AnimationRunner(a);
19         animation[3] = AnimationRunner(a);
20
21         animation[0].ChangeSprite(numbers);
22         animation[1].ChangeSprite(numbers);
23         animation[2].ChangeSprite(numbers);
24         animation[3].ChangeSprite(numbers);
25
26         if (number > 9999) {
27                 animation[0].SetColOffset(9);
28                 animation[1].SetColOffset(9);
29                 animation[2].SetColOffset(9);
30                 animation[3].SetColOffset(9);
31         } else {
32                 animation[0].SetColOffset(number / 1000 % 10);
33                 animation[1].SetColOffset(number / 100 % 10);
34                 animation[2].SetColOffset(number / 10 % 10);
35                 animation[3].SetColOffset(number % 10);
36         }
37 }
38
39 void NumberAnimation::Start(State &s) {
40         if (number > 999) {
41                 animation[0].Start(s);
42         } else if (number > 99) {
43                 animation[1].Start(s);
44         } else if (number > 9) {
45                 animation[2].Start(s);
46         } else {
47                 animation[3].Start(s);
48         }
49 }
50
51 bool NumberAnimation::Running() const {
52         return animation[0].Running() || animation[1].Running() || animation[2].Running() || animation[3].Running();
53 }
54
55 void NumberAnimation::CheckTimers(State &s) {
56         if (animation[0].Running() && animation[0].Frame() == 1) {
57                 animation[1].Start(s);
58         } else if (animation[1].Running() && animation[1].Frame() == 1) {
59                 animation[2].Start(s);
60         } else if (animation[2].Running() && animation[2].Frame() == 1) {
61                 animation[3].Start(s);
62         }
63 }
64
65
66 int NumberAnimation::Width() const {
67         if (number < 10) {
68                 return animation[0].GetSprite()->Width();
69         } else if (number < 100) {
70                 return 2 * animation[0].GetSprite()->Width();
71         } else if (number < 1000) {
72                 return 3 * animation[0].GetSprite()->Width();
73         } else {
74                 return 4 * animation[0].GetSprite()->Width();
75         }
76 }
77
78 int NumberAnimation::Height() const {
79         return animation[0].GetSprite()->Height();
80 }
81
82 void NumberAnimation::Draw(SDL_Surface *dest, const Vector<int> &positionIn) const {
83         Vector<int> position(positionIn);
84         Vector<int> step(animation[0].GetSprite()->Width(), 0);
85         if (number > 999) {
86                 if (animation[0].Running()) {
87                         animation[0].Draw(dest, position);
88                 }
89                 position += step;
90         }
91         if (number > 99) {
92                 if (animation[1].Running() || animation[0].Running()) {
93                         animation[1].Draw(dest, position);
94                 }
95                 position += step;
96         }
97         if (number > 9) {
98                 if (animation[2].Running() || animation[1].Running() || animation[0].Running()) {
99                         animation[2].Draw(dest, position);
100                 }
101                 position += step;
102         }
103         if (animation[3].Running() || animation[2].Running() || animation[1].Running() || animation[0].Running()) {
104                 animation[3].Draw(dest, position);
105         }
106 }
107
108 }