GameObject / GameComponent Unit Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace | |
{ | |
static int incremental = 0; | |
} | |
struct Object | |
: public core::GameObject | |
{ | |
}; | |
struct Component | |
: public core::GameComponent | |
{ | |
Component(core::GameObject& owner) | |
: core::GameComponent(owner) | |
{} | |
}; | |
static | |
void | |
basic() | |
{ | |
struct Counter | |
: public core::GameComponent | |
{ | |
Counter(core::GameObject& owner) | |
: core::GameComponent(owner) | |
, counter(++incremental) | |
{ | |
} | |
int counter; | |
}; | |
Object o; | |
BOOST_CHECK(!o.has<Component>()); | |
BOOST_CHECK_THROW(o.component<Component>(), core::Exception); | |
o.add<Component>(); | |
BOOST_CHECK(o.has<Component>()); | |
// Yerk... | |
BOOST_CHECK(&o.component<Component>() != nullptr); | |
o.remove<Component>(); | |
BOOST_CHECK(!o.has<Component>()); | |
BOOST_CHECK_THROW(o.remove<Component>(), core::Exception); | |
o.add<Counter>(); | |
o.add<Counter>(); | |
BOOST_CHECK_EQUAL(o.component<Counter>().counter, 2); | |
BOOST_CHECK_EQUAL(o.component<Counter>().counter, 2); | |
} | |
static | |
void | |
fight() | |
{ | |
class NotDamageable: | |
public core::Exception | |
{ | |
public: | |
NotDamageable() | |
: core::Exception("impossible to damage") | |
{} | |
}; | |
struct Armor | |
: public core::GameComponent | |
{ | |
Armor(core::GameObject& owner, | |
int armor = 5) | |
: core::GameComponent(owner) | |
, _value(armor) | |
{} | |
CORE_ATTRIBUTE_RW(int, value); | |
}; | |
struct Health | |
: public core::GameComponent | |
{ | |
Health(core::GameObject& owner, | |
int health = 100) | |
: core::GameComponent(owner) | |
, _value(health) | |
{} | |
int | |
damage(int value) | |
{ | |
if (this->_owner.has<Armor>()) | |
{ | |
value = std::max(0, value - this->_owner.component<Armor>().value()); | |
} | |
this->_value -= value; | |
return this->_value; | |
} | |
int | |
operator -(int value) | |
{ | |
return this->damage(value); | |
} | |
CORE_ATTRIBUTE_RW(int, value); | |
}; | |
struct Attack | |
: public core::GameComponent | |
{ | |
Attack(core::GameObject& owner, | |
unsigned int damage = 10) | |
: core::GameComponent(owner) | |
, _damage(damage) | |
{ | |
} | |
void | |
operator() (core::GameObject& target) | |
{ | |
if (!target.has<Health>()) | |
throw NotDamageable(); | |
target.component<Health>().damage(this->_damage); | |
} | |
CORE_ATTRIBUTE_RW(unsigned int, damage); | |
}; | |
struct Character | |
: public core::GameObject | |
{ | |
Character(int health = 100, | |
int armor = 0, | |
int attack = 3): | |
core::GameObject() | |
{ | |
this->add<Health>(health); | |
this->add<Armor>(armor); | |
this->add<Attack>(attack); | |
} | |
}; | |
struct Invulnerable | |
: public Character | |
{ | |
Invulnerable() | |
: Character() | |
{ | |
this->remove<Health>(); | |
} | |
}; | |
/*------, | |
| Start | | |
`------*/ | |
int bob_attack = 5; | |
Character bob(100, 1, bob_attack); | |
// Exception. | |
{ | |
Invulnerable terkhil; | |
BOOST_CHECK_THROW(bob.component<Attack>()(terkhil), NotDamageable); | |
} | |
// Attack. | |
{ | |
int alice_hp = 80; | |
int alice_armor = 3; | |
Character alice(alice_hp, alice_armor, 4); | |
bob.component<Attack>()(alice); | |
BOOST_CHECK_EQUAL(alice.component<Health>().value(), | |
alice_hp - (bob_attack - alice_armor)); | |
} | |
} | |
CORE_TEST_SUITE() | |
{ | |
auto& suite = boost::unit_test::framework::master_test_suite(); | |
suite.add(BOOST_TEST_CASE(basic), 0, 3); | |
suite.add(BOOST_TEST_CASE(fight), 0, 3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment