AbstractBattle.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonLang.Protocol;
  5. using CommonAI.ZoneClient;
  6. using CommonAI.Zone;
  7. using RoomService.Net.BsServer;
  8. using CommonLang;
  9. using CommonAI.RTS;
  10. using CommonLang.Vector;
  11. using CommonAI.Zone.ZoneEditor;
  12. using CommonAI;
  13. using CommonLang.Concurrent;
  14. namespace CommonAIClient.Client
  15. {
  16. public abstract class AbstractBattle : Disposable, ILayerClient
  17. {
  18. private static AtomicInteger s_alloc_object_count = new AtomicInteger(0);
  19. private static AtomicInteger s_active_object_count = new AtomicInteger(0);
  20. /// <summary>
  21. /// 未释放实例数量
  22. /// </summary>
  23. public static int ActiveZoneLayerCount { get { return s_active_object_count.Value; } }
  24. /// <summary>
  25. /// 分配实例数量
  26. /// </summary>
  27. public static int AllocZoneLayerCount { get { return s_alloc_object_count.Value; } }
  28. private ZoneLayer layer;
  29. //private SyncMessageQueue<Task> task_queue = new SyncMessageQueue<Task>();
  30. private List<Task> task_queue = new List<Task>();
  31. public ZoneLayer Layer
  32. {
  33. get { return layer; }
  34. }
  35. public ZoneActor Actor
  36. {
  37. get { return layer.Actor; }
  38. }
  39. public EditorTemplates DataRoot
  40. {
  41. get;
  42. private set;
  43. }
  44. public AbstractBattle(EditorTemplates datas)
  45. {
  46. s_alloc_object_count++;
  47. s_active_object_count++;
  48. this.DataRoot = datas;
  49. this.layer = TemplateManager.Factory.CreateClientZoneLayer(datas, this);
  50. }
  51. ~AbstractBattle()
  52. {
  53. s_alloc_object_count--;
  54. }
  55. protected override void Disposing()
  56. {
  57. //lock (task_queue)
  58. {
  59. this.task_queue.Clear();
  60. }
  61. this.layer.Dispose();
  62. s_active_object_count--;
  63. }
  64. // public virtual ZoneLayer GenLayer()
  65. // {
  66. // return new ZoneLayer(DataRoot, this);
  67. // }
  68. public abstract bool IsNet { get; }
  69. public abstract int CurrentPing { get; }
  70. public abstract long RecvPackages { get; }
  71. public abstract long SendPackages { get; }
  72. public abstract KickedByServerNotifyB2C KickMessage { get; }
  73. public virtual void BeginUpdate(int intervalMS)
  74. {
  75. this.layer.BeginUpdate(intervalMS);
  76. }
  77. public virtual void Update()
  78. {
  79. //this.task_queue.ProcessMessages(doTask);
  80. //lock (task_queue)
  81. {
  82. var num = task_queue.Count;
  83. for (int i = 0; i < num; i++)
  84. {
  85. doTask(task_queue[i]);
  86. }
  87. task_queue.RemoveRange(0, num);
  88. }
  89. this.layer.Update();
  90. }
  91. //-------------------------------------------------------------------
  92. public abstract void SendAction(CommonAI.Zone.Action action);
  93. public virtual void BattleReady(bool bok) { }
  94. //-------------------------------------------------------------------
  95. public void QueueTask(Task task)
  96. {
  97. //task_queue.Enqueue(task);
  98. //lock (task_queue)
  99. {
  100. task_queue.Add(task);
  101. }
  102. }
  103. private void doTask(Task task)
  104. {
  105. task(this);
  106. }
  107. public delegate void Task(AbstractBattle bc);
  108. }
  109. }