Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

GameObject / GameComponent Unit Test

View GameObjectTest.cc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
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
Something went wrong with that request. Please try again.