Skip to content

Instantly share code, notes, and snippets.

@twobob
Created May 8, 2015 23:46
Show Gist options
  • Save twobob/f77462efce7df5a8c0ad to your computer and use it in GitHub Desktop.
Save twobob/f77462efce7df5a8c0ad to your computer and use it in GitHub Desktop.
3x3 UpdateGridOnTrigger Supporting MoveWhenFar sorting logic
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
// could easily be refactored into a static class...
public class MoveWhenFar : MonoBehaviour {
readonly static float TerrainSize = 256f; // D R Y... refactor centrally...
GameObject[] floorTiles;
public static List<GameObject> NorthTiles;
public static List<GameObject> EastTiles;
public static List<GameObject> SouthTiles;
public static List<GameObject> WestTiles;
Vector3 middleOffset;
public static List<GameObject> CompleteTileList;
// used to subsort the lists.
List<GameObject> NSlist;
List<GameObject> EWlist;
// for debug
List<Color> TileColors;
// Use this for initialization
void Start () {
// For Debug only
TileColors = new List<Color>{Color.red, new Color(1f,.3f,0f), new Color(1f,.6f,0f), new Color(1f,.9f,0f),
new Color(1f,1f,.3f),new Color(1f,1f,.6f),new Color(1f,1f,1f)};
middleOffset = new Vector3(128f, 0f, 128f); // <-- in case you need to adjust pivots
floorTiles = GameObject.FindGameObjectsWithTag("Tile"); // <-- For simple identification, there should be 9 of them
NorthTiles = new List<GameObject>(3);
EastTiles = new List<GameObject>(3);
WestTiles = new List<GameObject>(3);
SouthTiles = new List<GameObject>(3);
CompleteTileList = new List<GameObject>(floorTiles);
NSlist = new List<GameObject>(
floorTiles
);
EWlist = new List<GameObject>(
floorTiles
);
//update lists
UpdateTilesLists ();
}
int maxTilesStackWidth = 3;
void UpdateTilesLists(){
// Get the SouthernMost
NSlist.Sort (delegate(GameObject c1, GameObject c2) {
return c1.transform.position.z.CompareTo (c2.transform.position.z); });
SouthTiles = NSlist.GetRange (0, 3);
// Get the NorthernMost
NSlist.Sort (delegate(GameObject c1, GameObject c2) {
return c2.transform.position.z.CompareTo (c1.transform.position.z); });
NorthTiles = NSlist.GetRange (0, 3);
EWlist.Sort (delegate(GameObject c1, GameObject c2) {
return c2.transform.position.x.CompareTo (c1.transform.position.x); });
EastTiles = EWlist.GetRange (0, 3);
EWlist.Sort (delegate(GameObject c1, GameObject c2) {
return c1.transform.position.x.CompareTo (c2.transform.position.x); });
WestTiles = EWlist.GetRange (0, 3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment