diff --git a/src/common/gfx.cpp b/src/common/gfx.cpp index 850014e6..3e4b4da4 100644 --- a/src/common/gfx.cpp +++ b/src/common/gfx.cpp @@ -73,12 +73,12 @@ SDL_Surface * gfx_createskinsurface( { int loops = 1; if (expand) - loops = NUM_SCHEMES; + loops = PlayerPalette::NUM_PALETTES; //Blit over loaded skin into player image set SDL_Surface * temp = SDL_CreateRGBSurface(skin->flags, 32 * loops, 32, skin->format->BitsPerPixel, skin->format->Rmask, skin->format->Gmask, skin->format->Bmask, skin->format->Amask); - //Take the loaded skin and colorize it for each state (normal, 3 frames of invincibiliy, shielded, tagged, ztarred. got shine) + //Take the loaded skin and colorize it for each state (normal, 3 frames of invincibility, shielded, tagged, ztarred, got shine, frozen) if (SDL_MUSTLOCK(temp)) SDL_LockSurface(temp); diff --git a/src/common/gfx.h b/src/common/gfx.h index 42d3d5c5..5c3da89c 100644 --- a/src/common/gfx.h +++ b/src/common/gfx.h @@ -34,6 +34,19 @@ struct RGB { Uint8 b; }; +enum PlayerPalette { + normal, + invincibility_1, + invincibility_2, + invincibility_3, + shielded, + tagged, + ztarred, + got_shine, + frozen, + NUM_PALETTES +}; + bool gfx_init(int w, int h, bool fullscreen); void gfx_changefullscreen(bool fullscreen); void gfx_flipscreen(); diff --git a/src/common/gfx/gfxPalette.cpp b/src/common/gfx/gfxPalette.cpp index 31fb42a9..04a19571 100644 --- a/src/common/gfx/gfxPalette.cpp +++ b/src/common/gfx/gfxPalette.cpp @@ -12,7 +12,7 @@ gfxPalette::gfxPalette() colorcodes[k] = NULL; for (int i = 0; i < 4; i++) - for (int j = 0; j < NUM_SCHEMES; j++) + for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++) colorschemes[i][j][k] = NULL; } } @@ -29,7 +29,7 @@ void gfxPalette::clear() colorcodes[k] = NULL; for (int i = 0; i < 4; i++) { - for (int j = 0; j < NUM_SCHEMES; j++) { + for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++) { delete [] colorschemes[i][j][k]; colorschemes[i][j][k] = NULL; } @@ -69,7 +69,7 @@ bool gfxPalette::load(const char* palette_path) colorcodes[k] = new Uint8[numcolors]; for (int i = 0; i < 4; i++) - for (int j = 0; j < NUM_SCHEMES; j++) + for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++) colorschemes[i][j][k] = new Uint8[numcolors]; } @@ -93,7 +93,7 @@ bool gfxPalette::load(const char* palette_path) counter += palette->pitch - palette->w * 3; for (int i = 0; i < 4; i++) { - for (int j = 0; j < NUM_SCHEMES; j++) { + for (int j = 0; j < PlayerPalette::NUM_PALETTES; j++) { for (int m = 0; m < numcolors; m++) { colorschemes[i][j][iRedIndex][m] = pixels[counter++]; colorschemes[i][j][iGreenIndex][m] = pixels[counter++]; diff --git a/src/common/gfx/gfxPalette.h b/src/common/gfx/gfxPalette.h index 7dd20151..0fc253d5 100644 --- a/src/common/gfx/gfxPalette.h +++ b/src/common/gfx/gfxPalette.h @@ -3,7 +3,7 @@ #include -#define NUM_SCHEMES 9 +#include "gfx.h" class gfxPalette { public: @@ -22,7 +22,7 @@ class gfxPalette { uint8_t* colorcodes[3]; //[colorcomponents][numcolors] //[numplayers][colorscheme][colorcomponents][numcolors] - uint8_t* colorschemes[4][NUM_SCHEMES][3]; + uint8_t* colorschemes[4][PlayerPalette::NUM_PALETTES][3]; unsigned short numcolors; }; diff --git a/src/smw/gamemodes/Star.cpp b/src/smw/gamemodes/Star.cpp index 94449f56..79a574a0 100644 --- a/src/smw/gamemodes/Star.cpp +++ b/src/smw/gamemodes/Star.cpp @@ -286,7 +286,7 @@ void CGM_Star::playerextraguy(CPlayer &player, short iType) } } -bool CGM_Star::isplayerstar(CPlayer * player) +bool CGM_Star::isplayerstar(const CPlayer * player) { for (short iPlayer = 0; iPlayer < list_players_cnt - 1; iPlayer++) { if (starPlayer[iPlayer] == player) diff --git a/src/smw/gamemodes/Star.h b/src/smw/gamemodes/Star.h index 9fde5b60..1ad17066 100644 --- a/src/smw/gamemodes/Star.h +++ b/src/smw/gamemodes/Star.h @@ -22,7 +22,7 @@ class CGM_Star : public CGM_TimeLimit PlayerKillType playerkilledself(CPlayer &player, KillStyle style); void playerextraguy(CPlayer &player, short iType); - bool isplayerstar(CPlayer * player); + bool isplayerstar(const CPlayer * player); CPlayer * swapplayer(short id, CPlayer * player); CPlayer * getstarplayer(short id) { return starPlayer[id]; diff --git a/src/smw/player.cpp b/src/smw/player.cpp index 21a34707..df08e4b6 100644 --- a/src/smw/player.cpp +++ b/src/smw/player.cpp @@ -858,33 +858,28 @@ void CPlayer::update_usePowerup() triggerPowerup(); } -void CPlayer::update_spriteColor() +PlayerPalette CPlayer::getPlayerPalette() const { - iSrcOffsetX = 0; - - bool fColorChosen = false; if (isInvincible()) { - iSrcOffsetX = animationstate; - fColorChosen = true; - } else { - if (game_values.gamemode->gamemode == game_mode_star) { - CGM_Star * starmode = (CGM_Star*) game_values.gamemode; - StarStyle starmodetype = starmode->getcurrentmodetype(); - if (starmodetype != StarStyle::Multi && starmode->isplayerstar(this)) { - iSrcOffsetX = (starmodetype == StarStyle::Ztar) ? 192 : 224; - fColorChosen = true; - } + return invincibility.getPlayerPalette(); + } + if (game_values.gamemode->gamemode == game_mode_star) { + CGM_Star * starmode = (CGM_Star*) game_values.gamemode; + StarStyle starmodetype = starmode->getcurrentmodetype(); + if (starmodetype != StarStyle::Multi && starmode->isplayerstar(this)) { + return (starmodetype == StarStyle::Ztar) ? PlayerPalette::ztarred : PlayerPalette::got_shine; } } - - if (!fColorChosen) { - if (game_values.gamemode->tagged == this) - iSrcOffsetX = 160; - else if (frozen) - iSrcOffsetX = 256; - else if (isShielded()) - iSrcOffsetX = 128; + if (game_values.gamemode->tagged == this) { + return PlayerPalette::tagged; + } + if (frozen) { + return PlayerPalette::frozen; } + if (isShielded()) { + return PlayerPalette::shielded; + } + return PlayerPalette::normal; } void CPlayer::tryFallingThroughPlatform(short movement_direction) @@ -1224,7 +1219,7 @@ void CPlayer::move() } } - update_spriteColor(); + iSrcOffsetX = 32 * getPlayerPalette(); if (!isready()) { if (state == PlayerState::Waiting) { @@ -1725,9 +1720,6 @@ void CPlayer::SetupNewPlayer() frozen = false; frozentimer = 0; - animationstate = 0; - animationtimer = 0; - killsinrow = 0; killsinrowinair = 0; extrajumps = 0; diff --git a/src/smw/player.h b/src/smw/player.h index f9f998cc..f9bd3aaa 100644 --- a/src/smw/player.h +++ b/src/smw/player.h @@ -90,6 +90,7 @@ class CPlayer short getGlobalID() const { return globalID; } short getTeamID() const { return teamID; } short getColorID() const { return colorID; } + PlayerPalette getPlayerPalette() const; short leftX() const { return ix; } short rightX() const { return ix + PW; } @@ -189,7 +190,6 @@ class CPlayer void update_waitingForRespawn(); void update_respawning(); void update_usePowerup(); - void update_spriteColor(); void updateFrozenStatus(int keymask); void tryFallingThroughPlatform(short); @@ -314,8 +314,6 @@ class CPlayer CPlayerAI * pPlayerAI; gfxSprite *sprites[PGFX_LAST]; - short animationstate; - short animationtimer; uint8_t sprite_state; short sprswitch; diff --git a/src/smw/player_components/PlayerInvincibility.cpp b/src/smw/player_components/PlayerInvincibility.cpp index a8d416dc..79602dd6 100644 --- a/src/smw/player_components/PlayerInvincibility.cpp +++ b/src/smw/player_components/PlayerInvincibility.cpp @@ -1,5 +1,6 @@ #include "PlayerInvincibility.h" +#include #include "GameMode.h" #include "GameValues.h" #include "player.h" @@ -8,6 +9,12 @@ extern CGameValues game_values; extern CResourceManager* rm; +static const std::array INVINCIBILITY_ANIMATION_STATES = {normal, invincibility_1, invincibility_2, invincibility_3}; +static const short INVINCIBILITY_TIME = 580; +static const short INVINCIBILITY_HURRYUP_TIME = 100; +static const short INVINCIBILITY_ANIMATION_DELAY = 4; +static const short INVINCIBILITY_HURRYUP_ANIMATION_DELAY = 7; + void PlayerInvincibility::reset() { invincible = false; @@ -18,8 +25,6 @@ void PlayerInvincibility::turn_on(CPlayer& player) { invincible = true; timer = 0; - player.animationstate = 0; - player.animationtimer = 0; player.shield.abort(); //Stop the invincible music if a player is already invincible @@ -42,21 +47,18 @@ bool PlayerInvincibility::is_on() const void PlayerInvincibility::update(CPlayer& player) { if (invincible) { - player.animationtimer++; - - if ((player.animationtimer > 3 && timer < 480) || player.animationtimer > 6) { - player.animationtimer = 0; - - player.animationstate += 32; - if (player.animationstate > 96) - player.animationstate = 0; - } - - if (++timer > 580) { - player.animationstate = 0; - player.animationtimer = 0; + if (++timer > INVINCIBILITY_TIME) { timer = 0; invincible = false; } } } + +PlayerPalette PlayerInvincibility::getPlayerPalette() const +{ + if (timer < INVINCIBILITY_TIME - INVINCIBILITY_HURRYUP_TIME) { + return INVINCIBILITY_ANIMATION_STATES[(timer / INVINCIBILITY_ANIMATION_DELAY) % INVINCIBILITY_ANIMATION_STATES.size()]; + } else { + return INVINCIBILITY_ANIMATION_STATES[((timer - (INVINCIBILITY_TIME - INVINCIBILITY_HURRYUP_TIME)) / INVINCIBILITY_HURRYUP_ANIMATION_DELAY) % INVINCIBILITY_ANIMATION_STATES.size()]; + } +} diff --git a/src/smw/player_components/PlayerInvincibility.h b/src/smw/player_components/PlayerInvincibility.h index b41a8f13..672a5f8e 100644 --- a/src/smw/player_components/PlayerInvincibility.h +++ b/src/smw/player_components/PlayerInvincibility.h @@ -1,6 +1,8 @@ #ifndef PLAYER_INVINCIBILITY #define PLAYER_INVINCIBILITY +#include "gfx.h" + class CPlayer; class PlayerInvincibility @@ -10,6 +12,7 @@ class PlayerInvincibility void update(CPlayer& player); bool is_on() const; + PlayerPalette getPlayerPalette() const; void turn_on(CPlayer& player); private: