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.

125 lines
4.6 KiB

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