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.

88 lines
3.3 KiB

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