// Update is called once per frame void Update() { //Sort through trucks so that they are kept in the right sets, active and idle List <EGFiretruck> toMoveToIdle = new List <EGFiretruck> (); for (int i = 0; i < _dispatcher.GetActiveTrucks().Count; i++) { EGFiretruck truck = _dispatcher.GetActiveTruckAtIndex(i); if (!truck.IsActive() && !truck.IsPuttingOutFire()) { //Don't remove them while iterating, remove afterwards toMoveToIdle.Add(truck); } } //Set the trucks that need setting for (int i = 0; i < toMoveToIdle.Count; i++) { EGFiretruck truck = toMoveToIdle[i]; SetTruckIdle(truck); } //Identify trucks that need to be removed List <EGFiretruck> toDestroy = new List <EGFiretruck> (); for (int i = 0; i < _dispatcher.GetIdleTrucks().Count; i++) { EGFiretruck truck = _dispatcher.GetIdleTruckAtIndex(i); TDTile truckTile = _map.GetTileForWorldPosition(truck.transform.position); //Trucks that are idle at the firehouse need to be removed if (truckTile.type == TDTile.Type.FIREHOUSE) { toDestroy.Add(truck); } } //Remove trucks that need to be removed for (int i = 0; i < toDestroy.Count; i++) { EGFiretruck truck = toDestroy[i]; _dispatcher.RemoveIdleTruck(truck); _firehouse.DecreaseTruckCount(); Destroy(truck.gameObject); } }
// Update is called once per frame void Update() { if (truck.IsActive() && !truck.IsWaitingForTraffic() && !truck.IsPuttingOutFire()) { if (waterStream != null) { Destroy(waterStream); waterStream = null; truck.SetPuttingOutFire(false); truck.TargetFlame = null; } } else if (!truck.IsActive() || truck.IsWaitingForTraffic()) { if (foundFlames.Count > 0) { truck.SetPuttingOutFire(true); if (waterStream == null) { waterStream = (GameObject)Instantiate(waterStreamPrefab); waterStream.transform.position = transform.position; } for (int i = foundFlames.Count - 1; i >= 0; i--) { if (foundFlames[i] == null) { foundFlames.RemoveAt(i); } } GameObject closestFlame = null; float shortestDist = 100; for (int i = 0; i < foundFlames.Count; i++) { GameObject flame = foundFlames[i]; if (closestFlame == null) { closestFlame = flame; shortestDist = Vector3.Distance(transform.position, closestFlame.transform.position); } else { float dist = Vector3.Distance(transform.position, flame.transform.position); if (dist < shortestDist) { closestFlame = flame; shortestDist = dist; } } } if (closestFlame != null) { truck.TargetFlame = closestFlame; float dist = Vector3.Distance(transform.position, closestFlame.transform.position); Vector3 streamScale = waterStream.transform.localScale; streamScale.x = dist; waterStream.transform.localScale = streamScale; float angle = Mathf.Atan2(closestFlame.transform.position.z - transform.position.z, closestFlame.transform.position.x - transform.position.x); angle = angle * Mathf.Rad2Deg; //Convert to local coordinate system angle = 90 - angle; float localizedCurrentAngle = waterStream.transform.eulerAngles.y; angle = angle - localizedCurrentAngle; angle = (angle + 360) % 360; if (angle > 180) { angle = angle - 360; } angle = angle - 90; waterStream.transform.Rotate(0, angle, 0); Vector3 flameScale = closestFlame.transform.localScale; flameScale.x -= waterRate * Time.deltaTime; flameScale.y -= waterRate * Time.deltaTime; flameScale.z -= waterRate * Time.deltaTime; closestFlame.transform.localScale = flameScale; if (flameScale.x <= waterRate) { EGFlame egFlame = closestFlame.transform.gameObject.GetComponent <EGFlame> (); if (egFlame != null) { egFlame.PutOut(); } Destroy(closestFlame.transform.gameObject); foundFlames.Remove(closestFlame); truck.SetPuttingOutFire(false); truck.TargetFlame = null; } } } else { if (waterStream != null) { Destroy(waterStream); waterStream = null; } truck.SetPuttingOutFire(false); truck.TargetFlame = null; } } else { truck.SetPuttingOutFire(false); truck.TargetFlame = null; } }