-
-
Save markdekuijer/2357f908b79a3deaf912a3d765464048 to your computer and use it in GitHub Desktop.
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class TurnbasedManager : MonoBehaviour | |
{ | |
public static TurnbasedManager Instance; | |
public HexGrid grid; | |
public GameObject endTurnButton; | |
public bool playerTurn; | |
bool enemyTurned; | |
int currentTurn; | |
//lists to check the states of all enemys | |
//also used to keep things togheter | |
public List<HexUnit> allyUnits = new List<HexUnit>(); | |
public List<HexUnit> enemyUnits = new List<HexUnit>(); | |
public List<HexCell> enemySpawns = new List<HexCell>(); | |
public bool allowSpawn; | |
private void Awake() | |
{ | |
if (Instance == null) | |
Instance = this; | |
} | |
private void Start() | |
{ | |
enemyTurned = true; | |
playerTurn = true; | |
} | |
void Update () | |
{ | |
if (playerTurn) | |
{ | |
endTurnButton.SetActive(true); | |
} | |
else | |
{ | |
if (enemyTurned) | |
return; | |
endTurnButton.SetActive(false); | |
StartCoroutine(GoThroughEnemys()); | |
enemyTurned = true; | |
} | |
} | |
//look at whos turn it is and depending on that switch some states of the units | |
public void InitNextTurn() | |
{ | |
playerTurn = !playerTurn; | |
if (playerTurn) | |
{ | |
for (int i = 0; i < allyUnits.Count; i++) | |
{ | |
allyUnits[i].hasMovedThisTurn = false; | |
allyUnits[i].hasAttackThisTurn = false; | |
allyUnits[i].hasTurned = false; | |
} | |
print("playerTurnStart"); | |
HexGameUI.instance.CloseSelect(); | |
currentTurn++; | |
} | |
else | |
{ | |
for (int i = 0; i < enemyUnits.Count; i++) | |
{ | |
enemyUnits[i].hasMovedThisTurn = false; | |
enemyUnits[i].hasAttackThisTurn = false; | |
enemyUnits[i].hasTurned = false; | |
} | |
print("enemyTurnStart"); | |
} | |
enemyTurned = false; | |
} | |
//function for enemys which first checks by a random number | |
//if any enemys will spawn at their camps | |
//after that it will check their best move from their current position | |
//I've decided to let them walk all at the same time but I could let them wait by checking the hasTurned bool inside them | |
IEnumerator GoThroughEnemys() | |
{ | |
if (allowSpawn) | |
{ | |
for (int i = 0; i < enemySpawns.Count; i++) | |
{ | |
if (Random.Range(0, 1) > 0.15) | |
continue; | |
int index = Random.Range(0, 5); | |
HexUnit u = Instantiate(HexGameUI.instance.unitTypes.unitTypeIDs[index].GetComponent<HexUnit>()); | |
u.Initialize(index, enemySpawns[i], true); | |
u.Grid = grid; | |
u.Location = enemySpawns[i]; | |
TurnbasedManager.Instance.enemyUnits.Add(u); | |
} | |
} | |
for (int i = 0; i < enemyUnits.Count; i++) | |
{ | |
if (enemyUnits[i].unitType.objectName == "castle") | |
continue; | |
enemyUnits[i].CalculateNextMove(grid, allyUnits); | |
//while (!enemyUnits[i].hasTurned) | |
//{ | |
yield return null; | |
//} | |
} | |
InitNextTurn(); | |
} | |
//loops through all enemys and searches for the closest enemy | |
//used for enemys to attack and walk around the map | |
public HexUnit GetClosestAlly(HexCoordinates coord, List<HexUnit> units) | |
{ | |
int MaxInt = int.MaxValue; | |
HexUnit u = null; | |
print(units.Count); | |
for (int i = 0; i < units.Count; i++) | |
{ | |
if(units[i].Location.coordinates.DistanceTo(coord) < MaxInt) | |
{ | |
u = units[i]; | |
MaxInt = units[i].Location.coordinates.DistanceTo(coord); | |
} | |
} | |
return u; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment