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.

91 lines
3.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Hookshot : Projectile
  5. {
  6. [SerializeField]
  7. private Transform hookObject;
  8. [SerializeField]
  9. private float ShotSpeed = 1;
  10. [SerializeField]
  11. private LineRenderer line;
  12. public override IEnumerator ProjectileLogic(Character player, float animationTime, Vector3 direction)
  13. {
  14. Block hitBlock = GetEndBlock(player.CurrentBlock, direction, ~player.Ignore);
  15. if (hitBlock == player.CurrentBlock)
  16. yield break;
  17. yield return StartCoroutine(lerpShot(hookObject, player.transform.position, hitBlock.VisualPosition, ShotSpeed * Vector3.Distance(player.transform.position,hitBlock.VisualPosition)/10));
  18. if (hitBlock.CurrentPlayer != null)
  19. {
  20. Block pullBlock = Block.GetOrCreateBlockAtPosition(player.CurrentBlock.position + direction, 1, ~player.Ignore);
  21. StartCoroutine(lerpShot(hookObject, hookObject.position, pullBlock.VisualPosition, ShotSpeed * Vector3.Distance(hookObject.position, pullBlock.VisualPosition) / 10));
  22. yield return player.StartCoroutine(hitBlock.CurrentPlayer.MoveToBlock(pullBlock, Character.Animation.Hit, ShotSpeed));
  23. }
  24. else
  25. {
  26. line.enabled = false;
  27. yield return player.StartCoroutine(player.MoveToBlock(hitBlock, Character.Animation.Jump, ShotSpeed * Vector3.Distance(player.transform.position, hitBlock.VisualPosition) / 10));
  28. }
  29. }
  30. private Block GetEndBlock(Block startBlock, Vector3 direction, LayerMask layerMask)
  31. {
  32. //setting up variables
  33. Vector3 position; // position wanted
  34. Block hit; //output of block detection
  35. Block retVal = startBlock; //block we'll actually move to
  36. //Check blocks in front until we hit an obstruction or went the distance
  37. for (int i = 1; i <= 30; i++)
  38. {
  39. //Next position to MoveTo
  40. position = startBlock.position + (direction * i);
  41. //if position is off the screen there is nothing to hit
  42. if (!Block.isPositionVisible(position))
  43. return startBlock;
  44. //if hit player return block they are standing on
  45. if (Block.isBlockAtPosition(position, 1, layerMask, out hit))
  46. {
  47. if (hit.CurrentPlayer != null)
  48. return hit;
  49. }
  50. //if block block paths return block infront of it
  51. if (Block.isBlockAtPosition(position + Vector3.up, 1, layerMask))
  52. {
  53. return Block.GetOrCreateBlockAtPosition(position - direction, 1, layerMask);
  54. }
  55. }
  56. return retVal;
  57. }
  58. private IEnumerator lerpShot(Transform target, Vector3 startPosition, Vector3 endPosition, float time)
  59. {
  60. float elapsedTime = 0;
  61. while (elapsedTime < time)
  62. {
  63. target.position = Vector3.Lerp(startPosition, endPosition, elapsedTime / time);
  64. line.SetPosition(1, target.localPosition);
  65. yield return new WaitForEndOfFrame();
  66. elapsedTime += Time.deltaTime;
  67. }
  68. target.position = endPosition;
  69. }
  70. }