/// <summary> /// 在无碰撞的前提下行走最远的距离 /// </summary> /// <param name="obj">移动物体,默认obj.Rigid为true</param> /// <param name="moveVec">移动的位移向量</param> private void MoveMax(GameObject obj, Vector moveVec) { /*由于四周是墙,所以人物永远不可能与越界方块碰撞*/ XYPosition nextPos = obj.Position + Vector.Vector2XY(moveVec); uint maxLen = collisionChecker.FindMax(obj, nextPos, moveVec); maxLen = (uint)Math.Min(maxLen, (obj.MoveSpeed / Map.Constant.numOfStepPerSecond)); obj.Move(new Vector(moveVec.angle, maxLen)); }
/// <summary> /// 碰撞检测,如果这样行走是否会与之碰撞,返回与之碰撞的物体 /// </summary> /// <param name="obj">移动的物体</param> /// <param name="moveVec">移动的位移向量</param> /// <returns>和它碰撞的物体</returns> public GameObject?CheckCollision(GameObject obj, Vector moveVec) { XYPosition nextPos = obj.Position + Vector.Vector2XY(moveVec); if (!obj.IsRigid) { if (gameMap.OutOfBound(obj)) { return(new OutOfBoundBlock(nextPos)); } return(null); } //在某列表中检查碰撞 Func <ArrayList, ReaderWriterLockSlim, GameObject> CheckCollisionInList = (ArrayList lst, ReaderWriterLockSlim listLock) => { GameObject?collisionObj = null; listLock.EnterReadLock(); try { foreach (GameObject listObj in lst) { if (WillCollide(obj, listObj, nextPos)) { collisionObj = listObj; break; } } } finally { listLock.ExitReadLock(); } return(collisionObj); }; GameObject collisionObj = null; foreach (var list in lists) { if ((collisionObj = CheckCollisionInList(list.Item1, list.Item2)) != null) { return(collisionObj); } } //如果越界,则与越界方块碰撞 if (gameMap.OutOfBound(obj)) { return(new OutOfBoundBlock(nextPos)); } return(null); }