WXMC/proj/unity/Assets/Scripts/WishManager.cs
2024-12-04 16:18:46 +08:00

276 lines
10 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System.IO;
using Newtonsoft.Json.Linq;
using UnityEngine.Networking;
public class WishManager : MonoBehaviour
{
public int totalWishCount = 10;
public GameObject wishCard;
public Transform rotateCenter;
public Transform initPosition;
public GameObject wishGroup;
public GameObject questionBoard;
private Object cardObj;
public float rotateSpeed = 1000.0f;
public bool hasWish = false;
private float radius = 14.0f;
private List<GameObject> wishCardList;
[SerializeField]
private List<PuzzleDrawTarot> puzzleDrawTarotList;
[SerializeField]
private GameObject chosenCard;
[SerializeField]
private WishGodPageManager wishGodPageManager;
private float hitDistance;
public Transform initCardTransform;
public Vector3 beforeChosenPosition;
public Vector3 beforeChosenRotation;
[SerializeField]
private bool isChoosing = false;
[SerializeField]
private float m_rotDegree = 13.5f;
private JArray cards;
// Start is called before the first frame update
void Start()
{
chosenCard = null;
hitDistance = -1;
initCardTransform = wishCard.transform;
//DirectoryInfo directoryInfo = new DirectoryInfo(Application.streamingAssetsPath);
//FileInfo[] files = directoryInfo.GetFiles();
//Debug.Log("Test: " + Application.streamingAssetsPath);
//for (int i = 0; i < files.Length; i++)
//{
// FileInfo file = files[i];
// Debug.Log(file.FullName);
//}
JObject obj = JObject.Parse(ReadData("Tarots"));
cards = (JArray)obj["cards"];
Debug.Log(ReadData("Tarots"));
cardObj = Resources.Load("Prefabs/WishScene/Card");
}
// Update is called once per frame
void Update()
{
CheckRotate();
CheckPickCard();
}
public string ReadData(string fileName)
{
string readData;
string fileUrl = Application.streamingAssetsPath + "/" + fileName + ".json";
#if UNITY_EDITOR
using (StreamReader sr = File.OpenText(fileUrl))
{
readData = sr.ReadToEnd();
sr.Close();
}
#else
var loadingRequest = UnityWebRequest.Get(fileUrl);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone && !loadingRequest.isNetworkError && !loadingRequest.isHttpError);
readData = System.Text.Encoding.UTF8.GetString(loadingRequest.downloadHandler.data);
#endif
return readData;
}
public void ShowAllSelectType()
{
wishGodPageManager.ShowAllSelectType(puzzleDrawTarotList);
}
public void ShowCard()
{
wishGroup.transform.position = new Vector3(transform.position.x, transform.position.y, wishGroup.transform.position.z);
if (hasWish) return;
wishCardList = new List<GameObject>();
for (int i = 0; i < totalWishCount; ++i)
{
GameObject obj = Instantiate(cardObj, wishGroup.transform) as GameObject;
obj.name = string.Format("Card {0}", i);
obj.transform.position = initPosition.position;
float t = (float)(i - totalWishCount / 2) / totalWishCount;
obj.transform.RotateAround(rotateCenter.position, new Vector3(0.0f, 0.0f, 1.0f), t * totalWishCount * m_rotDegree);
obj.GetComponent<WishCard>().CardReturnButton.onClick.AddListener(HideCard);
obj.GetComponent<WishCard>().CardQuestionButton.onClick.AddListener(delegate
{
questionBoard.SetActive(true);
questionBoard.GetComponent<QuestionBoard>().chosenCard = obj;
obj.GetComponent<WishCard>().CardFront.SetActive(false);
});
var card = cards[i];
obj.GetComponent<WishCard>().CardFrontText.SetText(
"Main Representation:\n" + card["main_representations"] +
"Advice:\n" + card["advice"] +
"Description:\n" + card["description"]);
if (Resources.Load<Sprite>(string.Format("Cards/{0}", card["name"])) == null)
{
Debug.Log(string.Format("Cards/{0}", card["name"]));
Debug.Log(card["name"]);
}
obj.GetComponent<WishCard>().CardFrontSprite.sprite = Resources.Load<Sprite>(string.Format("Cards/{0}", card["name"]));
wishCardList.Add(obj);
}
hasWish = true;
}
void CheckRotate()
{
if (wishCardList == null || isChoosing) return;
if (Input.touchCount != 0)
{
Touch touch = Input.GetTouch(0);
//float speedX = Input.GetAxis("Mouse X");
float speedX = touch.deltaPosition.x;
if (
/* Set boundary at first and last card */
Vector2.Distance(new Vector2(wishCardList[0].transform.position.x, wishCardList[0].transform.position.y), new Vector2(0, -radius)) < 20 && speedX > 0 ||
Vector2.Distance(new Vector2(wishCardList[wishCardList.Count - 1].transform.position.x, wishCardList[wishCardList.Count - 1].transform.position.y), new Vector2(0, -radius)) < 20 && speedX < 0
)
return;
/* Debug.Log(Vector2.Distance(new Vector2(wishObjectList[0].transform.position.x, wishObjectList[0].transform.position.y), new Vector2(0, -radius)));
Debug.Log(Input.GetAxis("Mouse X"));*/
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
if (hit.collider != null && hit.collider.transform == transform)
{
foreach (GameObject obj in wishCardList)
{
obj.transform.RotateAround(rotateCenter.position, new Vector3(0.0f, 0.0f, 1.0f), speedX * rotateSpeed * Time.deltaTime);
}
}
}
}
void CheckPickCard()
{
if (wishCardList == null) return;
if (Input.GetMouseButtonDown(0) && !isChoosing)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
if (hit.collider != null)
{
WishCard theCard = hit.collider.gameObject.GetComponent<WishCard>();
if (theCard != null && !theCard.isSlot)
{
Debug.Log("Hit card:");
Debug.Log(hit.collider.gameObject.name);
isChoosing = true;
chosenCard = hit.collider.gameObject;
hitDistance = hit.distance;
beforeChosenPosition = hit.collider.gameObject.transform.position;
beforeChosenRotation = hit.collider.gameObject.transform.rotation.eulerAngles;
chosenCard.transform.DOScale(chosenCard.transform.localScale * 1.5f, 0.1f);
chosenCard.transform.DORotate(new Vector3(0, 0, 0), 1.0f);
}
if (theCard != null && theCard.isSlot && theCard.SlotOfCard != null)
{
NewWishManager.main.ShowCard(theCard);
}
}
}
if (Input.GetMouseButton(0) && chosenCard != null)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 mouseWorldPosition = ray.GetPoint(hitDistance);
chosenCard.transform.position = new Vector3(mouseWorldPosition.x, mouseWorldPosition.y, chosenCard.transform.position.z);
}
if (Input.GetMouseButtonUp(0))
{
if (chosenCard != null)
{
WishCard chosenCardScript = chosenCard.GetComponent<WishCard>();
WishCard chosenCardHole = GetNearestColldingSlotCard(chosenCardScript);
if (NewWishManager.main.SetWishSlotCard(chosenCardScript, chosenCardHole))
{
chosenCard.transform.DORotate(chosenCardHole.transform.rotation.eulerAngles, 0.5f);
chosenCard.transform.DOScale(chosenCardHole.transform.localScale, 0.5f);
chosenCard.transform.DOMove(chosenCardHole.transform.position, 0.7f).OnComplete(delegate {
//chosenCard.transform.DOScaleY(chosenCard.transform.localScale.y * -1, 1.0f);
//chosenCard.transform.DOScaleZ(chosenCard.transform.localScale.z * -1, 1.0f).OnComplete(delegate {
// chosenCard.transform.DOScale(chosenCard.transform.localScale * 3.0f, 1.0f);
// chosenCard.transform.DOMoveX(0, 1.0f);
// chosenCard.transform.DOMoveY(0, 1.0f);
// chosenCard.GetComponent<WishCard>().CardFront.SetActive(true);
// chosenCard = null;
//});
});
}
else
{
chosenCard.transform.DOScale(initCardTransform.localScale, 0.5f);
chosenCard.transform.DORotate(beforeChosenRotation, 0.5f);
chosenCard.transform.DOMove(beforeChosenPosition, 0.7f).OnComplete(delegate { isChoosing = false; });
chosenCard = null;
hitDistance = -1;
beforeChosenPosition = new Vector3(0.0f, 0.0f, 0.0f);
}
}
chosenCard = null;
isChoosing = false;
}
}
private WishCard GetNearestColldingSlotCard(WishCard card)
{
if (card.CollidingEmptySlotCards.Count == 0)
{
return null;
}
if (card.CollidingEmptySlotCards.Count == 1)
{
return card.CollidingEmptySlotCards[0];
}
WishCard result = card.CollidingEmptySlotCards[0];
float resultDist = Vector2.Distance(card.transform.position, result.transform.position);
for (int i = 1; i < card.CollidingEmptySlotCards.Count; i++)
{
WishCard iCard = card.CollidingEmptySlotCards[i];
float dist = Vector2.Distance(card.transform.position, iCard.transform.position);
if (dist < resultDist)
{
result = iCard;
resultDist = dist;
}
}
return result;
}
public void HideCard()
{
NewWishManager.main.HideCard();
}
}