Advertisement
Guest User

Timing issue

a guest
Jul 12th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.57 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6.  
  7. public class Piece : MonoBehaviour
  8. {
  9.     public enum PieceType
  10.     {
  11.         Red = 1,
  12.         DarkGreen = 2,
  13.         LightBlue = 3,
  14.         Purple = 4,
  15.         Yellow = 5,
  16.         Comet = 6,
  17.         Star = 7,
  18.         Moon = 8,
  19.         Sun = 9,
  20.         All = 10,
  21.     }
  22.  
  23.     public int X { get { return _x; } }
  24.     public int Y { get { return _y; } }
  25.     public int Z { get { return _z; } }
  26.  
  27.     public PieceType Type { get { return _type; } }
  28.     public GameObject selectPrefab;
  29.     public delegate void onNotityPieceStop(Piece piece, List<Piece> matches);
  30.     public event onNotityPieceStop OnPieceStopped;
  31.  
  32.     [SerializeField] float _timeToMoveInSeconds = 0.3f; // in seconds.
  33.     [SerializeField] float _selectTimeInSeconds = 2.0f; // in seconds.
  34.     [SerializeField] PieceType _type;
  35.     int _x;
  36.     int _y;
  37.     int _z;
  38.     static Piece _clickedPiece = null;
  39.     static Piece _targetPiece = null;
  40.     static bool _isInDrag = false;
  41.     static bool _isSelected = false;
  42.     bool _isMoving = false;
  43.     Board _board;
  44.     Vector3 _previousPosition;
  45.     bool _isReturning = false;
  46.  
  47.     void Awake()
  48.     {
  49.         _board = FindObjectOfType<Board>();
  50.     }
  51.  
  52.     void Start()
  53.     {
  54.         OnPieceStopped += ClearOrSwitchBackHandeler;
  55.     }
  56.  
  57.     protected virtual void ClearOrSwitchBackHandeler(Piece piece, List<Piece> matches)
  58.     {
  59.         var partnerPiece = _board.AllPieces[(int)_previousPosition.x, (int)_previousPosition.y];
  60.         var partnerMatches = GetMatchesAt(partnerPiece);
  61.  
  62.         if (matches == null && partnerMatches == null) // Either this piece, and partner piece do not have matches, return to original spots.
  63.         {
  64.             if (!_isReturning)
  65.             {
  66.                 _isReturning = true;
  67.                 MoveTo(_previousPosition, _timeToMoveInSeconds);
  68.                 return;
  69.             }
  70.             else
  71.             {
  72.                 _isReturning = false;
  73.                 return;
  74.             }
  75.         }
  76.         else if (matches == null && partnerMatches != null) // This piece doesn't have matches but partner does. Stand still, don't do anything.
  77.         {
  78.             return;
  79.         }
  80.         else if (matches != null)
  81.         {
  82.             foreach (var pieceToDestroy in matches)
  83.             {
  84.                 _board.AllPieces[pieceToDestroy.X, pieceToDestroy.Y] = null;
  85.                 Destroy(pieceToDestroy.gameObject);
  86.             }
  87.         }
  88.     }
  89.  
  90.     /// <summary>
  91.     /// Set the Tile coördinates with x, y, z coordinates.
  92.     /// </summary>
  93.     /// <param name="x"></param>
  94.     /// <param name="y"></param>
  95.     /// <param name="z"></param>
  96.     public void Set(int x, int y, int z = 0)
  97.     {
  98.         _x = x;
  99.         _y = y;
  100.         _z = z;
  101.     }
  102.  
  103.     //TODO move to other class.
  104.     // START OF Input based on touch input to move pieces.
  105.     void OnMouseDown()
  106.     {
  107.         _isInDrag = true;
  108.  
  109.         if (_clickedPiece == null && _targetPiece == null)
  110.         {
  111.             _clickedPiece = this;
  112.         }
  113.  
  114.         if (_clickedPiece != this && !_isSelected)
  115.         {
  116.             _clickedPiece = this;
  117.         }
  118.         else if(_clickedPiece != this && _isSelected)
  119.         {
  120.             CancelSelect();
  121.  
  122.             _targetPiece = this;
  123.  
  124.             SwitchPieces(_clickedPiece, _targetPiece);
  125.  
  126.             _clickedPiece = null;
  127.             _targetPiece = null;
  128.         }
  129.     }
  130.     void OnMouseEnter()
  131.     {
  132.         if (_isInDrag)
  133.         {
  134.             if (_clickedPiece != null && _clickedPiece != this)
  135.             {
  136.                 _targetPiece = this;                
  137.  
  138.             }
  139.         }
  140.     }
  141.     void OnMouseUp()
  142.     {
  143.         _isInDrag = false;
  144.  
  145.         if (_clickedPiece != null && _targetPiece != null)
  146.         {
  147.             SwitchPieces(_clickedPiece, _targetPiece);
  148.  
  149.             _clickedPiece = null;
  150.             _targetPiece = null;
  151.         }
  152.  
  153.         if (_clickedPiece == this)
  154.         {
  155.             SelectPieceAt(transform.position);
  156.         }
  157.     }
  158.     // END OF Input based on touch input to move pieces.
  159.  
  160.     void SelectPieceAt(Vector3 position)
  161.     {
  162.         if (!_isSelected)
  163.         {
  164.             _isSelected = true;
  165.             StartCoroutine(SelectPieceRoutine(position));
  166.         }
  167.     }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. // Some other code regarding selecting a piece ( UI stuff )
  174.  
  175.  
  176.  
  177.    public void SwitchPieces(Piece clickedPiece, Piece targetPiece)
  178.     {
  179.         clickedPiece.MoveTo(targetPiece.transform.position, _timeToMoveInSeconds);
  180.         targetPiece.MoveTo(clickedPiece.transform.position, _timeToMoveInSeconds);
  181.     }
  182.  
  183.     public void MoveTo(Vector3 destination, float timeToMove)
  184.     {        
  185.         StartCoroutine(MovePieceToRoutine(transform.position, destination, timeToMove));
  186.     }
  187.  
  188.     IEnumerator MovePieceToRoutine(Vector3 startPosition, Vector3 destination, float timeToMove)
  189.     {
  190.         if (!_isMoving)
  191.         {
  192.             _previousPosition = startPosition;
  193.             _isMoving = true;
  194.             var reachedDestination = false;
  195.             var elapsedTime = 0f;
  196.  
  197.             while (!reachedDestination)
  198.             {
  199.                 var t = Mathf.Clamp(elapsedTime / timeToMove, 0f, 1f);
  200.                 t = t * t * t * (t * (t * 6 - 15) + 10); // Smootherstep interpolation formula.
  201.  
  202.                 transform.position = Vector3.Lerp(startPosition, destination, t);
  203.  
  204.                 elapsedTime += Time.deltaTime;
  205.  
  206.                 if (Vector3.Distance(transform.position, destination) < 0.01f)
  207.                 {
  208.                     transform.position = destination;
  209.  
  210.                     Set((int)destination.x, (int)destination.y);
  211.                     _board.AllPieces[X, Y] = this;
  212.  
  213.                     reachedDestination = true;
  214.  
  215.                     break;
  216.                 }
  217.  
  218.                 yield return null;
  219.             }
  220.  
  221.             _isMoving = false;
  222.  
  223.             OnPieceStopped(this, GetMatchesAt(this));
  224.  
  225.             yield break;
  226.         }
  227.         else
  228.         {
  229.             Debug.Log("MovePieceToRoutine: Piece is already moving.");
  230.         }
  231.     }
  232.     //TODO Implement an event that checks for deadlocks.    
  233.  
  234.     public List<Piece> GetMatchesAt(Piece startPiece, int minimumMatches = 3)
  235.     {
  236.         if (startPiece == null)
  237.         {
  238.             Debug.LogError("GetMatchesAt: variable startPiece is null. Returning null as match list.");
  239.             return null;
  240.         }
  241.  
  242.         var allMatches = new List<Piece>();
  243.         var directionalList = new List<Piece>();
  244.         var counter = 0;
  245.         Vector2[] searchDirections = { Vector2.right, Vector2.left, Vector2.up, Vector2.down }; // Set directions of search, don't need other ones then these ones.
  246.  
  247.         foreach (var direction in searchDirections)
  248.         {
  249.             counter++;
  250.  
  251.             for (int i = 0; i < minimumMatches; i++)
  252.             {
  253.                 Piece nextPiece = null;
  254.                 var nextPositionToCheck = new Vector2(startPiece.X + ((int)direction.x * i), startPiece.Y + ((int)direction.y * i)); // Calculate next position of the piece to check.
  255.  
  256.                 if (isWithinBounds(nextPositionToCheck))
  257.                 {
  258.                     nextPiece = _board.AllPieces[(int)nextPositionToCheck.x, (int)nextPositionToCheck.y];
  259.                 }
  260.  
  261.                 if (nextPiece == null || nextPiece._type != _type) // In case of hitting the edge of the board. Or empty spot.
  262.                 {
  263.                     break;
  264.                 }
  265.                 else if (!directionalList.Contains(nextPiece))
  266.                 {
  267.                     directionalList.Add(nextPiece);
  268.                 }
  269.             }
  270.            
  271.             if (counter % 2 == 0) // So we seperate horizontal and vertical matches. Otherwise the count of matches gives nasty sideffects.
  272.             {
  273.                 if (directionalList.Count >= minimumMatches)
  274.                 {
  275.                     allMatches = allMatches.Union(directionalList).ToList();
  276.                     directionalList.Clear();
  277.                 }
  278.  
  279.                 directionalList.Clear();
  280.             }
  281.         }
  282.  
  283.         if (allMatches.Count >= minimumMatches)
  284.         {
  285.             return allMatches;
  286.         }
  287.         else
  288.         {
  289.             return null;
  290.         }
  291.     }
  292.  
  293.     bool isWithinBounds(Vector3 position)
  294.     {
  295.         if (position.x > _board.Width - 1 || position.x < 0 || position.y > _board.Height - 1 || position.y < 0 || position.z != 0)
  296.         {
  297.             return false;
  298.         }
  299.  
  300.         return true;
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement