Advertisement
Guest User

KindanotreallyfinishedHexGridCode

a guest
May 27th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Hex : MonoBehaviour {
  5.  
  6.     protected float hexWidth;
  7.     protected float hexHeight;
  8.     protected float hexHorizDistance; //The horizontal distance between adjacent hexes
  9.     protected float hexVertDistance;
  10.     public static float size = .32f;
  11.     // Use this for initialization
  12.     void HexStart () {
  13.         hexHeight = size * 2;
  14.         hexVertDistance = hexHeight * 3 / 4;
  15.         hexWidth = Mathf.Sqrt(3) / 2 * hexHeight;
  16.         hexHorizDistance = hexWidth;
  17.     }
  18.    
  19.     // Update is called once per frame
  20.     void Update () {
  21.    
  22.     }
  23. }
  24. public class HexPosition : Hex {
  25.  
  26.     public GameObject cornerHexPrefab;
  27.     public Vector2 locationInArray;
  28.     private HexCorner[] arrayOfHexCorners = new HexCorner[5];
  29.     Vector2 position;
  30.  
  31.     void Start()
  32.     {
  33.        
  34.         transform.position = new Vector3((locationInArray.x * hexHorizDistance),(locationInArray.y * hexVertDistance));
  35.         position = HexOffset((int)locationInArray.x,(int)locationInArray.y);
  36.         transform.position = new Vector3(position.x, position.y);
  37.         for (int i = 0; i <= 5; i++)
  38.         {
  39.             arrayOfHexCorners[i] = Instantiate(cornerHexPrefab, transform.position, transform.rotation) as HexCorner;
  40.             arrayOfHexCorners[i].cornerNumber = i;
  41.             arrayOfHexCorners[i].CornerStart();
  42.         }
  43.     }
  44.    
  45.     // Update is called once per frame
  46.     void Update ()
  47.     {
  48.    
  49.     }
  50.     public Vector2 HexOffset(int x, int y)
  51.     {
  52.         Vector2 position = Vector2.zero;
  53.  
  54.         if (y % 2 == 0)
  55.         {
  56.             position.x = x * hexHorizDistance;
  57.             position.y = y * hexVertDistance;
  58.         }
  59.         else
  60.         {
  61.             position.x = (x + 0.5f) * hexHorizDistance;
  62.             position.y = y * hexVertDistance;
  63.         }
  64.  
  65.         return position;
  66.     }
  67. }
  68. public class HexCorner : HexPosition {
  69.  
  70.     public int cornerNumber;
  71.     private Vector2 cornerPosition;
  72.     private Vector2 centerOfHex;
  73.  
  74.     // Use this for initialization
  75.     public void CornerStart ()
  76.     {
  77.         centerOfHex = transform.position;
  78.         cornerPosition = HexCornerPos(centerOfHex, size, cornerNumber);
  79.         transform.position = new Vector3(cornerPosition.x, cornerPosition.y);
  80.     }
  81.    
  82.     // Update is called once per frame
  83.     void Update ()
  84.     {
  85.    
  86.     }
  87.  
  88.     public Vector2 HexCornerPos(Vector2 center, float size, float cornerNumber)
  89.     {
  90.         float angle_deg = 60 * cornerNumber + 30;
  91.         var angle_rad = Mathf.PI / 180 * angle_deg;
  92.         return new Vector2(center.x + size * Mathf.Cos(angle_rad),
  93.                      center.y + size * Mathf.Sin(angle_rad));
  94.     }
  95. }
  96. public class HexGrid : MonoBehaviour {
  97.  
  98.     GameObject[,] hexGridArray;
  99.  
  100.     // Use this for initialization
  101.     void Start () {
  102.     //this would create a hexPosition (and run the start method) for each location in the array.
  103.     }
  104.    
  105.     // Update is called once per frame
  106.     void Update () {
  107.    
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement