Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Block : MonoBehaviour
- {
- public GridPos Position; // Our current grid position
- private const float moveTime = 0.2f;
- private bool isMoving;
- void Update()
- {
- 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?
- {
- StartCoroutine(MoveDown()); // Begin moving one cell down
- }
- }
- public void Matched()
- {
- Map.Instance.Layout[Position] = null; // Set our current position as occupied
- Destroy(gameObject); // Destroy this block
- }
- IEnumerator MoveDown()
- {
- isMoving = true; // We're moving!
- GridPos endPosGrid = new GridPos(Position.X, Position.Y-1);
- Vector3 startPos = transform.position;
- Vector3 endPos = Map.Instance.GridToWorld(endPosGrid);
- Map.Instance.Layout[Position] = null; // Mark our current position as unoccupied
- Map.Instance.Layout[endPosGrid] = this; // Mark our new position as occupied
- Position = endPosGrid;
- // Visually move to our new position over a set time
- float t = 0;
- while (t < moveTime)
- {
- transform.position = Vector3.MoveTowards(startPos, endPos, t/moveTime); // Lerp our position
- t += Time.deltaTime;
- yield return null;
- }
- transform.position = endPos; // Fix overshoot
- isMoving = false; // We're done moving
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement