diff --git a/src/smw/world.cpp b/src/smw/world.cpp index 6f4c8336..510a5a91 100644 --- a/src/smw/world.cpp +++ b/src/smw/world.cpp @@ -62,12 +62,7 @@ int popNextInt(std::list& list, int defval = 0) * WorldMovingObject **********************************/ -WorldMovingObject::WorldMovingObject() -{ - SetPosition(0, 0); -} - -void WorldMovingObject::Init(short iCol, short iRow, short iSprite, short iInitialDirection, short tilesize) +WorldMovingObject::WorldMovingObject(short iCol, short iRow, short iSprite, short iInitialDirection, short tilesize) { iTileSize = tilesize; iTileSheet = tilesize == TILESIZE ? 0 : 1; @@ -171,10 +166,8 @@ WorldPlayer::WorldPlayer() {} WorldPlayer::WorldPlayer(short iCol, short iRow) - : WorldMovingObject() -{ - WorldMovingObject::Init(iCol, iRow, 0, 0, 32); -} + : WorldMovingObject(iCol, iRow, 0, 0, 32) +{} void WorldPlayer::Draw(short iMapOffsetX, short iMapOffsetY) const { @@ -196,41 +189,21 @@ void WorldPlayer::SetSprite(short iPlayer) * WorldVehicle **********************************/ -WorldVehicle::WorldVehicle() : - WorldMovingObject() -{} - -WorldVehicle::~WorldVehicle() +WorldVehicle::WorldVehicle() + : WorldMovingObject(0, 0, 0, 0, 0) {} -void WorldVehicle::Init(short iCol, short iRow, short iAction, short iSprite, short minMoves, short maxMoves, bool spritePaces, short iInitialDirection, short boundary, short tilesize) +WorldVehicle::WorldVehicle(short iCol, short iRow, short iAction, short iSprite, short minMoves, short maxMoves, bool spritePaces, short iInitialDirection, short boundary, short tilesize) + : WorldMovingObject(iCol, iRow, iSprite, iInitialDirection, tilesize) + , iMinMoves(minMoves) + , iMaxMoves(maxMoves) + , iActionId(iAction) + , fEnabled(true) + , fSpritePaces(spritePaces) + , iBoundary(boundary) { - WorldMovingObject::Init(iCol, iRow, iSprite, iInitialDirection, tilesize); - - fEnabled = true; - - short iRectOffsetX = 0; - short iRectOffsetY = 0; - - if (iDrawSprite >= 0 && iDrawSprite <= 8) { - iRectOffsetX = 0; - iRectOffsetY = iDrawSprite * tilesize; - } - for (short iRect = 0; iRect < 5; iRect++) - srcRects[iRect] = {iRect * tilesize + iRectOffsetX, iRectOffsetY, tilesize, tilesize}; - - iNumMoves = 0; - iActionId = iAction; - - iMinMoves = minMoves; - iMaxMoves = maxMoves; - - fSpritePaces = spritePaces; - iPaceOffset = 0; - iPaceTimer = 0; - - iBoundary = boundary; + srcRects[iRect] = {iRect * tilesize, 0, tilesize, tilesize}; } void WorldVehicle::Move() @@ -653,8 +626,7 @@ WorldMap::WorldMap(const std::string& path, short tilesize) short iBoundary = popNextInt(tokens); - vehicles.emplace_back(WorldVehicle()); - vehicles.back().Init(iCol, iRow, iStage, iSprite, iMinMoves, iMaxMoves, fSpritePaces, iInitialDirection, iBoundary, iTileSize); + vehicles.emplace_back(iCol, iRow, iStage, iSprite, iMinMoves, iMaxMoves, fSpritePaces, iInitialDirection, iBoundary, iTileSize); if (vehicles.size() >= iNumVehicles) iReadType = 16; diff --git a/src/smw/world.h b/src/smw/world.h index 0f0afe44..93070f33 100644 --- a/src/smw/world.h +++ b/src/smw/world.h @@ -42,11 +42,9 @@ struct WorldMapTile { class WorldMovingObject { public: - WorldMovingObject(); + WorldMovingObject(short iCol, short iRow, short iSprite, short iInitialDirection, short tilesize); virtual ~WorldMovingObject() = default; - void Init(short iCol, short iRow, short iSprite, short iInitialDirection, short tilesize); - virtual void Move(short iDirection); virtual bool Update(); @@ -80,38 +78,36 @@ class WorldPlayer : public WorldMovingObject { void Draw(short iWorldOffsetX, short iWorldOffsetY) const; }; -class WorldVehicle : public WorldMovingObject -{ - public: - - WorldVehicle(); - ~WorldVehicle(); - - void Init(short iCol, short iRow, short iAction, short iSprite, short iMinMoves, short iMaxMoves, bool fSpritePaces, short iInitialDirection, short iBoundary, short tilesize); - void Move(); - - bool Update(); - void Draw(short iWorldOffsetX, short iWorldOffsetY, bool fVehiclesSleeping) const; +class WorldVehicle : public WorldMovingObject { +public: + WorldVehicle(); + WorldVehicle(short iCol, short iRow, short iAction, short iSprite, + short iMinMoves, short iMaxMoves, + bool fSpritePaces, short iInitialDirection, + short iBoundary, short tilesize); - private: + void Move(); + bool Update(); + void Draw(short iWorldOffsetX, short iWorldOffsetY, bool fVehiclesSleeping) const; - void SetNextDest(); +private: + void SetNextDest(); - SDL_Rect srcRects[5]; + SDL_Rect srcRects[5]; - short iMinMoves; - short iMaxMoves; - short iNumMoves; + short iMinMoves = 5; + short iMaxMoves = 8; + short iNumMoves = 0; - short iActionId; + short iActionId = 0; - bool fEnabled; + bool fEnabled = false; - bool fSpritePaces; - short iPaceOffset; - short iPaceTimer; + bool fSpritePaces = true; + short iPaceOffset = 0; + short iPaceTimer = 0; - short iBoundary; + short iBoundary = 0; friend class WorldMap; friend void AddVehicleToTile(short iCol, short iRow, short iType); diff --git a/src/worldeditor/worldeditor.cpp b/src/worldeditor/worldeditor.cpp index d537e2c3..de71de37 100644 --- a/src/worldeditor/worldeditor.cpp +++ b/src/worldeditor/worldeditor.cpp @@ -1967,19 +1967,7 @@ void ReadVehiclesIntoEditor() vehiclelist.clear(); for (const WorldVehicle& vehicle : g_worldmap.vehicles) { - WorldVehicle * vehiclecopy = new WorldVehicle(); - - vehiclecopy->iDrawSprite = vehicle.iDrawSprite; - vehiclecopy->iActionId = vehicle.iActionId; - vehiclecopy->currentTile.x = vehicle.currentTile.x; - vehiclecopy->currentTile.y = vehicle.currentTile.y; - vehiclecopy->iMinMoves = vehicle.iMinMoves; - vehiclecopy->iMaxMoves = vehicle.iMaxMoves; - vehiclecopy->fSpritePaces = vehicle.fSpritePaces; - vehiclecopy->iDrawDirection = vehicle.iDrawDirection; - vehiclecopy->iBoundary = vehicle.iBoundary; - - vehiclelist.push_back(vehiclecopy); + vehiclelist.push_back(new WorldVehicle(vehicle)); } } @@ -1992,48 +1980,21 @@ void WriteVehiclesIntoWorld() g_worldmap.vehicles.reserve(vehiclelist.size()); for (const WorldVehicle* vehicle : vehiclelist) { - WorldVehicle vehiclecopy; - vehiclecopy.iDrawSprite = vehicle->iDrawSprite; - vehiclecopy.iActionId = vehicle->iActionId; - vehiclecopy.currentTile.x = vehicle->currentTile.x; - vehiclecopy.currentTile.y = vehicle->currentTile.y; - vehiclecopy.iMinMoves = vehicle->iMinMoves; - vehiclecopy.iMaxMoves = vehicle->iMaxMoves; - vehiclecopy.fSpritePaces = vehicle->fSpritePaces; - vehiclecopy.iDrawDirection = vehicle->iDrawDirection; - vehiclecopy.iBoundary = vehicle->iBoundary; - g_worldmap.vehicles.emplace_back(std::move(vehiclecopy)); + g_worldmap.vehicles.emplace_back(*vehicle); } } void AddVehicleToTile(short iCol, short iRow, short iType) { - std::vector::iterator itr = vehiclelist.begin(), lim = vehiclelist.end(); - WorldVehicle * newvehicle = NULL; - while (itr != lim) { - WorldVehicle * vehicle = *itr; - if (vehicle->currentTile.x == iCol && vehicle->currentTile.y == iRow) { - newvehicle = vehicle; - break; - } - - itr++; - } - - if (!newvehicle) { - newvehicle = new WorldVehicle(); - newvehicle->currentTile.x = iCol; - newvehicle->currentTile.y = iRow; - vehiclelist.push_back(newvehicle); - } - - newvehicle->iDrawSprite = g_wvVehicleStamp.iDrawSprite; - newvehicle->iActionId = g_wvVehicleStamp.iActionId; - newvehicle->iMinMoves = g_wvVehicleStamp.iMinMoves; - newvehicle->iMaxMoves = g_wvVehicleStamp.iMaxMoves; - newvehicle->fSpritePaces = g_wvVehicleStamp.fSpritePaces; - newvehicle->iDrawDirection = g_wvVehicleStamp.iDrawDirection; - newvehicle->iBoundary = g_wvVehicleStamp.iBoundary; + const auto it = std::find_if(vehiclelist.begin(), vehiclelist.end(), + [iCol, iRow](WorldVehicle* vehicle) { + return vehicle->currentTile.x == iCol && vehicle->currentTile.y == iRow; + }); + + WorldVehicle* newvehicle = (it != vehiclelist.cend()) + ? *it + : vehiclelist.emplace_back(new WorldVehicle(g_wvVehicleStamp)); + newvehicle->SetPosition(iCol, iRow); } void RemoveVehicleFromTile(short iCol, short iRow)