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.

150 lines
5.1 KiB

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, bool useBlockDirection = false)
  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 && endBlock.CurrentPlayer != player)
  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. retVal = Block.GetOrCreateBlockAtPosition(position, 1, layerMask);
  69. ////If block at Position is walkable set it to last known position
  70. //if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  71. // retVal = hit;
  72. ////else if block down one is walkable
  73. //else if (Block.isBlockAtPosition(position + Vector3.down, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  74. // retVal = hit;
  75. }
  76. return retVal;
  77. }
  78. /// <summary>
  79. /// Jumps up obstacle of specific height. Jumps as high as possible
  80. /// </summary>
  81. /// <param name="direction">Direction of obstacle</param>
  82. /// <param name="height">max height</param>
  83. private Block JumpUp(Block StartBlock, Transform transform, LayerMask layerMask)
  84. {
  85. //setting up variables
  86. Vector3 position; // position wanted
  87. Block hit; //output of block detection
  88. Block retVal = StartBlock; //block we'll actually move to
  89. //Check blocks in going up then move to the heighest walkable one
  90. for (int i = 0; i <= Height; i++)
  91. {
  92. //next position up the tower
  93. position = StartBlock.position + direction.ToVector(transform) + (Vector3.up * i);
  94. //if block is walkable set it to last known position
  95. if (Block.isBlockAtPosition(position, 1, layerMask, out hit) && hit.isWalkable(layerMask))
  96. retVal = hit;
  97. }
  98. return retVal;
  99. }
  100. #endregion Helper Functions
  101. #region Serialisation functions
  102. public override void CopyToken(BlockToken token)
  103. {
  104. base.CopyToken(token);
  105. direction = ((DirectionToken)token).direction;
  106. }
  107. public override BlockToken ToToken(BlockToken token = null)
  108. {
  109. if (token == null)
  110. token = new DirectionToken(this);
  111. DirectionToken retVal = (DirectionToken)base.ToToken(token);
  112. retVal.direction = direction;
  113. return retVal;
  114. }
  115. #endregion Serialisation functions
  116. }