Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Hex : MonoBehaviour {
- protected float hexWidth;
- protected float hexHeight;
- protected float hexHorizDistance; //The horizontal distance between adjacent hexes
- protected float hexVertDistance;
- public static float size = .32f;
- // Use this for initialization
- void HexStart () {
- hexHeight = size * 2;
- hexVertDistance = hexHeight * 3 / 4;
- hexWidth = Mathf.Sqrt(3) / 2 * hexHeight;
- hexHorizDistance = hexWidth;
- }
- // Update is called once per frame
- void Update () {
- }
- }
- public class HexPosition : Hex {
- public GameObject cornerHexPrefab;
- public Vector2 locationInArray;
- private HexCorner[] arrayOfHexCorners = new HexCorner[5];
- Vector2 position;
- void Start()
- {
- transform.position = new Vector3((locationInArray.x * hexHorizDistance),(locationInArray.y * hexVertDistance));
- position = HexOffset((int)locationInArray.x,(int)locationInArray.y);
- transform.position = new Vector3(position.x, position.y);
- for (int i = 0; i <= 5; i++)
- {
- arrayOfHexCorners[i] = Instantiate(cornerHexPrefab, transform.position, transform.rotation) as HexCorner;
- arrayOfHexCorners[i].cornerNumber = i;
- arrayOfHexCorners[i].CornerStart();
- }
- }
- // Update is called once per frame
- void Update ()
- {
- }
- public Vector2 HexOffset(int x, int y)
- {
- Vector2 position = Vector2.zero;
- if (y % 2 == 0)
- {
- position.x = x * hexHorizDistance;
- position.y = y * hexVertDistance;
- }
- else
- {
- position.x = (x + 0.5f) * hexHorizDistance;
- position.y = y * hexVertDistance;
- }
- return position;
- }
- }
- public class HexCorner : HexPosition {
- public int cornerNumber;
- private Vector2 cornerPosition;
- private Vector2 centerOfHex;
- // Use this for initialization
- public void CornerStart ()
- {
- centerOfHex = transform.position;
- cornerPosition = HexCornerPos(centerOfHex, size, cornerNumber);
- transform.position = new Vector3(cornerPosition.x, cornerPosition.y);
- }
- // Update is called once per frame
- void Update ()
- {
- }
- public Vector2 HexCornerPos(Vector2 center, float size, float cornerNumber)
- {
- float angle_deg = 60 * cornerNumber + 30;
- var angle_rad = Mathf.PI / 180 * angle_deg;
- return new Vector2(center.x + size * Mathf.Cos(angle_rad),
- center.y + size * Mathf.Sin(angle_rad));
- }
- }
- public class HexGrid : MonoBehaviour {
- GameObject[,] hexGridArray;
- // Use this for initialization
- void Start () {
- //this would create a hexPosition (and run the start method) for each location in the array.
- }
- // Update is called once per frame
- void Update () {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement