You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.4 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Logic block which deals with jumping a character in a direction
  6. /// </summary>
  7. [CreateAssetMenu(menuName = "Major Project/Jump Block")]
  8. [System.Serializable]
  9. public class Jump : LogicBlock
  10. {
  11. [SerializeField]
  12. [Tooltip("Direction to Jump")]
  13. protected Direction direction = Direction.Forward;
  14. [SerializeField]
  15. [Tooltip("Max distance Jump can go")]
  16. protected int Distance = 2;
  17. [SerializeField]
  18. [Tooltip("Max height Jump can go")]
  19. protected int Height = 1;
  20. #region Class Functions
  21. /// <summary>
  22. /// Implementation of BlockLogic, moves the player forward
  23. /// </summary>
  24. /// <param name="player">Player to move</param>
  25. protected override void BlockLogic(Character player, float animationTime)
  26. {
  27. player.Jump(direction, animationTime);
  28. }
  29. /// <summary>
  30. /// Returns the block that the character will endUp on after they use this logic element
  31. /// </summary>
  32. /// <param name="startBlock">block character is on</param>
  33. /// <param name="layerMask">layers to ignore</param>
  34. /// <returns>block which character will finish on after performing this function </returns>
  35. public override Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask)
  36. {
  37. //if there is a block infront JumpUp else LongJump
  38. if (Block.isBlockAtPosition(startBlock.position + direction.ToVector(transform) + Vector3.up, 1, layerMask))
  39. return JumpUp(startBlock, transform, layerMask);
  40. else
  41. return JumpLong(startBlock, transform, layerMask);
  42. }
  43. #endregion Class Functions
  44. #region Helper Functions
  45. private Block JumpLong(Block StartBlock, Transform transform, LayerMask layerMask)
  46. {
  47. //setting up variables
  48. Vector3 position; // position wanted
  49. Block hit; //output of block detection
  50. Block retVal = StartBlock; //block we'll actually move to
  51. //Check blocks in front until we hit an obstruction or went the distance
  52. for (int i = 1; i <= Distance; i++)
  53. {
  54. //Next position to MoveTo
  55. position = StartBlock.position + (direction.ToVector(transform) * i);
  56. //if jump is obstructed, stop and go to last known block
  57. if (Block.isBlockAtPosition(position + Vector3.up, 1, layerMask))
  58. break;
  59. //If block at Position is walkable set it to last known position
  60. if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  61. retVal = hit;
  62. //else if block down one is walkable
  63. else if (Block.isBlockAtPosition(position + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  64. retVal = hit;
  65. }
  66. return retVal;
  67. }
  68. /// <summary>
  69. /// Jumps up obstacle of specific height. Jumps as high as possible
  70. /// </summary>
  71. /// <param name="direction">Direction of obstacle</param>
  72. /// <param name="height">max height</param>
  73. private Block JumpUp(Block StartBlock, Transform transform, LayerMask layerMask)
  74. {
  75. //setting up variables
  76. Vector3 position; // position wanted
  77. Block hit; //output of block detection
  78. Block retVal = StartBlock; //block we'll actually move to
  79. //Check blocks in going up then move to the heighest walkable one
  80. for (int i = 0; i <= Height; i++)
  81. {
  82. //next position up the tower
  83. position = StartBlock.position + direction.ToVector(transform) + (Vector3.up * i);
  84. //if block is walkable set it to last known position
  85. if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  86. retVal = hit;
  87. }
  88. return retVal;
  89. }
  90. #endregion Helper Functions
  91. #region Serialisation functions
  92. public override void CopyToken(BlockToken token)
  93. {
  94. base.CopyToken(token);
  95. direction = ((DirectionToken)token).direction;
  96. }
  97. public override BlockToken ToToken(BlockToken token = null)
  98. {
  99. if (token == null)
  100. token = new DirectionToken(this);
  101. DirectionToken retVal = (DirectionToken)base.ToToken(token);
  102. retVal.direction = direction;
  103. return retVal;
  104. }
  105. #endregion Serialisation functions
  106. }