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.

96 lines
3.4 KiB

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