public void Initialize() { _cells = new List <AsteroidCell>(); for (int i = 0; i < 10; i++) { GameObject anchorObject = GameObject.Instantiate(Resources.Load("AsteroidCellAnchor")) as GameObject; anchorObject.name = "AsteroidCell" + i; anchorObject.transform.position = new Vector3(100000, 100000, 100000); AsteroidCell cell = anchorObject.GetComponent <AsteroidCell>(); cell.Initialize(this); cell.PopulateAsteroids(); _cells.Add(cell); } }
// Update is called once per frame public void PerFrameUpdate() { Vector3 playerPos = GameManager.Inst.PlayerControl.PlayerShip.transform.position; if (StaticUtility.IsInArea(playerPos, transform.position, new Vector3(Size.x / 2 + 0.5f, Size.y / 2 + 0.5f, Size.z / 2 + 0.5f) * CellSize)) { List <Vector3> nearestCoords = FindNearestCellCoords(); foreach (Vector3 coord in nearestCoords) { Vector3 cellPos = transform.position + coord * CellSize; if (!IsCoordTaken(cellPos) && Mathf.Abs(coord.x * 2) <= Size.x && Mathf.Abs(coord.y * 2) <= Size.y && Mathf.Abs(coord.z * 2) <= Size.z) { AsteroidCell cell = GetAvailableCell(playerPos); if (cell != null) { cell.transform.position = cellPos; } } } } else { List <Vector3> edgeCoords = FindHorizontalEdgeCoords(); Vector3 centerCellPos = edgeCoords[0] * CellSize + transform.position; int cellIndex = 0; foreach (Vector3 coord in edgeCoords) { Vector3 cellPos = transform.position + coord * CellSize; //check if the coord is along the edge of the field if (!IsCoordTaken(cellPos) && IsCoordOnEdge(coord)) { //Debug.Log(coord); AsteroidCell cell = _cells[cellIndex]; if (cell != null && cellIndex <= 9) { cell.transform.position = cellPos; cellIndex++; } } } } //check how close player is to the border to calculate a value between 0 and 1 //to set fog intensity, fog color, and sun flare size _gradient = 0; Vector3 playerDist = playerPos - transform.position; if (StaticUtility.IsInArea(playerPos, transform.position, new Vector3(Size.x * CellRadius - FogStart, Size.y * CellRadius - FogStart, Size.z * CellRadius - FogStart))) { _gradient = 1; } else { if (Mathf.Abs(playerDist.x) > Size.x * CellRadius - FogStart) { _gradient = Mathf.Clamp01((Size.x * CellRadius - Mathf.Abs(playerDist.x)) / FogStart); } else if (Mathf.Abs(playerDist.y) > Size.y * CellRadius - FogStart) { _gradient = Mathf.Clamp01((Size.y * CellRadius - Mathf.Abs(playerDist.y)) / FogStart); } else if (Mathf.Abs(playerDist.z) > Size.z * CellRadius - FogStart) { _gradient = Mathf.Clamp01((Size.z * CellRadius - Mathf.Abs(playerDist.z)) / FogStart); } } //set fog intensity RenderSettings.fogDensity = Mathf.Lerp(0.0002f, FogIntensity, _gradient); RenderSettings.fogColor = Color.Lerp(new Color(0.01f, 0.015f, 0.02f, 1f), FogColor, _gradient); //set sun intensity foreach (Sun sun in GameManager.Inst.WorldManager.CurrentSystem.Suns) { if (sun.Flare != null) { sun.Flare.brightness = Mathf.Lerp(0.6f, 0, _gradient); } sun.Sunlight.intensity = Mathf.Lerp(1, SunIntensity, _gradient); } //set ambient RenderSettings.ambientLight = Color.Lerp(GameManager.Inst.WorldManager.CurrentSystem.AmbientColor, AmbientColor, _gradient); }