diff --git a/src/smw/world.cpp b/src/smw/world.cpp index 97e6ebac..6f4c8336 100644 --- a/src/smw/world.cpp +++ b/src/smw/world.cpp @@ -63,16 +63,10 @@ int popNextInt(std::list& list, int defval = 0) **********************************/ WorldMovingObject::WorldMovingObject() - : iDrawSprite(0) - , iDrawDirection(0) - , iTileSize(0) { SetPosition(0, 0); } -WorldMovingObject::~WorldMovingObject() -{} - void WorldMovingObject::Init(short iCol, short iRow, short iSprite, short iInitialDirection, short tilesize) { iTileSize = tilesize; @@ -86,17 +80,17 @@ void WorldMovingObject::Init(short iCol, short iRow, short iSprite, short iIniti void WorldMovingObject::Move(short iDirection) { if (iDirection == 0) { - iDestTileY--; + destTile.y--; iState = 1; } else if (iDirection == 1) { - iDestTileY++; + destTile.y++; iState = 2; } else if (iDirection == 2) { - iDestTileX--; + destTile.x--; iState = 3; iDrawDirection = 1; } else if (iDirection == 3) { - iDestTileX++; + destTile.x++; iState = 4; iDrawDirection = 0; } @@ -112,38 +106,38 @@ bool WorldMovingObject::Update() } if (iState == 1) { - iy -= 2; - if (iy < iDestTileY * iTileSize) { - iy = iDestTileY * iTileSize; + pos.y -= 2; + if (pos.y < destTile.y * iTileSize) { + pos.y = destTile.y * iTileSize; iState = 0; - iCurrentTileY = iDestTileY; + currentTile.y = destTile.y; return true; } } else if (iState == 2) { //down - iy += 2; - if (iy > iDestTileY * iTileSize) { - iy = iDestTileY * iTileSize; + pos.y += 2; + if (pos.y > destTile.y * iTileSize) { + pos.y = destTile.y * iTileSize; iState = 0; - iCurrentTileY = iDestTileY; + currentTile.y = destTile.y; return true; } } else if (iState == 3) { //left - ix -= 2; - if (ix < iDestTileX * iTileSize) { - ix = iDestTileX * iTileSize; + pos.x -= 2; + if (pos.x < destTile.x * iTileSize) { + pos.x = destTile.x * iTileSize; iState = 0; - iCurrentTileX = iDestTileX; + currentTile.x = destTile.x; return true; } } else if (iState == 4) { //right - ix += 2; - if (ix > iDestTileX * iTileSize) { - ix = iDestTileX * iTileSize; + pos.x += 2; + if (pos.x > destTile.x * iTileSize) { + pos.x = destTile.x * iTileSize; iState = 0; - iCurrentTileX = iDestTileX; + currentTile.x = destTile.x; return true; } @@ -159,12 +153,9 @@ void WorldMovingObject::FaceDirection(short iDirection) void WorldMovingObject::SetPosition(short iCol, short iRow) { - ix = iCol * iTileSize; - iy = iRow * iTileSize; - iCurrentTileX = iCol; - iCurrentTileY = iRow; - iDestTileX = iCol; - iDestTileY = iRow; + pos = {iCol * iTileSize, iRow * iTileSize}; + currentTile = {iCol, iRow}; + destTile = {iCol, iRow}; iState = 0; iAnimationFrame = 0; @@ -187,7 +178,7 @@ WorldPlayer::WorldPlayer(short iCol, short iRow) void WorldPlayer::Draw(short iMapOffsetX, short iMapOffsetY) const { - rm->spr_player[iDrawSprite][iAnimationFrame + iDrawDirection]->draw(ix + iMapOffsetX, iy + iMapOffsetY, 0, 0, 32, 32); + rm->spr_player[iDrawSprite][iAnimationFrame + iDrawDirection]->draw(pos.x + iMapOffsetX, pos.y + iMapOffsetY, 0, 0, 32, 32); } void WorldPlayer::SetSprite(short iPlayer) @@ -259,14 +250,14 @@ void WorldVehicle::SetNextDest() if (iState != 0 || iMaxMoves == 0) return; - WorldMapTile * tile = &g_worldmap.tiles[iCurrentTileX][iCurrentTileY]; + WorldMapTile * tile = &g_worldmap.tiles[currentTile.x][currentTile.y]; short iPlayerCurrentTileX, iPlayerCurrentTileY; g_worldmap.GetPlayerCurrentTile(&iPlayerCurrentTileX, &iPlayerCurrentTileY); if (iNumMoves-- <= 0) { - if (tile->iType == 0 && (iPlayerCurrentTileX != iCurrentTileX || iPlayerCurrentTileY != iCurrentTileY) && - g_worldmap.NumVehiclesInTile(iCurrentTileX, iCurrentTileY) <= 1) + if (tile->iType == 0 && (iPlayerCurrentTileX != currentTile.x || iPlayerCurrentTileY != currentTile.y) && + g_worldmap.NumVehiclesInTile(currentTile.x, currentTile.y) <= 1) return; } @@ -281,13 +272,13 @@ void WorldVehicle::SetNextDest() for (short iDirection = 0; iDirection < 4; iDirection++) { bool fIsDoor = false; if (iDirection == 0) - fIsDoor = g_worldmap.IsDoor(iCurrentTileX, iCurrentTileY - 1) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(iCurrentTileX, iCurrentTileY - 1) == iBoundary); + fIsDoor = g_worldmap.IsDoor(currentTile.x, currentTile.y - 1) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x, currentTile.y - 1) == iBoundary); else if (iDirection == 1) - fIsDoor = g_worldmap.IsDoor(iCurrentTileX, iCurrentTileY + 1) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(iCurrentTileX, iCurrentTileY + 1) == iBoundary); + fIsDoor = g_worldmap.IsDoor(currentTile.x, currentTile.y + 1) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x, currentTile.y + 1) == iBoundary); else if (iDirection == 2) - fIsDoor = g_worldmap.IsDoor(iCurrentTileX - 1, iCurrentTileY) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(iCurrentTileX - 1, iCurrentTileY) == iBoundary); + fIsDoor = g_worldmap.IsDoor(currentTile.x - 1, currentTile.y) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x - 1, currentTile.y) == iBoundary); else if (iDirection == 3) - fIsDoor = g_worldmap.IsDoor(iCurrentTileX + 1, iCurrentTileY) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(iCurrentTileX + 1, iCurrentTileY) == iBoundary); + fIsDoor = g_worldmap.IsDoor(currentTile.x + 1, currentTile.y) || (iBoundary != 0 && g_worldmap.GetVehicleBoundary(currentTile.x + 1, currentTile.y) == iBoundary); if (tile->fConnection[iDirection] && !fIsDoor) iConnections[iNumConnections++] = iDirection; @@ -307,7 +298,7 @@ bool WorldVehicle::Update() short iPlayerTileX, iPlayerTileY; g_worldmap.GetPlayerCurrentTile(&iPlayerTileX, &iPlayerTileY); - if (iCurrentTileX == iPlayerTileX && iCurrentTileY == iPlayerTileY) + if (currentTile.x == iPlayerTileX && currentTile.y == iPlayerTileY) return true; SetNextDest(); @@ -332,14 +323,14 @@ bool WorldVehicle::Update() void WorldVehicle::Draw(short iWorldOffsetX, short iWorldOffsetY, bool fVehiclesSleeping) const { if (fVehiclesSleeping) { - SDL_Rect rDst = {ix + iWorldOffsetX, iy + iWorldOffsetY, iTileSize, iTileSize}; + SDL_Rect rDst = {pos.x + iWorldOffsetX, pos.y + iWorldOffsetY, iTileSize, iTileSize}; #ifdef USE_SDL2 SDL_BlitSurface(rm->spr_worldvehicle[iTileSheet].getSurface(), &srcRects[4], blitdest, &rDst); #else // SDL_BlitSurface uses a non-const source rect SDL_BlitSurface(rm->spr_worldvehicle[iTileSheet].getSurface(), const_cast(&srcRects[4]), blitdest, &rDst); #endif } else { - SDL_Rect rDst = {ix + iWorldOffsetX + iPaceOffset, iy + iWorldOffsetY, iTileSize, iTileSize}; + SDL_Rect rDst = {pos.x + iWorldOffsetX + iPaceOffset, pos.y + iWorldOffsetY, iTileSize, iTileSize}; #ifdef USE_SDL2 SDL_BlitSurface(rm->spr_worldvehicle[iTileSheet].getSurface(), &srcRects[iDrawDirection + iAnimationFrame], blitdest, &rDst); #else // SDL_BlitSurface uses a non-const source rect @@ -905,8 +896,8 @@ bool WorldMap::Save(const std::string& szPath) const for (short iVehicle = 0; iVehicle < vehicles.size(); iVehicle++) { fprintf(file, "%d,", vehicles[iVehicle].iDrawSprite); fprintf(file, "%d,", vehicles[iVehicle].iActionId); - fprintf(file, "%d,", vehicles[iVehicle].iCurrentTileX); - fprintf(file, "%d,", vehicles[iVehicle].iCurrentTileY); + fprintf(file, "%d,", vehicles[iVehicle].currentTile.x); + fprintf(file, "%d,", vehicles[iVehicle].currentTile.y); fprintf(file, "%d,", vehicles[iVehicle].iMinMoves); fprintf(file, "%d,", vehicles[iVehicle].iMaxMoves); fprintf(file, "%d,", vehicles[iVehicle].fSpritePaces); @@ -1207,8 +1198,8 @@ bool WorldMap::IsVehicleMoving() const void WorldMap::GetPlayerPosition(short * iPlayerX, short * iPlayerY) const { - *iPlayerX = player.ix; - *iPlayerY = player.iy; + *iPlayerX = player.pos.x; + *iPlayerY = player.pos.y; } void WorldMap::SetPlayerPosition(short iPlayerCol, short iPlayerRow) @@ -1218,14 +1209,14 @@ void WorldMap::SetPlayerPosition(short iPlayerCol, short iPlayerRow) void WorldMap::GetPlayerCurrentTile(short * iPlayerCurrentTileX, short * iPlayerCurrentTileY) const { - *iPlayerCurrentTileX = player.iCurrentTileX; - *iPlayerCurrentTileY = player.iCurrentTileY; + *iPlayerCurrentTileX = player.currentTile.x; + *iPlayerCurrentTileY = player.currentTile.y; } void WorldMap::GetPlayerDestTile(short * iPlayerDestTileX, short * iPlayerDestTileY) const { - *iPlayerDestTileX = player.iDestTileX; - *iPlayerDestTileY = player.iDestTileY; + *iPlayerDestTileX = player.destTile.x; + *iPlayerDestTileY = player.destTile.y; } short WorldMap::GetPlayerState() const @@ -1241,7 +1232,7 @@ short WorldMap::GetVehicleInPlayerTile(short * vehicleIndex) const if (!vehicle.fEnabled) continue; - if (vehicle.iCurrentTileX == player.iCurrentTileX && vehicle.iCurrentTileY == player.iCurrentTileY) { + if (vehicle.currentTile.x == player.currentTile.x && vehicle.currentTile.y == player.currentTile.y) { *vehicleIndex = i; return vehicle.iActionId; } @@ -1253,11 +1244,11 @@ short WorldMap::GetVehicleInPlayerTile(short * vehicleIndex) const bool WorldMap::GetWarpInPlayerTile(short * iWarpCol, short * iWarpRow) const { - short iWarp = tiles[player.iCurrentTileX][player.iCurrentTileY].iWarp; + short iWarp = tiles[player.currentTile.x][player.currentTile.y].iWarp; if (iWarp < 0) return false; - Vec2s pos = warps[iWarp].getOtherSide({player.iCurrentTileX, player.iCurrentTileY}); + Vec2s pos = warps[iWarp].getOtherSide({player.currentTile.x, player.currentTile.y}); *iWarpCol = pos.x; *iWarpRow = pos.y; return true; @@ -1294,7 +1285,7 @@ short WorldMap::NumVehiclesInTile(short iTileX, short iTileY) const if (!vehicle.fEnabled) continue; - if (vehicle.iCurrentTileX == iTileX && vehicle.iCurrentTileY == iTileY) + if (vehicle.currentTile.x == iTileX && vehicle.currentTile.y == iTileY) iVehicleCount++; } diff --git a/src/smw/world.h b/src/smw/world.h index 71125052..0f0afe44 100644 --- a/src/smw/world.h +++ b/src/smw/world.h @@ -40,38 +40,35 @@ struct WorldMapTile { short iVehicleBoundary; }; -class WorldMovingObject -{ - public: - WorldMovingObject(); - virtual ~WorldMovingObject(); +class WorldMovingObject { +public: + WorldMovingObject(); + virtual ~WorldMovingObject() = default; - void Init(short iCol, short iRow, short iSprite, short iInitialDirection, short tilesize); - virtual void Move(short iDirection); - virtual bool Update(); - void FaceDirection(short iDirection); - void SetPosition(short iCol, short iRow); + void Init(short iCol, short iRow, short iSprite, short iInitialDirection, short tilesize); - protected: + virtual void Move(short iDirection); + virtual bool Update(); - short ix; - short iy; - short iCurrentTileX; - short iCurrentTileY; - short iDestTileX; - short iDestTileY; + void FaceDirection(short iDirection); + void SetPosition(short iCol, short iRow); - short iState; - short iDrawSprite; - short iDrawDirection; - short iAnimationFrame; - short iAnimationTimer; +protected: + Vec2s pos; + Vec2s currentTile; + Vec2s destTile; - short iTileSize; - short iTileSheet; + short iState = 0; + short iDrawSprite = 0; + short iDrawDirection = 0; + short iAnimationFrame = 0; + short iAnimationTimer = 0; - friend class WorldMap; - friend void takescreenshot(); + short iTileSize = 0; + short iTileSheet = 0; + + friend class WorldMap; + friend void takescreenshot(); }; class WorldPlayer : public WorldMovingObject { diff --git a/src/worldeditor/worldeditor.cpp b/src/worldeditor/worldeditor.cpp index 4a9b2e62..d537e2c3 100644 --- a/src/worldeditor/worldeditor.cpp +++ b/src/worldeditor/worldeditor.cpp @@ -848,8 +848,8 @@ int main(int argc, char *argv[]) g_wvVehicleStamp.iDrawSprite = 0; g_wvVehicleStamp.iActionId = 0; - g_wvVehicleStamp.iCurrentTileX = 0; - g_wvVehicleStamp.iCurrentTileY = 0; + g_wvVehicleStamp.currentTile.x = 0; + g_wvVehicleStamp.currentTile.y = 0; g_wvVehicleStamp.iMinMoves = 5; g_wvVehicleStamp.iMaxMoves = 8; g_wvVehicleStamp.fSpritePaces = true; @@ -1206,7 +1206,7 @@ int editor_edit() std::vector::iterator itr = vehiclelist.begin(), lim = vehiclelist.end(); while (itr != lim) { WorldVehicle * vehicle = *itr; - if (vehicle->iCurrentTileX == iCol && vehicle->iCurrentTileY == iRow) { + if (vehicle->currentTile.x == iCol && vehicle->currentTile.y == iRow) { g_wvVehicleStamp.iDrawSprite = vehicle->iDrawSprite; g_wvVehicleStamp.iActionId = vehicle->iActionId; g_wvVehicleStamp.iMinMoves = vehicle->iMinMoves; @@ -1705,7 +1705,7 @@ int editor_edit() std::vector::iterator itr = vehiclelist.begin(), lim = vehiclelist.end(); while (itr != lim) { WorldVehicle * vehicle = *itr; - if (vehicle->iCurrentTileX == iCol && vehicle->iCurrentTileY == iRow) { + if (vehicle->currentTile.x == iCol && vehicle->currentTile.y == iRow) { iStageDisplay = vehicle->iActionId; break; } @@ -1849,8 +1849,8 @@ int editor_edit() while (itr != lim) { WorldVehicle * vehicle = *itr; - short ix = (vehicle->iCurrentTileX - draw_offset_col) * TILESIZE + draw_offset_x; - short iy = (vehicle->iCurrentTileY - draw_offset_row) * TILESIZE + draw_offset_y; + short ix = (vehicle->currentTile.x - draw_offset_col) * TILESIZE + draw_offset_x; + short iy = (vehicle->currentTile.y - draw_offset_row) * TILESIZE + draw_offset_y; SDL_Rect r = {ix, iy, 32, 32}; SDL_FillRect(blitdest, &r, color); @@ -1971,8 +1971,8 @@ void ReadVehiclesIntoEditor() vehiclecopy->iDrawSprite = vehicle.iDrawSprite; vehiclecopy->iActionId = vehicle.iActionId; - vehiclecopy->iCurrentTileX = vehicle.iCurrentTileX; - vehiclecopy->iCurrentTileY = vehicle.iCurrentTileY; + vehiclecopy->currentTile.x = vehicle.currentTile.x; + vehiclecopy->currentTile.y = vehicle.currentTile.y; vehiclecopy->iMinMoves = vehicle.iMinMoves; vehiclecopy->iMaxMoves = vehicle.iMaxMoves; vehiclecopy->fSpritePaces = vehicle.fSpritePaces; @@ -1995,8 +1995,8 @@ void WriteVehiclesIntoWorld() WorldVehicle vehiclecopy; vehiclecopy.iDrawSprite = vehicle->iDrawSprite; vehiclecopy.iActionId = vehicle->iActionId; - vehiclecopy.iCurrentTileX = vehicle->iCurrentTileX; - vehiclecopy.iCurrentTileY = vehicle->iCurrentTileY; + vehiclecopy.currentTile.x = vehicle->currentTile.x; + vehiclecopy.currentTile.y = vehicle->currentTile.y; vehiclecopy.iMinMoves = vehicle->iMinMoves; vehiclecopy.iMaxMoves = vehicle->iMaxMoves; vehiclecopy.fSpritePaces = vehicle->fSpritePaces; @@ -2012,7 +2012,7 @@ void AddVehicleToTile(short iCol, short iRow, short iType) WorldVehicle * newvehicle = NULL; while (itr != lim) { WorldVehicle * vehicle = *itr; - if (vehicle->iCurrentTileX == iCol && vehicle->iCurrentTileY == iRow) { + if (vehicle->currentTile.x == iCol && vehicle->currentTile.y == iRow) { newvehicle = vehicle; break; } @@ -2022,8 +2022,8 @@ void AddVehicleToTile(short iCol, short iRow, short iType) if (!newvehicle) { newvehicle = new WorldVehicle(); - newvehicle->iCurrentTileX = iCol; - newvehicle->iCurrentTileY = iRow; + newvehicle->currentTile.x = iCol; + newvehicle->currentTile.y = iRow; vehiclelist.push_back(newvehicle); } @@ -2041,7 +2041,7 @@ void RemoveVehicleFromTile(short iCol, short iRow) std::vector::iterator itr = vehiclelist.begin(), lim = vehiclelist.end(); while (itr != lim) { WorldVehicle * vehicle = *itr; - if (vehicle->iCurrentTileX == iCol && vehicle->iCurrentTileY == iRow) { + if (vehicle->currentTile.x == iCol && vehicle->currentTile.y == iRow) { delete (*itr); itr = vehiclelist.erase(itr); @@ -4180,7 +4180,7 @@ int editor_stage() while (itrVehicle != limVehicle) { WorldVehicle * vehicle = *itrVehicle; if (vehicle->iActionId == iEditStage) { - RemoveVehicleFromTile(vehicle->iCurrentTileX, vehicle->iCurrentTileY); + RemoveVehicleFromTile(vehicle->currentTile.x, vehicle->currentTile.y); } else if (vehicle->iActionId > iEditStage) { vehicle->iActionId--; } @@ -4762,8 +4762,8 @@ int resize_world() std::vector::iterator itrVehicle = vehiclelist.begin(), limVehicle = vehiclelist.end(); while (itrVehicle != limVehicle) { - if ((*itrVehicle)->iCurrentTileX >= iWidth || (*itrVehicle)->iCurrentTileY >= iHeight) { - RemoveVehicleFromTile((*itrVehicle)->iCurrentTileX, (*itrVehicle)->iCurrentTileY); + if ((*itrVehicle)->currentTile.x >= iWidth || (*itrVehicle)->currentTile.y >= iHeight) { + RemoveVehicleFromTile((*itrVehicle)->currentTile.x, (*itrVehicle)->currentTile.y); //List was modified, restart. itrVehicle = vehiclelist.begin(); limVehicle = vehiclelist.end(); @@ -4811,8 +4811,8 @@ void takescreenshot() while (itr != lim) { WorldVehicle * vehicle = *itr; - short ix = vehicle->iCurrentTileX * iTileSize; - short iy = vehicle->iCurrentTileY * iTileSize; + short ix = vehicle->currentTile.x * iTileSize; + short iy = vehicle->currentTile.y * iTileSize; rm->spr_worldvehicle[iScreenshotSize].draw(ix, iy, vehicle->iDrawDirection * iTileSize, vehicle->iDrawSprite * iTileSize, iTileSize, iTileSize);