Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Linq;
- using NUnit.Framework;
- using System.Collections.Generic;
- public class Unit
- {
- public string Name = "Player";
- public int HitPoints = 2;
- public Powerup powerup = new Powerup ();
- public Hex Hex { get; protected set; }
- public delegate void UnitMovedDelegate (Hex oldHex, Hex newHex);
- public event UnitMovedDelegate OnUnitMoved;
- public void Destroy (Hex h)
- {
- h.RemoveUnit (this);
- h.HexMap.score.CurrentScore++;;
- }
- public bool SetHex (Hex newHex)
- {
- Hex oldHex = Hex;
- if (oldHex != null) {
- if (newHex.Active && Vector3.Distance (oldHex.Position (), newHex.Position ()) < 2f) {
- if (newHex.Units ().Count (x => x.Name == "Enemy") == 0) {
- if (Hex != null) {
- Hex.RemoveUnit (this);
- }
- Hex = newHex;
- Hex.AddUnit (this);
- if (OnUnitMoved != null) {
- OnUnitMoved (oldHex, newHex);
- }
- } else {
- Unit enemy = newHex.Units ().FirstOrDefault ();
- enemy.Destroy (newHex);
- GameObject.Destroy (newHex.HexMap.GetUnitGO (enemy));
- return true;
- }
- return false;
- }
- return true;
- } else {
- if (Hex != null) {
- Hex.RemoveUnit (this);
- }
- Hex = newHex;
- Hex.AddUnit (this);
- if (OnUnitMoved != null) {
- OnUnitMoved (oldHex, newHex);
- }
- }
- return false;
- }
- public void DoTurn (Hex newHex)
- {
- if (SetHex (newHex))
- return;
- if ((newHex.HexMap.Units.Count (x => x.Name == "Enemy") == 0)) {
- System.Collections.Generic.List<Hex> activeHexes = new System.Collections.Generic.List<Hex> ();
- foreach (Hex hex in newHex.HexMap.Hexes) {
- if (hex.Active) {
- activeHexes.Add (hex);
- }
- }
- System.Random rand = new System.Random ();
- Hex h;
- do {
- h = (activeHexes.ToArray ()) [rand.Next (0, activeHexes.Count)];
- } while(h.Units ().Count () != 0);
- newHex.HexMap.SpawnUnitAt (new Unit () { Name = "Enemy" }, newHex.HexMap.EnemyPrefab, h.Q, h.R);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement