From ac871cbdd6477432df33ea22e99c9be83cec0e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 22:26:45 +0100 Subject: [PATCH] Enemies can no longer Overheal --- src/c/encounter.c | 5 +++++ test/c/test_encounter.c | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/c/encounter.c b/src/c/encounter.c index cdd4c39..0f48860 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -40,6 +40,11 @@ int playerHealth(int health, int damage, int armor) void enemyHeal(enemy *enemy, int healAmount) { int currentHealth = getEnemyHealth(enemy); + int maxHealth = getEnemyMaxHealth(enemy); + if (currentHealth + healAmount > maxHealth) + { + healAmount = maxHealth - currentHealth; + } setEnemyHealth(enemy, currentHealth + healAmount); } diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 3cc2cc8..32d6d00 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -266,7 +266,7 @@ void test_randomIntRange(void) void test_enemyCanHeal(void) { //arrange - int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; + int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 20; int healAmount= 10, expectedHealth = 11; int result; //act @@ -277,5 +277,18 @@ void test_enemyCanHeal(void) TEST_ASSERT_EQUAL(expectedHealth, result); } +void test_enemyNoOverheal(void) +{ + //arrange + int enemyHealth = 1, enemyDamage = 4, enemyArmor = 4, enemyMaxHealth = 5; + int healAmount= 10, expectedHealth = 5; + int result; + //act + enemy test = {enemyHealth, enemyDamage, enemyArmor, enemyMaxHealth}; + enemyHeal(&test, healAmount); + result = getEnemyHealth(&test); + //assert + TEST_ASSERT_EQUAL(expectedHealth, result); +} #endif // TEST