Guest User

Untitled

a guest
Apr 25th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class Hex : MonoBehaviour {
  5.     #region constants
  6.               //Directions
  7.     public const int RIGHT = 0,
  8.               BOTTOM_RIGHT = 1,
  9.               BOTTOM_LEFT = 2,
  10.               LEFT = 3,
  11.               TOP_LEFT = 4,
  12.               TOP_RIGHT = 5,
  13.              
  14.               //Special Cells
  15.               EXPAND = 1,
  16.               ALIGN = 2,
  17.               ROTATE = 3,
  18.               EXCLAVE = 4,
  19.              
  20.               // Allegiances
  21.               NEUTRAL = 0;
  22.    
  23.     const float longSpan = 0.866025404f;// longSpan is the long side of the triangle
  24.     const float shortSpan = .5f; // shortSpan is short side
  25.    
  26.     static readonly float[,] XM = new float[,] { { 1 / (2*longSpan), -1 }, { 1 / longSpan, 0 } };
  27.     static readonly float[,] YM = new float[,] { { 1 / (2*longSpan), 1 }, { -1 / (2*longSpan), 1 } };
  28.     #endregion
  29.    
  30.     #region members
  31.     public Transform arrow,
  32.                      align,
  33.                      expand,
  34.                      exclave,
  35.                      rotate;
  36.     private Transform icon;
  37.    
  38.     Grid grid;
  39.    
  40.     public int special, allegiance;
  41.     #endregion
  42.    
  43.     #region properties
  44.     public Vector2 HexPosition {
  45.         get { return Pixel2hex(new Vector2(transform.position.x,transform.position.z)); }
  46.         set { Vector2 h2p = Hex2pixel(value);
  47.               transform.position = new Vector3(h2p.x,transform.position.y,h2p.y);
  48.               print("Hex moved to" + h2p + " new position is " + transform.position);}
  49.     }
  50.     public int Direction { get {
  51.         float angle = 0; Vector3 axis = Vector3.zero;
  52.         transform.rotation.ToAngleAxis(out angle, out axis);
  53.         return (int)((angle%360)/60); }
  54.     }
  55.     #endregion
  56.    
  57.     #region behaviors
  58.     void Start () {
  59.         transform.rotation = Quaternion.AngleAxis((int)(Random.value*6),Vector3.up);
  60.         if(Random.value<.05f)
  61.           special = (int)((Random.value*2)*(Random.value*2))+1;
  62.         allegiance = NEUTRAL;
  63.         icon = (Transform) Instantiate(
  64.                            special==ALIGN?align:
  65.                            special==EXPAND?expand:
  66.                            special==ROTATE?rotate:
  67.                            special==EXCLAVE?exclave:arrow);
  68.        
  69.         icon.parent = transform;
  70.         icon.localPosition = Vector3.zero;
  71.     }
  72.    
  73.     void Update () {
  74.    
  75.     }
  76.     #endregion
  77.    
  78.     #region helpers
  79.     public static Vector2 FloorDot(Vector2 v, float[,] mtx) {
  80.       float j = Mathf.Floor(mtx[0,0]*v.x + mtx[0,1]*v.y);
  81.       float k = Mathf.Floor(mtx[1,0]*v.x + mtx[1,1]*v.y);
  82.       return new Vector2(j,k);
  83.     }
  84.    
  85.     public static Vector2 Pixel2hex(Vector2 pv) {
  86.       Vector2 mx = FloorDot(pv, XM);
  87.       int x = Mathf.FloorToInt((mx.x+mx.y+2)/3);
  88.       Vector2 my = FloorDot(pv, YM);
  89.       int y = Mathf.FloorToInt((my.x+my.y+2)/3);
  90.       return new Vector2(x,y);
  91.     }
  92.    
  93.     public static Vector2 Hex2pixel(Vector2 hv) {
  94.       float x, y;
  95.       x = hv.x * (2*longSpan) + hv.y*longSpan;
  96.       y = hv.y;
  97.       return new Vector2(x,y*1.5f);
  98.     }
  99.     #endregion
  100.    
  101.     #region game logic
  102.     Hex[] Neighbors() {
  103.         Hex[] n = new Hex[6];
  104.         Vector2 hv = HexPosition;
  105.         n[0] = grid.Get(new Vector2(hv.x+1,hv.y));
  106.         n[1] = grid.Get(new Vector2(hv.x,hv.y+1));
  107.         n[2] = grid.Get(new Vector2(hv.x-1,hv.y+1));
  108.         n[3] = grid.Get(new Vector2(hv.x-1,hv.y));
  109.         n[4] = grid.Get(new Vector2(hv.x,hv.y-1));
  110.         n[5] = grid.Get(new Vector2(hv.x+1,hv.y-1));
  111.         return n;
  112.     }
  113.    
  114.     Hex Neighbor(int direction) {
  115.         Vector2 hv = HexPosition;
  116.         switch(direction) {
  117.             case 0: return grid.Get(new Vector2(hv.x+1,hv.y));
  118.             case 1: return grid.Get(new Vector2(hv.x,hv.y+1));
  119.             case 2: return grid.Get(new Vector2(hv.x-1,hv.y+1));
  120.             case 3: return grid.Get(new Vector2(hv.x-1,hv.y));
  121.             case 4: return grid.Get(new Vector2(hv.x,hv.y-1));
  122.             case 5: return grid.Get(new Vector2(hv.x+1,hv.y-1));
  123.         }
  124.         return grid.Get(hv);
  125.     }
  126.    
  127.     Hex Neighbor() {
  128.         return Neighbor(Direction);
  129.     }
  130.    
  131.     public void Proliferate() {
  132.         Hex t = Neighbor();
  133.         if(t==null) return;
  134.         switch(special) {
  135.             case ALIGN:
  136.                 transform.rotation = transform.rotation;
  137.                 special = ALIGN;
  138.                 if(t.allegiance == grid.turn) allegiance = NEUTRAL;
  139.                 special = 0;
  140.                 goto default;
  141.             default:
  142.                 foreach(Hex cn in ConnectedNeighbors())
  143.                 Assimilate(cn);
  144.             break;
  145.         }
  146.     }
  147.  
  148.     void Assimilate(Hex t) {
  149.         if(t.allegiance == grid.turn) return;
  150.         t.allegiance = grid.turn;
  151.         t.Proliferate();
  152.     }
  153.  
  154.     List<Hex> ConnectedNeighbors() {
  155.         List<Hex> cns = new List<Hex>();
  156.         foreach(Hex n in Neighbors())
  157.             if(n!=null) {
  158.                 if(n.allegiance!=NEUTRAL &&
  159.                   ( n.special==Hex.EXPAND || Neighbor()==this) ||
  160.                     special==Hex.EXPAND || Neighbor()==n)
  161.                 cns.Add(n);
  162.             }
  163.         return cns;
  164.     }
  165.  
  166.     List<Hex> ConnectedNeighborhood() {
  167.         List<Hex> cnh = new List<Hex>();
  168.         cnh.Add(this);
  169.         if(special==Hex.ALIGN) {
  170.             Hex n = this;
  171.             while(n.Neighbor()!=null) {
  172.                 cnh.Add(n.Neighbors()[Direction]);
  173.                 n = n.Neighbors()[Direction];
  174.             }
  175.         }
  176.         int count;
  177.         do {
  178.             count = cnh.Count;
  179.                 foreach(Hex cn in cnh)
  180.                     foreach(Hex cnn in cn.ConnectedNeighbors())
  181.                         if(!cnh.Contains(cnn)) cnh.Add(cnn);
  182.         } while (cnh.Count>count);
  183.         return cnh;
  184.     }
  185.     #endregion
  186. }
Add Comment
Please, Sign In to add comment