Skip to content

Commit

Permalink
Reduced the number of moving platform path friends
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed May 27, 2024
1 parent 56fda30 commit 12d0028
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 74 deletions.
38 changes: 14 additions & 24 deletions src/common/MovingPlatformPaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ class MovingPlatformPath {
m_platform = platform;
}

float speed() const { return m_speed; }
const Vec2f& currentPos0() const { return m_currentPos[0]; }
const Vec2f& currentPos1() const { return m_currentPos[1]; }
const Vec2f& velocity0() const { return m_velocity[0]; }

protected:
MovingPlatform* m_platform = nullptr;

float m_speed;
Vec2f m_startPos;
Vec2f m_endPos;
float m_speed;

Vec2f m_velocity[2];
Vec2f m_currentPos[2];

friend class MovingPlatform;
friend class CMap;
friend void loadcurrentmap();
friend void insert_platforms_into_map();
};


Expand All @@ -52,6 +52,10 @@ class StraightPath : public MovingPlatformPath {
bool Move(short type) override;
void Reset() override;

const Vec2f& startPos() const { return m_startPos; }
const Vec2f& endPos() const { return m_endPos; }
float angle() const { return m_angle; }

protected:
void SetVelocity(short type);

Expand All @@ -60,11 +64,6 @@ class StraightPath : public MovingPlatformPath {

unsigned short m_currentStep[2] = {0, 0};
Vec2f* m_goalPoint[2] = {&m_startPos, &m_startPos};

friend class MovingPlatform;
friend class CMap;
friend void loadcurrentmap();
friend void insert_platforms_into_map();
};


Expand All @@ -78,11 +77,6 @@ class StraightPathContinuous : public StraightPath {

private:
Vec2f m_edge;

friend class MovingPlatform;
friend class CMap;
friend void loadcurrentmap();
friend void insert_platforms_into_map();
};


Expand All @@ -95,16 +89,15 @@ class EllipsePath : public MovingPlatformPath {
void SetPosition(short type);
void Reset() override;

const Vec2f& centerPos() const { return m_startPos; }
const Vec2f& radius() const { return m_radius; }
float startAngle() const { return m_startAngle; }

private:
Vec2f m_radius;
float m_startAngle;

float m_angle[2];

friend class MovingPlatform;
friend class CMap;
friend void loadcurrentmap();
friend void insert_platforms_into_map();
};


Expand All @@ -115,7 +108,4 @@ class FallingPath : public MovingPlatformPath {
PlatformPathType typeId() const override { return PlatformPathType::Falling; }
bool Move(short type) override;
void Reset() override;

private:
friend class MovingPlatform;
};
36 changes: 18 additions & 18 deletions src/common/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,23 +995,23 @@ void CMap::saveMap(const std::string& file)
mapfile.write_i32(iPathType);

if (auto* path = dynamic_cast<StraightPath*>(platforms[iPlatform]->pPath)) {
mapfile.write_float(path->m_startPos.x);
mapfile.write_float(path->m_startPos.y);
mapfile.write_float(path->m_endPos.x);
mapfile.write_float(path->m_endPos.y);
mapfile.write_float(path->m_speed);
mapfile.write_float(path->startPos().x);
mapfile.write_float(path->startPos().y);
mapfile.write_float(path->endPos().x);
mapfile.write_float(path->endPos().y);
mapfile.write_float(path->speed());
} else if (auto* path = dynamic_cast<StraightPathContinuous*>(platforms[iPlatform]->pPath)) {
mapfile.write_float(path->m_startPos.x);
mapfile.write_float(path->m_startPos.y);
mapfile.write_float(path->m_angle);
mapfile.write_float(path->m_speed);
mapfile.write_float(path->startPos().x);
mapfile.write_float(path->startPos().y);
mapfile.write_float(path->angle());
mapfile.write_float(path->speed());
} else if (auto* path = dynamic_cast<EllipsePath*>(platforms[iPlatform]->pPath)) {
mapfile.write_float(path->m_radius.x);
mapfile.write_float(path->m_radius.y);
mapfile.write_float(path->m_startPos.x);
mapfile.write_float(path->m_startPos.y);
mapfile.write_float(path->m_angle[0]);
mapfile.write_float(path->m_speed);
mapfile.write_float(path->radius().x);
mapfile.write_float(path->radius().y);
mapfile.write_float(path->centerPos().x);
mapfile.write_float(path->centerPos().y);
mapfile.write_float(path->startAngle());
mapfile.write_float(path->speed());
}
}

