StoneStatue.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. public class StoneStatue:MonoBehaviour
  6. {
  7. public GameObject bulletPrefab;
  8. public GameObject parent;
  9. private AttackController ac;
  10. [Header("子弹发射设置,子弹参数在AttackControlller的行军式中设置")]
  11. [LabelText("子弹数量")] public int bulletCount = 3;
  12. [LabelText("基础速度")] public float baseSpeed = 10f;
  13. [LabelText("速度随机范围")] public float speedRandomRange = 2f;
  14. [LabelText("角度随机范围")] public float angleRandomRange = 5f;
  15. [LabelText("子弹伤害与击破者伤害的比率")] public float damageRate = 1;
  16. [Header("向左发射角度")]
  17. [LabelText("向左发射的基础角度")] public float leftBaseAngle = 135f;
  18. [LabelText("向左发射的角度范围")] public float leftAngleRange = 60f;
  19. [Header("向右发射角度")]
  20. [LabelText("向右发射的基础角度")] public float rightBaseAngle = 45f;
  21. [LabelText("向右发射的角度范围")] public float rightAngleRange = 60f;
  22. public GameObject disappearEffect;
  23. private void Awake()
  24. {
  25. ac = GetComponent<AttackController>();
  26. }
  27. private void OnTriggerEnter(Collider other)
  28. {
  29. if (other.gameObject.layer == 20)
  30. {
  31. AttackTrigger attackTrigger = other.gameObject.GetComponent<AttackTrigger>();
  32. if(attackTrigger != null)
  33. {
  34. AttackController.AttackMethod attackMethod = other.gameObject.GetComponent<AttackTrigger>().attackMethod;
  35. AttackInfo attackInfo = attackMethod.attackInfo;
  36. Character attackFrom = attackTrigger.owner;
  37. if ((attackFrom.CompareTag("Demonic") || attackFrom.CompareTag("Player")) && attackInfo.attackMethod_Type == AttackMethod_Type.Attack_Summon)
  38. {
  39. foreach (AttackEffect attackEffect in attackInfo.attackEffect)
  40. {
  41. if (attackEffect == AttackEffect.BlowUp)
  42. {
  43. Vector3 vec3 = attackMethod.attackInfo.blowUp.dir;
  44. int attackDir = 0;
  45. switch (attackMethod.attackInfo.blowUp.directionType)
  46. {
  47. case AttackInfo.BlowUp.DirectionType.Common:
  48. attackDir = attackFrom.bodyTrans.localScale.x < 0 ? -1 : 1;
  49. break;
  50. case AttackInfo.BlowUp.DirectionType.Spread:
  51. attackDir = attackFrom.transform.position.x < transform.position.x ? -1 : 1;
  52. break;
  53. }
  54. if (attackDir < 0)
  55. {
  56. vec3.x = -vec3.x;
  57. }
  58. if (GameManager.instance.isWoodRockEnable)
  59. {
  60. int randomInt = Random.Range(0, 100);
  61. if (randomInt < GameManager.instance.woodRockProbability && GameManager.instance.player.deadDemonicList.Count > 0)
  62. {
  63. randomInt = Random.Range(0, GameManager.instance.player.deadDemonicList.Count);
  64. DeadDemonicInformation deadDemonicInformation = GameManager.instance.player.deadDemonicList[randomInt];
  65. Demonic demonic = GameManager.instance.player.CreateDemonic(deadDemonicInformation.id);
  66. demonic.attackController.attackSummonId = 0;
  67. demonic.Attack_summon();
  68. GameManager.instance.player.deadDemonicList.Remove(deadDemonicInformation);
  69. PoolManager.Instantiate(Resources.Load<GameObject>("Prefab/SoulFlower"), demonic.transform.position + Vector3.up * 2, Quaternion.identity, demonic.transform).GetComponent<SoulFlower>().Init(true);
  70. }
  71. }
  72. Shoot(vec3, attackFrom);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. private void Shoot(Vector3 attackDir,Character attackFrom)
  80. {
  81. if (bulletPrefab == null)
  82. {
  83. Debug.LogError("bulletPrefab未设置!");
  84. return;
  85. }
  86. // 判断攻击方向(向左还是向右)
  87. bool isAttackingRight = attackDir.x > 0;
  88. // 根据方向选择发射角度范围
  89. float baseAngle = isAttackingRight ? rightBaseAngle : leftBaseAngle;
  90. float angleRange = isAttackingRight ? rightAngleRange : leftAngleRange;
  91. // 计算角度步长(均匀分布)
  92. float angleStep = angleRange / (bulletCount - 1);
  93. float startAngle = baseAngle - angleRange / 2f;
  94. for (int i = 0; i < bulletCount; i++)
  95. {
  96. // 计算基础角度(均匀分布)
  97. float currentAngle = startAngle + angleStep * i;
  98. // 添加随机角度偏移
  99. float randomAngleOffset = Random.Range(-angleRandomRange, angleRandomRange);
  100. float finalAngle = currentAngle + randomAngleOffset;
  101. // 计算方向向量
  102. float rad = finalAngle * Mathf.Deg2Rad;
  103. Vector3 bulletDirection = new Vector3(Mathf.Cos(rad), Mathf.Sin(rad), 0f);
  104. // 计算随机速度
  105. float randomSpeed = baseSpeed + Random.Range(-speedRandomRange, speedRandomRange);
  106. Vector3 bulletVelocity = bulletDirection * randomSpeed;
  107. // 创建子弹
  108. CreateBullet(bulletDirection, bulletVelocity, attackFrom);
  109. parent.SetActive(false);
  110. }
  111. }
  112. private void CreateBullet(Vector3 direction, Vector3 velocity,Character attackFrom)
  113. {
  114. GameObject bulletObj = PoolManager.Instantiate(bulletPrefab, transform.position + Vector3.up * 2, Quaternion.identity);
  115. // 设置子弹朝向
  116. bulletObj.transform.right = direction;
  117. // 获取子弹组件并设置属性
  118. Bullet bullet = bulletObj.GetComponent<Bullet>();
  119. if (bullet != null)
  120. {
  121. ac.attackMethod_march[0].attackInfo.damage = (int)(attackFrom.attackController.attackMethod_summon[0].attackInfo.damage * damageRate);
  122. bullet.BeShoot(attackFrom, transform.position, direction,false,false,null, ac.attackMethod_march[0]);
  123. }
  124. //Debug.Log($"发射子弹,方向:{direction},速度:{velocity.magnitude}");
  125. }
  126. private void OnDisable()
  127. {
  128. if(disappearEffect != null) PoolManager.Instantiate(disappearEffect, transform.position, new Quaternion(0, 0, 0, 0));
  129. GameManager.instance.player.rebornSkills.Remove(this);
  130. }
  131. }