From 766cdff5ffa72b65d7f106658d1603f47739b2ba Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 27 Oct 2023 11:05:14 +0800 Subject: + init --- GameCode/GridVisualizer.cs | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 GameCode/GridVisualizer.cs (limited to 'GameCode/GridVisualizer.cs') diff --git a/GameCode/GridVisualizer.cs b/GameCode/GridVisualizer.cs new file mode 100644 index 0000000..ed73bf2 --- /dev/null +++ b/GameCode/GridVisualizer.cs @@ -0,0 +1,56 @@ +using UnityEngine; + +public class GridVisualizer : MonoBehaviour +{ + public Vector2 min; + + public Vector2 max; + + public GameObject prefab; + + public Vector2Int numberOfObjects; + + internal GridObject[,] spawnedObjects; + + public static GridVisualizer instance; + + private void Start() + { + instance = this; + SpawnGrid(); + } + + public void BulletCall(Vector2 worldSpacePosition) + { + Vector2Int gridPos = WorldToGridSpace(worldSpacePosition); + float power = Vector2.Distance(worldSpacePosition, GridToWorldSpace(gridPos)); + spawnedObjects[gridPos.x, gridPos.y].BopCall(power); + } + + public Vector2 GridToWorldSpace(int x, int y) + { + return GridToWorldSpace(new Vector2Int(x, y)); + } + + public Vector2 GridToWorldSpace(Vector2Int gridPos) + { + return new Vector2(Mathf.Lerp(min.x, max.x, (float)gridPos.x / (float)numberOfObjects.x), Mathf.Lerp(min.y, max.y, (float)gridPos.y / (float)numberOfObjects.y)); + } + + public Vector2Int WorldToGridSpace(Vector2 pos) + { + return new Vector2Int((int)(Mathf.InverseLerp(min.x, max.x, pos.x) * (float)numberOfObjects.x), (int)(Mathf.InverseLerp(min.y, max.y, pos.y) * (float)numberOfObjects.y)); + } + + private void SpawnGrid() + { + spawnedObjects = new GridObject[numberOfObjects.x, numberOfObjects.y]; + for (int i = 0; i < numberOfObjects.x; i++) + { + for (int j = 0; j < numberOfObjects.y; j++) + { + spawnedObjects[i, j] = Object.Instantiate(prefab, GridToWorldSpace(i, j), Quaternion.identity).GetComponent(); + } + } + } +} -- cgit v1.1-26-g67d0