Guest User

Untitled

a guest
Jan 17th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. public class SphereContainsDataState : BaseAI.DataAIState
  2. {
  3. protected float sphereRadius = 0.0f;
  4. protected LayerMask layerMask;
  5. protected Vector3 offsetPos = Vector3.zero;
  6. protected Quaternion offsetRot = Quaternion.identity;
  7. protected Matrix4x4 parentTransform = Matrix4x4.identity;
  8.  
  9. protected Collider[] overlappedColliders = null;
  10. protected int maxOverlappedColliders = 20;
  11.  
  12. public SphereContainsDataState(BaseAI newThinker) : base(newThinker) { }
  13.  
  14. protected override void UpdateData(float deltaTime)
  15. {
  16. Vector3 spherePos = Vector3.zero;
  17. Quaternion sphereRot = Quaternion.identity; // (we don't use this, but the calc needs it anyways)
  18.  
  19. MathManager.CalcRelativeTransforms(parentTransform, offsetPos, offsetRot, out spherePos, out sphereRot);
  20.  
  21. if ((overlappedColliders == null) || (overlappedColliders.Length != maxOverlappedColliders)) { overlappedColliders = new Collider[maxOverlappedColliders]; }
  22. Physics.OverlapSphereNonAlloc(spherePos, sphereRadius, overlappedColliders, layerMask);
  23.  
  24. //TemporaryShape.DebugBox(new Vector3(sphereRadius * 2, sphereRadius * 2, sphereRadius * 2), 0.1f, spherePos, sphereRot);
  25.  
  26. base.UpdateData(deltaTime);
  27. }
  28. }
  29.  
  30. public class CanSeeDataState : SphereContainsDataState
  31. {
  32. // The radius of the vision sphere
  33. protected float viewRadius = 10.0f;
  34. // The local offset from the parent to the center of the vision sphere
  35. protected Vector3 visionOffset = Vector3.zero;
  36. // (if NULL, we just assume the vision cone is attached to the EnemyAI we're processing on)
  37. protected GameObject visionAttachedTo = null;
  38. // Should we update the parameters we use for our vision sweep?
  39. protected bool visionCheckParamsDirty = true;
  40.  
  41. public CanSeeDataState(BaseAI newThinker) : base(newThinker) { }
  42.  
  43. public CanSeeDataState(BaseAI newThinker, LayerMask visibleLayers, float newRadius, Vector3 newOffset) : base(newThinker)
  44. {
  45. layerMask = visibleLayers;
  46. viewRadius = newRadius;
  47. visionOffset = newOffset;
  48. }
  49.  
  50. protected override void UpdateData(float deltaTime)
  51. {
  52. if (visionCheckParamsDirty)
  53. {
  54. UpdateVisionCheckParameters();
  55. visionCheckParamsDirty = false;
  56. }
  57.  
  58. base.UpdateData(deltaTime);
  59.  
  60. // @TODO: Run over the overlappedColliders, and throw out any that can't actually be seen for whatever reason. Like maybe they have an invisibility state, or similar
  61. // AI-driven reasons we can't actually see them right now.
  62. }
  63.  
  64. protected virtual void UpdateVisionCheckParameters()
  65. {
  66. sphereRadius = viewRadius;
  67. offsetPos = visionOffset;
  68. if (visionAttachedTo != null) { parentTransform = visionAttachedTo.gameObject.transform.localToWorldMatrix; }
  69. else { parentTransform = ourAI.gameObject.transform.localToWorldMatrix; }
  70. }
  71. }
  72.  
  73. public class EnemyCanSeeDataState : CanSeeDataState
  74. {
  75. // Block/ignore the vision parameter constructors in base. You don't need to specify vision settings through the state, it draws on EnemyAI for that.
  76. public EnemyCanSeeDataState(BaseAI newThinker) : base(newThinker) { }
  77.  
  78. protected override void UpdateVisionCheckParameters()
  79. {
  80. EnemyAI ourEnemyAI = (EnemyAI)ourAI;
  81.  
  82. sphereRadius = ourEnemyAI.visionRadius;
  83. offsetPos = ourEnemyAI.visionOffset;
  84. parentTransform = ourEnemyAI.gameObject.transform.localToWorldMatrix;
  85. }
  86. }
  87.  
  88. public class ForgiveUnseenDataState : EnemyCanSeeDataState
  89. {
  90. protected Dictionary<GameObject, float> forgivenessAccumulator = new Dictionary<GameObject, float>();
  91. protected Dictionary<GameObject, LifeAndDeath> forgivenObjToLife = new Dictionary<GameObject, LifeAndDeath>();
  92. protected const float ForgivenessTimeout = 5.0f;
  93.  
  94. public ForgiveUnseenDataState(BaseAI newThinker) : base(newThinker) { }
  95.  
  96. protected override void UpdateData(float deltaTime)
  97. {
  98. EnemyAI ourEnemyAI = (EnemyAI)ourAI;
  99. if (ourEnemyAI == null) { return; }
  100.  
  101. base.UpdateData(deltaTime);
  102.  
  103. // First, make sure everything presently in our HateList is represented in our forgiveness list. If it wasn't already there, we assume it's fresh hate (delicious, fresh hate)
  104. foreach (KeyValuePair<LifeAndDeath, float> hate in ourEnemyAI.HateList)
  105. {
  106. if (!forgivenessAccumulator.ContainsKey(hate.Key.gameObject))
  107. {
  108. forgivenessAccumulator.Add(hate.Key.gameObject,-deltaTime);
  109. forgivenObjToLife.Add(hate.Key.gameObject, hate.Key);
  110. }
  111. }
  112.  
  113. // Then, go through and additionally reset forgiveness on anything we can see
  114. for (int index = 0; index < overlappedColliders.Length; index++)
  115. {
  116. if (forgivenessAccumulator.ContainsKey(overlappedColliders[index].gameObject))
  117. {
  118. forgivenessAccumulator[overlappedColliders[index].gameObject] = -deltaTime;
  119. }
  120. }
  121.  
  122. // Finally, accrue forgiveness time on everything (which is why we initialized to -deltaTime). Anything that reaches forgiveness threshold - forgive!
  123. List<KeyValuePair<GameObject, float>> forgivenessAccumulatorList = new List<KeyValuePair<GameObject, float>>(forgivenessAccumulator);
  124. foreach (KeyValuePair<GameObject, float> forgivenessPair in forgivenessAccumulatorList)
  125. {
  126. float newForgiveness = forgivenessPair.Value + deltaTime;
  127. if (newForgiveness >= ForgivenessTimeout) { Forgive(ourEnemyAI, forgivenessPair.Key); }
  128. else
  129. {
  130. forgivenessAccumulator[forgivenessPair.Key] = newForgiveness;
  131. }
  132. }
  133. }
  134.  
  135. protected void Forgive(EnemyAI forgivingAI, GameObject receivingForgiveness)
  136. {
  137. forgivenessAccumulator.Remove(receivingForgiveness);
  138. forgivingAI.ClearHate(forgivenObjToLife[receivingForgiveness]);
  139. forgivenObjToLife.Remove(receivingForgiveness);
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment