You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.3 KiB
116 lines
4.3 KiB
using System.Collections;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class Card : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler{
|
|
public GameObject cardCanvas;
|
|
private GameObject cardPreview;
|
|
public static bool preparingCard = false;
|
|
public static GameObject cardBeingPrepared;
|
|
public bool preparedCard = false;
|
|
public static Vector3 preparingOffset = new Vector3(0f,40f);
|
|
public bool thrown = false;
|
|
private GameObject actionMenu;
|
|
private static GameObject thrownCards;
|
|
|
|
void Start() {
|
|
cardPreview = GameObject.FindGameObjectsWithTag("CardPreview")[0];
|
|
thrownCards = GameObject.FindGameObjectsWithTag("ThrownCards")[0];
|
|
actionMenu = transform.Find("ActionMenu").gameObject;
|
|
actionMenu.SetActive(false);
|
|
GetComponent<Button>().onClick.AddListener(OnClickCard);
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData edata) {
|
|
if(cardPreview && !cardBeingPrepared) {
|
|
SetPreview();
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData edata) {
|
|
if(cardPreview && !cardBeingPrepared) {
|
|
HidePreview();
|
|
}
|
|
}
|
|
public void OnClickCard() {
|
|
if ((!preparingCard || cardBeingPrepared != gameObject) && !thrown) {
|
|
cardBeingPrepared = gameObject;
|
|
SetPreview();
|
|
preparingCard = true;
|
|
}
|
|
}
|
|
|
|
public void Update() {
|
|
if(cardBeingPrepared == gameObject && !thrown) {
|
|
if (!preparedCard) {
|
|
transform.position += preparingOffset;
|
|
preparedCard = true;
|
|
}
|
|
} else {
|
|
if (preparedCard) {
|
|
transform.position -= preparingOffset;
|
|
preparedCard = false;
|
|
}
|
|
}
|
|
actionMenu.SetActive(preparedCard);
|
|
}
|
|
|
|
private void SetPreview() {
|
|
cardPreview.GetComponentsInChildren<RawImage>()[0].enabled = true;
|
|
cardPreview.GetComponentsInChildren<Text>()[0].enabled = true;
|
|
cardPreview.GetComponentsInChildren<Text>()[1].enabled = true;
|
|
cardPreview.GetComponentsInChildren<Text>()[2].enabled = true;
|
|
cardPreview.GetComponentsInChildren<RawImage>()[1].enabled = true;
|
|
cardPreview.GetComponentsInChildren<TMP_Text>()[0].enabled = true;
|
|
cardPreview.GetComponentsInChildren<Text>()[0].text = gameObject.GetComponentsInChildren<Text>()[0].text;
|
|
cardPreview.GetComponentsInChildren<Text>()[1].text = gameObject.GetComponentsInChildren<Text>()[1].text;
|
|
cardPreview.GetComponentsInChildren<Text>()[2].text = gameObject.GetComponentsInChildren<Text>()[2].text;
|
|
cardPreview.GetComponentsInChildren<RawImage>()[1].texture = gameObject.GetComponentsInChildren<RawImage>()[1].texture;
|
|
cardPreview.GetComponentsInChildren<TMP_Text>()[0].text = gameObject.GetComponentsInChildren<TMP_Text>()[0].text;
|
|
cardPreview.GetComponentsInChildren<RawImage>()[0].texture = gameObject.GetComponentsInChildren<RawImage>()[0].texture;
|
|
}
|
|
|
|
private void HidePreview() {
|
|
cardPreview.GetComponentsInChildren<RawImage>()[0].enabled = false;
|
|
cardPreview.GetComponentsInChildren<Text>()[0].enabled = false;
|
|
cardPreview.GetComponentsInChildren<Text>()[1].enabled = false;
|
|
cardPreview.GetComponentsInChildren<Text>()[2].enabled = false;
|
|
cardPreview.GetComponentsInChildren<RawImage>()[1].enabled = false;
|
|
cardPreview.GetComponentsInChildren<TMP_Text>()[0].enabled = false;
|
|
}
|
|
|
|
public void ThrowCard() {
|
|
var canvas = Instantiate(cardCanvas,new Vector3(0, 0.51f+thrownCards.transform.childCount*0.01f, -2.79f), transform.rotation);
|
|
canvas.GetComponent<Canvas>().worldCamera = Camera.main;
|
|
var newCard = Instantiate(gameObject, Vector3.zero, Quaternion.identity, canvas.transform);
|
|
newCard.transform.localScale = Vector3.one;
|
|
newCard.GetComponent<Card>().thrown = true;
|
|
canvas.transform.SetParent(thrownCards.transform);
|
|
canvas.transform.localScale = new Vector3(0.0025f, 0.0025f);
|
|
GetComponentsInChildren<RawImage>()[0].enabled = false;
|
|
GetComponentsInChildren<Text>()[0].enabled = false;
|
|
GetComponentsInChildren<Text>()[1].enabled = false;
|
|
GetComponentsInChildren<Text>()[2].enabled = false;
|
|
GetComponentsInChildren<RawImage>()[1].enabled = false;
|
|
GetComponentsInChildren<TMP_Text>()[0].enabled = false;
|
|
canvas.transform.Rotate(new Vector3(90f, 0f, 0f));
|
|
canvas.transform.Rotate(new Vector3(0f, 0f, Random.Range(-15, 15)));
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public void DoContextualAction() {
|
|
if(preparedCard) {
|
|
ThrowCard();
|
|
}
|
|
}
|
|
|
|
public void CancelContextualAction() {
|
|
if (preparedCard) {
|
|
cardBeingPrepared = null;
|
|
}
|
|
}
|
|
}
|
|
|