private void OnCollisionEnter(Collision other) { switch (other.gameObject.tag) { case "Avatar": _avaColl = true; break; case "Shell": break; case "Meteor": _behaviour?.Stop(); break; case "Car": break; case "Road": break; default: throw new ArgumentException("Wrong tag: " + other.gameObject.tag); } _behaviour?.HandleCollisionEnter(other); }
// check lights during an emergency private void CheckMoveInEmergency() { for (int i = _vehiclesOnZ.Count - 1; i >= 0; i--) { VehicleBehaviour vehicle = _vehiclesOnZ[i]; foreach (LightStopZ lightStop in GetComponentsInChildren <LightStopZ>()) { if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && lightStop.gameObject.name.Equals("East to West") && zEast.greenLight.activeSelf) { vehicle.SetNextRoad(); vehicle.Continue(); _vehiclesOnZ.Remove(vehicle); break; } if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && lightStop.gameObject.name.Equals("West to East") && zWest.greenLight.activeSelf) { vehicle.SetNextRoad(); vehicle.Continue(); _vehiclesOnZ.Remove(vehicle); break; } vehicle.Stop(); } } for (int i = _vehiclesOnX.Count - 1; i >= 0; i--) { VehicleBehaviour vehicle = _vehiclesOnX[i]; foreach (LightStopX lightStop in GetComponentsInChildren <LightStopX>()) { if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && lightStop.gameObject.name.Equals("South to North") && xSouth.greenLight.activeSelf) { vehicle.SetNextRoad(); vehicle.Continue(); _vehiclesOnX.Remove(vehicle); break; } if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && lightStop.gameObject.name.Equals("North to South") && xNorth.greenLight.activeSelf) { vehicle.SetNextRoad(); vehicle.Continue(); _vehiclesOnX.Remove(vehicle); break; } vehicle.Stop(); } } }
private void OnTriggerEnter(Collider other) { if (other.gameObject.GetComponent <CarBackCollider>() == null) { return; } VehicleBehaviour vehicle = other.gameObject.GetComponentInParent <VehicleBehaviour>(); if (vehicle == null) { return; } if (ReferenceEquals(vehicle, _parent)) { return; } _parent.Stop(); }
// move vehicles if able private void MoveVehicles() { for (int i = _vehiclesTurning.Count - 1; i >= 0; i--) { VehicleBehaviour vehicle = _vehiclesTurning[i]; if (Handler.IsSomethingOnFire && vehicle.CompareTag("firebrigade")) { vehicle.SetNextRoad(); vehicle.Continue(); _vehiclesTurning.Remove(vehicle); continue; } if (vehicle.NextRoad == null) { vehicle.SetNextRoad(); } if (vehicle.NextRoad != null) { if (vehicle.NextRoad.GetComponent <WaypointPath>().GetCongestion() > vehicle.NextRoad.GetComponent <WaypointPath>().CongestionThreshold) { vehicle.SetNextRoad(); } } if (vehicle.IsUnableToMove) { vehicle.Stop(); } else if (vehicle.LeftJunctionLeave || vehicle.IsGoingStraightAtJunction) { _vehiclesTurning.Remove(vehicle); } else if (vehicle.RightJunctionCrossing) { if (_rightLane.TrafficInLane) { vehicle.Stop(); } else { vehicle.Continue(); _vehiclesTurning.Remove(vehicle); } } else if (vehicle.RightJunctionJoin) { if (_rightLane.TrafficInLane || _leftLane.TrafficInLane) { vehicle.Stop(); } else { vehicle.Continue(); _vehiclesTurning.Remove(vehicle); } } else if (vehicle.LeftJunctionJoin) { if (_rightLane.TrafficInLane) { vehicle.Stop(); } else { vehicle.Continue(); _vehiclesTurning.Remove(vehicle); } } } }
// move traffic on Z if the coast is clear private void MoveTrafficOnZ() { int vehiclesTurningRight = 0; for (int i = _vehiclesOnZ.Count - 1; i >= 0; i--) { VehicleBehaviour vehicle = _vehiclesOnZ[i]; if (vehicle.NextRoad == null) { vehicle.SetNextRoad(); } if (vehicle.NextRoad != null) { if (vehicle.NextRoad.GetComponent <WaypointPath>().GetCongestion() > vehicle.NextRoad.GetComponent <WaypointPath>().CongestionThreshold) { print(vehicle.gameObject.name + " setting another road on " + gameObject.name); vehicle.SetNextRoad(); } } if (vehicle.IsUnableToMove) { vehicle.Stop(); } else if (vehicle.IsGoingStraightAtCross || vehicle.LeftCross || vehicle.IsGoingStraightAtJunction || vehicle.LeftJunctionLeave) { foreach (LightStopZ lightStop in GetComponentsInChildren <LightStopZ>()) { if (!ReferenceEquals(lightStop.VehicleAtLight, vehicle)) { continue; } if (((vehicle.IsGoingStraightAtCross || vehicle.IsGoingStraightAtJunction) && !lightStop.StraightOn.TrafficInLane && !lightStop.Front.TrafficInLane) || ((vehicle.LeftCross || vehicle.LeftJunctionLeave) && !lightStop.LeftTurn.TrafficInLane && !lightStop.Front.TrafficInLane) ) { vehicle.Continue(); _vehiclesOnZ.Remove(vehicle); } else { vehicle.Stop(); } } } else { foreach (LightStopZ lightStop in GetComponentsInChildren <LightStopZ>()) { if (lightStop.CrossLane == null) { continue; } if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && !lightStop.CrossLane.TrafficInLane && !lightStop.RightTurn.TrafficInLane) { vehicle.Continue(); _vehiclesOnZ.Remove(vehicle); break; } if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && lightStop.CrossLane.TrafficInLane && !lightStop.RightTurn.TrafficInLane) { vehiclesTurningRight++; vehicle.Stop(); } else { vehicle.Stop(); } } } } if (vehiclesTurningRight != 2) { return; } for (int i = _vehiclesOnZ.Count - 1; i >= 0; i--) { VehicleBehaviour vehicle = _vehiclesOnZ[i]; vehicle.Continue(); _vehiclesOnZ.Remove(vehicle); } }
// move traffic on X if the coast is clear private void MoveTrafficOnX() { int vehiclesTurningRight = 0; for (int i = _vehiclesOnX.Count - 1; i >= 0; i--) { VehicleBehaviour vehicle = _vehiclesOnX[i]; if (vehicle.NextRoad == null && !vehicle.IsUnableToMove) { vehicle.SetNextRoad(); } if (vehicle.NextRoad != null) { if (vehicle.NextRoad.GetComponent <WaypointPath>().GetCongestion() > vehicle.NextRoad.GetComponent <WaypointPath>().CongestionThreshold) { vehicle.SetNextRoad(); } } if (vehicle.IsUnableToMove) { vehicle.Stop(); } if (!vehicle.RightCross) { foreach (LightStopX lightStop in GetComponentsInChildren <LightStopX>()) { if (!ReferenceEquals(lightStop.VehicleAtLight, vehicle)) { continue; } if ((vehicle.IsGoingStraightAtCross && !lightStop.StraightOn.TrafficInLane && !lightStop.Front.TrafficInLane) || (vehicle.LeftCross && !lightStop.LeftTurn.TrafficInLane && !lightStop.Front.TrafficInLane) || (vehicle.LeftJunctionJoin && !lightStop.LeftTurn.TrafficInLane) || (vehicle.RightJunctionJoin && !lightStop.RightTurn.TrafficInLane) ) { vehicle.Continue(); _vehiclesOnX.Remove(vehicle); } else { vehicle.Stop(); } } } else { foreach (LightStopX lightStop in GetComponentsInChildren <LightStopX>()) { if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && !lightStop.CrossLane.TrafficInLane && !lightStop.RightTurn.TrafficInLane) { vehicle.Continue(); _vehiclesOnX.Remove(vehicle); break; } if (ReferenceEquals(lightStop.VehicleAtLight, vehicle) && lightStop.CrossLane.TrafficInLane && !lightStop.RightTurn.TrafficInLane) { vehiclesTurningRight++; vehicle.Stop(); } else { vehicle.Stop(); } } } } if (vehiclesTurningRight != 2) { return; } for (int i = _vehiclesOnX.Count - 1; i >= 0; i--) { VehicleBehaviour vehicle = _vehiclesOnX[i]; vehicle.Continue(); _vehiclesOnX.Remove(vehicle); } }