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 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. public abstract bool CanMove { get; }
  24. public abstract bool IsRigid { get; }
  25. public abstract ShapeType Shape { get; }
  26. protected int isRemoved = 0;
  27. public bool IsRemoved
  28. {
  29. get
  30. {
  31. return (Interlocked.CompareExchange(ref isRemoved, 0, 0) == 1);
  32. }
  33. }
  34. public virtual bool TryToRemove()
  35. {
  36. return Interlocked.Exchange(ref isRemoved, 1) == 0;
  37. }
  38. public int Radius { get; }
  39. public virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
  40. public GameObj(XY initPos, int initRadius, GameObjType initType)
  41. {
  42. this.position = this.birthPos = initPos;
  43. this.Radius = initRadius;
  44. this.type = initType;
  45. ID = Interlocked.Increment(ref currentMaxID);
  46. }
  47. }
  48. }