Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class Grid : MonoBehaviour {
- #region constants
- const int SQUARE = 0,
- HEXAGONAL = 1,
- DONUT = 2;
- #endregion
- #region members
- Hex[,] grid;
- public Transform hex;
- public int size, turn;
- Vector2[] startPosition;
- float fairness, connectedness=0;
- Plane xz = new Plane(new Vector3(0f, 1f, 0f), 0f);
- #endregion;
- #region behaviors
- void Start () {
- CreateBoard(3,SQUARE,2);
- }
- void Update () {
- float enter;
- Ray worldRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- xz.Raycast(worldRay, out enter);
- worldRay.GetPoint(enter);
- }
- #endregion
- #region methods
- public void CreateBoard(int size, int shape, int playerCount) {
- startPosition = new Vector2[playerCount];
- this.size = size;
- switch(shape) {
- case SQUARE:
- grid = new Hex[size,size];
- for(int x=0; x<size; x++)
- for(int y=0; y<size; y++) {
- Transform newHex = (Transform) Instantiate(hex);
- grid[x,y] = (Hex) newHex.GetComponent(typeof(Hex));
- grid[x,y].HexPosition = new Vector2(x,y);
- grid[x,y].allegiance = 5;
- }
- for(int i=0; i<playerCount; i++)
- startPosition[i] = i==0?new Vector2(0,0):
- i==1?new Vector2(size-1,size-1):
- i==2?new Vector2(0,size-1):
- i==3?new Vector2(size-1,0):
- new Vector2(size/2,size/2);
- break;
- case HEXAGONAL:
- grid = new Hex[size*2-1,size*2-1];
- for(int x=0; x<size*2-1; x++)
- for(int y=0; y<size*2-1; y++)
- if(!(x+y<size-1 || x+y>=3*(size)-2)) {
- Transform newHex = (Transform) Instantiate(hex);
- grid[x,y] = (Hex) newHex.GetComponent(typeof(Hex));
- grid[x,y].HexPosition = new Vector2(x,y);
- }
- if(playerCount<3)
- for(int i=0; i<playerCount; i++)
- startPosition[i] = i==0?new Vector2(0,size-1):
- i==1?new Vector2(size*2-1,size):
- new Vector2(size,size);
- break;
- }
- }
- Vector2 BoardCenter() {
- return Hex.Hex2pixel(new Vector2(grid.Length,grid.Length))*.5f;
- }
- bool IsValid(Vector2 hv) {
- return hv.x>=0 && hv.y>=0 && hv.x<grid.Length && hv.y<grid.Length && Get(hv)!=null;
- }
- public Hex Get(Vector2 hv) {
- if(!IsValid(hv)) return null;
- return grid[(int)hv.x,(int)hv.y];
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment