Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MapCoordinateUtility {
- public static Vector2Int getTileCoordinates(Vector3 pos) {
- int x, y;
- x = Mathf.RoundToInt(pos.x / 1.5f);
- if (x % 2 == 0) {
- y = Mathf.RoundToInt((pos.z - 0.866f) / 1.732f);
- } else {
- y = Mathf.RoundToInt(pos.z / 1.732f);
- }
- return new Vector2Int(x, y);
- }
- public static Vector3 getTileCoordinates(int x, int y) {
- Vector3 newCoords = new Vector3(1.5f * (float)x, 0f, 1.732f * (float)y);
- if (x % 2 == 0) {
- newCoords += new Vector3(0f, 0f, 0.866f);
- }
- return newCoords;
- }
- public static Vector3 getTileCoordinates(Vector2Int coords) {
- return getTileCoordinates(coords.x, coords.y);
- }
- public static Vector3 getMapLocationFromScreenPoint(Camera camera, Vector3 screenCoords) {
- //https://answers.unity.com/questions/269760/ray-finding-out-x-and-z-coordinates-where-it-inter.html
- Ray ray = camera.ScreenPointToRay(screenCoords);
- //Create a plane at 0,0,0 whose normal points to +Y:
- Plane hPlane = new Plane(Vector3.up, Vector3.zero);
- //Plane.Raycast stores the distance from ray.origin to the hit point in this variable:
- float distance = 0;
- //If the ray hits the plane...
- if (hPlane.Raycast(ray, out distance)) {
- // get the hit point:
- return ray.GetPoint(distance);
- }
- return Vector3.positiveInfinity;
- }
- public static Vector2Int[] getNeighborCoordsOfLocation(Vector2Int neighborCenter) {
- List<Vector2Int> neighborCoords = new List<Vector2Int>();
- //Directly Above
- Vector2Int directlyAbove = new Vector2Int(neighborCenter.x, neighborCenter.y + 1);
- neighborCoords.Add(directlyAbove);
- //Directly Below
- Vector2Int directlyBelow = new Vector2Int(neighborCenter.x, neighborCenter.y - 1);
- neighborCoords.Add(directlyBelow);
- //Upper Right
- Vector2Int upperRight;
- if (neighborCenter.x % 2 == 0) {
- upperRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y + 1);
- } else {
- upperRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y);
- }
- neighborCoords.Add(upperRight);
- //Lower Right
- Vector2Int lowerRight;
- if (neighborCenter.x % 2 == 0) {
- lowerRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y);
- } else {
- lowerRight = new Vector2Int(neighborCenter.x + 1, neighborCenter.y - 1);
- }
- neighborCoords.Add(lowerRight);
- //Upper Left
- Vector2Int upperLeft;
- if (neighborCenter.x % 2 == 0) {
- upperLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y + 1);
- } else {
- upperLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y);
- }
- neighborCoords.Add(upperLeft);
- //Lower Left
- Vector2Int lowerLeft;
- if (neighborCenter.x % 2 == 0) {
- lowerLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y);
- } else {
- lowerLeft = new Vector2Int(neighborCenter.x - 1, neighborCenter.y - 1);
- }
- neighborCoords.Add(lowerLeft);
- return neighborCoords.ToArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement