Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Libraries
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class Card : MonoBehaviour
- {
- //Current card state
- enum CardState
- {
- Set,
- Dragging,
- }
- CardState currentState;
- float angle;
- public float AngleDifference;
- public float CurrentAngle;
- public float RotationOffset = 90;
- bool Swapping;
- int RecentDirection = 1;
- public bool Spin = false;
- public float SpinSpeed = 30;
- //Reference to other script
- public CardFace Face;
- int SwapFace = 0;
- public Transform initialPosition;
- // Start is called before the first frame update
- void Start()
- {
- currentState = CardState.Set;
- Face = this.GetComponentInChildren<CardFace>();
- }
- // Update is called once per frame
- void Update()
- {
- UpdateSpin();
- UpdateState();
- }
- void UpdateSpin()
- {
- CurrentAngle = transform.eulerAngles.y;
- if (Spin)
- {
- float aSpeed = SpinSpeed * Time.deltaTime;
- if (Swapping)
- {
- CheckSwap();
- if (CheckSpeed(AngleDifference, aSpeed))
- {
- aSpeed = Mathf.Abs(AngleDifference);
- DoSwap();
- }
- }
- else
- {
- CheckSpin();
- if (CheckSpeed(AngleDifference, aSpeed))
- {
- aSpeed = Mathf.Abs(AngleDifference);
- FinishSpin();
- }
- }
- transform.Rotate(new Vector3(0, 1, 0), aSpeed * RecentDirection);
- }
- }
- void UpdateState()
- {
- switch (currentState)
- {
- case CardState.Set:
- break;
- case CardState.Dragging:
- break;
- }
- }
- void CheckSwap()
- {
- AngleDifference = Mathf.DeltaAngle(CurrentAngle, CheckAngle());
- }
- void DoSwap()
- {
- Swapping = false;
- Face.Swap(SwapFace);
- }
- void CheckSpin()
- {
- AngleDifference = Mathf.DeltaAngle(CurrentAngle, HorizontalAngle());
- }
- void FinishSpin()
- {
- Spin = false;
- }
- bool CheckSpeed (float Angle, float Speed)
- {
- return Mathf.Abs(Angle) < Mathf.Abs(Speed);
- }
- float CheckAngle()
- {
- Vector3 Direction = Camera.main.transform.position - transform.position;
- Quaternion Rotation = Quaternion.LookRotation(Direction);
- float destY = Rotation.eulerAngles.y + RotationOffset;
- float destI = destY;
- if (destI >= 180)
- {
- destI -= 180;
- }
- else
- {
- destI += 180;
- }
- float DestinationAngle = destY;
- if (Mathf.Abs(CurrentAngle - destI) < Mathf.Abs(CurrentAngle - destY))
- {
- DestinationAngle = destI;
- }
- Debug.Log(destY + " OR " + destI + " = Destination:" + DestinationAngle);
- return DestinationAngle;
- }
- float HorizontalAngle()
- {
- float Dest = 0;
- if(CurrentAngle > 90 && CurrentAngle < 270)
- {
- Dest = 180;
- }
- return Dest;
- }
- void SpinToAngle(float aSpeed)
- {
- angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, CheckAngle(), aSpeed);
- transform.eulerAngles = new Vector3(0, angle, 0);
- }
- public void Swap(int i)
- {
- if (!Swapping)
- {
- Spin = true;
- if (AngleDifference < 0)
- {
- RecentDirection *= -1;
- }
- Swapping = true;
- SwapFace = i;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement