Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using System.Collections.Generic;
  5.  
  6. public class Unit
  7. {
  8. public string Name = "Player";
  9. public int HitPoints = 2;
  10. public Powerup powerup = new Powerup ();
  11.  
  12.  
  13. public Hex Hex { get; protected set; }
  14.  
  15. public delegate void UnitMovedDelegate (Hex oldHex, Hex newHex);
  16.  
  17. public event UnitMovedDelegate OnUnitMoved;
  18.  
  19. public void Destroy (Hex h)
  20. {
  21. h.RemoveUnit (this);
  22. h.HexMap.score.CurrentScore++;;
  23. }
  24.  
  25. public bool SetHex (Hex newHex)
  26. {
  27. Hex oldHex = Hex;
  28. if (oldHex != null) {
  29. if (newHex.Active && Vector3.Distance (oldHex.Position (), newHex.Position ()) < 2f) {
  30. if (newHex.Units ().Count (x => x.Name == "Enemy") == 0) {
  31. if (Hex != null) {
  32. Hex.RemoveUnit (this);
  33. }
  34.  
  35. Hex = newHex;
  36.  
  37. Hex.AddUnit (this);
  38.  
  39.  
  40.  
  41. if (OnUnitMoved != null) {
  42. OnUnitMoved (oldHex, newHex);
  43. }
  44. } else {
  45. Unit enemy = newHex.Units ().FirstOrDefault ();
  46. enemy.Destroy (newHex);
  47. GameObject.Destroy (newHex.HexMap.GetUnitGO (enemy));
  48. return true;
  49. }
  50. return false;
  51. }
  52. return true;
  53. } else {
  54. if (Hex != null) {
  55. Hex.RemoveUnit (this);
  56. }
  57.  
  58. Hex = newHex;
  59.  
  60. Hex.AddUnit (this);
  61.  
  62.  
  63.  
  64. if (OnUnitMoved != null) {
  65. OnUnitMoved (oldHex, newHex);
  66. }
  67. }
  68. return false;
  69. }
  70.  
  71. public void DoTurn (Hex newHex)
  72. {
  73. if (SetHex (newHex))
  74. return;
  75.  
  76. if ((newHex.HexMap.Units.Count (x => x.Name == "Enemy") == 0)) {
  77. System.Collections.Generic.List<Hex> activeHexes = new System.Collections.Generic.List<Hex> ();
  78. foreach (Hex hex in newHex.HexMap.Hexes) {
  79. if (hex.Active) {
  80. activeHexes.Add (hex);
  81. }
  82. }
  83.  
  84. System.Random rand = new System.Random ();
  85. Hex h;
  86. do {
  87. h = (activeHexes.ToArray ()) [rand.Next (0, activeHexes.Count)];
  88. } while(h.Units ().Count () != 0);
  89. newHex.HexMap.SpawnUnitAt (new Unit () { Name = "Enemy" }, newHex.HexMap.EnemyPrefab, h.Q, h.R);
  90. }
  91.  
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement