From 7263bddbde91d555decd58f043f6b43f54ba2b00 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 19 Aug 2012 22:51:53 +0200 Subject: [PATCH 1/1] added some assertions --- src/battle/BattleState.cpp | 17 +++++++++++++++- src/battle/BattleState.h | 29 ++++++++++++++-------------- src/battle/states/PerformAttacks.cpp | 2 +- src/main.cpp | 2 +- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index b934039..d0bb108 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -21,6 +21,7 @@ #include "../graphics/Sprite.h" #include +#include #include using app::Application; @@ -103,6 +104,7 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { } void BattleState::LoadSpellMenu(vector::size_type index) { + assert(index >= 0 && index < 4); spellMenus[index].Clear(); spellMenus[index].Reserve(HeroAt(index).Spells().size()); for (vector::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) { @@ -112,6 +114,7 @@ void BattleState::LoadSpellMenu(vector::size_type index) { } void BattleState::LoadIkariMenu(vector::size_type index) { + assert(index >= 0 && index < 4); ikariMenus[index].Clear(); ikariMenus[index].Reserve(6); @@ -264,7 +267,7 @@ class OrderCompare { void BattleState::CalculateAttackOrder() { attackOrder.reserve(monsters.size() + NumHeroes()); - for (int i(0); i < numHeroes; ++i) { + for (int i(0); i < NumHeroes(); ++i) { attackOrder.push_back(Order(i, false)); } for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { @@ -287,6 +290,11 @@ void BattleState::NextAttack() { } } +bool BattleState::AttacksFinished() const { + return attackCursor >= int(attackOrder.size()) + || Victory() || Defeat(); +} + void BattleState::CalculateDamage() { AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index)); if (ac.GetType() == AttackChoice::DEFEND) return; @@ -370,6 +378,7 @@ void BattleState::ApplyDamage() { for (int i(0); i < MaxMonsters(); ++i) { if (ts.IsBad(i)) { MonsterAt(i).SubtractHealth(ts.GetAmount(i)); + // TODO: collect reward if dead } } } else { @@ -401,12 +410,14 @@ void BattleState::UpdateWorld(float deltaT) { } void BattleState::Render(SDL_Surface *screen) { + assert(screen); Vector offset(CalculateScreenOffset(screen)); RenderBackground(screen, offset); RenderMonsters(screen, offset); } void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offset) { + assert(screen); // black for now SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); SDL_Rect destRect; @@ -418,6 +429,7 @@ void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offse } void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) { + assert(screen); for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { if (MonsterPositionOccupied(i)) { // TODO: better solution for running animations @@ -433,6 +445,7 @@ void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) } void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { + assert(screen); for (int i(0); i < numHeroes; ++i) { if (heroes[i].AttackAnimation() && heroes[i].AttackAnimation()->Running()) { heroes[i].AttackAnimation()->DrawCenter(screen, heroesPositions[i] + offset); @@ -446,6 +459,7 @@ void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { } void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) { + assert(screen); int tagHeight(attackTypeMenu.Height()); int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2); @@ -455,6 +469,7 @@ void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) } void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector &offset) { + assert(screen); int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2); int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2); diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 578098f..ab9f731 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -22,6 +22,7 @@ #include "../graphics/Animation.h" #include "../graphics/Menu.h" +#include #include #include @@ -57,7 +58,7 @@ public: , numHeroes(0) , activeHero(-1) , attackCursor(-1) - , ranAway(false) { } + , ranAway(false) { assert(background && res); } public: void AddMonster(const Monster &); @@ -94,19 +95,19 @@ public: Hero &ActiveHero() { return heroes[activeHero]; } const Hero &ActiveHero() const { return heroes[activeHero]; } - Hero &HeroAt(int index) { return heroes[index]; } - const Hero &HeroAt(int index) const { return heroes[index]; } - Monster &MonsterAt(int index) { return monsters[index]; } - const Monster &MonsterAt(int index) const { return monsters[index]; } + Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; } + const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; } + Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; } + const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; } - const HeroTag &HeroTagAt(int index) const { return heroTags[index]; } - const geometry::Point &HeroTagPositionAt(int index) const { return heroTagPositions[index]; } + const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; } + const geometry::Point &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; } - bool HasChosenAttackType() const { return attackChoices[activeHero].GetType() != AttackChoice::UNDECIDED; } - AttackChoice &ActiveHeroAttackChoice() { return attackChoices[activeHero]; } - const AttackChoice &ActiveHeroAttackChoice() const { return attackChoices[activeHero]; } - AttackChoice &AttackChoiceAt(int index) { return attackChoices[index]; } - const AttackChoice &AttackChoiceAt(int index) const { return attackChoices[index]; } + bool HasChosenAttackType() const { return ActiveHeroAttackChoice().GetType() != AttackChoice::UNDECIDED; } + AttackChoice &ActiveHeroAttackChoice() { return AttackChoiceAt(activeHero); } + const AttackChoice &ActiveHeroAttackChoice() const { return AttackChoiceAt(activeHero); } + AttackChoice &AttackChoiceAt(int index) { assert(index >= 0 && index < NumHeroes()); return attackChoices[index]; } + const AttackChoice &AttackChoiceAt(int index) const { assert(index >= 0 && index < NumHeroes()); return attackChoices[index]; } bool AttackSelectionDone() const { return activeHero >= numHeroes; } int NumHeroes() const { return numHeroes; } @@ -129,10 +130,10 @@ public: void CalculateAttackOrder(); void NextAttack(); - bool AttacksFinished() const { return attackCursor >= int(attackOrder.size()); } + bool AttacksFinished() const; void CalculateDamage(); void ApplyDamage(); - const Order &CurrentAttack() const { return attackOrder[attackCursor]; }; + const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; }; void ClearAllAttacks(); bool Victory() const; diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 86d7a7c..8550318 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -39,6 +39,7 @@ void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) { } void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) { + battle->ClearAllAttacks(); ctrl = 0; } @@ -63,7 +64,6 @@ void PerformAttacks::HandleEvents(const Input &input) { battle->ApplyDamage(); battle->NextAttack(); if (battle->AttacksFinished()) { - battle->ClearAllAttacks(); ctrl->PopState(); return; } diff --git a/src/main.cpp b/src/main.cpp index 1781e5f..f7707ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,7 +98,7 @@ int main(int argc, char **argv) { monster.SetHealth(8); monster.SetStats(Stats(14, 6, 6, 6, 6, 6, 6)); monster.SetReward(3, 5); - ComplexAnimation monsterAttackAnimation(&monsterSprite, 120); + ComplexAnimation monsterAttackAnimation(&monsterSprite, 4 * framerate); monsterAttackAnimation.AddFrame(0, 1, Vector(16, 0)); monsterAttackAnimation.AddFrame(0, 0, Vector(16, 0)); monsterAttackAnimation.AddFrame(0, 1, Vector(16, 0)); -- 2.39.2