python六边形网格,六边形寻路格子绘制

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class AdventureIsland : MonoBehaviour

{

static AdventureIsland instante;

public AdventureGround ground;

public Transform AdventureGridRoot;

public int width = 10;

public int height = 10;

public string ID;

public float gridSize = 0.2f;

public List adventureGrids = new List();

public static AdventureIsland Instante()

{

return instante;

}

public void Awake()

{

instante = this;

}

// Start is called before the first frame update

void Start()

{

}

// Update is called once per frame

void Update()

{

}

public void RefreshInEditor()

{

AdventureGrid[] adventureGrids = GetAdventureGrids();

foreach(var v in adventureGrids)

{

v.RefreshGridInfoInEditor();

}

}

public AdventureGrid[] GetAdventureGrids()

{

return AdventureGridRoot.GetComponentsInChildren();

}

public Vector3 GetPosByCoordinate(int x, int y)

{

float radius = gridSize / 2f;

float h = radius + radius * Mathf.Sin(Mathf.PI / 6);

float w = radius * Mathf.Cos(Mathf.PI / 6);

float left = -width * w;

float top = height * h / 2f;

return new Vector3(left + w * x * 2 + y % 2 * w, 0, top - h * y);

}

public Point GetCoordinateByPos(Vector3 pos)

{

float radius = gridSize / 2f;

float w = radius * Mathf.Cos(Mathf.PI / 6);

float h = radius + radius * Mathf.Sin(Mathf.PI / 6);

float left = -width * w;

float top = height * h / 2f;

int j = Mathf.RoundToInt((top - pos.z) / h);

int i = Mathf.RoundToInt((pos.x - left - j % 2 * w) / (w * 2));

return new Point { x = i, y = j };

}

void OnDrawGizmos()

{

float radius = gridSize / 2f;

float y = radius + radius * Mathf.Sin(Mathf.PI / 6);

float x = radius * Mathf.Cos(Mathf.PI / 6);

float left = -width * x;

float top = height * y/2f;

for (int i = 0; i < width; i++)

{

for (int j = 0; j < height; j++)

{

Vector3 pos = transform.position + GetPosByCoordinate(i, j);// new Vector3(left + i * x * 2 + j % 2 * x, 0, top - j * y);

Gizmos.color = Color.red;

Gizmos.DrawSphere(pos, 0.05f);

for (int k = 0; k < 6; k++)

{

float angle1 = Mathf.PI / 3f * k + Mathf.PI / 6f;

float angle2 = Mathf.PI / 3f * ((k + 1) % 6) + Mathf.PI / 6f;

Vector3 p1 = pos + new Vector3(radius * Mathf.Cos(angle1), 0, radius * Mathf.Sin(angle1));

Vector3 p2 = pos + new Vector3(radius * Mathf.Cos(angle2), 0, radius * Mathf.Sin(angle2));

Gizmos.color = Color.blue;

Gizmos.DrawLine(p1, p2);

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值