Created
June 14, 2017 15:06
-
-
Save tinjaw/6b813567c1e7df35174c0febdb790019 to your computer and use it in GitHub Desktop.
Companion Code for Blog Entry on Hexasphere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *********************************************************************** | |
// Assembly : Testing Project | |
// Author : Chaim Krause | |
// Created : 05-26-2017 | |
// | |
// Last Modified By : Chaim Krause | |
// Last Modified On : 05-30-2017 | |
// *********************************************************************** | |
// <copyright file="GameManager.cs" company="Combat Coding"> | |
// CC BY 4.0 | |
// https://creativecommons.org/licenses/by/4.0/ | |
// </copyright> | |
// <summary></summary> | |
// *********************************************************************** | |
using System.Collections.Generic; | |
using HexasphereGrid; | |
using UnityEngine; | |
public class GameManager : MonoBehaviour | |
{ | |
#region Fields | |
private GamePlayer _WhosTurnIsIt = GamePlayer.Blue; | |
private int gameturn = 1; | |
private Hexasphere hexa; | |
private GamePlayer[] owner; | |
private List<GamePlayer> unclaimed = new List<GamePlayer>() { GamePlayer.BlueMatch, GamePlayer.RedMatch, GamePlayer.None }; | |
#endregion Fields | |
#region Enums | |
public enum GamePlayer | |
{ | |
None, | |
Blue, | |
Red, | |
BlueMatch, | |
RedMatch | |
} | |
#endregion Enums | |
#region Properties | |
private GamePlayer WhosTurnIsIt | |
{ | |
get | |
{ | |
return _WhosTurnIsIt; | |
} | |
set | |
{ | |
_WhosTurnIsIt = value; | |
} | |
} | |
#endregion Properties | |
#region Methods | |
private bool IsAdjacentToOwnTile(GamePlayer whosTurnIsIt, int[] neighbors) | |
{ | |
foreach (int index in neighbors) | |
{ | |
if (owner[index] == whosTurnIsIt) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
private bool IsOppositeClear(int polarOpposite) | |
{ | |
return owner[polarOpposite] == GamePlayer.None; | |
} | |
private void OnTileClick(int tileIndex) | |
{ | |
Debug.Log(tileIndex); | |
int[] neighbors = hexa.GetTileNeighbours(tileIndex); | |
int polarOpposite = hexa.GetTileAtPolarOpposite(tileIndex); | |
if ((unclaimed.IndexOf(owner[tileIndex]) != -1) & (IsAdjacentToOwnTile(WhosTurnIsIt, neighbors) | (gameturn == 1))) | |
{ | |
if (WhosTurnIsIt == GamePlayer.Blue) | |
{ | |
hexa.SetTileColor(tileIndex, Color.blue); | |
if (owner[tileIndex] == GamePlayer.BlueMatch) | |
{ | |
// Blue is the winner | |
Debug.Log("Blue is the winner"); | |
} | |
if (IsOppositeClear(polarOpposite)) | |
{ | |
hexa.SetTileColor(polarOpposite, new Color(0, 0.75F, 1, 1)); | |
if (owner[tileIndex] == GamePlayer.RedMatch) | |
{ | |
// Red is the winner | |
Debug.Log("Red is the winner"); | |
} | |
if (owner[polarOpposite] == GamePlayer.None) | |
{ | |
owner[polarOpposite] = GamePlayer.BlueMatch; | |
} | |
} | |
hexa.highlightColor = Color.red; | |
} | |
else | |
{ | |
hexa.SetTileColor(tileIndex, Color.red); | |
if (IsOppositeClear(polarOpposite)) | |
{ | |
hexa.SetTileColor(polarOpposite, new Color(1, 0.5F, 0.5F, 1)); | |
if (owner[polarOpposite] == GamePlayer.None) | |
{ | |
owner[polarOpposite] = GamePlayer.RedMatch; | |
} | |
} | |
hexa.highlightColor = Color.blue; | |
} | |
owner[tileIndex] = WhosTurnIsIt; | |
if (WhosTurnIsIt == GamePlayer.Red) | |
{ | |
gameturn += 1; | |
} | |
SwitchSide(); | |
} | |
} | |
private void Start() | |
{ | |
hexa = Hexasphere.GetInstance("Hexasphere"); | |
hexa.OnTileClick += OnTileClick; | |
owner = new GamePlayer[hexa.tiles.Length]; | |
for (int i = 0; i < owner.Length; i++) | |
{ | |
owner[i] = GamePlayer.None; | |
} | |
} | |
private GamePlayer SwitchSide() | |
{ | |
if (WhosTurnIsIt == GamePlayer.Blue) | |
{ | |
WhosTurnIsIt = GamePlayer.Red; | |
} | |
else | |
{ | |
WhosTurnIsIt = GamePlayer.Blue; | |
} | |
return WhosTurnIsIt; | |
} | |
#endregion Methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment