Implement GolemFist

This commit is contained in:
henriquenakashima 2020-09-20 15:45:59 -04:00
parent 4ca7b7537a
commit d1af817bc6
11 changed files with 106 additions and 19 deletions

View file

@ -333,4 +333,5 @@ public class BattleController : MonoBehaviour, IBattleEffectApplier, IBattleEffe
// MonsterViewpoint // MonsterViewpoint
public BattleHero[] Heroes {get => this.heroes;} public BattleHero[] Heroes {get => this.heroes;}
public BattleMonster[] Monsters {get => this.monsters;}
} }

View file

@ -31,6 +31,10 @@ public class BattleParticipant
return this.maxHP; return this.maxHP;
} }
public bool IsAlive() {
return this.hp > 0;
}
public int DealDamage(int damage) { public int DealDamage(int damage) {
int blockedDamage = Math.Min(damage, this.block); int blockedDamage = Math.Min(damage, this.block);
if (blockedDamage > 0) { if (blockedDamage > 0) {
@ -43,6 +47,10 @@ public class BattleParticipant
} }
public int Heal(int amount) { public int Heal(int amount) {
if (!IsAlive()) {
return 0;
}
amount = Math.Min(this.maxHP - this.hp, amount); amount = Math.Min(this.maxHP - this.hp, amount);
this.hp += amount; this.hp += amount;
return amount; return amount;

View file

@ -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;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f42a2de2b37632b489b9f6aebbf55b54
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -5,12 +5,12 @@ using UnityEngine;
public class VampireFangsMCard : MonsterCard public class VampireFangsMCard : MonsterCard
{ {
public VampireFangsMCard(BattleParticipant owner) 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.") { "Steals 2 HP from target. Target: highest HP hero.") {
} }
public override IEnumerator Use(IBattleEffectApplier effectApplier, MonsterViewpoint monsterViewpoint) { public override IEnumerator Use(IBattleEffectApplier effectApplier, MonsterViewpoint monsterViewpoint) {
BattleParticipant target = DetermineTarget(monsterViewpoint); BattleParticipant target = AIUtils.GetHeroWithHighestHP(monsterViewpoint);
DealDamageEffect damageEffect = new DealDamageEffect(target, 2); DealDamageEffect damageEffect = new DealDamageEffect(target, 2);
effectApplier.Apply(damageEffect); effectApplier.Apply(damageEffect);
if (damageEffect.AmountDealt > 0) { if (damageEffect.AmountDealt > 0) {
@ -19,21 +19,4 @@ public class VampireFangsMCard : MonsterCard
} }
yield break; 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];
}
} }

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ea3ae8263c16c404e8dc33782de382a9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -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];
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 69cce7f2fac935f429b1a72da0b465bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -4,4 +4,5 @@ using UnityEngine;
public interface MonsterViewpoint { public interface MonsterViewpoint {
BattleHero[] Heroes {get;} BattleHero[] Heroes {get;}
BattleMonster[] Monsters {get;}
} }

View file

@ -4,6 +4,7 @@ using UnityEngine;
public enum MCardId { public enum MCardId {
VampireFangs, VampireFangs,
GolemFist,
} }
public class MonsterCardDatabase : CardDatabase<MonsterCard> public class MonsterCardDatabase : CardDatabase<MonsterCard>
@ -14,6 +15,8 @@ public class MonsterCardDatabase : CardDatabase<MonsterCard>
{ {
case MCardId.VampireFangs: case MCardId.VampireFangs:
return new VampireFangsMCard(owner); return new VampireFangsMCard(owner);
case MCardId.GolemFist:
return new GolemFistMCard(owner);
default: default:
throw new System.ArgumentException("MCardId unknown", "id"); throw new System.ArgumentException("MCardId unknown", "id");
} }