../src/battle/HeroTag.cpp \
../src/battle/Monster.cpp \
../src/battle/MoveMenu.cpp \
+../src/battle/NumberAnimation.cpp \
../src/battle/PartyLayout.cpp \
../src/battle/SmallHeroTag.cpp \
../src/battle/TargetSelection.cpp
./src/battle/HeroTag.o \
./src/battle/Monster.o \
./src/battle/MoveMenu.o \
+./src/battle/NumberAnimation.o \
./src/battle/PartyLayout.o \
./src/battle/SmallHeroTag.o \
./src/battle/TargetSelection.o
./src/battle/HeroTag.d \
./src/battle/Monster.d \
./src/battle/MoveMenu.d \
+./src/battle/NumberAnimation.d \
./src/battle/PartyLayout.d \
./src/battle/SmallHeroTag.d \
./src/battle/TargetSelection.d
../src/battle/HeroTag.cpp \
../src/battle/Monster.cpp \
../src/battle/MoveMenu.cpp \
+../src/battle/NumberAnimation.cpp \
../src/battle/PartyLayout.cpp \
../src/battle/SmallHeroTag.cpp \
../src/battle/TargetSelection.cpp
./src/battle/HeroTag.o \
./src/battle/Monster.o \
./src/battle/MoveMenu.o \
+./src/battle/NumberAnimation.o \
./src/battle/PartyLayout.o \
./src/battle/SmallHeroTag.o \
./src/battle/TargetSelection.o
./src/battle/HeroTag.d \
./src/battle/Monster.d \
./src/battle/MoveMenu.d \
+./src/battle/NumberAnimation.d \
./src/battle/PartyLayout.d \
./src/battle/SmallHeroTag.d \
./src/battle/TargetSelection.d
--- /dev/null
+/*
+ * 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);
+ }
+}
+
+}
--- /dev/null
+/*
+ * NumberAnimation.h
+ *
+ * Created on: Aug 12, 2012
+ * Author: holy
+ */
+
+#ifndef BATTLE_NUMBERANIMATION_H_
+#define BATTLE_NUMBERANIMATION_H_
+
+#include "../graphics/ComplexAnimation.h"
+
+#include "../geometry/Point.h"
+
+namespace app {
+ class Application;
+ class State;
+}
+
+namespace battle {
+
+class NumberAnimation {
+
+public:
+ NumberAnimation() : number(-1) { }
+ NumberAnimation(int number, const graphics::ComplexAnimation &prototype, const graphics::Sprite *numbers);
+
+public:
+ void Start(app::State &);
+ bool Running() const;
+ void CheckTimers(app::State &);
+
+ int Width() const;
+ int Height() const;
+
+ void Draw(SDL_Surface *dest, const geometry::Point<int> &position) const;
+
+private:
+ int number;
+ graphics::ComplexAnimation animation[4];
+
+};
+
+}
+
+#endif /* BATTLE_NUMBERANIMATION_H_ */
using app::Input;
using geometry::Point;
using geometry::Vector;
+using std::vector;
namespace battle {
void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
ctrl = &c;
battle->WriteOrder(order);
+ numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
+ numberPosition.reserve(numberAnimation.size());
}
void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
void PerformAttacks::HandleEvents(const Input &input) {
- if (titleBarTimer.Running() || (moveAnimation && moveAnimation->Running())) return;
-
- if (moveAnimation) moveAnimation->Stop();
- titleBarTimer.Clear();
-
- ++cursor;
-
- while (cursor < int(order.size())) {
- if (order[cursor].isMonster) {
- if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
- } else {
- if (battle->HeroAt(order[cursor].index).Health() > 0) break;
- }
- ++cursor;
- }
-
- if (cursor >= int(order.size())) {
+ CheckNumberAnimation();
+ if (HasAnimationsRunning()) return;
+ ResetAnimation();
+ AdvanceCursor();
+ if (Finished()) {
battle->ClearAllAttacks();
ctrl->PopState();
return;
case AttackChoice::SWORD:
titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!";
moveAnimation = hero.AttackAnimation();
+ numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
+ if (ac.Selection().TargetsEnemies()) {
+ numberPosition.push_back(
+ battle->MonsterPositions()[ac.Selection().SingleSelection()]);
+ } else {
+ numberPosition.push_back(
+ battle->HeroesPositions()[ac.Selection().SingleSelection()]);
+ }
break;
case AttackChoice::MAGIC:
titleBarText = ac.GetSpell()->Name();
}
}
- titleBarTimer = GraphicsTimers().StartCountdown(1500);
+ if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
if (moveAnimation) moveAnimation->Start(*this);
}
+void PerformAttacks::CheckNumberAnimation() {
+ if (moveAnimation && moveAnimation->Running()) return;
+ if (!moveAnimation || moveAnimation->JustFinished()) {
+ for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
+ i->Start(*this);
+ }
+ } else {
+ for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
+ i->CheckTimers(*this);
+ }
+ }
+}
+
+bool PerformAttacks::HasAnimationsRunning() const {
+ if (titleBarTimer.Running()) return true;
+ if (moveAnimation && moveAnimation->Running()) return true;
+ for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
+ if (i->Running()) return true;
+ }
+ return false;
+}
+
+void PerformAttacks::ResetAnimation() {
+ if (moveAnimation) moveAnimation->Stop();
+ titleBarTimer.Clear();
+ numberAnimation.clear();
+ numberPosition.clear();
+}
+
+void PerformAttacks::AdvanceCursor() {
+ ++cursor;
+ while (cursor < int(order.size())) {
+ if (order[cursor].isMonster) {
+ if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
+ } else {
+ if (battle->HeroAt(order[cursor].index).Health() > 0) break;
+ }
+ ++cursor;
+ }
+}
+
void PerformAttacks::UpdateWorld(float deltaT) {
battle->RenderHeroes(screen, offset);
battle->RenderSmallHeroTags(screen, offset);
RenderTitleBar(screen, offset);
+ RenderNumbers(screen, offset);
}
void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
}
+void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) {
+ for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
+ if (numberAnimation[i].Running()) {
+ Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
+ numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
+ }
+ }
+}
+
}
#include "../../app/State.h"
#include "../BattleState.h"
+#include "../NumberAnimation.h"
#include "../../geometry/Vector.h"
+#include "../../graphics/ComplexAnimation.h"
#include <vector>
-namespace graphics { class Animation; }
-
namespace battle {
class PerformAttacks
virtual void UpdateWorld(float deltaT);
virtual void Render(SDL_Surface *);
+private:
+ void CheckNumberAnimation();
+ bool HasAnimationsRunning() const;
+ void ResetAnimation();
+ void AdvanceCursor();
+ bool Finished() const { return cursor >= int(order.size()); }
+
private:
void RenderTitleBar(SDL_Surface *screen, const geometry::Vector<int> &offset);
+ void RenderNumbers(SDL_Surface *screen, const geometry::Vector<int> &offset);
private:
app::Application *ctrl;
const char *titleBarText;
app::Timer<Uint32> titleBarTimer;
std::vector<BattleState::Order> order;
+ std::vector<NumberAnimation> numberAnimation;
+ std::vector<geometry::Point<int> > numberPosition;
int cursor;
};
bool Running() const {
return timer.Running() && (repeat || timer.Iteration() < NumFrames());
}
+ bool JustFinished() const {
+ return timer.JustHit() && timer.Iteration() == NumFrames();
+ }
const app::Timer<Uint32> &GetTimer() { return timer; }