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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. protected readonly object gameObjLock = new();
  12. public object GameLock => gameObjLock;
  13. protected readonly XY birthPos;
  14. private GameObjType type;
  15. public GameObjType Type => type;
  16. private static long currentMaxID = 0; // 目前游戏对象的最大ID
  17. public const long invalidID = long.MaxValue; // 无效的ID
  18. public const long noneID = long.MinValue;
  19. public long ID { get; }
  20. private XY position;
  21. public XY Position
  22. {
  23. get => position;
  24. protected
  25. set
  26. {
  27. lock (gameObjLock)
  28. {
  29. position = value;
  30. }
  31. }
  32. }
  33. protected PlaceType place;
  34. public PlaceType Place { get => place; }
  35. private XY facingDirection = new(1, 0);
  36. public XY FacingDirection
  37. {
  38. get => facingDirection;
  39. set
  40. {
  41. lock (gameObjLock)
  42. facingDirection = value;
  43. }
  44. }
  45. public abstract bool IsRigid { get; }
  46. public abstract ShapeType Shape { get; }
  47. private bool canMove;
  48. public bool CanMove
  49. {
  50. get => canMove;
  51. set
  52. {
  53. lock (gameObjLock)
  54. {
  55. canMove = value;
  56. }
  57. }
  58. }
  59. private bool isResetting;
  60. public bool IsResetting
  61. {
  62. get => isResetting;
  63. set
  64. {
  65. lock (gameObjLock)
  66. {
  67. isResetting = value;
  68. }
  69. }
  70. }
  71. public int Radius { get; }
  72. /// </summary>
  73. /// <returns> 依具体类及该方法参数而定,默认为false </returns>
  74. protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
  75. bool IGameObj.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj);
  76. public GameObj(XY initPos, int initRadius, GameObjType initType)
  77. {
  78. this.Position = this.birthPos = initPos;
  79. this.Radius = initRadius;
  80. this.type = initType;
  81. ID = Interlocked.Increment(ref currentMaxID);
  82. }
  83. }
  84. }