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.

IMoveable.cs 1.6 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using Preparation.Utility;
  3. namespace Preparation.Interface
  4. {
  5. public interface IMoveable : IGameObj
  6. {
  7. object MoveLock { get; }
  8. public int MoveSpeed { get; }
  9. public long Move(XY moveVec);
  10. protected bool IgnoreCollide(IGameObj targetObj); // 忽略碰撞,在具体类中实现
  11. public bool WillCollideWith(IGameObj? targetObj, XY nextPos) // 检查下一位置是否会和目标物碰撞
  12. {
  13. if (targetObj == null)
  14. return false;
  15. // 会移动的只有子弹和人物,都是Circle
  16. if (!targetObj.IsRigid || targetObj.ID == ID)
  17. return false;
  18. if (IgnoreCollide(targetObj))
  19. return false;
  20. if (targetObj.Shape == ShapeType.Circle)
  21. {
  22. return XY.Distance(nextPos, targetObj.Position) < targetObj.Radius + Radius;
  23. }
  24. else // Square
  25. {
  26. long deltaX = Math.Abs(nextPos.x - targetObj.Position.x), deltaY = Math.Abs(nextPos.y - targetObj.Position.y);
  27. if (deltaX >= targetObj.Radius + Radius || deltaY >= targetObj.Radius + Radius)
  28. return false;
  29. if (deltaX < targetObj.Radius || deltaY < targetObj.Radius)
  30. return true;
  31. else
  32. return ((long)(deltaX - targetObj.Radius) * (deltaX - targetObj.Radius)) + ((long)(deltaY - targetObj.Radius) * (deltaY - targetObj.Radius)) <= (long)Radius * (long)Radius;
  33. }
  34. }
  35. }
  36. }