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.

100 lines
3.2 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [CreateAssetMenu(menuName = "Major Project/Pick Ups/Lasso Block")]
  5. [System.Serializable]
  6. public class Lasso : LogicBlock
  7. {
  8. [SerializeField]
  9. [Header("Max distance checked before returning")]
  10. private int MaxDistance = 30;
  11. [SerializeField]
  12. [Header("Direction to shoot in")]
  13. private Direction Direction;
  14. [SerializeField]
  15. private GameObject ShotPrefab;
  16. [SerializeField]
  17. private float ShotSpeed = 1;
  18. public override Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask)
  19. {
  20. //setting up variables
  21. Vector3 position; // position wanted
  22. Block hit; //output of block detection
  23. Block retVal = startBlock; //block we'll actually move to
  24. //Check blocks in front until we hit an obstruction or went the distance
  25. for (int i = 1; i <= MaxDistance; i++)
  26. {
  27. //Next position to MoveTo
  28. position = startBlock.position + (Direction.ToVector(transform) * i);
  29. //if hit player return block they are standing on
  30. if (Block.isBlockAtPosition(position, 1, layerMask, out hit)) {
  31. if (hit.CurrentPlayer != null)
  32. return hit;
  33. }
  34. //if block block paths return block infront of it
  35. if (Block.isBlockAtPosition(position + Vector3.up,1,layerMask))
  36. {
  37. return Block.GetOrCreateBlockAtPosition(position - Direction.ToVector(transform), 1, layerMask);
  38. }
  39. }
  40. return retVal;
  41. }
  42. protected override IEnumerator BlockLogic(Character player, float animationTime, bool useBlockDirection = false)
  43. {
  44. Block hitBlock = GetEndBlock(player.CurrentBlock, player.transform, ~player.Ignore);
  45. Debug.Log("hookshot: " + hitBlock.position, hitBlock.gameObject);
  46. if (hitBlock == player.CurrentBlock)
  47. yield break;
  48. Debug.Log("Instantiating shot");
  49. GameObject shot = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  50. shot.transform.localScale = Vector3.one * 0.33f;
  51. yield return player.StartCoroutine(lerpShot(shot.transform, player.transform.position, hitBlock.VisualPosition,ShotSpeed));
  52. Destroy(shot);
  53. if (hitBlock.CurrentPlayer != null)
  54. {
  55. Block pullBlock = Block.GetOrCreateBlockAtPosition(player.CurrentBlock.position + Direction.ToVector(player.transform), 1, ~player.Ignore);
  56. yield return player.StartCoroutine(hitBlock.CurrentPlayer.MoveToBlock(pullBlock, Character.Animation.Hit, ShotSpeed));
  57. }
  58. else
  59. {
  60. yield return player.StartCoroutine(player.MoveToBlock(hitBlock, Character.Animation.Jump, ShotSpeed));
  61. }
  62. }
  63. private IEnumerator lerpShot(Transform target, Vector3 startPosition,Vector3 endPosition, float time)
  64. {
  65. float elapsedTime = 0;
  66. while (elapsedTime < time)
  67. {
  68. target.transform.position = Vector3.Lerp(startPosition, endPosition, elapsedTime / time);
  69. yield return new WaitForEndOfFrame();
  70. elapsedTime += Time.deltaTime;
  71. }
  72. target.position = endPosition;
  73. }
  74. }