Expand Down Expand Up @@ -1753,11 +1753,11 @@ void CMap::drawThumbnailPlatforms(SDL_Surface * targetSurface)
MovingPlatformPath * basepath = platform->pPath;

if (auto* path = dynamic_cast<StraightPath*>(basepath)) {
DrawPlatform(path->typeId(), platform->iTileData, path->m_startPos.x * 2.f, path->m_startPos.y * 2.f, path->m_endPos.x * 2.f, path->m_endPos.y * 2.f, 0.0f, 0.0f, 0.0f, 2, platform->iTileWidth, platform->iTileHeight, true, true);
DrawPlatform(path->typeId(), platform->iTileData, path->startPos().x * 2.f, path->startPos().y * 2.f, path->endPos().x * 2.f, path->endPos().y * 2.f, 0.0f, 0.0f, 0.0f, 2, platform->iTileWidth, platform->iTileHeight, true, true);
} else if (auto* path = dynamic_cast<StraightPathContinuous*>(basepath)) {
DrawPlatform(path->typeId(), platform->iTileData, path->m_startPos.x * 2.f, path->m_startPos.y * 2.f, 0, 0, path->m_angle, 0.0f, 0.0f, 2, platform->iTileWidth, platform->iTileHeight, true, true);
DrawPlatform(path->typeId(), platform->iTileData, path->startPos().x * 2.f, path->startPos().y * 2.f, 0, 0, path->angle(), 0.0f, 0.0f, 2, platform->iTileWidth, platform->iTileHeight, true, true);
} else if (auto* path = dynamic_cast<EllipsePath*>(basepath)) {
DrawPlatform(path->typeId(), platform->iTileData, path->m_startPos.x * 2.f, path->m_startPos.y * 2.f, 0, 0, path->m_startAngle, path->m_radius.x * 2, path->m_radius.y * 2, 2, platform->iTileWidth, platform->iTileHeight, true, true);
DrawPlatform(path->typeId(), platform->iTileData, path->centerPos().x * 2.f, path->centerPos().y * 2.f, 0, 0, path->startAngle(), path->radius().x * 2, path->radius().y * 2, 2, platform->iTileWidth, platform->iTileHeight, true, true);
}
}

