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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 IsRigid { get; }
  24. public abstract ShapeType Shape { get; }
  25. private AtomicBool isRemoved = new(false);
  26. public AtomicBool IsRemoved { get => isRemoved; }
  27. public virtual bool TryToRemove()
  28. {
  29. return IsRemoved.TrySet(true);
  30. }
  31. public int Radius { get; }
  32. public virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
  33. public GameObj(XY initPos, int initRadius, GameObjType initType)
  34. {
  35. this.position = this.birthPos = initPos;
  36. this.Radius = initRadius;
  37. this.type = initType;
  38. ID = Interlocked.Increment(ref currentMaxID);
  39. }
  40. }
  41. }