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.

GameObj.cs 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 GameObj : IGameObj
  10. {
  11. private readonly ReaderWriterLockSlim gameObjReaderWriterLock = new();
  12. public ReaderWriterLockSlim GameObjReaderWriterLock => gameObjReaderWriterLock;
  13. protected readonly object gameObjLock = new();
  14. public object GameLock => gameObjLock;
  15. protected readonly XY birthPos;
  16. private readonly GameObjType type;
  17. public GameObjType Type => type;
  18. private static long currentMaxID = 0; // 目前游戏对象的最大ID
  19. public const long invalidID = long.MaxValue; // 无效的ID
  20. public long ID { get; }
  21. protected XY position;
  22. public abstract XY Position { get; }
  23. protected XY facingDirection = new(1, 0);
  24. public abstract XY FacingDirection { get; }
  25. protected bool canMove;
  26. public abstract bool CanMove { get; }
  27. public abstract bool IsRigid { get; }
  28. public abstract ShapeType Shape { get; }
  29. protected bool isRemoved = false;
  30. public virtual bool IsRemoved
  31. {
  32. get
  33. {
  34. gameObjReaderWriterLock.EnterReadLock();
  35. try
  36. {
  37. return isRemoved;
  38. }
  39. finally
  40. {
  41. gameObjReaderWriterLock.ExitReadLock();
  42. }
  43. }
  44. }
  45. public virtual void TryToRemove()
  46. {
  47. gameObjReaderWriterLock.EnterWriteLock();
  48. try
  49. {
  50. isRemoved = true;
  51. }
  52. finally
  53. {
  54. gameObjReaderWriterLock.ExitWriteLock();
  55. }
  56. }
  57. public int Radius { get; }
  58. public virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
  59. public GameObj(XY initPos, int initRadius, GameObjType initType)
  60. {
  61. this.position = this.birthPos = initPos;
  62. this.Radius = initRadius;
  63. this.type = initType;
  64. ID = Interlocked.Increment(ref currentMaxID);
  65. }
  66. }
  67. }