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

@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class BattleCardPrefabConstants
{
public const int CARD_WIDTH = 122;
public const int CARD_HEIGHT = 155;
}

View file

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

View file

@ -19,8 +19,9 @@ public class BattlePlayerControls : MonoBehaviour
private BattleDeck<HeroCard> heroDeck; private BattleDeck<HeroCard> heroDeck;
private BattleDeck<MonsterCard> monsterDeck; private BattleDeck<MonsterCard> monsterDeck;
private List<GameObject> cardsInHandUIs;
private PlayArea playArea; private PlayArea playArea;
private HeroHandControls heroHandControls;
private Dictionary<BaseCard, GameObject> cardsInPlayUIs; private Dictionary<BaseCard, GameObject> cardsInPlayUIs;
public void Init( public void Init(
@ -29,6 +30,8 @@ public class BattlePlayerControls : MonoBehaviour
this.monsterDeck = monsterDeck; this.monsterDeck = monsterDeck;
this.playArea = playArea; this.playArea = playArea;
this.heroHandControls = new HeroHandControls(this, this.uiCanvas, this.cardPrefab);
this.heroHandControls.OnCardClicked += OnCardClicked;
this.cardsInPlayUIs = new Dictionary<BaseCard, GameObject>(); 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) { private void SetMonsterHand(List<MonsterCard> hand) {
// TODO // 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) { private void OnCardClicked(HeroCard card) {
if (this.singleTargeting == null) { if (this.singleTargeting == null) {
if (this.battleController.CanUseHeroCard(card)) { if (this.battleController.CanUseHeroCard(card)) {
@ -113,7 +85,7 @@ public class BattlePlayerControls : MonoBehaviour
} }
public void NotifyHeroHandPileChanged() { public void NotifyHeroHandPileChanged() {
SetHeroHand(this.heroDeck.GetHandPile().ToList()); this.heroHandControls.SetHeroHand(this.heroDeck.GetHandPile().ToList());
} }
public void NotifyMonsterHandPileChanged() { public void NotifyMonsterHandPileChanged() {
@ -162,11 +134,11 @@ public class BattlePlayerControls : MonoBehaviour
private Vector3 CalculatePlayAreaCardPosition(int index, int count) { private Vector3 CalculatePlayAreaCardPosition(int index, int count) {
const int SPACING = 8; const int SPACING = 8;
const int DELTA = CARD_HEIGHT + SPACING; const int DELTA = BattleCardPrefabConstants.CARD_HEIGHT + SPACING;
const int PADDING = 8; const int PADDING = 8;
return new Vector3(PADDING + CARD_WIDTH / 2, return new Vector3(PADDING + BattleCardPrefabConstants.CARD_WIDTH / 2,
Screen.height - PADDING - DELTA * index - CARD_HEIGHT / 2, Screen.height - PADDING - DELTA * index - BattleCardPrefabConstants.CARD_HEIGHT / 2,
0); 0);
} }
} }

View file

@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class HeroHandControls
{
public delegate void CardClickedAction(HeroCard card);
public event CardClickedAction OnCardClicked;
private MonoBehaviour root;
private GameObject uiCanvas;
private GameObject cardPrefab;
private List<GameObject> cardsInHandUIs;
public HeroHandControls(MonoBehaviour root, GameObject uiCanvas, GameObject cardPrefab) {
this.root = root;
this.uiCanvas = uiCanvas;
this.cardPrefab = cardPrefab;
}
public void SetHeroHand(List<HeroCard> hand) {
if (this.cardsInHandUIs != null) {
foreach (GameObject cardInHandUI in this.cardsInHandUIs) {
GameObject.Destroy(cardInHandUI);
}
}
this.cardsInHandUIs = new List<GameObject>();
for (int i = 0; i < hand.Count; i++) {
HeroCard card = hand[i];
GameObject cardUI = GameObject.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 Vector3 CalculateHandCardPosition(int index, int count) {
const int SPACING = 8;
const int DELTA = BattleCardPrefabConstants.CARD_WIDTH + SPACING;
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index,
SPACING + BattleCardPrefabConstants.CARD_HEIGHT / 2,
0);
}
}

View file

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