176 lines
6.8 KiB
C#
176 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using DG.Tweening;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class Card : MonoBehaviour
|
|
{
|
|
// ========= serializeField ========
|
|
[SerializeField] private int coast = 9;
|
|
[SerializeField] private string title = "Card";
|
|
|
|
// ========= private =========
|
|
private int ID;
|
|
private Canvas canvas;
|
|
private SpriteRenderer sprite_renderer;
|
|
// private bool is_drag = false;
|
|
|
|
// ========= Hover Message =========
|
|
private readonly Vector3 hover_position = Vector3.zero;
|
|
private readonly Quaternion hover_rotation = Quaternion.identity;
|
|
private const int hover_layer_sort_order = 100;
|
|
private float hover_duration;
|
|
private CardStatus card_status;
|
|
private TurnSingleton turnSingleton;
|
|
private TurnEventManagement turnManager;
|
|
private Collider2D areaCollider2D;
|
|
private Camera mainCamera;
|
|
private CardState cardState;
|
|
|
|
// =============== UI ================
|
|
private SpriteRenderer cardBackground;
|
|
private TMP_Text coastUI;
|
|
private TMP_Text titleUI;
|
|
|
|
|
|
private struct CardStatus
|
|
{
|
|
public Vector3 POSITION;
|
|
public Quaternion ROTATION;
|
|
public Vector3 SCALE;
|
|
public int SORTING_ORDER;
|
|
}
|
|
|
|
private enum CardState
|
|
{
|
|
WAIT,
|
|
STAR_DRAG,
|
|
DRAG_IN_COLLIDER,
|
|
DRAG_OUT_COLLIDER,
|
|
}
|
|
|
|
|
|
// ========= debug =========
|
|
private readonly Vector3 debug_draw_position = Vector3.zero;
|
|
|
|
private void Awake()
|
|
{
|
|
sprite_renderer = GetComponent<SpriteRenderer>();
|
|
turnSingleton = TurnSingleton.Instance;
|
|
card_status.SCALE = transform.localScale;
|
|
transform.localScale = Vector3.zero;
|
|
|
|
canvas = transform.Find("Canvas").GetComponent<Canvas>();
|
|
cardBackground = transform.GetChild(0).GetComponent<SpriteRenderer>();
|
|
coastUI = canvas.transform.GetChild(0).GetComponent<TMP_Text>();
|
|
titleUI = canvas.transform.GetChild(1).GetChild(0).GetComponent<TMP_Text>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (canvas.renderMode == RenderMode.WorldSpace && canvas.worldCamera == null) canvas.worldCamera = Camera.main;
|
|
coastUI.text = coast.ToString();
|
|
titleUI.text = title;
|
|
if (mainCamera == null) mainCamera = Camera.main;
|
|
}
|
|
|
|
public void SetCollider2D(Collider2D collider2) => areaCollider2D = collider2;
|
|
public void SetID(int id) => ID = id;
|
|
public void SetTurnManager(TurnEventManagement turnEventManagement) => turnManager = turnEventManagement;
|
|
public void SetHoverDuration(float drag_duration) => hover_duration = drag_duration;
|
|
|
|
private void OnMouseDown()
|
|
{
|
|
cardState = CardState.STAR_DRAG;
|
|
}
|
|
|
|
private void OnMouseDrag()
|
|
{
|
|
if (cardState == CardState.WAIT) return;
|
|
if (!turnSingleton.VerificationSystemStatus(turnManager.GetState(), TURN_OS_STATE.STAY_SELF_TURN)) return;
|
|
Vector3 mousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);
|
|
mousePosition.z = 0f;
|
|
bool insideArea = areaCollider2D.OverlapPoint(mousePosition);
|
|
if (insideArea)
|
|
{
|
|
if (cardState == CardState.DRAG_IN_COLLIDER) return;
|
|
cardState = CardState.DRAG_IN_COLLIDER;
|
|
print("inside");
|
|
Sequence seq = DOTween.Sequence();
|
|
seq.Join(transform.DOMove(hover_position, hover_duration));
|
|
seq.Join(transform.DOScale(card_status.SCALE * 2f, hover_duration));
|
|
seq.Join(transform.DORotateQuaternion(hover_rotation, hover_duration));
|
|
sprite_renderer.sortingOrder = hover_layer_sort_order;
|
|
cardBackground.sortingOrder = hover_layer_sort_order - 1;
|
|
canvas.sortingOrder = hover_layer_sort_order + 1;
|
|
}
|
|
else
|
|
{
|
|
cardState = CardState.DRAG_OUT_COLLIDER;
|
|
transform.localScale = card_status.SCALE / 2f;
|
|
transform.position = mousePosition;
|
|
}
|
|
}
|
|
|
|
private void OnMouseUp()
|
|
{
|
|
Sequence seq = DOTween.Sequence();
|
|
seq.Join(transform.DOMove(card_status.POSITION, hover_duration));
|
|
seq.Join(transform.DOScale(card_status.SCALE, hover_duration));
|
|
seq.Join(transform.DORotateQuaternion(card_status.ROTATION, hover_duration));
|
|
sprite_renderer.sortingOrder = card_status.SORTING_ORDER;
|
|
cardBackground.sortingOrder = card_status.SORTING_ORDER - 1;
|
|
canvas.sortingOrder = card_status.SORTING_ORDER + 1;
|
|
cardState = CardState.WAIT;
|
|
}
|
|
// **************************
|
|
|
|
|
|
public void DrawCard(STRUCT_TURN_CARD.LET_CARD_DEAL_SELF args)
|
|
{
|
|
List<REQUEST_METHODS> verificationList = new() { REQUEST_METHODS.POST };
|
|
if (!turnSingleton.VerificationRequestsMethods(args.METHOD, verificationList)) return;
|
|
if (!turnSingleton.VerificationSystemStatus(turnManager.GetState(), TURN_OS_STATE.STAY_SELF_TURN)) return;
|
|
if (!turnSingleton.VerificationActionStatus(turnManager.GetActionState(), TURN_ACTION_STATE.DEAL_RUNNING)) return;
|
|
if (args.ID != ID) return; // 不接受其他ID的卡牌以及其他消息
|
|
Vector3 move_position = args.POSITION;
|
|
Quaternion rotation = args.ROTATION;
|
|
float duration = args.DURATION;
|
|
int layer_sort_order = args.LAYER_SORT_ORDER;
|
|
move_position.z = -layer_sort_order * 0.5f;
|
|
|
|
sprite_renderer.sortingOrder = layer_sort_order * 3;
|
|
cardBackground.sortingOrder = layer_sort_order * 3 - 1;
|
|
canvas.sortingOrder = layer_sort_order * 3 + 1;
|
|
Sequence seq = DOTween.Sequence();
|
|
seq.Join(transform.DOMove(move_position, duration));
|
|
seq.Join(transform.DOScale(card_status.SCALE, duration));
|
|
seq.Join(transform.DORotateQuaternion(rotation, duration));
|
|
seq.OnComplete(() =>
|
|
{
|
|
// 记录默认数值
|
|
card_status.POSITION = move_position;
|
|
card_status.ROTATION = rotation;
|
|
card_status.SORTING_ORDER = layer_sort_order * 3;
|
|
});
|
|
}
|
|
|
|
public void DropCard(STRUCT_TURN_CARD.LET_CARD_DROP_SELF args) // 弃牌
|
|
{
|
|
List<REQUEST_METHODS> verificationList = new() { REQUEST_METHODS.DELETE };
|
|
if (!turnSingleton.VerificationRequestsMethods(args.METHOD, verificationList)) return;
|
|
|
|
card_status.SCALE = transform.localScale;
|
|
transform.localScale = Vector3.zero;
|
|
if (args.ID != ID) return; // 不接受其他ID的卡牌以及其他消息
|
|
float duration_scale = args.DURATION / 3f;
|
|
float duration_move = args.DURATION - duration_scale;
|
|
Sequence seq = DOTween.Sequence();
|
|
seq.Append(transform.DOScale(new Vector3(0.05f, 0.05f, 0.05f), duration_scale));
|
|
seq.Append(DOTween.Sequence()
|
|
.Join(transform.DOMove(args.POSITION, duration_move))
|
|
.Join(transform.DORotateQuaternion(Quaternion.identity, duration_move)));
|
|
seq.OnComplete(() => { Destroy(gameObject); });
|
|
}
|
|
} |