From 119e71e86a5e7c957c74eb3411372162a15a743a Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 3 May 2020 21:08:27 +0200 Subject: [PATCH] Allow AsInt from Float EffectParameter, and AsFloat from Int EffectParameter. --- src/Library/EffectParameter.hpp | 6 ++++++ tests/LibraryTests/EffectParameterTests.cpp | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/Library/EffectParameter.hpp b/src/Library/EffectParameter.hpp index 4040a26..61736bd 100644 --- a/src/Library/EffectParameter.hpp +++ b/src/Library/EffectParameter.hpp @@ -34,6 +34,9 @@ namespace CreatureLib::Library { } int64_t AsInt() const { if (_type != EffectParameterType::Int) { + if (_type == EffectParameterType::Float) { + return static_cast(std::get(_value)); + } std::stringstream ss; ss << "Cast effect parameter to int, but was " << EffectParameterTypeHelper::ToString(_type); throw CreatureException(ss.str()); @@ -42,6 +45,9 @@ namespace CreatureLib::Library { } float AsFloat() const { if (_type != EffectParameterType::Float) { + if (_type == EffectParameterType::Int) { + return static_cast(std::get(_value)); + } std::stringstream ss; ss << "Cast effect parameter to float, but was " << EffectParameterTypeHelper::ToString(_type); throw CreatureException(ss.str()); diff --git a/tests/LibraryTests/EffectParameterTests.cpp b/tests/LibraryTests/EffectParameterTests.cpp index 6fb6669..a841e73 100644 --- a/tests/LibraryTests/EffectParameterTests.cpp +++ b/tests/LibraryTests/EffectParameterTests.cpp @@ -13,6 +13,7 @@ TEST_CASE("Bool EffectParameter", "[Library]") { TEST_CASE("Int EffectParameter", "[Library]") { auto p = EffectParameter((int64_t)684); REQUIRE(p.AsInt() == 684); + REQUIRE(p.AsFloat() == 684); } TEST_CASE("String EffectParameter", "[Library]") { @@ -23,6 +24,7 @@ TEST_CASE("String EffectParameter", "[Library]") { TEST_CASE("Float EffectParameter", "[Library]") { auto p = EffectParameter(1.5f); REQUIRE(p.AsFloat() == Approx(1.5f)); + REQUIRE(p.AsInt() == 1); } #endif \ No newline at end of file