Compare commits

..

No commits in common. "27b2f1b025a05d9401b206c40b1e2dbef062bd96" and "ae43a9f4b947987bfd0120fbde24b1d3ef63b8f4" have entirely different histories.

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using Debug = UnityEngine.Debug; using Debug = UnityEngine.Debug;
using UnityEditor.Animations;
using DG.Tweening; using DG.Tweening;
public class Attack : MonoBehaviour public class Attack : MonoBehaviour
@ -17,7 +18,7 @@ public class Attack : MonoBehaviour
[Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间 [Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间
private float lastAttackTime = 0f; // 上次攻击的时间 private float lastAttackTime = 0f; // 上次攻击的时间
[HideInInspector] [HideInInspector]
public List<GameObject> bulltes = new List<GameObject>(); public List<GameObject> bulltes = new List<GameObject>();
@ -26,10 +27,10 @@ public class Attack : MonoBehaviour
[Header("火焰动画控制器")] public List<Animator> fireAnis; [Header("火焰动画控制器")] public List<Animator> fireAnis;
[Header("子弹起始点")] public Transform BulletStartPos; [Header("子弹起始点")] public Transform BulletStartPos;
[Header("攻击速度")] public float AttackSpeed = 1f; [Header("攻击速度")] public float AttackSpeed=1f;
public Vector2 direction; public Vector2 direction;
[Header("子弹速度加成")] public float roleBulletSpeedAdd = 0f; [Header("子弹速度加成")]public float roleBulletSpeedAdd=0f;
//[Header("子弹长度")] public float scaleFactor; //[Header("子弹长度")] public float scaleFactor;
[Header("子弹数量")] public int BulletNumber = 1; [Header("子弹数量")] public int BulletNumber = 1;
[Header("攻击碰撞体")] public CircleCollider2D attackCollider; [Header("攻击碰撞体")] public CircleCollider2D attackCollider;
@ -40,8 +41,19 @@ public class Attack : MonoBehaviour
[Header("攻击范围显示脚本")] public CharacterClick characterClick; [Header("攻击范围显示脚本")] public CharacterClick characterClick;
public bool isAttack = true; public bool isAttack = true;
public bool flag = false; public bool flag = false;
[HideInInspector] public float timer = 0; [HideInInspector] public float timer = 0;
//public float AttackScope
//{
// [DebuggerStepThrough]
// get => attackScope;
// [DebuggerStepThrough]
// set
// {
// attackScope = value;
// GetComponent<CircleCollider2D>().radius = attackScope;
// //Debug.Log("攻击半径"+GetComponent<CircleCollider2D>().radius);
// }
//}
public async void Start() public async void Start()
{ {
@ -63,7 +75,7 @@ public class Attack : MonoBehaviour
{ {
if (attackCollider) if (attackCollider)
{ {
Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius); Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius);
@ -91,7 +103,7 @@ public class Attack : MonoBehaviour
lastAttackTime = Time.time; // 更新上次攻击时间 lastAttackTime = Time.time; // 更新上次攻击时间
} }
await Task.Delay(100); // 延迟,避免过于频繁地检测 await Task.Delay(100); // 延迟,避免过于频繁地检测
} }
@ -108,17 +120,15 @@ public class Attack : MonoBehaviour
if (flag) if (flag)
{ {
timer += Time.deltaTime; timer += Time.deltaTime;
if (timer > AttackStayTime) if (timer>AttackStayTime)
{ {
animator.SetInteger("State", 0); animator.SetInteger("State", 0);
isAttack = true; isAttack = true;
timer = 0; timer = 0;
flag = false; flag = false;
} }
} }
//Debug.Log(role.name+"攻击范围的图片大小"+attackRangeSprite.transform.localScale + "color" + attackRangeSprite.color+ "color.a" + attackRangeSprite.color.a + "是否显示"+ attackRangeSprite.enabled);
} }
public void GetAllColliderInAttackRange(Collider2D[] colliders) public void GetAllColliderInAttackRange(Collider2D[] colliders)
@ -126,7 +136,7 @@ public class Attack : MonoBehaviour
foreach (Collider2D collider in colliders) foreach (Collider2D collider in colliders)
{ {
Role targetRole = collider.GetComponent<Role>(); Role targetRole = collider.GetComponent<Role>();
if (targetRole && targetRole.camp != role.camp) if (targetRole && targetRole.camp != role.camp)
{ {
role.animationHighlight = 1; role.animationHighlight = 1;
@ -141,7 +151,12 @@ public class Attack : MonoBehaviour
else if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying) else if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
{ {
animator.SetInteger("State", 1); animator.SetInteger("State", 1);
flag = true; flag = true;
} }
lastAttackTime = Time.time; // 更新上次攻击时间 lastAttackTime = Time.time; // 更新上次攻击时间
@ -162,6 +177,45 @@ public class Attack : MonoBehaviour
} }
} }
} }
/*public void attack(Role targetRole)
{
if (bulletPrefab == null)
{
Debug.LogError("子弹预制体为空");
return;
}
Vector2 direction = (targetRole.transform.position - transform.position).normalized;
if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
{
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
BulletGamobj.GetComponent<Bullet>().role = role;
BulletGamobj.GetComponent<Bullet>().attackObj = this;
// BulletGamobj.transform.up = direction;
BulletGamobj.transform.position =new Vector2(transform.position.x+0.15f, transform.position.y+0.2f);
bulltes.Add(BulletGamobj);
return;
}
else
{
for (int i = 0; i < bulletData.BulletScattering; i++)
{
// 改变子弹方向,根据角色自身的攻击范围来计算
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
BulletGamobj.GetComponent<Bullet>().role = role;
BulletGamobj.GetComponent<Bullet>().attackObj = this;
BulletGamobj.transform.up = direction;
BulletGamobj.transform.position = transform.position;
bulltes.Add(BulletGamobj);
}
}
}*/
public void Pointattack() public void Pointattack()
{ {
@ -171,11 +225,11 @@ public class Attack : MonoBehaviour
Debug.LogError("子弹预制体为空"); Debug.LogError("子弹预制体为空");
return; return;
} }
for (int i = 0; i < BulletNumber; i++) for (int i = 0; i < BulletNumber; i++)
{ {
// 改变子弹方向,根据角色自身的攻击范围来计算 // 改变子弹方向,根据角色自身的攻击范围来计算
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root); GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
BulletGamobj.GetComponent<Bullet>().role = role; BulletGamobj.GetComponent<Bullet>().role = role;
BulletGamobj.GetComponent<Bullet>().attackObj = this; BulletGamobj.GetComponent<Bullet>().attackObj = this;
@ -185,6 +239,7 @@ public class Attack : MonoBehaviour
BulletGamobj.transform.position = BulletStartPos.position; BulletGamobj.transform.position = BulletStartPos.position;
bulltes.Add(BulletGamobj); bulltes.Add(BulletGamobj);
} }
} }
@ -255,7 +310,7 @@ public class Attack : MonoBehaviour
// offsetX = -((originalWidth * (role.AttackRange / 2)) / 4f +0.15f); // offsetX = -((originalWidth * (role.AttackRange / 2)) / 4f +0.15f);
//} //}
offsetX = (newWidth - originalWidth) / 2; offsetX = (newWidth - originalWidth) / 2;
if (offsetX > 0) if (offsetX>0)
{ {
offsetX += 0.15f; offsetX += 0.15f;
} }
@ -292,7 +347,7 @@ public class Attack : MonoBehaviour
bulltes.Add(bulletGameObj); bulltes.Add(bulletGameObj);
} }
public void EndFire() public void EndFire()
{ {
@ -338,7 +393,12 @@ public class Attack : MonoBehaviour
//boxSize.y = role.AttackRange/3; // 设置矩形高度 //boxSize.y = role.AttackRange/3; // 设置矩形高度
// 更新矩形碰撞体的尺寸 // 更新矩形碰撞体的尺寸
attackColliderBox.size = boxSize; attackColliderBox.size = boxSize;
//float originalWidth = attackRangeSprite.bounds.size.x;
//attackRangeSprite.transform.localScale = new Vector3(boxSize.x*1.4f/4, boxSize.y, 1f);
characterClick.OrSizeX = boxSize.x / characterClick.StartSizeX; // * 2f 因为 radius 是半径,图片的尺寸通常是直径 characterClick.OrSizeX = boxSize.x / characterClick.StartSizeX; // * 2f 因为 radius 是半径,图片的尺寸通常是直径
characterClick.OrSizeY = boxSize.y * 2 / characterClick.StartSizeY; characterClick.OrSizeY = boxSize.y * 2 / characterClick.StartSizeY;
float offSetX = (characterClick.StartSizeX - characterClick.OrSizeX) / 2; float offSetX = (characterClick.StartSizeX - characterClick.OrSizeX) / 2;
@ -349,6 +409,9 @@ public class Attack : MonoBehaviour
attackRangeSprite.transform.position = new Vector2(attackRangeSprite.transform.position.x - offSetX, attackRangeSprite.transform.position.y); attackRangeSprite.transform.position = new Vector2(attackRangeSprite.transform.position.x - offSetX, attackRangeSprite.transform.position.y);
//characterClick.OrSizeY = 1; //characterClick.OrSizeY = 1;
Debug.Log("修改图片宽度"); Debug.Log("修改图片宽度");
} }