From 3e0004d4cbaaf4ec42bcc5787412a118ccc6832d Mon Sep 17 00:00:00 2001 From: henriquenakashima Date: Sat, 19 Sep 2020 16:02:38 -0400 Subject: [PATCH] Implement TargetingPolicy, Cover can only target other allies --- Assets/Battle/BaseCard.cs | 14 ++++++++------ Assets/Battle/BattleController.cs | 15 +++++++++++++-- Assets/Battle/BattleHero.cs | 9 ++++++++- Assets/Battle/BattleMonster.cs | 2 +- Assets/Battle/BattleParticipant.cs | 9 +++++++++ Assets/Battle/BattleParticipantSprite.cs | 4 ++-- Assets/Battle/BattlePlayerControls.cs | 4 ++-- Assets/Battle/BattleScene.unity | 4 ++-- Assets/Battle/CardDatabase.cs | 15 ++++++++++++--- Assets/Battle/Cards/CardCover.cs | 6 ++++-- Assets/Battle/Cards/CardCrashingPunch.cs | 7 ++++--- Assets/Battle/HeroDeck.cs | 4 ++-- Assets/Battle/Targeting.meta | 8 ++++++++ Assets/Battle/Targeting/AnyTargetPolicy.cs | 10 ++++++++++ Assets/Battle/Targeting/AnyTargetPolicy.cs.meta | 11 +++++++++++ Assets/Battle/Targeting/OtheHeroTargetPolicy.cs | 16 ++++++++++++++++ .../Targeting/OtheHeroTargetPolicy.cs.meta | 11 +++++++++++ Assets/Battle/{ => Targeting}/SingleTargeting.cs | 9 +++++++++ .../{ => Targeting}/SingleTargeting.cs.meta | 0 Assets/Battle/Targeting/TargetingPolicy.cs | 8 ++++++++ Assets/Battle/Targeting/TargetingPolicy.cs.meta | 11 +++++++++++ 21 files changed, 151 insertions(+), 26 deletions(-) create mode 100644 Assets/Battle/Targeting.meta create mode 100644 Assets/Battle/Targeting/AnyTargetPolicy.cs create mode 100644 Assets/Battle/Targeting/AnyTargetPolicy.cs.meta create mode 100644 Assets/Battle/Targeting/OtheHeroTargetPolicy.cs create mode 100644 Assets/Battle/Targeting/OtheHeroTargetPolicy.cs.meta rename Assets/Battle/{ => Targeting}/SingleTargeting.cs (65%) rename Assets/Battle/{ => Targeting}/SingleTargeting.cs.meta (100%) create mode 100644 Assets/Battle/Targeting/TargetingPolicy.cs create mode 100644 Assets/Battle/Targeting/TargetingPolicy.cs.meta diff --git a/Assets/Battle/BaseCard.cs b/Assets/Battle/BaseCard.cs index 30053fe..8f4cc8b 100644 --- a/Assets/Battle/BaseCard.cs +++ b/Assets/Battle/BaseCard.cs @@ -8,13 +8,15 @@ public enum CardType { public abstract class BaseCard { - private CardId id; - private string displayName; - private int cost; - private CardType type; - private string effectText; + protected BattleParticipant owner; + protected CardId id; + protected string displayName; + protected int cost; + protected CardType type; + protected string effectText; - public BaseCard(CardId id, string displayName, int cost, CardType type, string effectText) { + public BaseCard(BattleParticipant owner, CardId id, string displayName, int cost, CardType type, string effectText) { + this.owner = owner; this.id = id; this.displayName = displayName; this.cost = cost; diff --git a/Assets/Battle/BattleController.cs b/Assets/Battle/BattleController.cs index 94f4fa1..6a6eedd 100644 --- a/Assets/Battle/BattleController.cs +++ b/Assets/Battle/BattleController.cs @@ -27,8 +27,6 @@ public class BattleController : MonoBehaviour private int energy; void Awake() { - this.heroDeck = new HeroDeck(cardIds); - heroes = new BattleHero[3]; heroSprites = new GameObject[3]; heroUIs = new HeroInfoUI[3]; @@ -69,12 +67,25 @@ public class BattleController : MonoBehaviour monsterUIs[i].Init(monsterSprites[i], monsters[i]); } + BattleParticipant[] cardOwners = new BattleParticipant[this.cardIds.Length]; + for (int i = 0; i < this.cardIds.Length; i++) { + cardOwners[i] = FindHeroByClass(CardDatabase.GetHeroIdOfCard(this.cardIds[i])); + } + this.heroDeck = new HeroDeck(this.cardIds, cardOwners); + this.battlePlayerControls.Init(this.heroDeck); this.battlePlayerControls.OnCardUsed += this.NotifyCardUsed; this.battlePlayerControls.OnTurnPassed += this.NotifyTurnPassed; this.heroDeck.OnHandPileChanged += this.battlePlayerControls.NotifyHandPileChanged; } + private BattleParticipant FindHeroByClass(HeroId heroId) { + foreach (BattleHero battleHero in this.heroes) { + if (battleHero.GetId() == heroId) return battleHero; + } + return null; + } + // Start is called before the first frame update void Start() { diff --git a/Assets/Battle/BattleHero.cs b/Assets/Battle/BattleHero.cs index 8a784fe..8e537fa 100644 --- a/Assets/Battle/BattleHero.cs +++ b/Assets/Battle/BattleHero.cs @@ -4,7 +4,10 @@ using UnityEngine; public class BattleHero: BattleParticipant { - public BattleHero(HeroId id) { + HeroId id; + + public BattleHero(HeroId id) : base(true) { + this.id = id; switch (id) { case HeroId.Alchemist: @@ -23,4 +26,8 @@ public class BattleHero: BattleParticipant throw new System.ArgumentException("HeroId unknown", "id"); } } + + public HeroId GetId() { + return this.id; + } } diff --git a/Assets/Battle/BattleMonster.cs b/Assets/Battle/BattleMonster.cs index 0baf298..62f058a 100644 --- a/Assets/Battle/BattleMonster.cs +++ b/Assets/Battle/BattleMonster.cs @@ -4,6 +4,6 @@ using UnityEngine; public class BattleMonster: BattleParticipant { - public BattleMonster() { + public BattleMonster() : base(false) { } } diff --git a/Assets/Battle/BattleParticipant.cs b/Assets/Battle/BattleParticipant.cs index 57f0936..1fa8956 100644 --- a/Assets/Battle/BattleParticipant.cs +++ b/Assets/Battle/BattleParticipant.cs @@ -5,11 +5,16 @@ using UnityEngine; public class BattleParticipant { + protected bool isHero; protected string displayName; protected int hp; protected int maxHP; protected int block; + public BattleParticipant(bool isHero) { + this.isHero = isHero; + } + public void Init() { this.hp = this.maxHP; } @@ -47,4 +52,8 @@ public class BattleParticipant public int GetBlock() { return this.block; } + + public bool IsHero() { + return this.isHero; + } } diff --git a/Assets/Battle/BattleParticipantSprite.cs b/Assets/Battle/BattleParticipantSprite.cs index 0b758b3..b306630 100644 --- a/Assets/Battle/BattleParticipantSprite.cs +++ b/Assets/Battle/BattleParticipantSprite.cs @@ -22,7 +22,7 @@ public class BattleParticipantSprite : MonoBehaviour // Update is called once per frame void Update() { - if (colorChanged && !this.playerControls.IsTargeting()) { + if (colorChanged && !this.playerControls.CouldTarget(participant)) { OnMouseExit(); } } @@ -35,7 +35,7 @@ public class BattleParticipantSprite : MonoBehaviour private bool colorChanged; void OnMouseEnter() { - if (this.playerControls.IsTargeting()) { + if (this.playerControls.CouldTarget(participant)) { SpriteRenderer sprite = GetComponent(); this.savedColor = sprite.color; sprite.color = Color.Lerp(this.savedColor, Color.white, .5f); diff --git a/Assets/Battle/BattlePlayerControls.cs b/Assets/Battle/BattlePlayerControls.cs index f4f3183..d83ec80 100644 --- a/Assets/Battle/BattlePlayerControls.cs +++ b/Assets/Battle/BattlePlayerControls.cs @@ -88,8 +88,8 @@ public class BattlePlayerControls : MonoBehaviour } } - public bool IsTargeting() { - return this.singleTargeting != null; + public bool CouldTarget(BattleParticipant participant) { + return this.singleTargeting != null && this.singleTargeting.CanTarget(participant); } public void NotifyHandPileChanged() { diff --git a/Assets/Battle/BattleScene.unity b/Assets/Battle/BattleScene.unity index 510c0e3..f43d5db 100644 --- a/Assets/Battle/BattleScene.unity +++ b/Assets/Battle/BattleScene.unity @@ -876,7 +876,7 @@ MonoBehaviour: m_HorizontalOverflow: 0 m_VerticalOverflow: 0 m_LineSpacing: 1 - m_Text: 'Draw pile: 5' + m_Text: 'Discard pile: 5' --- !u!222 &1522237123 CanvasRenderer: m_ObjectHideFlags: 0 @@ -982,7 +982,7 @@ MonoBehaviour: m_GameObject: {fileID: 2103461256} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: b39e026c50828b04ab3d0627aaf962d6, type: 3} + m_Script: {fileID: 11500000, guid: 077ba059e9689fa4990b9aea8bff3ded, type: 3} m_Name: m_EditorClassIdentifier: text: {fileID: 1522237122} diff --git a/Assets/Battle/CardDatabase.cs b/Assets/Battle/CardDatabase.cs index af073c9..362cbc9 100644 --- a/Assets/Battle/CardDatabase.cs +++ b/Assets/Battle/CardDatabase.cs @@ -9,15 +9,24 @@ public enum CardId { public class CardDatabase { - public static BaseCard CreateCard(CardId id) { + private static Dictionary CARD_TO_HERO = new Dictionary() { + {CardId.CrashingPunch, HeroId.Brute}, + {CardId.Cover, HeroId.Brute}, + }; + + public static BaseCard CreateCard(CardId id, BattleParticipant owner) { switch (id) { case CardId.CrashingPunch: - return new CardCrashingPunch(); + return new CardCrashingPunch(owner); case CardId.Cover: - return new CardCover(); + return new CardCover(owner); default: throw new System.ArgumentException("CardId unknown", "id"); } } + + public static HeroId GetHeroIdOfCard(CardId id) { + return CARD_TO_HERO[id]; + } } diff --git a/Assets/Battle/Cards/CardCover.cs b/Assets/Battle/Cards/CardCover.cs index d9cdfc8..41340dd 100644 --- a/Assets/Battle/Cards/CardCover.cs +++ b/Assets/Battle/Cards/CardCover.cs @@ -4,12 +4,14 @@ using UnityEngine; public class CardCover : BaseCard { - public CardCover() : base(CardId.Cover, "Cover", 1, CardType.Action, "Block 3 damage on another target ally.") { + public CardCover(BattleParticipant owner) + : base(owner, CardId.Cover, "Cover", 1, CardType.Action, + "Block 3 damage on another target ally.") { } public override IEnumerator Use(BattlePlayerControls playerControls) { - SingleTargeting singleTargeting = new SingleTargeting(); + SingleTargeting singleTargeting = new SingleTargeting(new OtherHeroTargetPolicy(this.owner)); yield return playerControls.StartCoroutine(playerControls.ChooseTarget(singleTargeting)); BattleParticipant target = singleTargeting.GetTarget(); Debug.Log("Covering " + target); diff --git a/Assets/Battle/Cards/CardCrashingPunch.cs b/Assets/Battle/Cards/CardCrashingPunch.cs index 724dcda..881a64f 100644 --- a/Assets/Battle/Cards/CardCrashingPunch.cs +++ b/Assets/Battle/Cards/CardCrashingPunch.cs @@ -4,12 +4,13 @@ using UnityEngine; public class CardCrashingPunch : BaseCard { - public CardCrashingPunch() : base(CardId.CrashingPunch, "Crashing Punch", 3, CardType.Action, "Deals 3 damage to target.") { - + public CardCrashingPunch(BattleParticipant owner) + : base(owner, CardId.CrashingPunch, "Crashing Punch", 3, CardType.Action, + "Deals 3 damage to target.") { } public override IEnumerator Use(BattlePlayerControls playerControls) { - SingleTargeting singleTargeting = new SingleTargeting(); + SingleTargeting singleTargeting = new SingleTargeting(new AnyTargetPolicy()); yield return playerControls.StartCoroutine(playerControls.ChooseTarget(singleTargeting)); BattleParticipant target = singleTargeting.GetTarget(); Debug.Log("Punching " + target); diff --git a/Assets/Battle/HeroDeck.cs b/Assets/Battle/HeroDeck.cs index 0be7ecb..3301487 100644 --- a/Assets/Battle/HeroDeck.cs +++ b/Assets/Battle/HeroDeck.cs @@ -17,13 +17,13 @@ public class HeroDeck private List handPile; private List discardPile; - public HeroDeck(CardId[] cardIds) { + public HeroDeck(CardId[] cardIds, BattleParticipant[] cardOwners) { this.drawPile = new List(); this.handPile = new List(); this.discardPile = new List(); for (int i = 0; i < cardIds.Length; i++) { - BaseCard newCard = CardDatabase.CreateCard(cardIds[i]); + BaseCard newCard = CardDatabase.CreateCard(cardIds[i], cardOwners[i]); this.drawPile.Add(newCard); } this.drawPile.Shuffle(); diff --git a/Assets/Battle/Targeting.meta b/Assets/Battle/Targeting.meta new file mode 100644 index 0000000..6715165 --- /dev/null +++ b/Assets/Battle/Targeting.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ed34b9735ea39304baf50786bc06033d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/Targeting/AnyTargetPolicy.cs b/Assets/Battle/Targeting/AnyTargetPolicy.cs new file mode 100644 index 0000000..12eb34f --- /dev/null +++ b/Assets/Battle/Targeting/AnyTargetPolicy.cs @@ -0,0 +1,10 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class AnyTargetPolicy: TargetingPolicy +{ + public override bool CanTarget(BattleParticipant participant) { + return true; + } +} diff --git a/Assets/Battle/Targeting/AnyTargetPolicy.cs.meta b/Assets/Battle/Targeting/AnyTargetPolicy.cs.meta new file mode 100644 index 0000000..d19763c --- /dev/null +++ b/Assets/Battle/Targeting/AnyTargetPolicy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 905e809c95dad9945b91b2a2f8906f54 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/Targeting/OtheHeroTargetPolicy.cs b/Assets/Battle/Targeting/OtheHeroTargetPolicy.cs new file mode 100644 index 0000000..16bed33 --- /dev/null +++ b/Assets/Battle/Targeting/OtheHeroTargetPolicy.cs @@ -0,0 +1,16 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class OtherHeroTargetPolicy: TargetingPolicy +{ + private BattleParticipant owner; + + public OtherHeroTargetPolicy(BattleParticipant owner) { + this.owner = owner; + } + + public override bool CanTarget(BattleParticipant participant) { + return participant.IsHero() && participant != this.owner; + } +} diff --git a/Assets/Battle/Targeting/OtheHeroTargetPolicy.cs.meta b/Assets/Battle/Targeting/OtheHeroTargetPolicy.cs.meta new file mode 100644 index 0000000..97ff1f6 --- /dev/null +++ b/Assets/Battle/Targeting/OtheHeroTargetPolicy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 929cb636783b68a4db231a06391a652e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Battle/SingleTargeting.cs b/Assets/Battle/Targeting/SingleTargeting.cs similarity index 65% rename from Assets/Battle/SingleTargeting.cs rename to Assets/Battle/Targeting/SingleTargeting.cs index bc27831..4cb29e4 100644 --- a/Assets/Battle/SingleTargeting.cs +++ b/Assets/Battle/Targeting/SingleTargeting.cs @@ -5,8 +5,17 @@ using UnityEngine.UI; public class SingleTargeting { + private TargetingPolicy targetingPolicy; private BattleParticipant participantClicked; + public SingleTargeting(TargetingPolicy targetingPolicy) { + this.targetingPolicy = targetingPolicy; + } + + public bool CanTarget(BattleParticipant participant) { + return this.targetingPolicy.CanTarget(participant); + } + public IEnumerator ChooseTarget() { while (this.participantClicked == null) { yield return null; diff --git a/Assets/Battle/SingleTargeting.cs.meta b/Assets/Battle/Targeting/SingleTargeting.cs.meta similarity index 100% rename from Assets/Battle/SingleTargeting.cs.meta rename to Assets/Battle/Targeting/SingleTargeting.cs.meta diff --git a/Assets/Battle/Targeting/TargetingPolicy.cs b/Assets/Battle/Targeting/TargetingPolicy.cs new file mode 100644 index 0000000..a0e77ce --- /dev/null +++ b/Assets/Battle/Targeting/TargetingPolicy.cs @@ -0,0 +1,8 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public abstract class TargetingPolicy +{ + public abstract bool CanTarget(BattleParticipant participant); +} diff --git a/Assets/Battle/Targeting/TargetingPolicy.cs.meta b/Assets/Battle/Targeting/TargetingPolicy.cs.meta new file mode 100644 index 0000000..df3da0c --- /dev/null +++ b/Assets/Battle/Targeting/TargetingPolicy.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 532a5ac0339ee5a4fb5c9e7888f0dd5f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: