Advertisement
Guest User

Unity Help

a guest
Apr 3rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. Group.cs
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. public class Group : MonoBehaviour {
  7.     // Time since last gravity tick
  8.     float lastFall = 0;
  9.  
  10.     void Start() {
  11.         // Default position not valid? Then it's game over
  12.         if (!isValidGridPos()) {
  13.             Debug.Log("GAME OVER");
  14.             Destroy(gameObject);
  15.         }
  16.     }
  17.  
  18.     void Update() {
  19.         // Move Left
  20.         if (Input.GetKeyDown(KeyCode.LeftArrow)) {
  21.             // Modify position
  22.             transform.position += new Vector3(-1, 0, 0);
  23.  
  24.             // See if valid
  25.             if (isValidGridPos())
  26.                 // It's valid. Update grid.
  27.                 updateGrid();
  28.             else
  29.                 // It's not valid. revert.
  30.                 transform.position += new Vector3(1, 0, 0);
  31.         }
  32.  
  33.         // Move Right
  34.         else if (Input.GetKeyDown(KeyCode.RightArrow)) {
  35.             // Modify position
  36.             transform.position += new Vector3(1, 0, 0);
  37.  
  38.             // See if valid
  39.             if (isValidGridPos())
  40.                 // It's valid. Update grid.
  41.                 updateGrid();
  42.             else
  43.                 // It's not valid. revert.
  44.                 transform.position += new Vector3(-1, 0, 0);
  45.         }
  46.  
  47.         // Rotate
  48.         else if (Input.GetKeyDown(KeyCode.UpArrow)) {
  49.             transform.Rotate(0, 0, -90);
  50.  
  51.             // See if valid
  52.             if (isValidGridPos())
  53.                 // It's valid. Update grid.
  54.                 updateGrid();
  55.             else
  56.                 // It's not valid. revert.
  57.                 transform.Rotate(0, 0, 90);
  58.         }
  59.  
  60.         // Move Downwards and Fall
  61.         else if (Input.GetKeyDown(KeyCode.DownArrow) ||
  62.                  Time.time - lastFall >= 1) {
  63.             // Modify position
  64.             transform.position += new Vector3(0, -1, 0);
  65.  
  66.             // See if valid
  67.             if (isValidGridPos()) {
  68.                 // It's valid. Update grid.
  69.                 updateGrid();
  70.             } else {
  71.                 // It's not valid. revert.
  72.                 transform.position += new Vector3(0, 1, 0);
  73.  
  74.                 // Clear filled horizontal lines
  75.                 Grid.deleteFullRows();
  76.  
  77.                 // Spawn next Group
  78.                 FindObjectOfType<Spawner>().spawnNext();
  79.  
  80.                 // Disable script
  81.                 enabled = false;
  82.             }
  83.  
  84.             lastFall = Time.time;
  85.         }
  86.     }
  87.  
  88.     bool isValidGridPos() {
  89.         foreach (Transform child in transform) {
  90.             Vector2 v = Grid.roundVec2(child.position);
  91.  
  92.             // Not inside Border?
  93.             if (!Grid.insideBorder(v))
  94.                 return false;
  95.  
  96.             // Block in grid cell (and not part of same group)?
  97.             if (Grid.grid[(int)v.x, (int)v.y] != null &&
  98.                 Grid.grid[(int)v.x, (int)v.y].parent != transform)
  99.                 return false;
  100.         }
  101.         return true;
  102.     }
  103.  
  104.     void updateGrid() {
  105.         // Remove old children from grid
  106.         for (int y = 0; y < Grid.h; ++y)
  107.             for (int x = 0; x < Grid.w; ++x)
  108.                 if (Grid.grid[x, y] != null)
  109.                     if (Grid.grid[x, y].parent == transform)
  110.                         Grid.grid[x, y] = null;
  111.  
  112.         // Add new children to grid
  113.         foreach (Transform child in transform) {
  114.             Vector2 v = Grid.roundVec2(child.position);
  115.             Grid.grid[(int)v.x, (int)v.y] = child;
  116.         }
  117.     }
  118. }
  119.  
  120. grid.cs
  121. using UnityEngine;
  122. using System.Collections;
  123.  
  124. public class Grid : MonoBehaviour {
  125.     // The Grid itself
  126.     public static int w = 10;
  127.     public static int h = 20;
  128.     public static Transform[,] grid = new Transform[w, h];
  129.  
  130.     public static Vector2 roundVec2(Vector2 v) {
  131.         return new Vector2(Mathf.Round(v.x),
  132.                            Mathf.Round(v.y));
  133.     }
  134.  
  135.     public static bool insideBorder(Vector2 pos) {
  136.         return ((int)pos.x >= 0 &&
  137.                 (int)pos.x < w &&
  138.                 (int)pos.y >= 0);
  139.     }
  140.  
  141.     public static void deleteRow(int y) {
  142.         for (int x = 0; x < w; ++x) {
  143.             Destroy(grid[x, y].gameObject);
  144.             grid[x, y] = null;
  145.         }
  146.     }
  147.  
  148.     public static void decreaseRow(int y) {
  149.         for (int x = 0; x < w; ++x) {
  150.             if (grid[x, y] != null) {
  151.                 // Move one towards bottom
  152.                 grid[x, y - 1] = grid[x, y];
  153.                 grid[x, y] = null;
  154.  
  155.                 // Update Block position
  156.                 grid[x, y - 1].position += new Vector3(0, -1, 0);
  157.             }
  158.         }
  159.     }
  160.  
  161.     public static void decreaseRowsAbove(int y) {
  162.         for (int i = y; i < h; ++i)
  163.             decreaseRow(i);
  164.     }
  165.  
  166.     public static bool isRowFull(int y) {
  167.         for (int x = 0; x < w; ++x)
  168.             if (grid[x, y] == null)
  169.                 return false;
  170.         return true;
  171.     }
  172.  
  173.     public static void deleteFullRows() {
  174.         for (int y = 0; y < h; ++y) {
  175.             if (isRowFull(y)) {
  176.                 deleteRow(y);
  177.                 decreaseRowsAbove(y + 1);
  178.                 --y;
  179.             }
  180.         }
  181.     }
  182. }
  183.  
  184. spawner.cs
  185. using UnityEngine;
  186. using System.Collections;
  187.  
  188. public class Spawner : MonoBehaviour {
  189.  
  190.     // Groups
  191.     public GameObject[] groups;
  192.  
  193.     public void spawnNext() {
  194.         // Random Index
  195.         int i = Random.Range(0, groups.Length);
  196.  
  197.         // Spawn Group at current Position
  198.         Instantiate(groups[i],
  199.                     transform.position,
  200.                     Quaternion.identity);
  201.     }
  202.  
  203.     void Start() {
  204.         // Spawn initial Group
  205.         spawnNext();
  206.     }
  207.  
  208.     // Update is called once per frame
  209.     void Update () {
  210.    
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement