You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Moveable.cs 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System.Threading;
  4. namespace GameClass.GameObj
  5. {
  6. /// <summary>
  7. /// 一切游戏元素的总基类,与THUAI4不同,继承IMoveable接口(出于一切物体其实都是可运动的指导思想)——LHR
  8. /// </summary>
  9. public abstract class Moveable : GameObj, IMoveable
  10. {
  11. protected readonly object moveObjLock = new();
  12. public object MoveLock => moveObjLock;
  13. public override XY Position
  14. {
  15. get
  16. {
  17. lock (gameObjLock)
  18. return position;
  19. }
  20. set
  21. {
  22. lock (gameObjLock)
  23. {
  24. position = value;
  25. }
  26. }
  27. }
  28. private bool isMoving;
  29. public bool IsMoving
  30. {
  31. get => isMoving;
  32. set
  33. {
  34. lock (gameObjLock)
  35. {
  36. isMoving = value;
  37. }
  38. }
  39. }
  40. public bool IsAvailable => !IsMoving && CanMove && !IsResetting; // 是否能接收指令
  41. protected int moveSpeed;
  42. /// <summary>
  43. /// 移动速度
  44. /// </summary>
  45. public int MoveSpeed
  46. {
  47. get => moveSpeed;
  48. set
  49. {
  50. lock (gameObjLock)
  51. {
  52. moveSpeed = value;
  53. }
  54. }
  55. }
  56. /// <summary>
  57. /// 原初移动速度
  58. /// </summary>
  59. public int OrgMoveSpeed { get; protected set; }
  60. // 移动,改变坐标
  61. public long MovingSetPos(XY moveVec)
  62. {
  63. if (moveVec.x != 0 || moveVec.y != 0)
  64. lock (gameObjLock)
  65. {
  66. FacingDirection = moveVec;
  67. this.Position += moveVec;
  68. }
  69. return moveVec * moveVec;
  70. }
  71. /// <summary>
  72. /// 设置移动速度
  73. /// </summary>
  74. /// <param name="newMoveSpeed">新速度</param>
  75. public void SetMoveSpeed(int newMoveSpeed)
  76. {
  77. MoveSpeed = newMoveSpeed;
  78. }
  79. /* /// <summary>
  80. /// 复活时数据重置
  81. /// </summary>
  82. public virtual void Reset(PlaceType place)
  83. {
  84. lock (gameObjLock)
  85. {
  86. this.FacingDirection = new XY(1, 0);
  87. isMoving = false;
  88. CanMove = false;
  89. IsResetting = true;
  90. this.Position = birthPos;
  91. this.Place= place;
  92. }
  93. }*/
  94. /// <summary>
  95. /// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable
  96. /// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现
  97. /// </summary>
  98. /// <returns> 依具体类及该方法参数而定,默认为false </returns>
  99. public Moveable(XY initPos, int initRadius, GameObjType initType) : base(initPos, initRadius, initType)
  100. {
  101. }
  102. }
  103. }