]> git.localhorst.tv Git - l2e.git/commitdiff
added AttackAnimation
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 13 Aug 2012 21:15:06 +0000 (23:15 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 13 Aug 2012 21:15:06 +0000 (23:15 +0200)
AttackAnimation will soon replace the clumsy misery in PerformAttacks

Debug/src/battle/subdir.mk
Release/src/battle/subdir.mk
src/battle/AttackAnimation.cpp [new file with mode: 0644]
src/battle/AttackAnimation.h [new file with mode: 0644]

index df33d4e449215a08782b92531d598ab83a76283f..d2ae20e325161ea296d98405316f00f28a872a87 100644 (file)
@@ -4,6 +4,7 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/battle/AttackAnimation.cpp \
 ../src/battle/AttackChoice.cpp \
 ../src/battle/AttackTypeMenu.cpp \
 ../src/battle/BattleState.cpp \
@@ -17,6 +18,7 @@ CPP_SRCS += \
 ../src/battle/TargetSelection.cpp 
 
 OBJS += \
+./src/battle/AttackAnimation.o \
 ./src/battle/AttackChoice.o \
 ./src/battle/AttackTypeMenu.o \
 ./src/battle/BattleState.o \
@@ -30,6 +32,7 @@ OBJS += \
 ./src/battle/TargetSelection.o 
 
 CPP_DEPS += \
+./src/battle/AttackAnimation.d \
 ./src/battle/AttackChoice.d \
 ./src/battle/AttackTypeMenu.d \
 ./src/battle/BattleState.d \
index 776e8603cc4c6f057eba8e1c58a0448795b5c509..9fd9c9c56f44ccf4f3925300ad2d402bfa632d3b 100644 (file)
@@ -4,6 +4,7 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
+../src/battle/AttackAnimation.cpp \
 ../src/battle/AttackChoice.cpp \
 ../src/battle/AttackTypeMenu.cpp \
 ../src/battle/BattleState.cpp \
@@ -17,6 +18,7 @@ CPP_SRCS += \
 ../src/battle/TargetSelection.cpp 
 
 OBJS += \
+./src/battle/AttackAnimation.o \
 ./src/battle/AttackChoice.o \
 ./src/battle/AttackTypeMenu.o \
 ./src/battle/BattleState.o \
@@ -30,6 +32,7 @@ OBJS += \
 ./src/battle/TargetSelection.o 
 
 CPP_DEPS += \
+./src/battle/AttackAnimation.d \
 ./src/battle/AttackChoice.d \
 ./src/battle/AttackTypeMenu.d \
 ./src/battle/BattleState.d \
diff --git a/src/battle/AttackAnimation.cpp b/src/battle/AttackAnimation.cpp
new file mode 100644 (file)
index 0000000..938982a
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * AttackAnimation.cpp
+ *
+ *  Created on: Aug 13, 2012
+ *      Author: holy
+ */
+
+#include "AttackAnimation.h"
+
+#include "BattleState.h"
+#include "../app/State.h"
+#include "../graphics/Animation.h"
+
+using geometry::Point;
+using graphics::Animation;
+using std::vector;
+
+namespace battle {
+
+AttackAnimation::~AttackAnimation() {
+       for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
+               i->animation->Stop();
+       }
+       for (vector<Animation *>::const_iterator i(foreignAnimations.begin()), end(foreignAnimations.end()); i != end; ++i) {
+               (*i)->Stop();
+       }
+}
+
+
+void AttackAnimation::Wait(int ms) {
+       text.push_back(WAIT);
+       text.push_back(ms);
+}
+
+void AttackAnimation::WaitForAnimation() {
+       text.push_back(WAIT_ANIMATION);
+}
+
+void AttackAnimation::PlayAttackAnimation() {
+       text.push_back(ATTACK_ANIMATION);
+}
+
+void AttackAnimation::PlaySpellAnimation() {
+       text.push_back(SPELL_ANIMATION);
+}
+
+void AttackAnimation::PlayTargetAnimation(graphics::Animation *a) {
+       text.push_back(TARGET_ANIMATION);
+       text.push_back(a);
+}
+
+void AttackAnimation::PlayFullscreenAnimation(graphics::Animation *a) {
+       text.push_back(FULLSCREEN_ANIMATION);
+       text.push_back(a);
+}
+
+
+void AttackAnimation::Start(BattleState *b, app::State *s) {
+       battle = b;
+       state = s;
+       cursor = 0;
+       Update();
+}
+
+void AttackAnimation::Update() {
+       while (!Finished()) {
+               if (ExecuteCommand()) break;
+       }
+}
+
+bool AttackAnimation::ExecuteCommand() {
+       switch (text[cursor].command) {
+               case WAIT:
+                       return ExecuteWait();
+               case WAIT_ANIMATION:
+                       return ExecuteWaitAnimation();
+               case ATTACK_ANIMATION:
+                       return ExecuteAttackAnimation();
+               case SPELL_ANIMATION:
+                       return ExecuteSpellAnimation();
+               case TARGET_ANIMATION:
+                       return ExecuteTargetAnimation();
+               case FULLSCREEN_ANIMATION:
+                       return ExecuteFullscreenAnimation();
+       }
+       ++cursor; // skip unknown command (which should never happen anyway)
+       return false;
+}
+
+bool AttackAnimation::ExecuteWait() {
+       if (timer.Started()) {
+               if (timer.Running()) {
+                       return true;
+               } else {
+                       cursor += 2;
+                       return false;
+               }
+       } else {
+               timer = state->GraphicsTimers().StartCountdown(text[cursor + 1].number);
+               return true;
+       }
+}
+
+
+bool AttackAnimation::ExecuteWaitAnimation() {
+       for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
+               if (i->animation->Running()) return true;
+       }
+       for (vector<Animation *>::const_iterator i(foreignAnimations.begin()), end(foreignAnimations.end()); i != end; ++i) {
+               if ((*i)->Running()) return true;
+       }
+       ++cursor;
+       return false;
+}
+
+bool AttackAnimation::ExecuteAttackAnimation() {
+       foreignAnimations.push_back(battle->HeroAt(0).AttackAnimation());
+       battle->HeroAt(0).AttackAnimation()->Start(*state);
+       ++cursor;
+       return false;
+}
+
+bool AttackAnimation::ExecuteSpellAnimation() {
+       foreignAnimations.push_back(battle->HeroAt(0).SpellAnimation());
+       battle->HeroAt(0).SpellAnimation()->Start(*state);
+       ++cursor;
+       return false;
+}
+
+bool AttackAnimation::ExecuteTargetAnimation() {
+       AnimationMemo am;
+       am.animation = (Animation *) text[cursor + 1].ptr;
+       am.animation->Start(*state);
+       am.position = Point<int>(100, 100);
+       animations.push_back(am);
+       cursor += 2;
+       return true;
+}
+
+bool AttackAnimation::ExecuteFullscreenAnimation() {
+       AnimationMemo am;
+       am.animation = (Animation *) text[cursor + 1].ptr;
+       am.animation->Start(*state);
+       am.position = Point<int>(100, 100);
+       animations.push_back(am);
+       cursor += 2;
+       return true;
+}
+
+
+bool AttackAnimation::Finished() const {
+       return cursor >= int(text.size());
+}
+
+void AttackAnimation::Render(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
+       for (vector<AnimationMemo>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
+               if (i->animation->Running()) {
+                       i->animation->Draw(screen, i->position + offset);
+               }
+       }
+}
+
+}
diff --git a/src/battle/AttackAnimation.h b/src/battle/AttackAnimation.h
new file mode 100644 (file)
index 0000000..a8ec014
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * AttackAnimation.h
+ *
+ *  Created on: Aug 13, 2012
+ *      Author: holy
+ */
+
+#ifndef BATTLE_ATTACKANIMATION_H_
+#define BATTLE_ATTACKANIMATION_H_
+
+#include "../app/Timer.h"
+#include "../geometry/Point.h"
+#include "../geometry/Vector.h"
+
+#include <vector>
+#include <SDL.h>
+
+namespace app { class State; }
+namespace graphics { class Animation; }
+
+namespace battle {
+
+class BattleState;
+
+class AttackAnimation {
+
+public:
+       AttackAnimation() : battle(0), state(0), cursor(0) { }
+       ~AttackAnimation();
+
+public:
+       void Wait(int ms);
+       void WaitForAnimation();
+       void PlayAttackAnimation();
+       void PlaySpellAnimation();
+       void PlayTargetAnimation(graphics::Animation *);
+       void PlayFullscreenAnimation(graphics::Animation *);
+
+public:
+       void Start(BattleState *, app::State *);
+       void Update();
+       bool Finished() const;
+       void Render(SDL_Surface *screen, const geometry::Vector<int> &offset) const;
+
+private:
+       bool ExecuteCommand();
+
+       bool ExecuteWait();
+       bool ExecuteWaitAnimation();
+       bool ExecuteAttackAnimation();
+       bool ExecuteSpellAnimation();
+       bool ExecuteTargetAnimation();
+       bool ExecuteFullscreenAnimation();
+
+private:
+       enum Command {
+               WAIT,
+               WAIT_ANIMATION,
+               ATTACK_ANIMATION,
+               SPELL_ANIMATION,
+               TARGET_ANIMATION,
+               FULLSCREEN_ANIMATION,
+       };
+       union Code {
+               Code(Command c) : command(c) { }
+               Code(int n) : number(n) { }
+               Code(void *p) : ptr(p) { }
+               Command command;
+               int number;
+               void *ptr;
+       };
+       BattleState *battle;
+       app::State *state;
+       std::vector<Code> text;
+       app::Timer<Uint32> timer;
+       struct AnimationMemo {
+               graphics::Animation *animation;
+               geometry::Point<int> position;
+       };
+       std::vector<AnimationMemo> animations;
+       std::vector<graphics::Animation *> foreignAnimations;
+       int cursor;
+
+};
+
+}
+
+#endif /* BATTLE_ATTACKANIMATION_H_ */