Guest User

Untitled

a guest
Apr 25th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class Grid : MonoBehaviour {
  5.     #region constants
  6.     const int SQUARE = 0,
  7.               HEXAGONAL = 1,
  8.               DONUT = 2;
  9.     #endregion
  10.    
  11.     #region members
  12.     Hex[,] grid;
  13.     public Transform hex;
  14.     public int size, turn;
  15.     Vector2[] startPosition;
  16.     float fairness, connectedness=0;
  17.     Plane xz = new Plane(new Vector3(0f, 1f, 0f), 0f);
  18.     #endregion;
  19.    
  20.     #region behaviors
  21.     void Start () {
  22.         CreateBoard(3,SQUARE,2);
  23.     }
  24.    
  25.     void Update () {
  26.         float enter;
  27.         Ray worldRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  28.         xz.Raycast(worldRay, out enter);
  29.         worldRay.GetPoint(enter);
  30.     }
  31.     #endregion
  32.    
  33.     #region methods
  34.     public void CreateBoard(int size, int shape, int playerCount) {
  35.         startPosition = new Vector2[playerCount];
  36.         this.size = size;
  37.         switch(shape) {
  38.             case SQUARE:
  39.                 grid = new Hex[size,size];
  40.                 for(int x=0; x<size; x++)
  41.                     for(int y=0; y<size; y++) {
  42.                         Transform newHex = (Transform) Instantiate(hex);
  43.                         grid[x,y] = (Hex) newHex.GetComponent(typeof(Hex));
  44.                         grid[x,y].HexPosition = new Vector2(x,y);
  45.                         grid[x,y].allegiance = 5;
  46.                     }
  47.                 for(int i=0; i<playerCount; i++)
  48.                     startPosition[i] = i==0?new Vector2(0,0):
  49.                                        i==1?new Vector2(size-1,size-1):
  50.                                        i==2?new Vector2(0,size-1):
  51.                                        i==3?new Vector2(size-1,0):
  52.                                             new Vector2(size/2,size/2);
  53.                 break;
  54.             case HEXAGONAL:
  55.                 grid = new Hex[size*2-1,size*2-1];
  56.                 for(int x=0; x<size*2-1; x++)
  57.                     for(int y=0; y<size*2-1; y++)
  58.                         if(!(x+y<size-1 || x+y>=3*(size)-2)) {
  59.                             Transform newHex = (Transform) Instantiate(hex);
  60.                             grid[x,y] = (Hex) newHex.GetComponent(typeof(Hex));
  61.                             grid[x,y].HexPosition = new Vector2(x,y);
  62.                         }
  63.                 if(playerCount<3)
  64.                     for(int i=0; i<playerCount; i++)
  65.                         startPosition[i] = i==0?new Vector2(0,size-1):
  66.                                            i==1?new Vector2(size*2-1,size):
  67.                                                 new Vector2(size,size);
  68.                 break;
  69.         }
  70. }
  71.    
  72.     Vector2 BoardCenter() {
  73.         return Hex.Hex2pixel(new Vector2(grid.Length,grid.Length))*.5f;
  74.     }
  75.    
  76.     bool IsValid(Vector2 hv) {
  77.         return hv.x>=0 && hv.y>=0 && hv.x<grid.Length && hv.y<grid.Length && Get(hv)!=null;
  78.     }
  79.    
  80.     public Hex Get(Vector2 hv) {
  81.         if(!IsValid(hv)) return null;
  82.         return grid[(int)hv.x,(int)hv.y];
  83.     }
  84.     #endregion
  85.    
  86. }
Add Comment
Please, Sign In to add comment