Refactor HeroHandControls out of BattlePlayerControls

This commit is contained in:
henriquenakashima 2020-09-20 14:12:04 -04:00
parent c0afc58c01
commit 57dbe84b3d
5 changed files with 92 additions and 36 deletions

View file

@ -19,8 +19,9 @@ public class BattlePlayerControls : MonoBehaviour
private BattleDeck<HeroCard> heroDeck;
private BattleDeck<MonsterCard> monsterDeck;
private List<GameObject> cardsInHandUIs;
private PlayArea playArea;
private HeroHandControls heroHandControls;
private Dictionary<BaseCard, GameObject> cardsInPlayUIs;
public void Init(
@ -29,6 +30,8 @@ public class BattlePlayerControls : MonoBehaviour
this.monsterDeck = monsterDeck;
this.playArea = playArea;
this.heroHandControls = new HeroHandControls(this, this.uiCanvas, this.cardPrefab);
this.heroHandControls.OnCardClicked += OnCardClicked;
this.cardsInPlayUIs = new Dictionary<BaseCard, GameObject>();
}
@ -45,41 +48,10 @@ public class BattlePlayerControls : MonoBehaviour
}
private void SetHeroHand(List<HeroCard> hand) {
if (this.cardsInHandUIs != null) {
foreach (GameObject cardInHandUI in this.cardsInHandUIs) {
Destroy(cardInHandUI);
}
}
this.cardsInHandUIs = new List<GameObject>();
for (int i = 0; i < hand.Count; i++) {
HeroCard card = hand[i];
GameObject cardUI = Instantiate(this.cardPrefab, this.uiCanvas.transform);
this.cardsInHandUIs.Add(cardUI);
cardUI.transform.position = CalculateHandCardPosition(i, hand.Count);
cardUI.GetComponent<CardUI>().Init(card);
int cardIndex = i;
cardUI.GetComponent<Button>().onClick.AddListener(delegate {OnCardClicked(card);});
}
}
private void SetMonsterHand(List<MonsterCard> hand) {
// TODO
}
private const int CARD_WIDTH = 122;
private const int CARD_HEIGHT = 155;
private Vector3 CalculateHandCardPosition(int index, int count) {
const int SPACING = 8;
const int DELTA = CARD_WIDTH + SPACING;
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index,
SPACING + CARD_HEIGHT / 2,
0);
}
private void OnCardClicked(HeroCard card) {
if (this.singleTargeting == null) {
if (this.battleController.CanUseHeroCard(card)) {
@ -113,7 +85,7 @@ public class BattlePlayerControls : MonoBehaviour
}
public void NotifyHeroHandPileChanged() {
SetHeroHand(this.heroDeck.GetHandPile().ToList());
this.heroHandControls.SetHeroHand(this.heroDeck.GetHandPile().ToList());
}
public void NotifyMonsterHandPileChanged() {
@ -162,11 +134,11 @@ public class BattlePlayerControls : MonoBehaviour
private Vector3 CalculatePlayAreaCardPosition(int index, int count) {
const int SPACING = 8;
const int DELTA = CARD_HEIGHT + SPACING;
const int DELTA = BattleCardPrefabConstants.CARD_HEIGHT + SPACING;
const int PADDING = 8;
return new Vector3(PADDING + CARD_WIDTH / 2,
Screen.height - PADDING - DELTA * index - CARD_HEIGHT / 2,
return new Vector3(PADDING + BattleCardPrefabConstants.CARD_WIDTH / 2,
Screen.height - PADDING - DELTA * index - BattleCardPrefabConstants.CARD_HEIGHT / 2,
0);
}
}