Last active
February 20, 2016 15:43
-
-
Save twobob/ed888d4e1d1bc4292adb to your computer and use it in GitHub Desktop.
3x3 UpdateGridOnTrigger example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class UpdateGridOnTrigger : MonoBehaviour { | |
MonoBehaviour script; | |
readonly static float TerrainSize = 256f; // set this | |
void Start(){ | |
// Get script references | |
script = GetComponent<MoveWhenFar>(); // Refactor to a single static reference perhaps | |
} | |
bool isContainedInList(GameObject obj, List<GameObject> list){ | |
return list.Contains(obj); | |
} | |
void OnTriggerEnter(){ | |
// do this based on Lists | |
// North / South, East / West Lists | |
/* | |
* check the lists - | |
* Prefer North / South over East West (doesn't matter) | |
* Corners are now implicitly handled by simply testing both lists | |
*/ | |
// North | |
if (isContainedInList(gameObject, MoveWhenFar.NorthTiles)) { | |
// Move south tiles North and update the tags. | |
for (int i = 0; i < MoveWhenFar.SouthTiles.Count; i++) { | |
MoveWhenFar.SouthTiles[i].transform.position = new Vector3( | |
MoveWhenFar.SouthTiles[i].transform.position.x, | |
MoveWhenFar.SouthTiles[i].transform.position.y, | |
MoveWhenFar.SouthTiles[i].transform.position.z + (3* TerrainSize)); | |
// Center | |
} | |
//North | |
} | |
// South | |
if (isContainedInList(gameObject, MoveWhenFar.SouthTiles)) { | |
// Move north tiles South and update the tags. | |
for (int i = 0; i < MoveWhenFar.NorthTiles.Count; i++) { | |
MoveWhenFar.NorthTiles[i].transform.position = new Vector3( | |
MoveWhenFar.NorthTiles[i].transform.position.x, | |
MoveWhenFar.NorthTiles[i].transform.position.y, | |
MoveWhenFar.NorthTiles[i].transform.position.z - (3* TerrainSize)); | |
} | |
//South | |
} | |
// East | |
if (isContainedInList(gameObject, MoveWhenFar.EastTiles)) { | |
// Move West tiles East and update the tags. | |
for (int o = 0; o < MoveWhenFar.WestTiles.Count; o++) { | |
MoveWhenFar.WestTiles[o].transform.position = new Vector3( | |
MoveWhenFar.WestTiles[o].transform.position.x + (3* TerrainSize), | |
MoveWhenFar.WestTiles[o].transform.position.y, | |
MoveWhenFar.WestTiles[o].transform.position.z ); | |
} | |
//East | |
} | |
// West | |
if (isContainedInList(gameObject, MoveWhenFar.WestTiles)) { | |
// Move south tiles North and update the tags. | |
for (int i = 0; i < MoveWhenFar.EastTiles.Count; i++) { | |
MoveWhenFar.EastTiles[i].transform.position = new Vector3( | |
MoveWhenFar.EastTiles[i].transform.position.x - (3* TerrainSize), | |
MoveWhenFar.EastTiles[i].transform.position.y, | |
MoveWhenFar.EastTiles[i].transform.position.z ); | |
} | |
//West | |
} | |
if (script == null) | |
return; | |
script.SendMessage ("UpdateTilesLists", SendMessageOptions.DontRequireReceiver); | |
//Ontrigger | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment