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.

117 lines
3.9 KiB

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