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

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