149 lines
5.2 KiB
C#
149 lines
5.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public class Card : MonoBehaviour
|
|
{
|
|
// ========= serializeField ========
|
|
[SerializeField] private int ID;
|
|
[SerializeField] private int instanceID;
|
|
|
|
// ========= private =========
|
|
|
|
private Canvas canvas;
|
|
private Camera main_camera;
|
|
private CardState card_state;
|
|
private bool is_setup = false;
|
|
private Collider2D area_collider2d;
|
|
private SpriteRenderer sprite_render;
|
|
private float card_drag_animation_duration;
|
|
private SpriteRenderer card_ui_background;
|
|
private TurnManagerSingleton manager_singleton;
|
|
private CardTransformFromHand card_transform_for_hand = new CardTransformFromHand();
|
|
private readonly Vector3 dropScale = new Vector3(0.05f, 0.05f, 0.05f);
|
|
|
|
private struct CardTransformFromHand // 存储卡牌处于手中时的位置信息
|
|
{
|
|
public Vector3 POSITION;
|
|
public Quaternion ROTATION;
|
|
public Vector3 SCALE;
|
|
public int LAYER_SORT_ORDER;
|
|
}
|
|
|
|
private enum CardState
|
|
{
|
|
WAIT = 0,
|
|
DRAG = 3
|
|
}
|
|
|
|
public int GetID(bool isInstance) => isInstance ? instanceID : ID;
|
|
|
|
public void set_area_collider2D(Collider2D collider2, float drag_duration)
|
|
{
|
|
area_collider2d = collider2;
|
|
card_drag_animation_duration = drag_duration;
|
|
}
|
|
|
|
private void unset_area_collider2D() => area_collider2d = null;
|
|
|
|
|
|
public void setup_card(int id, Camera camera1)
|
|
{
|
|
if (is_setup) return;
|
|
instanceID = id;
|
|
main_camera = camera1;
|
|
name = $"card_{instanceID}";
|
|
gameObject.SetActive(false);
|
|
card_transform_for_hand.SCALE = transform.localScale;
|
|
transform.localPosition = Vector3.zero;
|
|
transform.localScale = Vector3.zero;
|
|
manager_singleton = TurnManagerSingleton.Instance;
|
|
sprite_render = GetComponent<SpriteRenderer>();
|
|
canvas = transform.GetChild(1).GetComponent<Canvas>();
|
|
card_ui_background = transform.GetChild(0).GetComponent<SpriteRenderer>();
|
|
card_state = CardState.WAIT;
|
|
is_setup = true;
|
|
}
|
|
|
|
private void OnMouseDown()
|
|
{
|
|
card_state = CardState.DRAG;
|
|
}
|
|
|
|
private void OnMouseUp()
|
|
{
|
|
Sequence seq = DOTween.Sequence();
|
|
seq.Join(transform.DOMove(card_transform_for_hand.POSITION, card_drag_animation_duration));
|
|
seq.Join(transform.DOScale(card_transform_for_hand.SCALE, card_drag_animation_duration));
|
|
seq.Join(transform.DORotateQuaternion(card_transform_for_hand.ROTATION, card_drag_animation_duration));
|
|
seq.onComplete = reset_card_status;
|
|
return;
|
|
|
|
void reset_card_status()
|
|
{
|
|
card_state = CardState.WAIT;
|
|
if (manager_singleton.GetNeedDebugLog()) manager_singleton.print_msg("Card", instanceID.ToString(), "卡牌已返回手牌");
|
|
}
|
|
}
|
|
|
|
private void OnMouseDrag()
|
|
{
|
|
if (card_state != CardState.DRAG) return;
|
|
Vector3 mouse_position = main_camera.ScreenToWorldPoint(Input.mousePosition);
|
|
bool inside_hand_area = area_collider2d.OverlapPoint(mouse_position);
|
|
if (inside_hand_area)
|
|
{
|
|
transform.localScale = card_transform_for_hand.SCALE;
|
|
transform.rotation = card_transform_for_hand.ROTATION;
|
|
transform.position = card_transform_for_hand.POSITION;
|
|
}
|
|
else
|
|
{
|
|
transform.localScale = card_transform_for_hand.SCALE / 4f;
|
|
transform.rotation = Quaternion.identity;
|
|
mouse_position.z = 0f;
|
|
transform.position = mouse_position;
|
|
}
|
|
}
|
|
|
|
|
|
public Sequence card_goto_drop(Transform drop_card_container, float animation_duration)
|
|
{
|
|
Sequence seq = DOTween.Sequence();
|
|
seq.Join(transform.DOScale(dropScale, animation_duration));
|
|
seq.Join(transform.DOMove(drop_card_container.position, animation_duration));
|
|
seq.Join(transform.DORotateQuaternion(Quaternion.identity, animation_duration));
|
|
seq.OnComplete(() =>
|
|
{
|
|
unset_area_collider2D();
|
|
transform.parent = drop_card_container;
|
|
transform.localPosition = Vector3.zero;
|
|
gameObject.SetActive(false);
|
|
});
|
|
return seq;
|
|
}
|
|
|
|
|
|
public void move_card_go_hand(Vector3 move_position, Quaternion do_rotation, float do_duration, int do_layer_sort_order,
|
|
Transform card_container)
|
|
{
|
|
transform.SetParent(card_container);
|
|
gameObject.SetActive(true);
|
|
move_position.z = -do_layer_sort_order * 0.5f;
|
|
sprite_render.sortingOrder = do_layer_sort_order * 3;
|
|
card_ui_background.sortingOrder = do_layer_sort_order * 3 - 1;
|
|
canvas.sortingOrder = do_layer_sort_order * 3 + 1;
|
|
Sequence seq = DOTween.Sequence();
|
|
seq.Join(transform.DOMove(move_position, do_duration));
|
|
seq.Join(transform.DOScale(card_transform_for_hand.SCALE, do_duration));
|
|
seq.Join(transform.DORotateQuaternion(do_rotation, do_duration));
|
|
seq.OnComplete(() => // 记录默认数值
|
|
{
|
|
card_transform_for_hand.POSITION = move_position;
|
|
card_transform_for_hand.ROTATION = do_rotation;
|
|
card_transform_for_hand.LAYER_SORT_ORDER = do_layer_sort_order * 3;
|
|
});
|
|
}
|
|
} |