diff --git a/Assets/Battle/BattleController.cs b/Assets/Battle/BattleController.cs index 5a67bf2..3158af3 100644 --- a/Assets/Battle/BattleController.cs +++ b/Assets/Battle/BattleController.cs @@ -333,4 +333,5 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe // MonsterViewpoint public BattleHero[] Heroes {get => this.heroes;} + public BattleMonster[] Monsters {get => this.monsters;} } diff --git a/Assets/Battle/BattleParticipant.cs b/Assets/Battle/BattleParticipant.cs index bb9ac33..1e66f99 100644 --- a/Assets/Battle/BattleParticipant.cs +++ b/Assets/Battle/BattleParticipant.cs @@ -31,6 +31,10 @@ public class BattleParticipant return this.maxHP; } + public bool IsAlive() { + return this.hp > 0; + } + public int DealDamage(int damage) { int blockedDamage = Math.Min(damage, this.block); if (blockedDamage > 0) { @@ -43,6 +47,10 @@ public class BattleParticipant } public int Heal(int amount) { + if (!IsAlive()) { + return 0; + } + amount = Math.Min(this.maxHP - this.hp, amount); this.hp += amount; return amount; diff --git a/Assets/Battle/CardsMonsters/GolemFistMCard.cs b/Assets/Battle/CardsMonsters/GolemFistMCard.cs new file mode 100644 index 0000000..2232fe3 --- /dev/null +++ b/Assets/Battle/CardsMonsters/GolemFistMCard.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class GolemFistMCard : MonsterCard +{ + public GolemFistMCard(BattleParticipant owner) + : base(owner, MCardId.GolemFist, "Golem Fist", 2, CardType.Attack, + "Deals 3 damage to target. Target: nearest hero.") { + } + + public override IEnumerator Use(IBattleEffectApplier effectApplier, MonsterViewpoint monsterViewpoint) { + BattleParticipant target = AIUtils.GetNearestHero(monsterViewpoint, this.owner); + DealDamageEffect damageEffect = new DealDamageEffect(target, 3); + effectApplier.Apply(damageEffect); + yield break; + } +} diff --git a/Assets/Battle/CardsMonsters/GolemFistMCard.cs.meta b/Assets/Battle/CardsMonsters/GolemFistMCard.cs.meta new file mode 100644 index 0000000..7acab89 --- /dev/null +++ b/Assets/Battle/CardsMonsters/GolemFistMCard.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f42a2de2b37632b489b9f6aebbf55b54 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/CardsMonsters/VampireFangsMCard.cs b/Assets/Battle/CardsMonsters/VampireFangsMCard.cs index 971c848..1dabdb3 100644 --- a/Assets/Battle/CardsMonsters/VampireFangsMCard.cs +++ b/Assets/Battle/CardsMonsters/VampireFangsMCard.cs @@ -5,12 +5,12 @@ using UnityEngine; public class VampireFangsMCard : MonsterCard { public VampireFangsMCard(BattleParticipant owner) - : base(owner, MCardId.VampireFangs, "VampireFangs", 2, CardType.Attack, + : base(owner, MCardId.VampireFangs, "Vampire Fangs", 2, CardType.Attack, "Steals 2 HP from target. Target: highest HP hero.") { } public override IEnumerator Use(IBattleEffectApplier effectApplier, MonsterViewpoint monsterViewpoint) { - BattleParticipant target = DetermineTarget(monsterViewpoint); + BattleParticipant target = AIUtils.GetHeroWithHighestHP(monsterViewpoint); DealDamageEffect damageEffect = new DealDamageEffect(target, 2); effectApplier.Apply(damageEffect); if (damageEffect.AmountDealt > 0) { @@ -19,21 +19,4 @@ public class VampireFangsMCard : MonsterCard } yield break; } - - private BattleParticipant DetermineTarget(MonsterViewpoint monsterViewpoint) { - return GetHeroWithHighestHP(monsterViewpoint); - } - - private BattleHero GetHeroWithHighestHP(MonsterViewpoint monsterViewpoint) { - int highestHPHeroIndex = -1; - int highestHP = -1; - for (int i = 0; i < monsterViewpoint.Heroes.Length; i++) { - int heroHP = monsterViewpoint.Heroes[i].GetHP(); - if (heroHP > highestHP) { - highestHPHeroIndex = i; - highestHP = heroHP; - } - } - return monsterViewpoint.Heroes[highestHPHeroIndex]; - } } diff --git a/Assets/Battle/MonsterAI.meta b/Assets/Battle/MonsterAI.meta new file mode 100644 index 0000000..c945233 --- /dev/null +++ b/Assets/Battle/MonsterAI.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ea3ae8263c16c404e8dc33782de382a9 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/MonsterAI/AIUtils.cs b/Assets/Battle/MonsterAI/AIUtils.cs new file mode 100644 index 0000000..a65b005 --- /dev/null +++ b/Assets/Battle/MonsterAI/AIUtils.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class AIUtils { + public static BattleHero GetNearestHero(MonsterViewpoint monsterViewpoint, + BattleParticipant owner) { + int ownerPosition = GetMonsterPosition(monsterViewpoint, owner); + + int nearestHeroIndex = -1; + int nearestDistance = int.MaxValue; + for (int i = 0; i < monsterViewpoint.Heroes.Length; i++) { + if (!monsterViewpoint.Heroes[i].IsAlive()) continue; + int distance = System.Math.Abs(i - ownerPosition); + if (distance < nearestDistance) { + nearestHeroIndex = i; + nearestDistance = distance; + } + } + return monsterViewpoint.Heroes[nearestHeroIndex]; + } + + public static int GetMonsterPosition(MonsterViewpoint monsterViewpoint, BattleParticipant monster) { + for (int i = 0; i < monsterViewpoint.Monsters.Length; i++) { + if (monsterViewpoint.Monsters[i] == monster) return i; + } + return -1; + } + + public static BattleHero GetHeroWithHighestHP(MonsterViewpoint monsterViewpoint) { + int highestHPHeroIndex = -1; + int highestHP = -1; + for (int i = 0; i < monsterViewpoint.Heroes.Length; i++) { + if (!monsterViewpoint.Heroes[i].IsAlive()) continue; + int heroHP = monsterViewpoint.Heroes[i].GetHP(); + if (heroHP > highestHP) { + highestHPHeroIndex = i; + highestHP = heroHP; + } + } + return monsterViewpoint.Heroes[highestHPHeroIndex]; + } +} diff --git a/Assets/Battle/MonsterAI/AIUtils.cs.meta b/Assets/Battle/MonsterAI/AIUtils.cs.meta new file mode 100644 index 0000000..c0feed3 --- /dev/null +++ b/Assets/Battle/MonsterAI/AIUtils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 69cce7f2fac935f429b1a72da0b465bd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/MonsterViewpoint.cs b/Assets/Battle/MonsterAI/MonsterViewpoint.cs similarity index 80% rename from Assets/Battle/MonsterViewpoint.cs rename to Assets/Battle/MonsterAI/MonsterViewpoint.cs index 0f9cc21..46711fc 100644 --- a/Assets/Battle/MonsterViewpoint.cs +++ b/Assets/Battle/MonsterAI/MonsterViewpoint.cs @@ -4,4 +4,5 @@ using UnityEngine; public interface MonsterViewpoint { BattleHero[] Heroes {get;} + BattleMonster[] Monsters {get;} } diff --git a/Assets/Battle/MonsterViewpoint.cs.meta b/Assets/Battle/MonsterAI/MonsterViewpoint.cs.meta similarity index 100% rename from Assets/Battle/MonsterViewpoint.cs.meta rename to Assets/Battle/MonsterAI/MonsterViewpoint.cs.meta diff --git a/Assets/Battle/MonsterCardDatabase.cs b/Assets/Battle/MonsterCardDatabase.cs index 004b68d..046f7cf 100644 --- a/Assets/Battle/MonsterCardDatabase.cs +++ b/Assets/Battle/MonsterCardDatabase.cs @@ -4,6 +4,7 @@ using UnityEngine; public enum MCardId { VampireFangs, + GolemFist, } public class MonsterCardDatabase : CardDatabase @@ -14,6 +15,8 @@ public class MonsterCardDatabase : CardDatabase { case MCardId.VampireFangs: return new VampireFangsMCard(owner); + case MCardId.GolemFist: + return new GolemFistMCard(owner); default: throw new System.ArgumentException("MCardId unknown", "id"); }