Created
March 21, 2016 16:00
-
-
Save IronMonk-UK/a91493c9900ad03ed529 to your computer and use it in GitHub Desktop.
Post-Implementation of the Inventory System
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; | |
/* The _Class class is used currently to assign stats to units. When a unit is instantiated, one of the functions below is called, setting the stats for that unit. | |
* I have been recommended to create a class for every unit class in game. At this time, until I have implemented AI further, I will be sticking to this class for unit classes. */ | |
public class _Class : MonoBehaviour { | |
public static _Class cInstance; | |
public static void Barbarian(UserUnit u){ | |
u.totalHealth = 15; | |
u.strength = 3; | |
u.defence = 1; | |
u.accuracy = .6f; | |
u.damageRollSides = 8; | |
u.posClass = "Knight"; | |
u.negClass = "Mercenary"; | |
u.moveLimit = 6; | |
u.axe = true; | |
} | |
public static void Knight(UserUnit u){ | |
u._class = "Knight"; | |
u.totalHealth = 12; | |
u.strength = 2; | |
u.defence = 2; | |
u.accuracy = .7f; | |
u.damageRollSides = 7; | |
u.posClass = "Mercenary"; | |
u.negClass = "Barbarian"; | |
u.moveLimit = 8; | |
u.spear = true; | |
} | |
public static void Mercenary(UserUnit u){ | |
u._class = "Mercenary"; | |
u.totalHealth = 10; | |
u.strength = 2; | |
u.defence = 1; | |
u.accuracy = .9f; | |
u.damageRollSides = 6; | |
u.posClass = "Barbarian"; | |
u.negClass = "Knight"; | |
u.moveLimit = 6; | |
u.sword = true; | |
} | |
} |
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 Button inv1; | |
public Text _inv1; | |
public Button inv2; | |
public Text _inv2; | |
public Button inv3; | |
public Text _inv3; | |
public Button inv4; | |
public Text _inv4; | |
public Button inv5; | |
public Text _inv5; | |
public List<Button> inv = new List<Button>(); | |
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() | |
{ | |
// This foreach loop was used to help me work out whether units were being created before or after the grid, in an attempt to use Grid.grid | |
// and the relative tile positions to position units upon instantiation | |
foreach (Unit u in units) | |
{ | |
if (!u.startPos) | |
{ | |
Debug.Log("A"); | |
foreach (List<Tile> l in map) | |
{ | |
Debug.Log("B"); | |
foreach (Tile t in l) | |
{ | |
Debug.Log("C"); | |
Debug.Log(u.gridPosition + " " + t.gridPosition); | |
if (u.gridPosition == t.gridPosition) | |
{ | |
transform.position = t._pos; | |
u.startPos = true; | |
Debug.Log("At start pos"); | |
} | |
} | |
} | |
} | |
} | |
// This foreach loop is to ensure that a units grid position is always updating to its current grid position | |
foreach (Unit u in units) { | |
foreach (List<Tile> l in grid.map) { | |
foreach (Tile t in l) { | |
if (u.transform.position == new Vector3(t.transform.position.x, t.transform.position.y + 2, t.transform.position.z)) | |
u.gridPosition = t.gridPosition; | |
} | |
} | |
} | |
if (units[currentUnitIndex].inventory.Count == 1) { inventory1(); } | |
if (units[currentUnitIndex].inventory.Count == 2) { inventory2(); } | |
if (units[currentUnitIndex].inventory.Count == 3) { inventory3(); } | |
if (units[currentUnitIndex].inventory.Count == 4) { inventory4(); } | |
if (units[currentUnitIndex].inventory.Count == 5) { inventory5(); } | |
// This for loop runs to ensure that any usable item, such as the health vial | |
for(int i = 0; i < units[currentUnitIndex].inventory.Count; i++) { | |
// if the item in inventory index i has the ItemType "Tool" | |
if (units[currentUnitIndex].inventory[i].itemType == Item.ItemType.Tool) { | |
// and if the Tool has the name "Health Vial" | |
if (units[currentUnitIndex].inventory[i].Name == "Health Vial") { | |
// create a temporary item variable, that is the inventory item with index i | |
Item item = units[currentUnitIndex].inventory[i]; | |
// Ensure that any previous listeners are removed from the button | |
inv[i].onClick.RemoveAllListeners(); | |
// Adds the item method _heal to the button's onClick | |
inv[i].onClick.AddListener(delegate { item._heal(units[currentUnitIndex]); }); | |
} | |
} | |
} | |
/* 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].currentHealth > 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].currentHealth.ToString() + " / " + units[currentUnitIndex].totalHealth.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].equippedWeapon.posType == target.equippedWeapon.weaponType) | |
{ | |
posHit = Random.Range(0.0f, 1.0f) <= (units[currentUnitIndex].accuracy + .2f); | |
} | |
else if (units[currentUnitIndex].equippedWeapon.negType == target.equippedWeapon.weaponType) | |
{ | |
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 + 1) + units[currentUnitIndex].equippedWeapon.Damage - target.defence); | |
target.currentHealth -= 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 - 2) + units[currentUnitIndex].equippedWeapon.Damage - target.defence); | |
target.currentHealth -= 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 + units[currentUnitIndex].equippedWeapon.Damage - target.defence); | |
target.currentHealth -= 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(grid.worldSize.x / 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.IronAxe(unit); | |
Items.sItems.IronAxe(unit); | |
Items.sItems.healthVial(unit); | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3((mapSize - 1) - Mathf.Floor(grid.worldSize.x / 2) + .5f, 1, -(grid.worldSize.y - 1) + Mathf.Floor(mapSize / 2) - .5f), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2(grid.worldSize.x - 1, - 1); | |
unit._name = "Harrison"; | |
unit._class = "Mercenary"; | |
_Class.Mercenary(unit); | |
Items.sItems.IronSword(unit); | |
units.Add(unit); | |
unit = ((GameObject)Instantiate(userUnitPrefab, new Vector3(4 - Mathf.Floor(grid.worldSize.x / 2) + .5f, 1, -4 + Mathf.Floor(grid.worldSize.y / 2) - .5f), Quaternion.Euler(new Vector3()))).GetComponent<UserUnit>(); | |
unit.gridPosition = new Vector2(grid.worldSize.x / 2 - 1, grid.worldSize.y / 2 - 1); | |
unit._name = "Alexander"; | |
unit._class = "Knight"; | |
_Class.Knight(unit); | |
Items.sItems.IronSpear(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 inventory1() | |
{ | |
_inv1.text = units[currentUnitIndex].inventory[0].Name; | |
_inv2.text = null; | |
_inv3.text = null; | |
_inv4.text = null; | |
_inv5.text = null; | |
} | |
public void inventory2() | |
{ | |
_inv1.text = units[currentUnitIndex].inventory[0].Name; | |
_inv2.text = units[currentUnitIndex].inventory[1].Name; | |
_inv3.text = null; | |
_inv4.text = null; | |
_inv5.text = null; | |
} | |
public void inventory3() | |
{ | |
_inv1.text = units[currentUnitIndex].inventory[0].Name; | |
_inv2.text = units[currentUnitIndex].inventory[1].Name; | |
_inv3.text = units[currentUnitIndex].inventory[2].Name; | |
_inv4.text = null; | |
_inv5.text = null; | |
} | |
public void inventory4() | |
{ | |
_inv1.text = units[currentUnitIndex].inventory[0].Name; | |
_inv2.text = units[currentUnitIndex].inventory[1].Name; | |
_inv3.text = units[currentUnitIndex].inventory[2].Name; | |
_inv4.text = units[currentUnitIndex].inventory[3].Name; | |
_inv5.text = null; | |
} | |
public void inventory5() | |
{ | |
_inv1.text = units[currentUnitIndex].inventory[0].Name; | |
_inv2.text = units[currentUnitIndex].inventory[1].Name; | |
_inv3.text = units[currentUnitIndex].inventory[2].Name; | |
_inv4.text = units[currentUnitIndex].inventory[3].Name; | |
_inv5.text = units[currentUnitIndex].inventory[4].Name; | |
} | |
} |
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; | |
public WeaponType weaponType; | |
public WeaponType posType; | |
public WeaponType negType; | |
void Start () { | |
} | |
void Update () { | |
if(weaponType == WeaponType.Axe) | |
{ | |
posType = WeaponType.Spear; | |
negType = WeaponType.Sword; | |
} else if (weaponType == WeaponType.Sword) | |
{ | |
posType = WeaponType.Axe; | |
negType = WeaponType.Spear; | |
} else if (weaponType == WeaponType.Spear) | |
{ | |
posType = WeaponType.Sword; | |
negType = WeaponType.Axe; | |
} | |
} | |
public enum ItemType | |
{ | |
Weapon, | |
Tool | |
} | |
public enum WeaponType | |
{ | |
None, | |
Sword, | |
Axe, | |
Spear | |
} | |
public Item (ItemType _itemType, WeaponType _weaponType, string _name, int _damage, int _hitChance, int _heal) | |
{ | |
itemType = _itemType; | |
weaponType = _weaponType; | |
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; } | |
} | |
public void _heal(Unit unit) | |
{ | |
if(unit.currentHealth < unit.totalHealth) { | |
unit.currentHealth += heal; | |
if(unit.currentHealth > unit.totalHealth) { | |
unit.currentHealth = unit.totalHealth; | |
} | |
unit.inventory.Remove(this); | |
unit.actions = 0; | |
} | |
} | |
} |
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(Item.ItemType.Weapon, Item.WeaponType.Sword, "Iron Sword", 5, 10, 0); | |
unit.inventory.Add(item); | |
} | |
public void IronAxe(Unit unit) | |
{ | |
Item item = new Item(Item.ItemType.Weapon, Item.WeaponType.Axe, "Iron Axe", 8, 7, 0); | |
unit.inventory.Add(item); | |
} | |
public void IronSpear(Unit unit) | |
{ | |
Item item = new Item(Item.ItemType.Weapon, Item.WeaponType.Spear, "Iron Spear", 7, 8, 0); | |
unit.inventory.Add(item); | |
} | |
public void healthVial(Unit unit) | |
{ | |
Item item = new Item(Item.ItemType.Tool, Item.WeaponType.None, "Health Vial", 0, 0, 10); | |
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 Vector3 _pos; | |
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); | |
_pos = new Vector3(transform.position.x, 1, transform.position.z); | |
} | |
void Update() { | |
if (movingTile) | |
rend.material.color = Color.green; | |
// This if statement has been altered, so the path is visibly shown to the player now | |
if (!movingTile && !blue && !grid.path.Contains(node)) | |
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; | |
} | |
foreach (Unit u in GameManager.gm.units) | |
{ | |
if (u.gridPosition == gridPosition && u != GameManager.gm.units[GameManager.gm.currentUnitIndex]) | |
{ | |
Debug.Log("Unit found"); | |
GameManager.gm.EnemyStatsUp(); | |
GameManager.gm._eName.text = u._name; | |
GameManager.gm._eClass.text = u._class; | |
GameManager.gm._eHealth.text = u.currentHealth.ToString() + " / " + u.totalHealth.ToString(); | |
GameManager.gm._eStrength.text = u.strength.ToString(); | |
GameManager.gm._eDefence.text = u.defence.ToString(); | |
GameManager.gm._eAccuracy.text = u.accuracy.ToString(); | |
} | |
} | |
} | |
void OnMouseExit() { | |
if (!movingTile) | |
rend.material.color = Color.white; | |
if (blue) | |
blue = false; | |
foreach (Unit u in GameManager.gm.units) { | |
if (u.gridPosition == gridPosition) { | |
GameManager.gm.EnemyStatsDown(); | |
} | |
} | |
} | |
void OnMouseDown() { | |
if (GameManager.gm.units[GameManager.gm.currentUnitIndex].moving) { | |
pathfinding.pathTraced = true; | |
StartCoroutine(GameManager.gm.moveCurrentPlayer()); | |
GameManager.gm.units[GameManager.gm.currentUnitIndex].gridPosition = gridPosition; | |
} | |
else if (GameManager.gm.units[GameManager.gm.currentUnitIndex].attacking) | |
GameManager.gm.attackWithCurrentPlayer(this); | |
} | |
} |
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 Unit : MonoBehaviour { | |
public Vector3 gridPosition = Vector2.zero; | |
[SerializeField] public List<Item> inventory = new List<Item>(); | |
public Item currentWeapon; | |
public bool moving = false; | |
public bool moved = false; | |
public bool attacking = false; | |
public int moveSpeed = 1; | |
public int actions = 2; | |
public int totalHealth; | |
public int currentHealth; | |
public int strength; | |
public int defence; | |
public int moveLimit; | |
public float accuracy; | |
public float damageRollSides; | |
public string _name; | |
public string _class; | |
public string posClass; | |
public string negClass; | |
public bool sword; | |
public bool axe; | |
public bool spear; | |
public bool startPos; | |
public Item equippedWeapon; | |
void Start() | |
{ | |
currentHealth = totalHealth; | |
} | |
void FixedUpdate() | |
{ | |
if (!startPos) { | |
foreach (List<Tile> l in GameManager.gm.map) { | |
foreach (Tile t in l) { | |
if (t.gridPosition == gridPosition) { | |
transform.position = t._pos; | |
startPos = true; | |
Debug.Log("At start pos"); | |
} | |
} | |
} | |
} | |
if(inventory[0].itemType == Item.ItemType.Weapon) | |
{ | |
equippedWeapon = inventory[0]; | |
} | |
} | |
// By being a virtual function, this allows child classes to override this function. | |
public virtual void turnUpdate(){ | |
// This if function makes sure that when a unit, either player or AI controlled, has used all its actions, the actions counter, and movement state booleans are reset. | |
if (actions <= 0) { | |
actions = 2; | |
moving = false; | |
attacking = false; | |
moved = false; | |
GameManager.gm.nextTurn(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment