Skip to content

Commit

Permalink
#6092: Action parsing code and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 5, 2022
1 parent aa4416c commit bffd647
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 29 deletions.
10 changes: 10 additions & 0 deletions include/ifx.h
Expand Up @@ -35,6 +35,9 @@ class IFxAction
// Returns the action delay in seconds
virtual float getDelay() = 0;

// Action duration in seconds
virtual float getDuration() = 0;

// True: Don't shake the entity this effect is attached to
virtual bool getIgnoreMaster() = 0;

Expand All @@ -51,6 +54,13 @@ class IFxAction

// Causes the sibling action to happen when this action does.
virtual const std::string& getFireSiblingAction() = 0;

// Let the delay be random between min and max (in seconds)
// If both are 0.0 no random delay is active and the regular delay is used instead
virtual std::pair<float, float> getRandomDelay() = 0;

// According to the docs this is not used
virtual float getRotate() = 0;
};

class IFxDeclaration :
Expand Down
55 changes: 32 additions & 23 deletions radiantcore/fx/FxAction.cpp
Expand Up @@ -10,14 +10,17 @@ namespace fx
FxAction::FxAction(FxDeclaration& fx) :
_fx(fx),
_type(Type::Undefined),
_delayInSeconds(0.0),
_delayInSeconds(0),
_durationInSeconds(0),
_shakeTime(0),
_shakeAmplitude(0),
_shakeDistance(0),
_shakeFalloff(false),
_shakeImpulse(0),
_ignoreMaster(false),
_noShadows(false)
_noShadows(false),
_randomDelay(0.0f, 0.0f),
_rotate(0)
{}

FxAction::Type FxAction::getType()
Expand Down Expand Up @@ -75,6 +78,21 @@ const std::string& FxAction::getFireSiblingAction()
return _fireSiblingAction;
}

std::pair<float, float> FxAction::getRandomDelay()
{
return _randomDelay;
}

float FxAction::getRotate()
{
return _rotate;
}

float FxAction::getDuration()
{
return _durationInSeconds;
}

void FxAction::parseFromTokens(parser::DefTokeniser& tokeniser)
{
while (tokeniser.hasMoreTokens())
Expand Down Expand Up @@ -119,30 +137,21 @@ void FxAction::parseFromTokens(parser::DefTokeniser& tokeniser)
{
_fireSiblingAction = tokeniser.nextToken();
}
#if 0
if (!token.Icmp("random")) {
FXAction.random1 = src.ParseFloat();
src.ExpectTokenString(",");
FXAction.random2 = src.ParseFloat();
FXAction.delay = 0.0f; // check random
continue;
}

if (!token.Icmp("delay")) {
FXAction.delay = src.ParseFloat();
continue;
else if (token == "random")
{
_randomDelay.first = string::convert<float>(tokeniser.nextToken());
tokeniser.assertNextToken(",");
_randomDelay.second = string::convert<float>(tokeniser.nextToken());
}

if (!token.Icmp("rotate")) {
FXAction.rotate = src.ParseFloat();
continue;
else if (token == "rotate")
{
_rotate = string::convert<float>(tokeniser.nextToken());
}

if (!token.Icmp("duration")) {
FXAction.duration = src.ParseFloat();
continue;
else if (token == "duration")
{
_durationInSeconds = string::convert<float>(tokeniser.nextToken());
}

#if 0
if (!token.Icmp("trackorigin")) {
FXAction.trackOrigin = src.ParseBool();
continue;
Expand Down
11 changes: 6 additions & 5 deletions radiantcore/fx/FxAction.h
Expand Up @@ -17,16 +17,17 @@ class FxAction :
std::string _name;

float _delayInSeconds;

float _durationInSeconds;
float _shakeTime;
float _shakeAmplitude;
float _shakeDistance;
bool _shakeFalloff;
float _shakeImpulse;
bool _ignoreMaster;
bool _noShadows;

std::string _fireSiblingAction;
std::pair<float, float> _randomDelay;
float _rotate;

public:
using Ptr = std::shared_ptr<FxAction>;
Expand All @@ -36,17 +37,17 @@ class FxAction :
Type getType() override;
const std::string& getName() override;
float getDelay() override;
float getDuration() override;
bool getIgnoreMaster() override;

float getShakeTime() override;
float getShakeAmplitude() override;
float getShakeDistance() override;
bool getShakeFalloff() override;
float getShakeImpulse() override;

bool getNoShadows() override;

const std::string& getFireSiblingAction() override;
std::pair<float, float> getRandomDelay() override;
float getRotate() override;

// Parses the action from the given tokens.
// The opening brace { will already have been been consumed by the calling code
Expand Down
22 changes: 21 additions & 1 deletion test/Fx.cpp
Expand Up @@ -84,10 +84,30 @@ TEST_F(FxTest, ParseActionName)
EXPECT_EQ(getFxByName("fx/parserTest/name")->getAction(0)->getName(), "Testaction");
}

TEST_F(FxTest, ParseFireSiblingAction)
TEST_F(FxTest, ParseActionFireSibling)
{
EXPECT_EQ(getFxByName("fx/sparks")->getAction(0)->getFireSiblingAction(), "");
EXPECT_EQ(getFxByName("fx/parserTest/sibling")->getAction(0)->getFireSiblingAction(), "SisterAction");
}

TEST_F(FxTest, ParseActionRandomDelay)
{
EXPECT_EQ(getFxByName("fx/sparks")->getAction(0)->getRandomDelay().first, 0.0f);
EXPECT_EQ(getFxByName("fx/sparks")->getAction(0)->getRandomDelay().second, 0.0f);
EXPECT_EQ(getFxByName("fx/parserTest/sibling")->getAction(1)->getRandomDelay().first, 0.9f);
EXPECT_EQ(getFxByName("fx/parserTest/sibling")->getAction(1)->getRandomDelay().second, 6.888f);
}

TEST_F(FxTest, ParseActionRotate)
{
EXPECT_EQ(getFxByName("fx/sparks")->getAction(0)->getRotate(), 0.0f);
EXPECT_EQ(getFxByName("fx/parserTest/sibling")->getAction(1)->getRotate(), 56.7f);
}

TEST_F(FxTest, ParseActionDuration)
{
EXPECT_EQ(getFxByName("fx/sparks")->getAction(0)->getDuration(), 0.5f);
EXPECT_EQ(getFxByName("fx/parserTest/shake")->getAction(1)->getDuration(), 0.0f); // not set
}

}
3 changes: 3 additions & 0 deletions test/resources/tdm/fx/parsertest.fx
Expand Up @@ -85,6 +85,9 @@ fx fx/parserTest/sibling
{
name "SisterAction"
delay 2.5
random 0.9,6.888
model "sparks.prt"
rotate 56.7
duration 0.8
}
}

0 comments on commit bffd647

Please sign in to comment.