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.

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