DefenceRoom.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Sfs2X.Entities.Data;
  5. public class DefenceRoom : BARoom
  6. {
  7. public const int SEND_ITEM_INTERVAL = 20 * 1000;
  8. private int lastAddItemTime = 0;
  9. public DefenceRoom(BAServer server) : base(server)
  10. {
  11. lastAddItemTime = GetTime ();
  12. // lastAddItemTime -= SEND_ITEM_INTERVAL;
  13. AttempAddStation (0, TeamUtil.Team.Blue.GetHashCode ());
  14. AttempAddStation (1, TeamUtil.Team.Red.GetHashCode());
  15. AddDoor (0, TeamUtil.Team.Blue.GetHashCode());
  16. AddDoor (1, TeamUtil.Team.Blue.GetHashCode());
  17. AddDoor (2, TeamUtil.Team.Red.GetHashCode());
  18. AddDoor (3, TeamUtil.Team.Red.GetHashCode());
  19. itemPosDict.Add (0, new BAItemPos (0));
  20. }
  21. private void SendItem()
  22. {
  23. SFSArray items = new SFSArray();
  24. foreach(KeyValuePair<int, BAItemPos> kvp in itemPosDict)
  25. {
  26. BAItemPos itemPos = kvp.Value;
  27. if(itemPos.itemId == -1)
  28. {
  29. itemPos.itemId = MapItem.BIGGER;
  30. ISFSObject itemObj = new SFSObject ();
  31. itemObj.PutInt ("i", itemPos.id);
  32. itemObj.PutInt ("t", itemPos.itemId);
  33. items.AddSFSObject (itemObj);
  34. }
  35. }
  36. if (items.Size () > 0) {
  37. ISFSObject data = new SFSObject ();
  38. data.PutSFSArray ("l", items);
  39. server.Send (new Message(Command.AddMapItem, data));
  40. }
  41. }
  42. public override void RemoveStation (int id)
  43. {
  44. foreach(KeyValuePair<int, BAStation> kvp in stationDict)
  45. {
  46. BAStation station = kvp.Value;
  47. if(station.id == id)
  48. {
  49. stationDict.Remove (station.crystalId);
  50. keyStationDestroyed (station.teamId);
  51. return;
  52. }
  53. }
  54. }
  55. private void keyStationDestroyed(int teamId)
  56. {
  57. int oppTeamId = TeamUtil.GetOpponentTeam(teamId).GetHashCode();
  58. SendBattleInfo(GetScoreData(oppTeamId));
  59. }
  60. public SFSObject GetScoreData(int winTeam)
  61. {
  62. if(isGameOver)
  63. {
  64. return null;
  65. }
  66. int freeTime = GetFreeTime();
  67. int time = roundTime-GetGameTime();
  68. if(time<=0)
  69. {
  70. winTeam = FindWinTeam();
  71. }
  72. SFSObject data = new SFSObject();
  73. data.PutInt("t", time);
  74. data.PutInt("f", freeTime);
  75. if(winTeam > 0)
  76. DealGameOver(data, winTeam);
  77. return data;
  78. }
  79. private int FindWinTeam()
  80. {
  81. int blueDoorCount = 0;
  82. int redDoorCount = 0;
  83. foreach(KeyValuePair<int, BADoor> kvp in doorDict)
  84. {
  85. BADoor door = kvp.Value;
  86. if (door.teamId == TeamUtil.Team.Blue.GetHashCode ())
  87. blueDoorCount++;
  88. else if (door.teamId == TeamUtil.Team.Red.GetHashCode ())
  89. redDoorCount++;
  90. }
  91. if(blueDoorCount > redDoorCount)
  92. return TeamUtil.Team.Blue.GetHashCode();
  93. else if(blueDoorCount < redDoorCount)
  94. return TeamUtil.Team.Red.GetHashCode();
  95. return TeamUtil.Team.Yellow.GetHashCode();
  96. }
  97. public override void Update ()
  98. {
  99. base.Update ();
  100. if(scoreTaskRunning)
  101. {
  102. if (GetTime () - lastSendScoreTime >= scoreTaskInterval) {
  103. lastSendScoreTime = GetTime ();
  104. SendBattleInfo (GetScoreData (-1));
  105. }
  106. if(GetGameTime() - lastAddItemTime >= SEND_ITEM_INTERVAL)
  107. {
  108. lastAddItemTime = GetGameTime ();
  109. SendItem ();
  110. }
  111. }
  112. }
  113. }