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.

129 lines
4.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [CreateAssetMenu(menuName = "Major Project/Pick Ups/HookShot Block")]
  5. [System.Serializable]
  6. public class HookShot : LogicBlock
  7. {
  8. [SerializeField]
  9. [Header("Direction to shoot in")]
  10. private Direction Direction;
  11. [SerializeField]
  12. private string ResourceName;
  13. [SerializeField]
  14. private float ShotSpeed = 1;
  15. public override Block GetEndBlock(Block startBlock, Transform transform, LayerMask layerMask)
  16. {
  17. //setting up variables
  18. Vector3 position; // position wanted
  19. Block hit; //output of block detection
  20. Block retVal = startBlock; //block we'll actually move to
  21. //Check blocks in front until we hit an obstruction or went the distance
  22. for (int i = 1; i <= 30; i++)
  23. {
  24. //Next position to MoveTo
  25. position = startBlock.position + (Direction.ToVector(transform) * i);
  26. //if position is off the screen there is nothing to hit
  27. if (!Block.isPositionVisible(position))
  28. return startBlock;
  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. public override void CopyToken(BlockToken token)
  75. {
  76. base.CopyToken(token);
  77. Direction = ((HookShotToken)token).Direction;
  78. ResourceName = ((HookShotToken)token).ResourceName;
  79. ShotSpeed = ((HookShotToken)token).ShotSpeed;
  80. }
  81. public override BlockToken ToToken(BlockToken token = null)
  82. {
  83. if (token == null)
  84. token = new HookShotToken(this);
  85. HookShotToken retVal = (HookShotToken)base.ToToken(token);
  86. retVal.Direction = Direction;
  87. retVal.ResourceName = ResourceName;
  88. retVal.ShotSpeed = ShotSpeed;
  89. return retVal;
  90. }
  91. }
  92. [System.Serializable]
  93. public class HookShotToken : BlockToken
  94. {
  95. public Direction Direction;
  96. public string ResourceName;
  97. public float ShotSpeed;
  98. public HookShotToken(LogicBlock block) : base(block) { }
  99. }