+/*
+ * NumberAnimation.cpp
+ *
+ * Created on: Aug 12, 2012
+ * Author: holy
+ */
+
+#include "NumberAnimation.h"
+
+#include "../geometry/operators.h"
+#include "../geometry/Vector.h"
+
+using app::State;
+using geometry::Point;
+using geometry::Vector;
+using graphics::ComplexAnimation;
+using graphics::Sprite;
+
+namespace battle {
+
+NumberAnimation::NumberAnimation(int number, const ComplexAnimation &a, const Sprite *numbers)
+: number(number) {
+ animation[0] = a;
+ animation[1] = a;
+ animation[2] = a;
+ animation[3] = a;
+
+ animation[0].ChangeSprite(numbers);
+ animation[1].ChangeSprite(numbers);
+ animation[2].ChangeSprite(numbers);
+ animation[3].ChangeSprite(numbers);
+
+ if (number > 9999) {
+ animation[0].SetColOffset(9);
+ animation[1].SetColOffset(9);
+ animation[2].SetColOffset(9);
+ animation[3].SetColOffset(9);
+ } else {
+ animation[0].SetColOffset(number / 1000 % 10);
+ animation[1].SetColOffset(number / 100 % 10);
+ animation[2].SetColOffset(number / 10 % 10);
+ animation[3].SetColOffset(number % 10);
+ }
+}
+
+void NumberAnimation::Start(State &s) {
+ if (number > 999) {
+ animation[0].Start(s);
+ } else if (number > 99) {
+ animation[1].Start(s);
+ } else if (number > 9) {
+ animation[2].Start(s);
+ } else {
+ animation[3].Start(s);
+ }
+}
+
+bool NumberAnimation::Running() const {
+ return animation[0].Running() || animation[1].Running() || animation[2].Running() || animation[3].Running();
+}
+
+void NumberAnimation::CheckTimers(State &s) {
+ if (animation[0].Running() && animation[0].Frame() == 1) {
+ animation[1].Start(s);
+ } else if (animation[1].Running() && animation[1].Frame() == 1) {
+ animation[2].Start(s);
+ } else if (animation[2].Running() && animation[2].Frame() == 1) {
+ animation[3].Start(s);
+ }
+}
+
+
+int NumberAnimation::Width() const {
+ if (number < 10) {
+ return animation[0].GetSprite()->Width();
+ } else if (number < 100) {
+ return 2 * animation[0].GetSprite()->Width();
+ } else if (number < 1000) {
+ return 3 * animation[0].GetSprite()->Width();
+ } else {
+ return 4 * animation[0].GetSprite()->Width();
+ }
+}
+
+int NumberAnimation::Height() const {
+ return animation[0].GetSprite()->Height();
+}
+
+void NumberAnimation::Draw(SDL_Surface *dest, const Point<int> &positionIn) const {
+ Point<int> position(positionIn);
+ Vector<int> step(animation[0].GetSprite()->Width(), 0);
+ if (number > 999) {
+ if (animation[0].Running()) {
+ animation[0].Draw(dest, position);
+ }
+ position += step;
+ }
+ if (number > 99) {
+ if (animation[1].Running() || animation[0].Running()) {
+ animation[1].Draw(dest, position);
+ }
+ position += step;
+ }
+ if (number > 9) {
+ if (animation[2].Running() || animation[1].Running() || animation[0].Running()) {
+ animation[2].Draw(dest, position);
+ }
+ position += step;
+ }
+ if (animation[3].Running() || animation[2].Running() || animation[1].Running() || animation[0].Running()) {
+ animation[3].Draw(dest, position);
+ }
+}
+
+}