Guest User

GridController.cs

a guest
Nov 8th, 2014
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class GridController : MonoBehaviour {
  8.  
  9.     float worldX = 0.4F;
  10.     float worldY = 0.4F;
  11.     float worldZ = 0.4F;   
  12.     bool IsGenerated = false;
  13.     List<GameObject> gridCubesList = new List<GameObject>();
  14.     float snap = 0.2F;
  15.     public GameObject prefab;
  16.     List<GameObject> freeCubesList = new List<GameObject>();
  17.     List<GameObject> GroundObjectCubesList = new List<GameObject>();
  18.     List<GameObject> possibleCubesList = new List<GameObject>();
  19.     List<Vector3> localPositionsList = new List<Vector3>();
  20.     List<Vector3> globalPositionsList = new List<Vector3>();
  21.     Vector3[] globalPositions;
  22.     List<float> distanceList = new List<float>();
  23.     int minIndex; // Shortest distance
  24.  
  25.     // Use this for initialization
  26.     void Start () {
  27.    
  28.     }
  29.    
  30.     // Update is called once per frame
  31.     void Update () {
  32.    
  33.     }
  34.    
  35.     // Generate grid given point as its 0, 0, 0 (rounded to 0.2)
  36.     public void Generate (Vector3 hit){
  37.         this.transform.position = hit;
  38.         Vector3 position = this.transform.position;
  39.         position.x = Mathf.Round(position.x / snap) * snap;
  40.         position.y = Mathf.Round(position.y / snap) * snap;
  41.         position.z = Mathf.Round(position.z / snap) * snap;
  42.         this.transform.position = position;
  43.         Vector3 gridStart = transform.position - new Vector3(worldX / 2, worldY / 2, worldZ / 2);
  44.         for(float x =0F; x<worldX; x += snap) {
  45.             for(float y =0F; y<worldY; y += snap) {
  46.                 for(float z =0F; z<worldZ; z += snap) {
  47.                     GameObject gridCube = Instantiate(prefab) as GameObject;
  48.                     gridCube.transform.position = new Vector3(gridStart.x + x, gridStart.y + y, gridStart.z + z);
  49.                     gridCubesList.Add(gridCube);
  50.                 }
  51.             }
  52.         }
  53.         IsGenerated = true;
  54.     }
  55.  
  56.     // Get closest free point in grid
  57.     public Vector3 snapPoint (GameObject GroundObject){
  58.         GroundObject.transform.position = this.transform.position;
  59.         foreach (GameObject gridCube in gridCubesList){
  60.             if(gridCube != null){
  61.                 gridTrigger trigger = gridCube.GetComponent<gridTrigger>();
  62.                 if(trigger.isTriggered(GroundObject)){
  63.                     localPositionsList.Add(this.transform.position - gridCube.transform.position);
  64.                     // EHTO EI TÄYTY etsi ongelma
  65.                     GroundObjectCubesList.Add(gridCube);
  66.                 }
  67.                 if(trigger.isTriggered(null) != true){
  68.                     if(gridCube != null){
  69.                     freeCubesList.Add(gridCube);
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         foreach (GameObject current in freeCubesList){
  75.             foreach(Vector3 localPosition in localPositionsList){
  76.                         foreach (GameObject freeCube in freeCubesList){
  77.                     Vector3 globalPosition = freeCube.transform.position + localPosition;
  78.                     if(globalPosition == freeCube.transform.position){
  79.                         possibleCubesList.Add(freeCube);
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         foreach (GameObject possibleCube in possibleCubesList){
  85.             distanceList.Add(Vector3.Distance(possibleCube.transform.position, transform.position));
  86.         }
  87.     //  float minVal = distanceList.Min();
  88.     //  minIndex = distanceList.IndexOf(minVal);
  89.         //minIndex = distanceList.IndexOf(distanceList.Min());
  90.         //return possibleCubes[minIndex].transform.position;
  91.         return Vector3.zero;
  92.     }
  93.    
  94.     public void Clear (){
  95.         foreach(GameObject clear in gridCubesList){
  96.             Destroy(clear);
  97.         }
  98.         gridCubesList = new List<GameObject>();
  99.         freeCubesList = new List<GameObject>();
  100.         GroundObjectCubesList = new List<GameObject>();
  101.         possibleCubesList = new List<GameObject>();
  102.         localPositionsList = new List<Vector3>();
  103.         globalPositionsList = new List<Vector3>();
  104.         distanceList = new List<float>();
  105.     }
  106. }
RAW Paste Data