Skip to content

Commit

Permalink
[cleanup] Add EDirection
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Aug 21, 2024
1 parent d98806b commit 0c3b79d
Show file tree
Hide file tree
Showing 20 changed files with 392 additions and 181 deletions.
1 change: 1 addition & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ end
nuget(nugetPackages)
filter { "toolset:msc*" }
defines { "_CRT_SECURE_NO_WARNINGS" } -- 4996: '$func': This function or variable may be unsafe. Consider using $func2 instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
defines { "_USE_MATH_DEFINES" } -- for M_PI
buildoptions { "/Zc:__cplusplus" } -- else __cplusplus would be 199711L
disablewarnings {
"4458", -- declaration of '$var' hides class member
Expand Down
17 changes: 3 additions & 14 deletions src/lib/game/data/units/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,12 @@ void cUnit::changeName (std::string&& newName)
//------------------------------------------------------------------------------
/** rotates the unit to the given direction */
//------------------------------------------------------------------------------
void cUnit::rotateTo (int newDir)
void cUnit::rotateTo (EDirection newDir)
{
if (newDir < 0 || newDir >= 8 || newDir == dir)
if (newDir == dir)
return;

int t = dir;
EDirection t = dir;
int dest = 0;

for (int i = 0; i < 8; ++i)
Expand All @@ -336,23 +336,12 @@ void cUnit::rotateTo (int newDir)
break;
}
++t;

if (t > 7)
t = 0;
}

if (dest < 4)
++dir;
else
--dir;

if (dir < 0)
dir += 8;
else
{
if (dir > 7)
dir -= 8;
}
}

//------------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/lib/game/data/units/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define game_data_units_unitH

#include "game/data/units/unitdata.h"
#include "utility/direction.h"
#include "utility/position.h"
#include "utility/signal/signal.h"

Expand Down Expand Up @@ -103,7 +104,7 @@ class cUnit
std::optional<std::string> getCustomName() const;
void changeName (std::string&& newName);

void rotateTo (int newDir);
void rotateTo (EDirection newDir);

