TiliTime.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. using System.Collections;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. public class TiliTime : MonoSingleton<TiliTime>
  6. {
  7. private Player player = null;
  8. /// <summary>
  9. /// 间隔计数用.
  10. /// </summary>
  11. private int mIntx = 0;
  12. /// <summary>
  13. /// 上一次计算的时间
  14. /// </summary>
  15. private long mLastTime = 0;
  16. /// <summary>
  17. /// 单键初始化
  18. /// </summary>
  19. protected override void OnAwake()
  20. {
  21. player = UserProxy.Instance.player;
  22. mLastTime = player.PrivateState.TiliTime;
  23. base.OnAwake();
  24. }
  25. public void Init()
  26. {
  27. player = UserProxy.Instance.player;
  28. mLastTime = player.PrivateState.TiliTime;
  29. }
  30. // Use this for initialization
  31. void Start()
  32. {
  33. player = UserProxy.Instance.player;
  34. mLastTime = player.PrivateState.TiliTime;
  35. }
  36. // Update is called once per frame
  37. void Update()
  38. {
  39. if (mIntx > 10)
  40. {
  41. mIntx = 0;
  42. long tempTime = GetTimeStamp() - mLastTime;
  43. if (tempTime > 300)
  44. {
  45. int temptili = (int)( (int)(tempTime / 300));
  46. mLastTime = GetTimeStamp();
  47. if (!CheckTiliIsFull())
  48. {
  49. AddTili(temptili);
  50. }
  51. }
  52. }
  53. mIntx++;
  54. }
  55. /// <summary>
  56. /// 检测体力值是否已经满了
  57. /// </summary>
  58. /// <returns></returns>
  59. private bool CheckTiliIsFull()
  60. {
  61. if (player.baseInfo.tili > 119)
  62. {
  63. return true;
  64. }
  65. return false;
  66. }
  67. private void AddTili(int temptili)
  68. {
  69. player.baseInfo.tili += temptili;
  70. // LogHelper.Log("检测溢出"+ player.curPower);
  71. if (player.baseInfo.tili > 120)
  72. {
  73. player.baseInfo.tili = 120;
  74. }
  75. player.myAccountChangedDelegate?.Invoke();
  76. }
  77. /// <summary>
  78. /// 发送获取体力的请求
  79. /// </summary>
  80. public void SendTili()
  81. {
  82. }
  83. public void InitTili()
  84. {
  85. }
  86. public void SetTiliTime(long temptime)
  87. {
  88. mLastTime = temptime;
  89. if (null != UserProxy.Instance.player.myAccountChangedDelegate)
  90. {
  91. UserProxy.Instance.player.myAccountChangedDelegate.Invoke();
  92. }
  93. }
  94. public long GetTiliTime()
  95. {
  96. return mLastTime;
  97. }
  98. /// <summary>
  99. /// 获取当前时间戳
  100. /// </summary>
  101. /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.</param>
  102. /// <returns></returns>
  103. public static long GetTimeStamp() => UserProxy.Instance.GetCurrentUnixTimeStamp();
  104. }