Skip to content

Commit

Permalink
Fix Battler Z ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
fmatthew5876 committed Jun 4, 2019
1 parent f3fb028 commit 996db04
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
30 changes: 26 additions & 4 deletions src/sprite_battler.cpp
Expand Up @@ -26,8 +26,8 @@
#include "reader_util.h"
#include "output.h"

Sprite_Battler::Sprite_Battler(Game_Battler* battler) :
battler(battler) {
Sprite_Battler::Sprite_Battler(Game_Battler* battler, int index) :
battler(battler), battle_index(index) {
}

Sprite_Battler::~Sprite_Battler() {
Expand Down Expand Up @@ -298,14 +298,32 @@ int Sprite_Battler::GetHeight() const {
return Sprite::GetHeight();
}

void Sprite_Battler::ResetZ() {
static_assert(Game_Battler::Type_Ally < Game_Battler::Type_Enemy, "Game_Battler enums re-ordered! Fix Z order logic here!");

constexpr int id_limit = 128;

int y = battler->GetBattleY();
if (graphic) {
y += graphic->GetHeight() / 2;
}

int z = battler->GetType();
z *= SCREEN_TARGET_HEIGHT * 2;
z += y;
z *= id_limit;
z += id_limit - battle_index;
z += Priority_Battler;

SetZ(z);
}

void Sprite_Battler::CreateSprite() {
sprite_name = battler->GetSpriteName();
hue = battler->GetHue();

SetX(battler->GetDisplayX());
SetY(battler->GetDisplayY());
// Battlers at the bottom appear above battlers at the top
SetZ(Priority_Battler + battler->GetBattleY());

// Not animated -> Monster
if (battler->GetBattleAnimationId() == 0) {
Expand All @@ -314,6 +332,7 @@ void Sprite_Battler::CreateSprite() {
SetOx(graphic->GetWidth() / 2);
SetOy(graphic->GetHeight() / 2);
SetBitmap(graphic);
ResetZ();
}
else {
FileRequestAsync* request = AsyncHandler::RequestFile("Monster", sprite_name);
Expand All @@ -327,6 +346,7 @@ void Sprite_Battler::CreateSprite() {
SetOy(24);
SetAnimationState(anim_state);
idling = true;
ResetZ();
}

SetVisible(!battler->IsHidden());
Expand Down Expand Up @@ -368,6 +388,8 @@ void Sprite_Battler::OnMonsterSpriteReady(FileRequestResult* result) {
SetOx(graphic->GetWidth() / 2);
SetOy(graphic->GetHeight() / 2);

ResetZ();

bool hue_change = hue != 0;
if (hue_change) {
BitmapRef new_graphic = Bitmap::Create(graphic->GetWidth(), graphic->GetHeight());
Expand Down
6 changes: 5 additions & 1 deletion src/sprite_battler.h
Expand Up @@ -57,8 +57,9 @@ class Sprite_Battler : public Sprite {
* Constructor.
*
* @param battler game battler to display
* @param battle_index battle index for Z ordering
*/
Sprite_Battler(Game_Battler* battler);
Sprite_Battler(Game_Battler* battler, int battle_index);

~Sprite_Battler() override;

Expand Down Expand Up @@ -102,6 +103,7 @@ class Sprite_Battler : public Sprite {
void OnMonsterSpriteReady(FileRequestResult* result);
void OnBattlercharsetReady(FileRequestResult* result, int32_t battler_index);
int GetMaxOpacity() const;
void ResetZ();

std::string sprite_name;
int hue = 0;
Expand All @@ -114,6 +116,7 @@ class Sprite_Battler : public Sprite {
int fade_out = 255;
int fade_out_incr = 15;
int flash_counter = 0;
int battle_index = 0;
LoopState loop_state = LoopState_DefaultAnimationAfterFinish;
bool old_hidden = false;
std::unique_ptr<BattleAnimation> animation;
Expand All @@ -124,4 +127,5 @@ class Sprite_Battler : public Sprite {
FileRequestBinding request_id;
};


#endif
7 changes: 6 additions & 1 deletion src/spriteset_battle.cpp
Expand Up @@ -49,8 +49,13 @@ Spriteset_Battle::Spriteset_Battle() {
}
}

int enemy_index = 0;
for (Game_Battler* b : battler) {
sprites.push_back(std::make_shared<Sprite_Battler>(b));
// For Z ordering, actors use actors id and enemies use troop id.
const int index = (b->GetType() == Game_Battler::Type_Ally)
? b->GetId() : enemy_index++;

sprites.push_back(std::make_shared<Sprite_Battler>(b, index));
if (b->GetType() == Game_Battler::Type_Ally) {
sprites.back()->SetVisible(false);
}
Expand Down

0 comments on commit 996db04

Please sign in to comment.