RecastCommon.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. namespace DotRecast.Recast
  21. {
  22. public static class RecastCommon
  23. {
  24. private static readonly int[] DirOffsetX = { -1, 0, 1, 0, };
  25. private static readonly int[] DirOffsetY = { 0, 1, 0, -1 };
  26. private static readonly int[] DirForOffset = { 3, 0, -1, 2, 1 };
  27. /// Sets the neighbor connection data for the specified direction.
  28. /// @param[in] span The span to update.
  29. /// @param[in] direction The direction to set. [Limits: 0 <= value < 4]
  30. /// @param[in] neighborIndex The index of the neighbor span.
  31. public static void SetCon(RcCompactSpan span, int direction, int neighborIndex)
  32. {
  33. int shift = direction * 6;
  34. int con = span.con;
  35. span.con = (con & ~(0x3f << shift)) | ((neighborIndex & 0x3f) << shift);
  36. }
  37. /// Gets neighbor connection data for the specified direction.
  38. /// @param[in] span The span to check.
  39. /// @param[in] direction The direction to check. [Limits: 0 <= value < 4]
  40. /// @return The neighbor connection data for the specified direction, or #RC_NOT_CONNECTED if there is no connection.
  41. public static int GetCon(RcCompactSpan s, int dir)
  42. {
  43. int shift = dir * 6;
  44. return (s.con >> shift) & 0x3f;
  45. }
  46. /// Gets the standard width (x-axis) offset for the specified direction.
  47. /// @param[in] direction The direction. [Limits: 0 <= value < 4]
  48. /// @return The width offset to apply to the current cell position to move in the direction.
  49. public static int GetDirOffsetX(int dir)
  50. {
  51. return DirOffsetX[dir & 0x03];
  52. }
  53. // TODO (graham): Rename this to rcGetDirOffsetZ
  54. /// Gets the standard height (z-axis) offset for the specified direction.
  55. /// @param[in] direction The direction. [Limits: 0 <= value < 4]
  56. /// @return The height offset to apply to the current cell position to move in the direction.
  57. public static int GetDirOffsetY(int dir)
  58. {
  59. return DirOffsetY[dir & 0x03];
  60. }
  61. /// Gets the direction for the specified offset. One of x and y should be 0.
  62. /// @param[in] offsetX The x offset. [Limits: -1 <= value <= 1]
  63. /// @param[in] offsetZ The z offset. [Limits: -1 <= value <= 1]
  64. /// @return The direction that represents the offset.
  65. public static int GetDirForOffset(int x, int y)
  66. {
  67. return DirForOffset[((y + 1) << 1) + x];
  68. }
  69. }
  70. }