Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 7th, 2013  |  syntax: C#  |  size: 1.43 KB  |  hits: 110  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Block : MonoBehaviour
  5. {
  6.         public GridPos Position; // Our current grid position
  7.        
  8.         private const float moveTime = 0.2f;
  9.         private bool isMoving;
  10.        
  11.         void Update()
  12.         {
  13.                 if (Position.Y > 0 && Map.Instance.Layout[Position+GridPos.Down] == null && !isMoving) // Are we above the first cell in our column, is the cell below us unoccupied and we are not currently moving?
  14.                 {
  15.                         StartCoroutine(MoveDown()); // Begin moving one cell down
  16.                 }
  17.         }
  18.        
  19.         public void Matched()
  20.         {
  21.                 Map.Instance.Layout[Position] = null; // Set our current position as occupied
  22.                 Destroy(gameObject); // Destroy this block
  23.         }
  24.        
  25.         IEnumerator MoveDown()
  26.         {
  27.                 isMoving = true; // We're moving!
  28.                
  29.                 GridPos endPosGrid = new GridPos(Position.X, Position.Y-1);
  30.                 Vector3 startPos = transform.position;
  31.                 Vector3 endPos = Map.Instance.GridToWorld(endPosGrid);
  32.                
  33.                 Map.Instance.Layout[Position] = null; // Mark our current position as unoccupied
  34.                 Map.Instance.Layout[endPosGrid] = this; // Mark our new position as occupied
  35.                 Position = endPosGrid;
  36.                
  37.                 // Visually move to our new position over a set time
  38.                 float t = 0;
  39.                 while (t < moveTime)
  40.                 {
  41.                         transform.position = Vector3.MoveTowards(startPos, endPos, t/moveTime); // Lerp our position
  42.                         t += Time.deltaTime;
  43.                         yield return null;
  44.                 }
  45.                
  46.                 transform.position = endPos; // Fix overshoot
  47.                 isMoving = false; // We're done moving
  48.         }
  49. }