Skip to content

Commit

Permalink
Fixed the actor drawing order in battles.
Browse files Browse the repository at this point in the history
This removed an old TODO, and permitted me to add
a fade out of the heroes stamina icons at battle wins.
  • Loading branch information
Yohann Ferreira committed Jun 29, 2012
1 parent 1333402 commit 50c9e81
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 66 deletions.
52 changes: 28 additions & 24 deletions src/modes/battle/battle.cpp
Expand Up @@ -259,7 +259,8 @@ BattleMode::BattleMode() :
_dialogue_supervisor(NULL),
_finish_supervisor(NULL),
_current_number_swaps(0),
_last_enemy_dying(false)
_last_enemy_dying(false),
_stamina_icon_alpha(1.0f)
{
IF_PRINT_DEBUG(BATTLE_DEBUG) << "constructor invoked" << endl;

Expand Down Expand Up @@ -336,7 +337,10 @@ void BattleMode::Reset() {
GetScriptSupervisor().Reset();
}


bool CompareActorsYCoord(BattleActor* one, BattleActor* other) {
// Compares the Y-coordinates of the actors, used to sort the actors before draw calls
return (one->GetYLocation() > other->GetYLocation());
}

void BattleMode::Update() {
// Update potential battle animations
Expand Down Expand Up @@ -364,6 +368,18 @@ void BattleMode::Update() {
}
}

// Update all actors animations and y-sorting
_battle_sprites.clear();
for (uint32 i = 0; i < _character_actors.size(); i++) {
_character_actors[i]->Update();
_battle_sprites.push_back(_character_actors[i]);
}
for (uint32 i = 0; i < _enemy_actors.size(); i++) {
_enemy_actors[i]->Update();
_battle_sprites.push_back(_enemy_actors[i]);
}
std::sort(_battle_sprites.begin(), _battle_sprites.end(), CompareActorsYCoord);

// If the battle is transitioning to/from a different mode, the sequence supervisor has control
if (_state == BATTLE_STATE_INITIAL || _state == BATTLE_STATE_EXITING) {
_sequence_supervisor->Update();
Expand Down Expand Up @@ -431,6 +447,11 @@ void BattleMode::Update() {
// If the battle is in either finish state, the finish supervisor has control
else if ((_state == BATTLE_STATE_VICTORY) || (_state == BATTLE_STATE_DEFEAT)) {
_finish_supervisor->Update();

// Make the heroes stamina icons fade out
if (_stamina_icon_alpha > 0.0f)
_stamina_icon_alpha -= (float)SystemManager->GetUpdateTime() / 800.0f;

return;
}

Expand Down Expand Up @@ -468,14 +489,6 @@ void BattleMode::Update() {
break;
}
}

// Update all actors
for (uint32 i = 0; i < _character_actors.size(); i++) {
_character_actors[i]->Update();
}
for (uint32 i = 0; i < _enemy_actors.size(); i++) {
_enemy_actors[i]->Update();
}
} // void BattleMode::Update()


Expand Down Expand Up @@ -989,22 +1002,13 @@ void BattleMode::_DrawSprites() {
// Else this target is invalid so don't draw anything
}

// TODO: Draw sprites in order based on their x and y coordinates on the screen (bottom to top, then left to right)
// Right now they are drawn randomly, which would look bad/inconsistent if the sprites overlapped during their actions

// Draw all character sprites
// Draw sprites in order based on their x and y coordinates on the screen (bottom to top)
VideoManager->SetDrawFlags(VIDEO_X_CENTER, VIDEO_Y_BOTTOM, VIDEO_BLEND, 0);
for (uint32 i = 0; i < _character_actors.size(); i++) {
_character_actors[i]->DrawSprite();
}

// Draw all enemy sprites
for (uint32 i = 0; i < _enemy_actors.size(); i++) {
_enemy_actors[i]->DrawSprite();
}
for (uint32 i = 0; i < _battle_sprites.size(); ++i)
_battle_sprites[i]->DrawSprite();

// Draw the attack point selector graphic
if (draw_point_selection == true) {
if (draw_point_selection) {
uint32 point = target.GetPoint();

VideoManager->SetDrawFlags(VIDEO_X_CENTER, VIDEO_Y_CENTER, VIDEO_BLEND, 0);
Expand Down Expand Up @@ -1124,7 +1128,7 @@ void BattleMode::_DrawStaminaBar() {
if (!_character_actors[i]->IsAlive())
continue;

_character_actors[i]->DrawStaminaIcon();
_character_actors[i]->DrawStaminaIcon(Color(1.0f, 1.0f, 1.0f, _stamina_icon_alpha));

if (!draw_icon_selection)
continue;
Expand Down
8 changes: 8 additions & 0 deletions src/modes/battle/battle.h
Expand Up @@ -409,6 +409,11 @@ class BattleMode : public hoa_mode_manager::GameMode {
std::list<private_battle::BattleActor*> _ready_queue;
//@}

/** \brief List used to draw character based on their y coordinate.
*** Sorted in the update() method.
**/
std::vector<private_battle::BattleActor*> _battle_sprites;

/** \brief The number of character swaps that the player may currently perform
*** The maximum number of swaps ever allowed is four, thus the value of this class member will always have the range [0, 4].
*** This member is also used to determine how many swap cards to draw on the battle screen.
Expand All @@ -421,6 +426,9 @@ class BattleMode : public hoa_mode_manager::GameMode {
**/
bool _last_enemy_dying;

//! \brief the Stamina Icon general transluency. Used to make the characters's stamina icon disappear on wins.
float _stamina_icon_alpha;

////////////////////////////// PRIVATE METHODS ///////////////////////////////

//! \brief Initializes all data necessary for the battle to begin
Expand Down
23 changes: 4 additions & 19 deletions src/modes/battle/battle_actors.cpp
Expand Up @@ -402,16 +402,17 @@ void BattleActor::DrawIndicators() const {
}


void BattleActor::DrawStaminaIcon() const {
void BattleActor::DrawStaminaIcon(const hoa_video::Color& color) const {
if (!IsAlive())
return;

VideoManager->Move(_x_stamina_location, _y_stamina_location);
// Make the stamina icon fade away when dying
if (_state == ACTOR_STATE_DYING)
_stamina_icon.Draw(Color(1.0f, 1.0f, 1.0f, 1.0f - _state_timer.PercentComplete()));
_stamina_icon.Draw(Color(color.GetRed(), color.GetGreen(),
color.GetBlue(), color.GetAlpha() - _state_timer.PercentComplete()));
else
_stamina_icon.Draw();
_stamina_icon.Draw(color);
}


Expand Down Expand Up @@ -856,33 +857,17 @@ BattleEnemy::BattleEnemy(GlobalEnemy* enemy) :
}
}



BattleEnemy::~BattleEnemy() {
delete _global_actor;
}



// Compares the Y-coordinates of the actors, used for sorting the actors up-down when drawing
bool BattleEnemy::operator<(const BattleEnemy & other) const {
// NOTE: this code is currently not working correctly
//if ((_y_location - ((*GetActor()).GetHeight())) > (other.GetYLocation() - (*(other.GetActor()).GetHeight())))
// return true;
return false;
}



void BattleEnemy::ResetActor() {
BattleActor::ResetActor();

vector<StillImage>& sprite_frames = *(_global_enemy->GetBattleSpriteFrames());
sprite_frames[3].DisableGrayScale();
}



void BattleEnemy::ChangeState(ACTOR_STATE new_state) {
BattleActor::ChangeState(new_state);

Expand Down
2 changes: 1 addition & 1 deletion src/modes/battle/battle_actors.h
Expand Up @@ -221,7 +221,7 @@ class BattleActor : public hoa_global::GlobalActor {
void DrawIndicators() const;

//! \brief Draws the stamina icon
void DrawStaminaIcon() const;
void DrawStaminaIcon(const hoa_video::Color& color = hoa_video::Color::white) const;

/** \brief Sets the action that the actor should execute next
*** \param action A pointer to the action that the actor should execute
Expand Down
24 changes: 2 additions & 22 deletions src/modes/battle/battle_sequence.cpp
Expand Up @@ -79,14 +79,6 @@ void SequenceSupervisor::Update() {
_battle->ChangeState(BATTLE_STATE_NORMAL);
break;
}

// Update the animations of all actors
for (uint32 i = 0; i < _battle->_character_actors.size(); i++) {
_battle->_character_actors[i]->Update(true);
}
for (uint32 i = 0; i < _battle->_enemy_actors.size(); i++) {
_battle->_enemy_actors[i]->Update(true);
}
}


Expand Down Expand Up @@ -328,7 +320,7 @@ void SequenceSupervisor::_DrawGUI() {

// Determine the draw order of stamina icons for all living actors
// A container to hold all actors that should have their stamina icons drawn
vector<BattleActor*> live_actors;
std::vector<BattleActor*> live_actors;

for (uint32 i = 0; i < _battle->_character_actors.size(); i++) {
if (_battle->_character_actors[i]->IsAlive())
Expand All @@ -339,7 +331,7 @@ void SequenceSupervisor::_DrawGUI() {
live_actors.push_back(_battle->_enemy_actors[i]);
}

vector<float> draw_positions(live_actors.size(), 0.0f);
std::vector<float> draw_positions(live_actors.size(), 0.0f);
for (uint32 i = 0; i < live_actors.size(); i++) {
switch (live_actors[i]->GetState()) {
case ACTOR_STATE_IDLE:
Expand All @@ -352,22 +344,10 @@ void SequenceSupervisor::_DrawGUI() {
}
}

// TODO: sort the draw positions container

// Draw the stamina bar
VideoManager->SetDrawFlags(VIDEO_X_CENTER, VIDEO_Y_BOTTOM, 0);
VideoManager->Move(STAMINA_BAR_POSITION_X + _gui_position_offset, STAMINA_BAR_POSITION_Y);
_battle->GetMedia().stamina_meter.Draw();

// Draw all stamina icons in order
VideoManager->SetDrawFlags(VIDEO_X_CENTER, VIDEO_Y_CENTER, 0);
for (uint32 i = 0; i < live_actors.size(); i++) {
if (live_actors[i]->IsEnemy() == false)
VideoManager->Move(STAMINA_BAR_POSITION_X - 25.0f + _gui_position_offset, draw_positions[i]);
else
VideoManager->Move(STAMINA_BAR_POSITION_X + 25.0f + _gui_position_offset, draw_positions[i]);
live_actors[i]->GetStaminaIcon().Draw();
}
} // void SequenceSupervisor::_DrawGUI()

} // namespace private_battle
Expand Down

0 comments on commit 50c9e81

Please sign in to comment.