/** checks if the unit can attack something at the offset.
* when forceAttack is false, the function only returns true,
Expand Down Expand Up @@ -222,7 +223,7 @@ class cUnit
const cStaticUnitData& getStaticUnitData() const;
cDynamicUnitData data; // basic data of the unit
const unsigned int iID; // the identification number of this unit
int dir = 0; // ?Frame of the unit/current direction the unit is facing?
EDirection dir = EDirection::North; // ?Frame of the unit/current direction the unit is facing?

private:
std::vector<unsigned int> storedUnitIds; // equivalent of storedUnits, for serialization only.
Expand Down
76 changes: 17 additions & 59 deletions src/lib/game/logic/attackjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
cAttackJob::cAttackJob (cUnit& aggressor, const cPosition& targetPosition, const cModel& model) :
aggressorId (aggressor.getId()),
targetPosition (targetPosition),
fireDir (calcFireDir (aggressor)),
fireDir (directionFromOffset (targetPosition - aggressor.getPosition()).value_or (aggressor.dir)),
counter (10),
state (eAJState::Rotating)
{
Expand Down Expand Up @@ -143,48 +143,6 @@ uint32_t cAttackJob::getChecksum (uint32_t crc) const
//------------------------------------------------------------------------------
// private functions

int cAttackJob::calcFireDir (const cUnit& aggressor)
{
auto dx = (float) (targetPosition.x() - aggressor.getPosition().x());
auto dy = (float) -(targetPosition.y() - aggressor.getPosition().y());
auto r = std::sqrt (dx * dx + dy * dy);

int fireDir = aggressor.dir;
if (r > 0.001f)
{
// 360 / (2 * PI) = 57.29577951f;
dx /= r;
dy /= r;
r = asinf (dx) * 57.29577951f;
if (dy >= 0)
{
if (r < 0)
r += 360;
}
else
r = 180 - r;

if (r >= 337.5f || r <= 22.5f)
fireDir = 0;
else if (r >= 22.5f && r <= 67.5f)
fireDir = 1;
else if (r >= 67.5f && r <= 112.5f)
fireDir = 2;
else if (r >= 112.5f && r <= 157.5f)
fireDir = 3;
else if (r >= 157.5f && r <= 202.5f)
fireDir = 4;
else if (r >= 202.5f && r <= 247.5f)
fireDir = 5;
else if (r >= 247.5f && r <= 292.5f)
fireDir = 6;
else if (r >= 292.5f && r <= 337.5f)
fireDir = 7;
}

return fireDir;
}

//------------------------------------------------------------------------------
void cAttackJob::lockTarget (const cMap& map, const cUnit& aggressor)
{
Expand Down Expand Up @@ -277,31 +235,31 @@ std::unique_ptr<cFx> cAttackJob::createMuzzleFx (const cUnit& aggressor)
case eMuzzleType::Big:
switch (fireDir)
{
case 0:
case EDirection::North:
offset.y() = -40;
break;
case 1:
case EDirection::NorthEast:
offset.x() = 32;
offset.y() = -32;
break;
case 2:
case EDirection::East:
offset.x() = 40;
break;
case 3:
case EDirection::SouthEast:
offset.x() = 32;
offset.y() = 32;
break;
case 4:
case EDirection::South:
offset.y() = 40;
break;
case 5:
case EDirection::SouthWest:
offset.x() = -32;
offset.y() = 32;
break;
case 6:
case EDirection::West:
offset.x() = -40;
break;
case 7:
case EDirection::NorthWest:
offset.x() = -32;
offset.y() = -32;
break;
Expand All @@ -319,31 +277,31 @@ std::unique_ptr<cFx> cAttackJob::createMuzzleFx (const cUnit& aggressor)
case eMuzzleType::MedLong:
switch (fireDir)
{
case 0:
case EDirection::North:
offset.y() = -20;
break;
case 1:
case EDirection::NorthEast:
offset.x() = 12;
offset.y() = -12;
break;
case 2:
case EDirection::East:
offset.x() = 20;
break;
case 3:
case EDirection::SouthEast:
offset.x() = 12;
offset.y() = 12;
break;
case 4:
case EDirection::South:
offset.y() = 20;
break;
case 5:
case EDirection::SouthWest:
offset.x() = -12;
offset.y() = 12;
break;
case 6:
case EDirection::West:
offset.x() = -20;
break;
case 7:
case EDirection::NorthWest:
offset.x() = -12;
offset.y() = -12;
break;
Expand Down
3 changes: 1 addition & 2 deletions src/lib/game/logic/attackjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class cAttackJob
}

private:
int calcFireDir (const cUnit& aggressor);
void lockTarget (const cMap& map, const cUnit& aggressor);
void releaseTargets (const cModel& model);
void fire (cModel& model);
Expand All @@ -88,7 +87,7 @@ class cAttackJob
int aggressorId = -1;
cPosition targetPosition;
std::vector<int> lockedTargets;
int fireDir = 0;
EDirection fireDir = EDirection::North;
int counter = 0;
enum class eAJState
{
Expand Down
14 changes: 7 additions & 7 deletions src/lib/game/logic/fxeffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,39 @@ void cFxContainer::run()

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
cFxMuzzle::cFxMuzzle (const cPosition& position_, int dir_, sID id_) :
cFxMuzzle::cFxMuzzle (const cPosition& position_, EDirection dir_, sID id_) :
cFx (false, position_),
dir (dir_),
id (id_)
{}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
cFxMuzzleBig::cFxMuzzleBig (const cPosition& position_, int dir_, sID id_) :
cFxMuzzleBig::cFxMuzzleBig (const cPosition& position_, EDirection dir_, sID id_) :
cFxMuzzle (position_, dir_, id_)
{
length = 6;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
cFxMuzzleMed::cFxMuzzleMed (const cPosition& position_, int dir_, sID id_) :
cFxMuzzleMed::cFxMuzzleMed (const cPosition& position_, EDirection dir_, sID id_) :
cFxMuzzle (position_, dir_, id_)
{
length = 6;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
cFxMuzzleMedLong::cFxMuzzleMedLong (const cPosition& position_, int dir_, sID id_) :
cFxMuzzleMedLong::cFxMuzzleMedLong (const cPosition& position_, EDirection dir_, sID id_) :
cFxMuzzle (position_, dir_, id_)
{
length = 16;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
cFxMuzzleSmall::cFxMuzzleSmall (const cPosition& position_, int dir_, sID id_) :
cFxMuzzleSmall::cFxMuzzleSmall (const cPosition& position_, EDirection dir_, sID id_) :
cFxMuzzle (position_, dir_, id_)
{
length = 6;
Expand Down Expand Up @@ -200,7 +200,7 @@ cFxCorpse::cFxCorpse (const cPosition& position_) :

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
cFxTracks::cFxTracks (const cPosition& position_, int dir_) :
cFxTracks::cFxTracks (const cPosition& position_, EDirection dir_) :
cFx (true, position_),
dir (dir_)
{
Expand All @@ -209,7 +209,7 @@ cFxTracks::cFxTracks (const cPosition& position_, int dir_) :

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
cFxRocket::cFxRocket (const cPosition& startPosition_, const cPosition& endPosition_, int dir_, bool bottom, sID id_) :
cFxRocket::cFxRocket (const cPosition& startPosition_, const cPosition& endPosition_, EDirection dir_, bool bottom, sID id_) :
cFx (bottom, startPosition_),
dir (dir_),
startPosition (startPosition_),
Expand Down
25 changes: 13 additions & 12 deletions src/lib/game/logic/fxeffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define game_logic_fxeffectsH

#include "game/data/units/unitdata.h"
#include "utility/direction.h"
#include "utility/position.h"

#include <memory>
Expand Down Expand Up @@ -105,41 +106,41 @@ class cFxContainer
class cFxMuzzle : public cFx
{
public:
int getDir() const { return dir; }
EDirection getDir() const { return dir; }
const sID& getId() const { return id; }

protected:
cFxMuzzle (const cPosition& position, int dir_, sID id);
cFxMuzzle (const cPosition&, EDirection, sID);

int dir;
EDirection dir;
const sID id;
};

class cFxMuzzleBig : public cFxMuzzle
{
public:
cFxMuzzleBig (const cPosition& position, int dir, sID id);
cFxMuzzleBig (const cPosition&, EDirection, sID);
void accept (IFxVisitor& visitor) const override { visitor.visit (*this); }
};

class cFxMuzzleMed : public cFxMuzzle
{
public:
cFxMuzzleMed (const cPosition& position, int dir, sID id);
cFxMuzzleMed (const cPosition&, EDirection, sID);
void accept (IFxVisitor& visitor) const override { visitor.visit (*this); }
};

class cFxMuzzleMedLong : public cFxMuzzle
{
public:
cFxMuzzleMedLong (const cPosition& position, int dir, sID id);
cFxMuzzleMedLong (const cPosition&, EDirection, sID);
void accept (IFxVisitor& visitor) const override { visitor.visit (*this); }
};

class cFxMuzzleSmall : public cFxMuzzle
{
public:
cFxMuzzleSmall (const cPosition& position, int dir, sID id);
cFxMuzzleSmall (const cPosition&, EDirection, sID);
void accept (IFxVisitor& visitor) const override { visitor.visit (*this); }
};

Expand Down Expand Up @@ -241,10 +242,10 @@ class cFxTracks : public cFx
public:
static constexpr int alphaStart = 100;
static constexpr int alphaEnd = 0;
const int dir;
const EDirection dir;

public:
cFxTracks (const cPosition& position, int dir_);
cFxTracks (const cPosition&, EDirection);
void accept (IFxVisitor& visitor) const override { visitor.visit (*this); }
};

Expand All @@ -253,22 +254,22 @@ class cFxRocket : public cFx
private:
static constexpr int speed = 8;
std::vector<std::unique_ptr<cFx>> subEffects;
int dir;
EDirection dir;
int distance;
const cPosition startPosition;
const cPosition endPosition;
const sID id;

public:
cFxRocket (const cPosition& startPosition, const cPosition& endPosition, int dir_, bool bottom, sID id);
cFxRocket (const cPosition& startPosition, const cPosition& endPosition, EDirection, bool bottom, sID);
void accept (IFxVisitor& visitor) const override { visitor.visit (*this); }
void run() override;
// return true, when the last smoke effect is finished.
// getLength() returns only the time until
// the rocket has reached the destination
bool isFinished() const override;
const sID getId() const { return id; }
int getDir() const { return dir; }
EDirection getDir() const { return dir; }
const std::vector<std::unique_ptr<cFx>>& getSubEffects() const { return subEffects; }
};

Expand Down
Loading

0 comments on commit 0c3b79d

Please sign in to comment.