InstanceNature.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using CommonAI.RTS; using CommonLang.Vector;
  2. using CommonAI.Zone.Helper;
  3. using CommonAI.Zone.Formula;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace CommonAI.Zone.Instance
  9. {
  10. /// <summary>
  11. /// 中立AI
  12. /// </summary>
  13. public class InstanceNature : InstanceUnit
  14. {
  15. private Vector2 mBasePos = new Vector2();
  16. private HateSystem mHateSystem;
  17. private StateFollowAndAttack mTracing;
  18. override public bool IsNature { get { return true; } }
  19. public InstanceNature(InstanceZone zone, UnitInfo info, string name, int force, int level)
  20. : base(zone, info, name, force, level)
  21. {
  22. mHateSystem = TemplateManager.Factory.CreateHateSystem(this);
  23. }
  24. override protected void onResetAI()
  25. {
  26. mTracing = null;
  27. mHateSystem.Clear();
  28. doSomething();
  29. }
  30. public bool followAndAttack(InstanceUnit src, AttackReason reason)
  31. {
  32. if (IsNoneSkill)
  33. {
  34. return false;
  35. }
  36. if (src != null)
  37. {
  38. if (Parent.IsAttackable(this, src, SkillTemplate.CastTarget.Enemy, reason, Info))
  39. {
  40. mHateSystem.Add(src);
  41. if ((mTracing == null) || (mTracing.Target != src))
  42. {
  43. mTracing = new StateFollowAndAttack(this, src);
  44. }
  45. changeState(mTracing);
  46. return true;
  47. }
  48. else
  49. {
  50. mHateSystem.Remove(src);
  51. }
  52. }
  53. return false;
  54. }
  55. protected override void onAdded(bool pointLv)
  56. {
  57. mBasePos.SetX(X);
  58. mBasePos.SetY(Y);
  59. base.onAdded(pointLv);
  60. }
  61. override protected void onUpdate(bool slowRefresh)
  62. {
  63. base.onUpdate(slowRefresh);
  64. }
  65. protected override void onStateChanged(State old_state, State state)
  66. {
  67. if (state is StateIdle)
  68. {
  69. followAndAttack(mHateSystem.GetHated(), AttackReason.Tracing);
  70. }
  71. }
  72. protected override void onMoveBlockWithObject(InstanceZoneObject obj)
  73. {
  74. if (obj is InstanceUnit)
  75. {
  76. var unit = obj as InstanceUnit;
  77. if (unit is InstancePet)
  78. {
  79. followAndAttack((unit as InstancePet).Master, AttackReason.MoveBlocked);
  80. return;
  81. }
  82. followAndAttack(obj as InstanceUnit, AttackReason.MoveBlocked);
  83. }
  84. }
  85. protected override void onDamaged(InstanceUnit attacker, AttackSource attack, int reduceHP)
  86. {
  87. if (attacker is InstancePet)
  88. {
  89. attacker = (attacker as InstancePet).Master;
  90. }
  91. mHateSystem.OnHitted(attacker, attack, reduceHP);
  92. followAndAttack(attacker, AttackReason.Damaged);
  93. }
  94. }
  95. //--------------------------------------------------------------------------------------------------------
  96. }