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.

104 lines
2.8 KiB

  1.  using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. public class ShootingCannon : ActiveBlock
  6. {
  7. [Header("Cannon Settings")]
  8. public bool shootingRight;
  9. public GameObject shootingObject;
  10. public Transform spawnLocation;
  11. public TextMeshPro counter;
  12. public TextMeshPro counter2;
  13. int countdowntimer = 3;
  14. public int countdown = 3;
  15. public override int GetInitative()
  16. {
  17. //order
  18. return 4;
  19. }
  20. private void Update()
  21. {
  22. counter.text = countdown.ToString();
  23. counter2.text = countdown.ToString();
  24. }
  25. public override IEnumerator OnEnvironmentTurn(PlayerData[] allPlayers)
  26. {
  27. countdown--;
  28. if (countdown > 0)
  29. {
  30. isFinished = true;
  31. yield break;
  32. }
  33. countdown = countdowntimer;
  34. Vector3 bulletPosition;
  35. Character[] hitPlayers = getHitPlayers(out bulletPosition);
  36. foreach (Character player in hitPlayers)
  37. {
  38. Vector3 moveToPosition = player.CurrentBlock.position + transform.forward;
  39. Block moveToBlock = Block.GetOrCreateBlockAtPosition(moveToPosition, 1, ~0);
  40. StartCoroutine(player.MoveToBlock(moveToBlock, Character.Animation.Hit, 1));
  41. }
  42. StartCoroutine(lerpShot(bulletPosition, Vector3.Distance(spawnLocation.position,bulletPosition)/15));
  43. if (hitPlayers.Length > 0)
  44. yield return new WaitForSeconds(1.5f);
  45. isFinished = true;
  46. yield break;
  47. }
  48. private Character[] getHitPlayers(out Vector3 endPosition)
  49. {
  50. Vector3 curPos;
  51. List<Character> retVal = new List<Character>();
  52. Block hit;
  53. endPosition = position + Vector3.up;
  54. for (int i = 1; i < 50; i++)
  55. {
  56. curPos = position + (transform.forward * i);
  57. endPosition = curPos + Vector3.one; //position which bullet will stop at
  58. if (Block.isBlockAtPosition(curPos, 1, ~0, out hit))
  59. {
  60. if (hit.CurrentPlayer != null)
  61. retVal.Add(hit.CurrentPlayer);
  62. }
  63. //this will stop the bullet
  64. if (Block.isBlockAtPosition(curPos + Vector3.up, 1, ~0))
  65. break;
  66. }//end forloop
  67. return retVal.ToArray();
  68. }
  69. private IEnumerator lerpShot(Vector3 endPosition, float time)
  70. {
  71. GameObject shot = Instantiate(shootingObject, spawnLocation.position, spawnLocation.rotation);
  72. Vector3 startPosition = spawnLocation.position;
  73. float elapsedTime = 0;
  74. while(elapsedTime < time)
  75. {
  76. shot.transform.position = Vector3.Lerp(startPosition, endPosition, elapsedTime / time);
  77. yield return new WaitForEndOfFrame();
  78. elapsedTime += Time.deltaTime;
  79. }
  80. //reset
  81. Destroy(shot);
  82. }
  83. }