To give the player a visualisation of where the largest amount of resources are located, we've created a heatmap. This will help the player choose the optimal location for the extractor building.
The heatmap script uses an array of vectors together with an intensity and radius variable for each point to calculate each point's contribution to the heat map.
Using a gradient texture the script will select a level of gradient depending on the variables for that point
In the code snippet below the arrays are created with the length of count, which is the amount of points. Then the script cycles through the points using a for loop. Each point is assigned a random position, radius and intensity.
positions = new Vector2[count]; radiuses = new float[count]; intensities= new float[count]; for (int i = 0; i < positions.Length; i++) { positions[i] = new Vector2(Random.Range(-scaleX, scaleX), Random.Range(-scaleY, scaleY)); radiuses[i] = Random.Range(radiusRangeMin, radiusRangeMax); intensities[i] = Random.Range(intensityMin, intensityMax); }
After the points have been calculated they're sent to the shader using SetVectorArray and SetFloatArray.
material.SetInt("_Points_Length", positions.Length); material.SetVectorArray("_Points", positions.Select(v => (Vector4)v).ToArray()); material.SetFloatArray("_Radius", radiuses); material.SetFloatArray("_intensity", intensities);
The GetIntensity method takes a vector2 position and returns the intensity of resources in that loaction by taking the intensity of all the points near the vector2 position.
This is the intensity that will decide the amount of resources that can be harvested from an extractor placed in that position.
public float GetIntensity(Vector2 pos) { float intensity = 0; for (int i = 0; i < positions.Length; i++) { if (Vector2.Distance(pos, positions[i]) < radiuses[i]) intensity += intensities[i]; } return intensity; }
0 kommentarer:
Send en kommentar