Refactor HeroHandControls out of BattlePlayerControls
This commit is contained in:
parent
c0afc58c01
commit
57dbe84b3d
5 changed files with 92 additions and 36 deletions
51
Assets/Battle/HeroHandControls.cs
Normal file
51
Assets/Battle/HeroHandControls.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue