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.

140 lines
4.6 KiB

5 years ago
5 years ago
  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 IEnumerator BlockLogic(Character player, float animationTime)
  26. {
  27. //player.Jump(direction, animationTime);
  28. player.justMoved = true;
  29. Block endBlock = GetEndBlock(player.CurrentBlock, player.transform, ~player.Ignore);
  30. yield return player.StartCoroutine(player.MoveToBlock(endBlock, Character.Animation.Jump, animationTime));
  31. }
  32. /// <summary>
  33. /// Returns the block that the character will endUp on after they use this logic element
  34. /// </summary>
  35. /// <param name="startBlock">block character is on</param>
  36. /// <param name="layerMask">layers to ignore</param>
  37. /// <returns>block which character will finish on after performing this function </returns>
  38. public override Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask)
  39. {
  40. //if there is a block infront JumpUp else LongJump
  41. if (Block.isBlockAtPosition(startBlock.position + direction.ToVector(transform) + Vector3.up, 1, layerMask))
  42. return JumpUp(startBlock, transform, layerMask);
  43. else
  44. return JumpLong(startBlock, transform, layerMask);
  45. }
  46. #endregion Class Functions
  47. #region Helper Functions
  48. private Block JumpLong(Block StartBlock, Transform transform, LayerMask layerMask)
  49. {
  50. //setting up variables
  51. Vector3 position; // position wanted
  52. Block hit; //output of block detection
  53. Block retVal = StartBlock; //block we'll actually move to
  54. //Check blocks in front until we hit an obstruction or went the distance
  55. for (int i = 1; i <= Distance; i++)
  56. {
  57. //Next position to MoveTo
  58. position = StartBlock.position + (direction.ToVector(transform) * i);
  59. //if jump is obstructed, stop and go to last known block
  60. if (Block.isBlockAtPosition(position + Vector3.up, 1, layerMask))
  61. break;
  62. //If block at Position is walkable set it to last known position
  63. if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  64. retVal = hit;
  65. //else if block down one is walkable
  66. else if (Block.isBlockAtPosition(position + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  67. retVal = hit;
  68. }
  69. return retVal;
  70. }
  71. /// <summary>
  72. /// Jumps up obstacle of specific height. Jumps as high as possible
  73. /// </summary>
  74. /// <param name="direction">Direction of obstacle</param>
  75. /// <param name="height">max height</param>
  76. private Block JumpUp(Block StartBlock, Transform transform, LayerMask layerMask)
  77. {
  78. //setting up variables
  79. Vector3 position; // position wanted
  80. Block hit; //output of block detection
  81. Block retVal = StartBlock; //block we'll actually move to
  82. //Check blocks in going up then move to the heighest walkable one
  83. for (int i = 0; i <= Height; i++)
  84. {
  85. //next position up the tower
  86. position = StartBlock.position + direction.ToVector(transform) + (Vector3.up * i);
  87. //if block is walkable set it to last known position
  88. if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  89. retVal = hit;
  90. }
  91. return retVal;
  92. }
  93. #endregion Helper Functions
  94. #region Serialisation functions
  95. public override void CopyToken(BlockToken token)
  96. {
  97. base.CopyToken(token);
  98. direction = ((DirectionToken)token).direction;
  99. }
  100. public override BlockToken ToToken(BlockToken token = null)
  101. {
  102. if (token == null)
  103. token = new DirectionToken(this);
  104. DirectionToken retVal = (DirectionToken)base.ToToken(token);
  105. retVal.direction = direction;
  106. return retVal;
  107. }
  108. #endregion Serialisation functions
  109. }