using Preparation.Interface; using Preparation.Utility; using System.Threading; namespace GameClass.GameObj { /// /// 一切游戏元素的总基类,与THUAI4不同,继承IMoveable接口(出于一切物体其实都是可运动的指导思想)——LHR /// public abstract class GameObj : IMoveable { protected readonly object gameObjLock = new(); /// /// 可移动物体专用锁 /// public object MoveLock => gameObjLock; protected readonly XY birthPos; private GameObjType type; public GameObjType Type => type; private static long currentMaxID = 0; //目前游戏对象的最大ID public const long invalidID = long.MaxValue; //无效的ID public const long noneID = long.MinValue; public long ID { get; } private XY position; public XY Position { get => position; protected set { lock (gameObjLock) { position = value; } } } public abstract bool IsRigid { get; } private XY facingDirection = new(1,0); public XY FacingDirection { get => facingDirection; set { lock (gameObjLock) facingDirection = value; } } public abstract ShapeType Shape { get; } private bool canMove; public bool CanMove { get => canMove; set { lock (gameObjLock) { canMove = value; } } } private bool isMoving; public bool IsMoving { get => isMoving; set { lock (gameObjLock) { isMoving = value; } } } private bool isResetting; public bool IsResetting { get => isResetting; set { lock (gameObjLock) { isResetting = value; } } } public bool IsAvailable => !IsMoving && CanMove && !IsResetting; //是否能接收指令 public int Radius { get; } private PlaceType place; public PlaceType Place { get => place; set { lock (gameObjLock) { place = value; } } } protected int moveSpeed; /// /// 移动速度 /// public int MoveSpeed { get => moveSpeed; set { lock (gameObjLock) { moveSpeed = value; } } } /// /// 原初移动速度 /// private int orgMoveSpeed; public int OrgMoveSpeed { get => orgMoveSpeed; protected set { orgMoveSpeed = value; } } // 移动,改变坐标 public long Move(XY moveVec) { lock (gameObjLock) { FacingDirection = moveVec; this.Position += moveVec; } return (long)(moveVec * moveVec); } /// /// 设置位置 /// /// 新位置 public void SetPosition(XY newpos) { Position = newpos; } /// /// 设置移动速度 /// /// 新速度 public void SetMoveSpeed(int newMoveSpeed) { MoveSpeed = newMoveSpeed; } /// /// 复活时数据重置 /// public virtual void Reset() { lock (gameObjLock) { facingDirection = new XY(1,0); isMoving = false; canMove = false; isResetting = true; this.position = birthPos; } } /// /// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable /// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现 /// /// 依具体类及该方法参数而定,默认为false protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false; bool IMoveable.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj); public GameObj(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType) { this.Position = this.birthPos = initPos; this.Radius = initRadius; this.place = initPlace; this.type = initType; ID = Interlocked.Increment(ref currentMaxID); } } }