Show cards in PlayArea
This commit is contained in:
parent
d67a7372c9
commit
8b33f3ff5a
6 changed files with 70 additions and 16 deletions
|
|
@ -16,15 +16,21 @@ public class BattlePlayerControls : MonoBehaviour
|
|||
|
||||
private HeroDeck heroDeck;
|
||||
private List<GameObject> cardsInHandUIs;
|
||||
private PlayArea playArea;
|
||||
private Dictionary<BaseCard, GameObject> cardsInPlayUIs;
|
||||
|
||||
public void Init(HeroDeck heroDeck) {
|
||||
public void Init(HeroDeck heroDeck, PlayArea playArea) {
|
||||
this.heroDeck = heroDeck;
|
||||
this.playArea = playArea;
|
||||
|
||||
this.cardsInPlayUIs = new Dictionary<BaseCard, GameObject>();
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
NotifyPlayAreaChanged();
|
||||
this.playArea.OnPlayAreaChanged += NotifyPlayAreaChanged;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
|
|
@ -45,19 +51,23 @@ public class BattlePlayerControls : MonoBehaviour
|
|||
BaseCard card = hand[i];
|
||||
GameObject cardUI = Instantiate(this.cardPrefab, this.uiCanvas.transform);
|
||||
this.cardsInHandUIs.Add(cardUI);
|
||||
cardUI.transform.position = CalculateCardPosition(i, hand.Count);
|
||||
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 CalculateCardPosition(int index, int count) {
|
||||
const int CARD_SIZE = 120;
|
||||
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_SIZE + SPACING;
|
||||
const int DELTA = CARD_WIDTH + SPACING;
|
||||
|
||||
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index, 160, 0);
|
||||
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index,
|
||||
160,
|
||||
0);
|
||||
}
|
||||
|
||||
private void OnCardClicked(BaseCard card) {
|
||||
|
|
@ -95,4 +105,54 @@ public class BattlePlayerControls : MonoBehaviour
|
|||
public void NotifyHandPileChanged() {
|
||||
SetHand(this.heroDeck.GetHandPile());
|
||||
}
|
||||
|
||||
private void NotifyPlayAreaChanged() {
|
||||
DestroyCardsThatLeftPlayArea();
|
||||
CreateCardsThatEnteredPlayArea();
|
||||
RepositionCardsInPlayArea();
|
||||
}
|
||||
|
||||
private void DestroyCardsThatLeftPlayArea() {
|
||||
List<BaseCard> cardsToRemove = new List<BaseCard>();
|
||||
foreach (KeyValuePair<BaseCard, GameObject> pair in this.cardsInPlayUIs) {
|
||||
if (!this.playArea.GetHeroPermanents().Contains(pair.Key)) {
|
||||
Destroy(pair.Value);
|
||||
cardsToRemove.Add(pair.Key);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (BaseCard cardToRemove in cardsToRemove) {
|
||||
this.cardsInPlayUIs.Remove(cardToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateCardsThatEnteredPlayArea() {
|
||||
foreach (BaseCard card in this.playArea.GetHeroPermanents()) {
|
||||
if (!this.cardsInPlayUIs.ContainsKey(card)) {
|
||||
GameObject cardUI = Instantiate(this.cardPrefab, this.uiCanvas.transform);
|
||||
// Leave position to be set by RepositionCardsInPlayArea();
|
||||
cardUI.GetComponent<CardUI>().Init(card);
|
||||
this.cardsInPlayUIs.Add(card, cardUI);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RepositionCardsInPlayArea() {
|
||||
List<BaseCard> permanentList = this.playArea.GetHeroPermanents();
|
||||
for (int i = 0; i < permanentList.Count; i++) {
|
||||
BaseCard card = permanentList[i];
|
||||
GameObject cardUI = this.cardsInPlayUIs[card];
|
||||
cardUI.transform.position = CalculatePlayAreaCardPosition(i, permanentList.Count);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 CalculatePlayAreaCardPosition(int index, int count) {
|
||||
const int SPACING = 8;
|
||||
const int DELTA = CARD_HEIGHT + SPACING;
|
||||
const int PADDING = 8;
|
||||
|
||||
return new Vector3(PADDING + CARD_WIDTH / 2,
|
||||
Screen.height - PADDING - DELTA * index - CARD_HEIGHT / 2,
|
||||
0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue