Created
March 13, 2016 13:39
-
-
Save IronMonk-UK/27f4930c9c9dd925b668 to your computer and use it in GitHub Desktop.
13/03 Iteration of Final Project
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 UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class GameManager : MonoBehaviour | |
{ | |
public static GameManager gm; | |
public GameObject tilePrefab; | |
public GameObject userUnitPrefab; | |
/*public Text _name; | |
public Text _class; | |
public Text _health; | |
public Text _strength; | |
public Text _defence; | |
public Text _accuracy; | |
public Text _eName; | |
public Text _eClass; | |
public Text _eHealth; | |
public Text _eStrength; | |
public Text _eDefence; | |
public Text _eAccuracy; | |
public Text announcer;*/ | |
public int mapSize; | |
public bool startMove; | |
bool posHit; | |
bool negHit; | |
bool hit; | |
public List<List<Tile>> map = new List<List<Tile>>(); | |
public List<Unit> units = new List<Unit>(); | |
public List<Vector2> moveTiles = new List<Vector2>(); | |
public Grid grid; | |
public Pathfinding pathfinding; | |
[SerializeField] | |
public int currentUnitIndex = 0; | |
void Awake() | |
{ | |
// Makes the instance variable the current GameManager class | |
gm = this; | |
grid = GameObject.FindWithTag("A*").GetComponent<Grid>(); | |
pathfinding = GameObject.FindWithTag("A*").GetComponent<Pathfinding>(); | |
//EnemyStatsDown (); | |
} | |
void Start() | |
{ | |
// Runs the generateMap function on start | |
//generateMap (); | |
// Runs the generatePlayers function on start | |
generateUnits(); | |
//announcer.text = ""; | |
} | |
void Update() | |
{ | |
Debug.Log(units[currentUnitIndex].inventory[0].Name); | |
Debug.Log(units[currentUnitIndex].inventory[0].Damage); | |
Debug.Log(units[currentUnitIndex].inventory[0].HitChance); | |
/* Looks at which player in the player list is currently moving | |
* and constantly runs the turnUpdate function for them, as it is in the update function */ | |
if (units[currentUnitIndex].health > 0) | |
{ | |
units[currentUnitIndex].turnUpdate(); | |
} | |
else | |
{ | |
nextTurn(); | |
} | |
// Sets the UI text to show the current units stats | |
/*_name.text = units [currentUnitIndex]._name; | |
_class.text = units [currentUnitIndex]._class; | |
_health.text = units [currentUnitIndex].health.ToString(); | |
_strength.text = units [currentUnitIndex].strength.ToString(); | |
_defence.text = units [currentUnitIndex].defence.ToString(); | |
_accuracy.text = units [currentUnitIndex].accuracy.ToString();*/ | |
if (units[currentUnitIndex].moved) | |
units[currentUnitIndex].moving = false; | |
if (units[currentUnitIndex].moving == false) | |
{ | |
foreach (Vector3 v in moveTiles) | |
{ | |
foreach (List<Tile> l in map) | |
{ | |
foreach (Tile t in l) | |
{ | |
if (v == t.gridPosition) | |
{ | |
t.movingTile = false; | |
} | |
} | |
} | |
} | |
} | |
} | |
/* The nextTurn function determines which player is currently having their turn | |
* The current player index starts at 0, so must have +1 added to it when counting players so both start from 1. | |
* When the nextTurn function is run, if the player index + 1 is lower than the amount in the players list | |
* it will increase the index by one, causing the turnUpdate function above to start for a new player. | |
* If the index + 1 ends up equal to the amount of players in the list, it will start back to 0.*/ | |
/* While this is not my ideal turn system, I believe that as I progress with my research, it will help me form a base towards the operation of the AI | |
* As this is one potential way I could have enemy units moving one at a time as an index goes through a list of enemies */ | |
public void nextTurn() | |
{ | |
if (currentUnitIndex + 1 < units.Count) | |
{ | |
currentUnitIndex++; | |
} | |
else | |
{ | |
currentUnitIndex = 0; | |
} | |
} | |
/* The moveCurrentPlayer function sets the current player units moveDestination position as the destTile, as set when the player clicks on a tile through the Tile class | |
* OnMouseDown function. */ | |
/* The moveCurrentPlayer function now runs through an if statement to make sure the current unit has not already moved. If it has not, they will use one action, and move to the | |
* destTile. This function also sets the current units gridPosition to be that of the destTile's gridPosition, and then sets the moved boolean to true, | |
* so that unit cannot move again. */ | |
public IEnumerator moveCurrentPlayer() | |
{ | |
/*if (units [currentUnitIndex].moved == false) { | |
units [currentUnitIndex].actions --; | |
units [currentUnitIndex].moveDestination = destTile.transform.position + Vector3.up; | |
units [currentUnitIndex].gridPosition = destTile.gridPosition; | |
units [currentUnitIndex].moved = true; | |
}*/ | |
if (units[currentUnitIndex].moved == false) | |
{ | |
/*for(int i = 0; i < grid.path.Count; i++) { | |
units[currentUnitIndex].transform.Translate(grid.path[i].tile.transform.position); | |
Debug.Log("Moved."); | |
}*/ | |
int i = 0; | |
/*if (i < grid.path.Count) | |
{ | |
Vector3 nextSpace = new Vector3(grid.path[i].tile.transform.position.x, grid.path[i].tile.transform.position.y + 2, grid.path[i].tile.transform.position.z); | |
units[currentUnitIndex].transform.position = nextSpace; | |
i++; | |
}*/ | |
units[currentUnitIndex].moved = true; | |
for (i = 0; i < units[currentUnitIndex].moveLimit; i++) | |
{ | |
Vector3 nextSpace = new Vector3(grid.path[i].tile.transform.position.x, grid.path[i].tile.transform.position.y + 2, grid.path[i].tile.transform.position.z); | |
units[currentUnitIndex].transform.position = nextSpace; | |
yield return new WaitForSeconds(2.5f); | |
} | |
if (i == units[currentUnitIndex].moveLimit) | |
{ | |
Debug.Log("Done thing"); | |
pathfinding.pathTraced = false; | |
units[currentUnitIndex].actions--; | |
} | |
Vector3 up = new Vector3(units[currentUnitIndex].transform.position.x, units[currentUnitIndex].transform.position.y - 2, units[currentUnitIndex].transform.position.z + 1); | |
Vector3 down = new Vector3(units[currentUnitIndex].transform.position.x, units[currentUnitIndex].transform.position.y - 2, units[currentUnitIndex].transform.position.z - 1); | |
Vector3 left = new Vector3(units[currentUnitIndex].transform.position.x - 1, units[currentUnitIndex].transform.position.y - 2, units[currentUnitIndex].transform.position.z); | |
Vector3 right = new Vector3(units[currentUnitIndex].transform.position.x + 1, units[currentUnitIndex].transform.position.y - 2, units[currentUnitIndex].transform.position.z); | |
/*int i = 0; | |
units[currentUnitIndex].transform.position = grid.path[i].tile.transform.position; | |
if (units[currentUnitIndex].transform.position == grid.path[i].tile.transform.position) | |
i++;*/ | |
/*foreach(Node n in grid.path) { | |
units[currentUnitIndex].transform.position = n.tile.transform.position; | |
}*/ | |
//units[currentUnitIndex].transform.position = grid.path[grid.path.Count - 1].tile.transform.position; | |
} | |
} | |
public void attackWithCurrentPlayer(Tile destTile) | |
{ | |
// The attackWithCurrentPlayer function sets the rules of combat within the game, dealing with how the currently selected player can engage in combat. | |
/* The variable target is created first. A foreach loop then runs, looking for any unit that has the same gridPosition as the tile being selected, | |
* as rather than targetting a unit, the tile they are on is selected. If a unit is found, they are set as the target. */ | |
Unit target = null; | |
foreach (Unit u in units) | |
{ | |
if (u.gridPosition == destTile.gridPosition) | |
{ | |
target = u; | |
} | |
} | |
/* So as not to create a giant line of specific gridPositions that can be attacked, I created 4 Vector3 variables, and implemented them into it. | |
* Each Vector3 represents a space above, below, left, or right, of the current units position */ | |
Vector3 up = new Vector2(target.gridPosition.x, target.gridPosition.y - 1); | |
Vector3 down = new Vector2(target.gridPosition.x, target.gridPosition.y + 1); | |
Vector3 left = new Vector2(target.gridPosition.x - 1, target.gridPosition.y); | |
Vector3 right = new Vector2(target.gridPosition.x + 1, target.gridPosition.y); | |
/* This if statement starts off by making sure that there is a target available. If so, the next if statement ensures that they are in a position that can be attacked by the | |
* current unit. If this returns true, an action is used by the current unit. A series of if statements are run after this, checking if the units class is weak against them, | |
* strong against them, or the same. At this point in time, units gain or lose accuracy based on this, and sets one of three booleans, based on the classes. | |
* THIS IS SUBJECT TO CHANGE. */ | |
if (target != null) | |
{ | |
if (units[currentUnitIndex].gridPosition == up || units[currentUnitIndex].gridPosition == down || units[currentUnitIndex].gridPosition == left || units[currentUnitIndex].gridPosition == right) | |
{ | |
units[currentUnitIndex].actions--; | |
if (units[currentUnitIndex].posClass == target._class) | |
{ | |
posHit = Random.Range(0.0f, 1.0f) <= (units[currentUnitIndex].accuracy + .2f); | |
} | |
else if (units[currentUnitIndex].negClass == target._class) | |
{ | |
negHit = Random.Range(0.0f, 1.0f) <= (units[currentUnitIndex].accuracy - .2f); | |
} | |
else | |
{ | |
hit = Random.Range(0.0f, 1.0f) <= units[currentUnitIndex].accuracy; | |
} | |
/* This set of if statements breaks down the damage dealt to an opposing unit if the unit hits. Based on the boolean activated above, damage will be dealt with | |
* positive or negative modifiers, or no modifers. If the above hit does not return true, the attack simply misses. If the unit being attacked is not on an | |
* adjacent tile, the debug log will return stating the target is not available. */ | |
if (posHit) | |
{ | |
int damageAmount = (int)Mathf.Floor(units[currentUnitIndex].strength + Random.Range(2, units[currentUnitIndex].damageRollSides + 2) - target.defence); | |
target.health -= damageAmount; | |
//announcer.text = (units[currentUnitIndex]._name + " hit " + target._name.ToString() + " effectively for " + damageAmount + " damage!"); | |
Debug.Log(units[currentUnitIndex]._name + " hit " + target._name + " effectively for " + damageAmount + " damage!"); | |
} | |
else if (negHit) | |
{ | |
int damageAmount = (int)Mathf.Floor(units[currentUnitIndex].strength + Random.Range(0, units[currentUnitIndex].damageRollSides - 1) - target.defence); | |
target.health -= damageAmount; | |
//announcer.text = (units[currentUnitIndex]._name + " hit " + target._name.ToString() + " ineffectively for " + damageAmount + " damage!"); | |
Debug.Log(units[currentUnitIndex]._name + " hit " + target._name + " ineffectively for " + damageAmount + " damage!"); | |
} | |
else if (hit) | |
{ | |
int damageAmount = (int)Mathf.Floor(units[currentUnitIndex].strength + Random.Range(0, units[currentUnitIndex].damageRollSides) - target.defence); | |
target.health -= damageAmount; | |
//announcer.text = (units[currentUnitIndex]._name + " hit " + target._name.ToString() + " for " + damageAmount + " damage!"); | |
Debug.Log(units[currentUnitIndex]._name + " hit " + target._name + " for " + damageAmount + " damage!"); | |
} | |
else | |
{ | |
Debug.Log(target._name + " miss."); | |
} | |
} | |
else | |
{ | |
Debug.Log("Target not available"); | |
} | |
} | |
} | |
void generateUnits() | |
{ | |
UserUnit unit; | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3(0 - Mathf.Floor(mapSize / 2) + .5f, 1, -0 + Mathf.Floor(mapSize / 2) - .5f), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2(0, 0); | |
unit._name = "Bentley"; | |
unit._class = "Barbarian"; | |
_Class.Barbarian(unit); | |
units.Add(unit); | |
Items.sItems.IronSword(unit); | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3((mapSize - 1) - Mathf.Floor(mapSize / 2) + .5f, 1, -(mapSize - 1) + Mathf.Floor(mapSize / 2) - .5f), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2(mapSize - 1, mapSize - 1); | |
unit._name = "Harrison"; | |
unit._class = "Mercenary"; | |
_Class.Mercenary(unit); | |
units.Add(unit); | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3(4 - Mathf.Floor(mapSize / 2) + .5f, 1, -4 + Mathf.Floor(mapSize / 2) - .5f), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2(mapSize / 2 - 1, mapSize / 2 - 1); | |
unit._name = "Alexander"; | |
unit._class = "Knight"; | |
_Class.Knight(unit); | |
units.Add(unit); | |
} | |
// These three functions are for use with the UI buttons, calling on the current unit and changing states within it, so it is either moving, attacking, or its turn has ended. | |
public void Move() | |
{ | |
if (units[currentUnitIndex].moving == false) | |
{ | |
units[currentUnitIndex].moving = true; | |
units[currentUnitIndex].attacking = false; | |
} | |
else | |
{ | |
units[currentUnitIndex].moving = false; | |
units[currentUnitIndex].attacking = false; | |
} | |
} | |
public void Attack() | |
{ | |
if (units[currentUnitIndex].attacking == false) | |
{ | |
units[currentUnitIndex].attacking = true; | |
units[currentUnitIndex].moving = false; | |
} | |
else | |
{ | |
units[currentUnitIndex].moving = false; | |
units[currentUnitIndex].attacking = false; | |
} | |
} | |
public void EndTurn() | |
{ | |
units[currentUnitIndex].moving = false; | |
units[currentUnitIndex].attacking = false; | |
units[currentUnitIndex].actions = 2; | |
units[currentUnitIndex].moved = false; | |
nextTurn(); | |
} | |
/*public void EnemyStatsUp(){ | |
_eName.enabled = true; | |
_eClass.enabled = true; | |
_eHealth.enabled = true; | |
_eStrength.enabled = true; | |
_eDefence.enabled = true; | |
_eAccuracy.enabled = true; | |
} | |
public void EnemyStatsDown(){ | |
_eName.enabled = false; | |
_eClass.enabled = false; | |
_eHealth.enabled = false; | |
_eStrength.enabled = false; | |
_eDefence.enabled = false; | |
_eAccuracy.enabled = false; | |
}*/ | |
/*public void IronSword() | |
{ | |
itemToAdd = new Item("Iron Sword", true, false, 5, 10, 0); | |
Debug.Log(itemToAdd.Name); | |
Debug.Log(itemToAdd.Weapon); | |
Debug.Log(itemToAdd.Damage); | |
Debug.Log(itemToAdd.HitChance); | |
_items.Add(itemToAdd); | |
}*/ | |
} |
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class Grid : MonoBehaviour { | |
public Vector2 worldSize; | |
public float nodeRadius; | |
float nodeDiameter; | |
int gridSizeX, gridSizeY; | |
public LayerMask terrainMask; | |
public Node[,] grid; | |
[SerializeField] | |
public GameObject tilePrefab; | |
public List<List<Tile>> map = new List<List<Tile>>(); | |
public List<Node> path = new List<Node>(); | |
void Start() { | |
nodeDiameter = nodeRadius * 2; | |
gridSizeX = Mathf.RoundToInt(worldSize.x / nodeDiameter); | |
gridSizeY = Mathf.RoundToInt(worldSize.y / nodeDiameter); | |
CreateGrid(); | |
} | |
void Update() { | |
if (grid != null) { | |
foreach (Node n in grid) { | |
n.tile.rend.material.color = (n.walkable) ? Color.white : Color.red; | |
if (path != null) | |
if (path.Contains(n)) | |
n.tile.rend.material.color = Color.black; | |
} | |
} | |
} | |
void CreateGrid(){ | |
map = new List<List<Tile>>(); | |
grid = new Node[gridSizeX, gridSizeY]; | |
Vector3 worldBottomLeft = transform.position - Vector3.right * worldSize.x / 2 - Vector3.forward * worldSize.y / 2; | |
for (int x = 0; x < gridSizeX; x++) { | |
List<Tile> row = new List<Tile>(); | |
for (int y = 0; y < gridSizeX; y++) { | |
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius); | |
bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, terrainMask)); | |
Tile tile = ((GameObject)Instantiate(tilePrefab, new Vector3(worldPoint.x, -1, worldPoint.z), Quaternion.Euler(new Vector3()))).GetComponent<Tile>(); | |
grid[x, y] = new Node(walkable, worldPoint, x, y, tile); | |
tile.gridPosition = new Vector2(x, y); | |
tile.node = grid[x, y]; | |
row.Add(tile); | |
} | |
map.Add(row); | |
} | |
} | |
private void AddNeighbour(ref List<Node> neighbours, int neighbourX, int neighbourY) | |
{ | |
if (neighbourX >= 0 && neighbourX < gridSizeX && neighbourY >= 0 && neighbourY < gridSizeY) | |
{ | |
neighbours.Add(grid[neighbourX, neighbourY]); | |
} | |
} | |
public List<Node> GetNeighbours(Node node) { | |
List<Node> neighbours = new List<Node>(); | |
AddNeighbour(ref neighbours, node.gridX + 1, node.gridY); | |
AddNeighbour(ref neighbours, node.gridX - 1, node.gridY); | |
AddNeighbour(ref neighbours, node.gridX, node.gridY + 1); | |
AddNeighbour(ref neighbours, node.gridX, node.gridY - 1); | |
return neighbours; | |
} | |
public Node NodeFromWorldPoint(Vector3 worldPos) { | |
float percentX = (worldPos.x + worldSize.x / 2) / worldSize.x; | |
float percentY = (worldPos.z + worldSize.y / 2) / worldSize.y; | |
percentX = Mathf.Clamp01(percentX); | |
percentY = Mathf.Clamp01(percentY); | |
int x = Mathf.RoundToInt((gridSizeX - 1) * percentX); | |
int y = Mathf.RoundToInt((gridSizeY - 1) * percentY); | |
return grid[x, y]; | |
} | |
void OnDrawGizmos() { | |
Gizmos.DrawWireCube(transform.position, new Vector3(worldSize.x, 1, worldSize.y)); | |
if(grid != null) { | |
foreach (Node n in grid) | |
{ | |
Gizmos.color = (n.walkable) ? Color.white : Color.red; | |
if (path != null) | |
if (path.Contains(n)) | |
Gizmos.color = Color.black; | |
Gizmos.DrawCube(n.worldPos, Vector3.one * (nodeDiameter - .1f)); | |
} | |
} | |
} | |
} |
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 UnityEngine; | |
using System; | |
using System.Collections; | |
public class Item { | |
string name; | |
int damage; | |
int hitChance; | |
int heal; | |
public ItemType itemType; | |
void Start () { | |
} | |
void Update () { | |
} | |
public enum ItemType | |
{ | |
Weapon, | |
Tool | |
} | |
public Item (ItemType _itemType, string _name, int _damage, int _hitChance, int _heal) | |
{ | |
itemType = _itemType; | |
name = _name; | |
damage = _damage; | |
hitChance = _hitChance; | |
heal = _heal; | |
} | |
public string Name | |
{ | |
get { return name; } | |
} | |
public int Damage | |
{ | |
get { return damage; } | |
} | |
public int HitChance | |
{ | |
get { return hitChance; } | |
} | |
public int Heal | |
{ | |
get { return heal; } | |
} | |
} |
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class Items : MonoBehaviour { | |
public static Items sItems; | |
void Awake () { | |
sItems = this; | |
} | |
void Update () { | |
} | |
public void IronSword(Unit unit) | |
{ | |
Item item = new Item(0, "Iron Sword", 5, 10, 0); | |
unit.inventory.Add(item); | |
} | |
} |
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 UnityEngine; | |
using System.Collections; | |
public class Tile : MonoBehaviour | |
{ | |
public Vector3 gridPosition = Vector2.zero; | |
public Vector3 prevMovePos = Vector2.zero; | |
public Renderer rend; | |
public bool movingTile; | |
public bool blue; | |
public Node node; | |
public Vector2 nodePos; | |
public Pathfinding pathfinding; | |
public Grid grid; | |
void Start() { | |
pathfinding = GameObject.FindWithTag("A*").GetComponent<Pathfinding>(); | |
grid = GameObject.FindWithTag("A*").GetComponent<Grid>(); | |
rend = GetComponent<Renderer>(); | |
nodePos = new Vector2(node.gridX, node.gridY); | |
} | |
void Update() { | |
if (movingTile) | |
rend.material.color = Color.green; | |
if (!movingTile && !blue) | |
rend.material.color = Color.white; | |
} | |
void OnMouseEnter() { | |
if (GameManager.gm.units[GameManager.gm.currentUnitIndex].moving) { | |
pathfinding.target.transform.position = new Vector3(gameObject.transform.position.x, 1, gameObject.transform.position.z); | |
} | |
if (!blue && !GameManager.gm.units[GameManager.gm.currentUnitIndex].attacking) { | |
blue = true; | |
rend.material.color = Color.blue; | |
} | |
else if (GameManager.gm.units[GameManager.gm.currentUnitIndex].attacking) { | |
rend.material.color = Color.red; | |
} | |
} | |
void OnMouseExit() { | |
if (!movingTile) | |
rend.material.color = Color.white; | |
if (blue) | |
blue = false; | |
foreach (Unit u in GameManager.gm.units) { | |
if (u.gridPosition == gridPosition) { | |
} | |
} | |
} | |
void OnMouseDown() { | |
if (GameManager.gm.units[GameManager.gm.currentUnitIndex].moving) { | |
pathfinding.pathTraced = true; | |
StartCoroutine(GameManager.gm.moveCurrentPlayer()); | |
} | |
else if (GameManager.gm.units[GameManager.gm.currentUnitIndex].attacking) | |
GameManager.gm.attackWithCurrentPlayer(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment