您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 

146 行
3.5 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CIGEnums;
  5. using SUISS.Core;
  6. using SUISS.Scheduling;
  7. using SUISSEngine;
  8. using UnityEngine;
  9. public class Boid : MonoBehaviour
  10. {
  11. private int totalChance
  12. {
  13. get
  14. {
  15. return this.seperateChance + this.alignChance + this.cohesionChance;
  16. }
  17. }
  18. private void Start()
  19. {
  20. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this.moveBehaviour = this.MoveBehaviour());
  21. }
  22. private void OnDestroy()
  23. {
  24. Scheduler instanceIfAvailable = SingletonMonobehaviour<Scheduler>.InstanceIfAvailable;
  25. if (instanceIfAvailable != null && this.moveBehaviour != null)
  26. {
  27. instanceIfAvailable.StopRoutine(this.moveBehaviour);
  28. this.moveBehaviour = null;
  29. }
  30. }
  31. private IEnumerator MoveBehaviour()
  32. {
  33. for (;;)
  34. {
  35. GridPoint? gridPoint = this.globalTarget;
  36. if (gridPoint == null)
  37. {
  38. goto IL_7B;
  39. }
  40. GridPoint position = this.movingUnit.position;
  41. GridPoint? gridPoint2 = this.globalTarget;
  42. if (position.DistanceTo(gridPoint2.Value) < UnityEngine.Random.Range(1f, 4f))
  43. {
  44. goto IL_7B;
  45. }
  46. IL_9B:
  47. yield return Timing.time + (double)UnityEngine.Random.Range(2f, 5f);
  48. continue;
  49. IL_7B:
  50. this.globalTarget = null;
  51. this.ChangeDirection();
  52. goto IL_9B;
  53. }
  54. yield break;
  55. }
  56. private void ChangeDirection()
  57. {
  58. GridPoint gridPoint = new GridPoint(0f, 0f);
  59. GridPoint gridPoint2 = new GridPoint(0f, 0f);
  60. GridPoint gridPoint3 = new GridPoint(0f, 0f);
  61. foreach (Boid boid in this.animals)
  62. {
  63. if (!(boid == this))
  64. {
  65. GridPoint right = boid.movingUnit.position - this.movingUnit.position;
  66. if (right.u * right.u + right.v * right.v < this.seperationDistance * this.seperationDistance)
  67. {
  68. gridPoint -= right.times(1f / (float)this.animals.Count);
  69. }
  70. else
  71. {
  72. gridPoint3 += right;
  73. }
  74. gridPoint2 += boid.movingUnit.movement;
  75. }
  76. }
  77. int num = UnityEngine.Random.Range(0, this.totalChance);
  78. if (num < this.seperateChance)
  79. {
  80. this.movingUnit.SetTargetLocation(this.movingUnit.position + gridPoint);
  81. }
  82. else if (num < this.seperateChance + this.alignChance)
  83. {
  84. this.movingUnit.SetTargetLocation(this.movingUnit.position + gridPoint2);
  85. }
  86. else
  87. {
  88. this.movingUnit.SetTargetLocation(this.movingUnit.position + gridPoint3);
  89. }
  90. }
  91. public void SetHerd(List<Boid> an)
  92. {
  93. this.animals = an;
  94. }
  95. public void SetGlobalTarget(GridPoint t)
  96. {
  97. this.movingUnit.SetTargetLocation(t);
  98. this.globalTarget = new GridPoint?(t);
  99. }
  100. public static void RunAwayFrom(GridIndex t)
  101. {
  102. GridPoint gridPoint = new GridPoint(t);
  103. Boid[] array = UnityEngine.Object.FindObjectsOfType<Boid>();
  104. foreach (Boid boid in array)
  105. {
  106. IsometricGrid grid = boid.movingUnit.grid;
  107. float num = boid.movingUnit.position.DistanceTo(gridPoint);
  108. if (num <= 5f)
  109. {
  110. GridPoint gridPoint2 = gridPoint - boid.movingUnit.position;
  111. GridPoint point = boid.movingUnit.position - gridPoint2.normalized.times(5f);
  112. GridIndex index = new GridIndex(point);
  113. if (grid.IsWithinBounds(index) && boid.GetComponent<AttachedElementTypeList>().Check((SurfaceType)grid[index].Type))
  114. {
  115. boid.SetGlobalTarget(point);
  116. }
  117. }
  118. }
  119. }
  120. [SerializeField]
  121. private CIGMovingUnit movingUnit;
  122. public float seperationDistance = 1f;
  123. public int seperateChance = 15;
  124. public int alignChance = 3;
  125. public int cohesionChance = 10;
  126. private IEnumerator moveBehaviour;
  127. private GridPoint? globalTarget;
  128. private List<Boid> animals;
  129. }