Expand Down
34 changes: 17 additions & 17 deletions src/common/movingplatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ MovingPlatform::MovingPlatform(TilesetTile ** tiledata, MapTile ** tiletypes, sh
rDstRect.w = w * iTileSize;
rDstRect.h = h * iTileSize;

fVelX = pPath->m_velocity[0].x;
fVelY = pPath->m_velocity[0].y;
fVelX = pPath->velocity0().x;
fVelY = pPath->velocity0().y;

fOldVelX = fVelX;
fOldVelY = fVelY;
Expand All @@ -132,7 +132,7 @@ MovingPlatform::~MovingPlatform()
void MovingPlatform::draw()
{
//Comment this back in to see the no spawn area of the platform
//SDL_Rect r = {(int)pPath->m_currentPos[1].x - iHalfWidth, (int)pPath->m_currentPos[1].y - iHalfHeight, iWidth, iHeight};
//SDL_Rect r = {(int)pPath->currentPos1().x - iHalfWidth, (int)pPath->currentPos1().y - iHalfHeight, iWidth, iHeight};
//SDL_FillRect(blitdest, &r, SDL_MapRGB(blitdest->format, 0, 0, 255));

rDstRect.x = ix - iHalfWidth + x_shake;
Expand Down Expand Up @@ -237,31 +237,31 @@ void MovingPlatform::draw(short iOffsetX, short iOffsetY)

void MovingPlatform::update()
{
fOldX = pPath->m_currentPos[0].x;
fOldY = pPath->m_currentPos[0].y;
fOldX = pPath->currentPos0().x;
fOldY = pPath->currentPos0().y;

fOldVelX = pPath->m_velocity[0].x;
fOldVelY = pPath->m_velocity[0].y;
fOldVelX = pPath->velocity0().x;
fOldVelY = pPath->velocity0().y;

//Path will affect new fVelX and fVelY to move the platform to it's next location
pPath->Move(0);

//Will move the path "shadow" to the next location (for spawn collision detection)
pPath->Move(1);

fVelX = pPath->m_velocity[0].x;
fVelY = pPath->m_velocity[0].y;
fVelX = pPath->velocity0().x;
fVelY = pPath->velocity0().y;

setXf(pPath->m_currentPos[0].x);
setYf(pPath->m_currentPos[0].y);
setXf(pPath->currentPos0().x);
setYf(pPath->currentPos0().y);
}

void MovingPlatform::ResetPath()
{
pPath->Reset();

setXf(pPath->m_currentPos[0].x);
setYf(pPath->m_currentPos[0].y);
setXf(pPath->currentPos0().x);
setYf(pPath->currentPos0().y);

fOldX = fx;
fOldY = fy;
Expand Down Expand Up @@ -1365,18 +1365,18 @@ short MovingPlatform::coldec_object(IO_MovingObject * object)

bool MovingPlatform::IsInNoSpawnZone(short iX, short iY, short w, short h)
{
short iTop = (short)pPath->m_currentPos[1].y - iHalfHeight;
short iTop = (short)pPath->currentPos1().y - iHalfHeight;

if (iY + h < iTop)
return false;

short iBottom = (short)pPath->m_currentPos[1].y + iHalfHeight;
short iBottom = (short)pPath->currentPos1().y + iHalfHeight;

if (iY >= iBottom)
return false;

short iLeft = (short)pPath->m_currentPos[1].x - iHalfWidth;
short iRight = (short)pPath->m_currentPos[1].x + iHalfWidth;
short iLeft = (short)pPath->currentPos1().x - iHalfWidth;
short iRight = (short)pPath->currentPos1().x + iHalfWidth;

//Deal with screen side overlap
if (iX + w < iLeft)
Expand Down
30 changes: 15 additions & 15 deletions src/leveleditor/leveleditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4861,23 +4861,23 @@ void loadcurrentmap()
g_Platforms[iPlatform].iPathType = g_map->platforms[iPlatform]->pPath->typeId();

if (auto* path = dynamic_cast<StraightPath*>(g_map->platforms[iPlatform]->pPath)) {
g_Platforms[iPlatform].iVelocity = (int)(path->m_speed * 4.0f);
g_Platforms[iPlatform].iStartX = (int)(path->m_startPos.x);
g_Platforms[iPlatform].iStartY = (int)(path->m_startPos.y);
g_Platforms[iPlatform].iEndX = (int)(path->m_endPos.x);
g_Platforms[iPlatform].iEndY = (int)(path->m_endPos.y);
g_Platforms[iPlatform].iVelocity = (int)(path->speed() * 4.0f);
g_Platforms[iPlatform].iStartX = (int)(path->startPos().x);
g_Platforms[iPlatform].iStartY = (int)(path->startPos().y);
g_Platforms[iPlatform].iEndX = (int)(path->endPos().x);
g_Platforms[iPlatform].iEndY = (int)(path->endPos().y);
} else if (auto* path = dynamic_cast<StraightPathContinuous*>(g_map->platforms[iPlatform]->pPath)) {
g_Platforms[iPlatform].iVelocity = (int)(path->m_speed * 4.0f);
g_Platforms[iPlatform].iStartX = (int)(path->m_startPos.x);
g_Platforms[iPlatform].iStartY = (int)(path->m_startPos.y);
g_Platforms[iPlatform].fAngle = path->m_angle;
g_Platforms[iPlatform].iVelocity = (int)(path->speed() * 4.0f);
g_Platforms[iPlatform].iStartX = (int)(path->startPos().x);
g_Platforms[iPlatform].iStartY = (int)(path->startPos().y);
g_Platforms[iPlatform].fAngle = path->angle();
} else if (auto* path = dynamic_cast<EllipsePath*>(g_map->platforms[iPlatform]->pPath)) {
g_Platforms[iPlatform].iVelocity = (short)(path->m_speed / 0.0030f);
g_Platforms[iPlatform].fRadiusX = path->m_radius.x;
g_Platforms[iPlatform].fRadiusY = path->m_radius.y;
g_Platforms[iPlatform].iStartX = (int)(path->m_startPos.x);
g_Platforms[iPlatform].iStartY = (int)(path->m_startPos.y);
g_Platforms[iPlatform].fAngle = path->m_startAngle;
g_Platforms[iPlatform].iVelocity = (short)(path->speed() / 0.0030f);
g_Platforms[iPlatform].fRadiusX = path->radius().x;
g_Platforms[iPlatform].fRadiusY = path->radius().y;
g_Platforms[iPlatform].iStartX = (int)(path->centerPos().x);
g_Platforms[iPlatform].iStartY = (int)(path->centerPos().y);
g_Platforms[iPlatform].fAngle = path->startAngle();
}

g_Platforms[iPlatform].UpdatePreview();
Expand Down

0 comments on commit 12d0028

Please sign in to comment.