void InitGameObjects() { System.Diagnostics.Stopwatch methodWatch = new System.Diagnostics.Stopwatch(); /*System.Diagnostics.Stopwatch instantiateWatch = new System.Diagnostics.Stopwatch(); * System.Diagnostics.Stopwatch transformWatch = new System.Diagnostics.Stopwatch(); * System.Diagnostics.Stopwatch parentingWatch = new System.Diagnostics.Stopwatch();*/ methodWatch.Start(); UnityTools.Instance.CleanChildren(gameObject); float gameObjectScale = GetWaterTileObjectScale(); for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { float posX = x * gameObjectScale - (levelTileSize / 2.0f) + (gameObjectScale / 2.0f) + gameObject.transform.position.x; float posY = y * gameObjectScale - (levelTileSize / 2.0f) + (gameObjectScale / 2.0f) + gameObject.transform.position.y; Vector3 pos = new Vector3(posX, posY, 1); //instantiateWatch.Start(); GameObject nextWaterObject = Instantiate(waterTileObject) as GameObject; //instantiateWatch.Stop(); //transformWatch.Start(); nextWaterObject.transform.localScale = new Vector3(gameObjectScale, gameObjectScale); nextWaterObject.transform.position = pos; nextWaterObject.collider2D.enabled = true; //transformWatch.Stop(); //parentingWatch.Start(); // nextWaterObject.transform.parent = gameObject.transform; //parentingWatch.Stop(); TWaterTile waterComponent = nextWaterObject.GetComponent <TWaterTile>(); if (waterComponent) { waterComponent.posX = x; waterComponent.posY = y; waterComponent.SetParent(this); } else { Debug.LogError("Error - WaterTile has no attached TWaterTile script!"); } waterGameObjects[x, y] = nextWaterObject; } } methodWatch.Stop(); /*Debug.Log ("Instantiate() took " + instantiateWatch.ElapsedMilliseconds + "ms for " + (mapHeight * mapWidth) + " objects."); * Debug.Log ("Transform took " + transformWatch.ElapsedMilliseconds + "ms for " + (mapHeight * mapWidth) + " objects."); * Debug.Log ("Parenting took " + parentingWatch.ElapsedMilliseconds + "ms for " + (mapHeight * mapWidth) + " objects.");*/ Debug.Log("TFollowerWater - InitGameObjects() with resolution:" + waterTilesPerTile + " took " + methodWatch.ElapsedMilliseconds + "ms."); }
void TestMouseControl() { if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2)) { Vector2 mousePos = Input.mousePosition; Ray ray = Camera.main.ScreenPointToRay(mousePos); RaycastHit2D[] hits = Physics2D.GetRayIntersectionAll(ray, 20.0f); foreach (RaycastHit2D hit in hits) { if (hit.collider) { if (hit.collider.tag == "Water") { TWaterTile waterComponent = hit.collider.gameObject.GetComponent <TWaterTile>(); if (waterComponent) { // Left mouse button if (Input.GetMouseButton(0)) { AddWaterToWaterTile(waterComponent.posX, waterComponent.posY); } // Right mouse button if (Input.GetMouseButton(1)) { SetCollisionAtTile(waterComponent.posX, waterComponent.posY, true); } // Middle mouse button if (Input.GetMouseButton(2)) { SetCollisionAtTile(waterComponent.posX, waterComponent.posY, false); } } else { Debug.LogError("Error - WaterTile has no attached TWaterTile component!"); } } } } } if (Input.GetKeyDown(KeyCode.M)) { UpdateCollisionMap(); } }
void UpdateCollisionMap() { Debug.Log("TFollowerWater - UpdateCollisionMap()"); foreach (GameObject waterTileObject in waterGameObjects) { Vector3 centerOfTileObject = waterTileObject.collider2D.renderer.bounds.center; centerOfTileObject.z -= 10.0f; TWaterTile waterComponent = waterTileObject.GetComponent <TWaterTile>(); Ray ray = new Ray(centerOfTileObject, Vector3.forward); RaycastHit2D[] hits = Physics2D.GetRayIntersectionAll(ray, 20.0f); // Debug.Log ("Center: " + centerOfTileObject + " hit: " + (hits.Length > 0 ? "YES" : "NO")); bool hitCollision = false; foreach (RaycastHit2D hit in hits) { if (hit.collider) { // all Tags defined as collisions for water if (hit.collider.tag == "Wall") { hitCollision = true; break; } } } int posArrayX = waterComponent.posX + 1; int posArrayY = waterComponent.posY + 1; if (hitCollision) { waterField[posArrayX, posArrayY] = COLLISION; mass[posArrayX, posArrayY] = 0.0f; } else { if (waterField[posArrayX, posArrayY] == COLLISION) { waterField[posArrayX, posArrayY] = EMPTY; } } } }
void DrawWaterMapOnTiles() { for (int x = 1; x <= mapWidth; x++) { for (int y = 1; y <= mapHeight; y++) { if (massChanged[x, y] == true) { TWaterTile waterTileObject = waterGameObjects[x - 1, y - 1].GetComponent <TWaterTile>(); if (waterTileObject) { if (waterField[x, y] == COLLISION) { if (debug) { waterTileObject.ShowCollision(); } else { waterTileObject.ClearDisplay(); } } else { if (mass[x, y] > 0.0f) { waterTileObject.DisplayMass(mass[x, y]); } else { //waterTileObject.ShowEmpty(); waterTileObject.ClearDisplay(); } } } else { Debug.LogError("TWaterTile script is not attached to child objects!"); break; } } } } }
// Update is called once per frame void Update() { if (deathComp && deathComp.IsDead()) { if (audio && audio.isPlaying) { audio.Stop(); } } if (activated) { if (startWaterAfterSec > 0.0f) { startWaterAfterSec -= Time.deltaTime; } else { if (playsSound && !audio.isPlaying) { audio.Play(); } if (mainWaterObject) { secSinceLastWaterTick += Time.deltaTime; if (secSinceLastWaterTick >= secBetweenWaterTicks) { for (int times = 0; times < waterMassTimesPerTick; times++) { Vector2 target = gameObject.transform.position; target.x += waterTargetPoint.x; target.y += waterTargetPoint.y; Collider2D[] hits = Physics2D.OverlapCircleAll(target, waterTargetRadius); List <Collider2D> waterHits = new List <Collider2D>(); foreach (Collider2D hit in hits) { if (hit.tag.Equals("Water")) { waterHits.Add(hit); } } for (int i = 0; i < waterHits.Count; i++) { int randomWaterTile = Random.Range(0, waterHits.Count); Collider2D choosenTile = waterHits[randomWaterTile]; if (choosenTile) { TWaterTile waterComponent = choosenTile.gameObject.GetComponent <TWaterTile>(); TFollowerWater tileWaterComponent = mainWaterObject.GetComponent <TFollowerWater>(); if (tileWaterComponent && waterComponent) { if (waterComponent.waterMass <= 1.0f) { tileWaterComponent.AddWaterToWaterTile(waterComponent.posX, waterComponent.posY, waterMassPerTick); } } } } } secSinceLastWaterTick = 0; } } else { print("TileWaterObject not set!"); } } } }