Guest User

PlayerInTown.cs

a guest
Nov 18th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using SharpDX.DirectInput;
  5. using System.Globalization;
  6. using Turbo.Plugins.Default;
  7.  
  8. namespace Turbo.Plugins.gjuz
  9. {
  10. public class PlayerInTown : BasePlugin, IInGameTopPainter, IAfterCollectHandler, INewAreaHandler, IKeyEventHandler
  11. {
  12. private Dictionary<int, IWatch> TownWatch { get; set; }
  13. private int PlayerCount { get; set; }
  14. private bool Show { get; set; }
  15.  
  16. public bool AlwaysStopTime { get; set; }
  17. public bool OnlyShowOnBountyTable { get; set; }
  18. public IKeyEvent ToggleKeyEvent { get; set; }
  19. public string isInTown { get; set; }
  20. public string notInTown { get; set; }
  21.  
  22. public IFont PortraitInfoFont { get; set; }
  23. public float OffsetXmultiplier { get; set; }
  24. public float OffsetYmultiplier { get; set; }
  25.  
  26. public PlayerInTown()
  27. {
  28. Enabled = true;
  29.  
  30. Show = false;
  31. AlwaysStopTime = false;
  32. OnlyShowOnBountyTable = false;
  33. }
  34.  
  35. public override void Load(IController hud)
  36. {
  37. base.Load(hud);
  38.  
  39. ToggleKeyEvent = Hud.Input.CreateKeyEvent(true, Key.F6, false, false, false);
  40.  
  41. TownWatch = new Dictionary<int, IWatch>();
  42. for (int i = 0; i < 4; i++)
  43. TownWatch.Add(i,Hud.Time.CreateWatch());
  44.  
  45. PortraitInfoFont = Hud.Render.CreateFont("tahoma", 7f, 255, 180, 147, 109, false, false, true);
  46. OffsetXmultiplier = 1.05f; //relative to portraitRect.Height
  47. OffsetYmultiplier = 0.95f; //relative to portraitRect.Width
  48.  
  49. isInTown = "in Town";
  50. notInTown = "TownTime";
  51. }
  52.  
  53. public void PaintTopInGame(ClipState clipState)
  54. {
  55. if (clipState != ClipState.BeforeClip) return;
  56. if (!Show) return;
  57.  
  58. foreach (IPlayer player in Hud.Game.Players)
  59. DrawPlayerInfo(player);
  60. }
  61.  
  62. private void DrawPlayerInfo(IPlayer player)
  63. {
  64. var portraitRect = player.PortraitUiElement.Rectangle;
  65. var OffsetX = portraitRect.Width * OffsetXmultiplier;
  66. var OffsetY = portraitRect.Height * OffsetYmultiplier;
  67. var YPos = portraitRect.Y + OffsetY;
  68. var XPos = portraitRect.X + OffsetX;
  69.  
  70. string text = String.Format("{0}: {1:mm\\:ss}",
  71. (player.IsInTown ? isInTown : notInTown),
  72. TimeSpan.FromMilliseconds( TownWatch[player.Index].ElapsedMilliseconds ));
  73.  
  74. var Layout = PortraitInfoFont.GetTextLayout(text);
  75. PortraitInfoFont.DrawText(Layout, XPos, YPos);
  76. }
  77.  
  78. public void AfterCollect()
  79. {
  80. //set watches
  81. TownWatches();
  82.  
  83. //resets not used stopwatches
  84. if (PlayerCount != Hud.Game.Players.Count())
  85. {
  86. List<int> l = new List<int> {0,1,2,3};
  87. foreach (var player in Hud.Game.Players)
  88. l.Remove(player.Index);
  89.  
  90. foreach (int i in l)
  91. ResetTownWatch(i);
  92.  
  93. PlayerCount = Hud.Game.Players.Count();
  94. }
  95. }
  96.  
  97. private void TownWatches()
  98. {
  99. if (!AlwaysStopTime && !Show) return; //if AlwaysStopTime = false: only stops time if bounty table was shown once (F6 key)
  100.  
  101. foreach (var player in Hud.Game.Players)
  102. {
  103. IWatch playerWatch = TownWatch[player.Index];
  104. if (player.IsInTown && !playerWatch.IsRunning)
  105. {
  106. playerWatch.Start();
  107. }
  108. else if (!player.IsInTown && playerWatch.IsRunning)
  109. {
  110. playerWatch.Stop();
  111. }
  112. }
  113. }
  114.  
  115. public void OnNewArea(bool newGame, ISnoArea area)
  116. {
  117. if (newGame)
  118. {
  119. Show = false;
  120.  
  121. PlayerCount = Hud.Game.Players.Count();
  122.  
  123. //reset
  124. ResetTownWatch();
  125. }
  126. }
  127.  
  128. private void ResetTownWatch()
  129. {
  130. // reset states if needed
  131. foreach (IWatch watch in TownWatch.Values)
  132. {
  133. if (watch.IsRunning || watch.ElapsedMilliseconds > 0)
  134. {
  135. watch.Reset();
  136. }
  137. }
  138. }
  139.  
  140. private void ResetTownWatch(int playerIndex)
  141. {
  142. // reset states if needed
  143. IWatch watch = TownWatch[playerIndex];
  144. if (watch.IsRunning || watch.ElapsedMilliseconds > 0)
  145. {
  146. watch.Reset();
  147. }
  148. }
  149.  
  150. public void OnKeyEvent(IKeyEvent keyEvent)
  151. {
  152. if (keyEvent.IsPressed && ToggleKeyEvent.Matches(keyEvent))
  153. {
  154. if (!Show) //toggle on if pressed F6
  155. Show = !Show; //no off switch !!
  156. else if(Show && OnlyShowOnBountyTable)
  157. Show = !Show;
  158. }
  159. }
  160. }
  161. }
Add Comment
Please, Sign In to add comment