Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class Hex : MonoBehaviour {
- #region constants
- //Directions
- public const int RIGHT = 0,
- BOTTOM_RIGHT = 1,
- BOTTOM_LEFT = 2,
- LEFT = 3,
- TOP_LEFT = 4,
- TOP_RIGHT = 5,
- //Special Cells
- EXPAND = 1,
- ALIGN = 2,
- ROTATE = 3,
- EXCLAVE = 4,
- // Allegiances
- NEUTRAL = 0;
- const float longSpan = 0.866025404f;// longSpan is the long side of the triangle
- const float shortSpan = .5f; // shortSpan is short side
- static readonly float[,] XM = new float[,] { { 1 / (2*longSpan), -1 }, { 1 / longSpan, 0 } };
- static readonly float[,] YM = new float[,] { { 1 / (2*longSpan), 1 }, { -1 / (2*longSpan), 1 } };
- #endregion
- #region members
- public Transform arrow,
- align,
- expand,
- exclave,
- rotate;
- private Transform icon;
- Grid grid;
- public int special, allegiance;
- #endregion
- #region properties
- public Vector2 HexPosition {
- get { return Pixel2hex(new Vector2(transform.position.x,transform.position.z)); }
- set { Vector2 h2p = Hex2pixel(value);
- transform.position = new Vector3(h2p.x,transform.position.y,h2p.y);
- print("Hex moved to" + h2p + " new position is " + transform.position);}
- }
- public int Direction { get {
- float angle = 0; Vector3 axis = Vector3.zero;
- transform.rotation.ToAngleAxis(out angle, out axis);
- return (int)((angle%360)/60); }
- }
- #endregion
- #region behaviors
- void Start () {
- transform.rotation = Quaternion.AngleAxis((int)(Random.value*6),Vector3.up);
- if(Random.value<.05f)
- special = (int)((Random.value*2)*(Random.value*2))+1;
- allegiance = NEUTRAL;
- icon = (Transform) Instantiate(
- special==ALIGN?align:
- special==EXPAND?expand:
- special==ROTATE?rotate:
- special==EXCLAVE?exclave:arrow);
- icon.parent = transform;
- icon.localPosition = Vector3.zero;
- }
- void Update () {
- }
- #endregion
- #region helpers
- public static Vector2 FloorDot(Vector2 v, float[,] mtx) {
- float j = Mathf.Floor(mtx[0,0]*v.x + mtx[0,1]*v.y);
- float k = Mathf.Floor(mtx[1,0]*v.x + mtx[1,1]*v.y);
- return new Vector2(j,k);
- }
- public static Vector2 Pixel2hex(Vector2 pv) {
- Vector2 mx = FloorDot(pv, XM);
- int x = Mathf.FloorToInt((mx.x+mx.y+2)/3);
- Vector2 my = FloorDot(pv, YM);
- int y = Mathf.FloorToInt((my.x+my.y+2)/3);
- return new Vector2(x,y);
- }
- public static Vector2 Hex2pixel(Vector2 hv) {
- float x, y;
- x = hv.x * (2*longSpan) + hv.y*longSpan;
- y = hv.y;
- return new Vector2(x,y*1.5f);
- }
- #endregion
- #region game logic
- Hex[] Neighbors() {
- Hex[] n = new Hex[6];
- Vector2 hv = HexPosition;
- n[0] = grid.Get(new Vector2(hv.x+1,hv.y));
- n[1] = grid.Get(new Vector2(hv.x,hv.y+1));
- n[2] = grid.Get(new Vector2(hv.x-1,hv.y+1));
- n[3] = grid.Get(new Vector2(hv.x-1,hv.y));
- n[4] = grid.Get(new Vector2(hv.x,hv.y-1));
- n[5] = grid.Get(new Vector2(hv.x+1,hv.y-1));
- return n;
- }
- Hex Neighbor(int direction) {
- Vector2 hv = HexPosition;
- switch(direction) {
- case 0: return grid.Get(new Vector2(hv.x+1,hv.y));
- case 1: return grid.Get(new Vector2(hv.x,hv.y+1));
- case 2: return grid.Get(new Vector2(hv.x-1,hv.y+1));
- case 3: return grid.Get(new Vector2(hv.x-1,hv.y));
- case 4: return grid.Get(new Vector2(hv.x,hv.y-1));
- case 5: return grid.Get(new Vector2(hv.x+1,hv.y-1));
- }
- return grid.Get(hv);
- }
- Hex Neighbor() {
- return Neighbor(Direction);
- }
- public void Proliferate() {
- Hex t = Neighbor();
- if(t==null) return;
- switch(special) {
- case ALIGN:
- transform.rotation = transform.rotation;
- special = ALIGN;
- if(t.allegiance == grid.turn) allegiance = NEUTRAL;
- special = 0;
- goto default;
- default:
- foreach(Hex cn in ConnectedNeighbors())
- Assimilate(cn);
- break;
- }
- }
- void Assimilate(Hex t) {
- if(t.allegiance == grid.turn) return;
- t.allegiance = grid.turn;
- t.Proliferate();
- }
- List<Hex> ConnectedNeighbors() {
- List<Hex> cns = new List<Hex>();
- foreach(Hex n in Neighbors())
- if(n!=null) {
- if(n.allegiance!=NEUTRAL &&
- ( n.special==Hex.EXPAND || Neighbor()==this) ||
- special==Hex.EXPAND || Neighbor()==n)
- cns.Add(n);
- }
- return cns;
- }
- List<Hex> ConnectedNeighborhood() {
- List<Hex> cnh = new List<Hex>();
- cnh.Add(this);
- if(special==Hex.ALIGN) {
- Hex n = this;
- while(n.Neighbor()!=null) {
- cnh.Add(n.Neighbors()[Direction]);
- n = n.Neighbors()[Direction];
- }
- }
- int count;
- do {
- count = cnh.Count;
- foreach(Hex cn in cnh)
- foreach(Hex cnn in cn.ConnectedNeighbors())
- if(!cnh.Contains(cnn)) cnh.Add(cnn);
- } while (cnh.Count>count);
- return cnh;
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment