Advertisement
NoneApplicableGames

card.cs

Jul 20th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. //Libraries
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7.  
  8. public class Card : MonoBehaviour
  9. {
  10.     //Current card state
  11.     enum CardState
  12.     {
  13.         Set,
  14.         Dragging,
  15.     }
  16.     CardState currentState;
  17.    
  18.     float angle;
  19.     public float AngleDifference;
  20.     public float CurrentAngle;
  21.     public float RotationOffset = 90;
  22.  
  23.  
  24.     bool Swapping;
  25.  
  26.     int RecentDirection = 1;
  27.  
  28.     public bool Spin = false;
  29.     public float SpinSpeed = 30;
  30.  
  31.     //Reference to other script
  32.     public CardFace Face;
  33.     int SwapFace = 0;
  34.  
  35.     public Transform initialPosition;
  36.     // Start is called before the first frame update
  37.     void Start()
  38.     {
  39.         currentState = CardState.Set;
  40.         Face = this.GetComponentInChildren<CardFace>();
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         UpdateSpin();
  47.         UpdateState();
  48.     }
  49.    
  50.    
  51.     void UpdateSpin()
  52.     {
  53.         CurrentAngle = transform.eulerAngles.y;
  54.        
  55.         if (Spin)
  56.         {
  57.             float aSpeed = SpinSpeed * Time.deltaTime;
  58.            
  59.             if (Swapping)
  60.             {
  61.                 CheckSwap();
  62.                 if (CheckSpeed(AngleDifference, aSpeed))
  63.                 {
  64.                     aSpeed = Mathf.Abs(AngleDifference);
  65.                     DoSwap();
  66.                 }
  67.             }
  68.            
  69.             else
  70.             {
  71.                 CheckSpin();
  72.                 if (CheckSpeed(AngleDifference, aSpeed))
  73.                 {
  74.                     aSpeed = Mathf.Abs(AngleDifference);
  75.                     FinishSpin();
  76.                 }
  77.             }
  78.             transform.Rotate(new Vector3(0, 1, 0), aSpeed * RecentDirection);
  79.         }
  80.     }
  81.    
  82.    
  83.     void UpdateState()
  84.     {
  85.         switch (currentState)
  86.         {
  87.             case CardState.Set:
  88.                 break;
  89.             case CardState.Dragging:
  90.                 break;
  91.         }
  92.     }
  93.  
  94.  
  95.     void CheckSwap()
  96.     {
  97.         AngleDifference = Mathf.DeltaAngle(CurrentAngle, CheckAngle());
  98.     }
  99.    
  100.     void DoSwap()
  101.     {
  102.         Swapping = false;
  103.         Face.Swap(SwapFace);
  104.     }
  105.    
  106.     void CheckSpin()
  107.     {
  108.         AngleDifference = Mathf.DeltaAngle(CurrentAngle, HorizontalAngle());
  109.     }
  110.    
  111.     void FinishSpin()
  112.     {
  113.         Spin = false;
  114.     }
  115.    
  116.     bool CheckSpeed (float Angle, float Speed)
  117.     {
  118.         return Mathf.Abs(Angle) < Mathf.Abs(Speed);
  119.     }
  120.    
  121.     float CheckAngle()
  122.     {
  123.         Vector3 Direction = Camera.main.transform.position - transform.position;
  124.         Quaternion Rotation = Quaternion.LookRotation(Direction);
  125.         float destY = Rotation.eulerAngles.y + RotationOffset;
  126.         float destI = destY;
  127.         if (destI >= 180)
  128.         {
  129.             destI -= 180;
  130.         }
  131.         else
  132.         {
  133.             destI += 180;
  134.         }
  135.         float DestinationAngle = destY;
  136.         if (Mathf.Abs(CurrentAngle - destI) < Mathf.Abs(CurrentAngle - destY))
  137.         {
  138.             DestinationAngle = destI;
  139.         }
  140.         Debug.Log(destY + " OR " + destI + " = Destination:" + DestinationAngle);
  141.         return DestinationAngle;
  142.     }
  143.    
  144.     float HorizontalAngle()
  145.     {
  146.         float Dest = 0;
  147.         if(CurrentAngle > 90 && CurrentAngle < 270)
  148.         {
  149.             Dest = 180;        
  150.         }
  151.         return Dest;
  152.     }
  153.    
  154.     void SpinToAngle(float aSpeed)
  155.     {
  156.         angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, CheckAngle(), aSpeed);
  157.  
  158.         transform.eulerAngles = new Vector3(0, angle, 0);
  159.     }
  160.    
  161.     public void Swap(int i)
  162.     {
  163.         if (!Swapping)
  164.         {
  165.             Spin = true;
  166.             if (AngleDifference < 0)
  167.             {
  168.                 RecentDirection *= -1;
  169.             }
  170.             Swapping = true;
  171.             SwapFace = i;
  172.         }
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement