UnityCommon/Role/Bullet.cs

253 lines
8.3 KiB
C#
Raw Normal View History

2024-12-05 16:25:46 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
2024-12-24 10:30:02 +08:00
using System.Linq;
2024-12-05 16:25:46 +08:00
public enum BulletType
{
Bullet,
2024-12-12 17:06:08 +08:00
Lightning,
Spraying //ԭ<><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-12-05 16:25:46 +08:00
}
2024-12-20 16:28:28 +08:00
public enum BulletAttributes
{
Not,
Gold,//<2F><>ľˮ<C4BE><CBAE><EFBFBD><EFBFBD>
Wood,
Water,
Fire,
Earth
}
2024-12-05 16:25:46 +08:00
public enum BulletMoveType
{/// <summary>
/// <20>ӵ<EFBFBD><D3B5>ƶ<EFBFBD><C6B6><EFBFBD>ʽ:<3A><><EFBFBD>Ե<EFBFBD>,
/// </summary>
PeerToPeer,
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
/// </summary>
2024-12-12 17:06:08 +08:00
PointToDirection,
Scope
2024-12-05 16:25:46 +08:00
}
2024-12-11 16:51:55 +08:00
2024-12-05 16:25:46 +08:00
public class BulletData
{
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>")] public int NumberOfBulletPenetrations = 1;
/// <summary>
/// <20>ӵ<EFBFBD>ɢ<EFBFBD><C9A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
[Header("<22>ӵ<EFBFBD>ɢ<EFBFBD><C9A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public int BulletScattering = 1;
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public int BulletBurstTimes = 1;
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public float BulletBurstInterval ;
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD>͸˥<CDB8><CBA5>")] public float BulletPenetrationAttenuation = 0.2f;
2024-12-11 16:51:55 +08:00
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>")] public float BulletSpeed = 5f;
2024-12-05 16:25:46 +08:00
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ")] public float BulletAttackCooldown = 1f;
2024-12-11 16:51:55 +08:00
[Header("<22>ӵ<EFBFBD><D3B5>˺<EFBFBD>")] public float attack = 10;
2024-12-20 16:28:28 +08:00
2024-12-05 16:25:46 +08:00
public BulletData()
{
}
public BulletData(int numberOfBulletPenetrations, int bulletScattering, int bulletBurstTimes, float bulletBurstInterval, float bulletPenetrationAttenuation, float bulletSpeed, float bulletAttackCooldown)
{
NumberOfBulletPenetrations = numberOfBulletPenetrations;
BulletScattering = bulletScattering;
BulletBurstTimes = bulletBurstTimes;
BulletBurstInterval = bulletBurstInterval;
BulletPenetrationAttenuation = bulletPenetrationAttenuation;
BulletSpeed = bulletSpeed;
BulletAttackCooldown = bulletAttackCooldown;
}
public override string ToString()
{
return
$"{nameof(NumberOfBulletPenetrations)}: {NumberOfBulletPenetrations}, {nameof(BulletScattering)}: {BulletScattering}, {nameof(BulletBurstTimes)}: {BulletBurstTimes}, {nameof(BulletBurstInterval)}: {BulletBurstInterval}, {nameof(BulletPenetrationAttenuation)}: {BulletPenetrationAttenuation}, {nameof(BulletSpeed)}: {BulletSpeed}, {nameof(BulletAttackCooldown)}: {BulletAttackCooldown}";
}
}
public class Bullet : MonoBehaviour
{
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>")] public Role role;
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>Χ<E4B7B6><CEA7><EFBFBD><EFBFBD>")] public Attack attackObj;
2024-12-12 17:06:08 +08:00
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>")] public BulletType myBulletType;
2024-12-20 16:28:28 +08:00
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>")] public BulletAttributes bulletAttributes;
2024-12-12 17:06:08 +08:00
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")] public BulletMoveType bulletMoveType;
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>")] public BulletData bulletData = new BulletData();
[Header("<22><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><><C4AC><EFBFBD><EFBFBD>10f")] public float BulletDeadTimer=10f;
2024-12-20 16:28:28 +08:00
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD>")] public Animator animator;
[Header("<22>ӵ<EFBFBD><D3B5>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD>")] public bool IsMove=true;
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD>ײ<EFBFBD><D7B2>")] public Collider2D Collider2D;
2024-12-24 14:10:25 +08:00
[Header("<22>ӵ<EFBFBD><D3B5><EFBFBD>ЧԤ<D0A7><D4A4><EFBFBD><EFBFBD>")] public List<GameObject> effectPres=new List<GameObject>();
2024-12-11 16:51:55 +08:00
private float timer = 0;
2024-12-05 16:25:46 +08:00
private void Update()
{
switch (this.bulletMoveType)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>ƶ<EFBFBD>
case BulletMoveType.PeerToPeer:
2024-12-20 16:28:28 +08:00
if (IsMove)
2024-12-12 17:06:08 +08:00
{
2024-12-20 16:28:28 +08:00
this.gameObject.transform.Translate(Vector3.up * Time.deltaTime * bulletData.BulletSpeed);
timer += Time.deltaTime;
if (timer > BulletDeadTimer)
{
attackObj.bulltes.Remove(this.gameObject);
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD>ѳɹ<D1B3><C9B9>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + attackObj.bulltes.Count);
Destroy(this.gameObject);
}
2024-12-12 17:06:08 +08:00
}
2024-12-20 16:28:28 +08:00
2024-12-05 16:25:46 +08:00
break;
case BulletMoveType.PointToDirection:
break;
2024-12-12 17:06:08 +08:00
case BulletMoveType.Scope:
break;
2024-12-05 16:25:46 +08:00
}
2024-12-11 16:51:55 +08:00
2024-12-12 17:06:08 +08:00
2024-12-11 16:51:55 +08:00
2024-12-05 16:25:46 +08:00
}
2024-12-11 16:51:55 +08:00
2024-12-05 16:25:46 +08:00
private void OnTriggerEnter2D(Collider2D collision)
{
2024-12-18 20:43:54 +08:00
//Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ");
2024-12-05 16:25:46 +08:00
Role Crole = collision.gameObject.GetComponent<Role>();
if(Crole)
{
if(Crole.camp!= role.camp)
{
2024-12-24 10:30:02 +08:00
Crole.PlayerBuff = role.storageBuff.ToList();
Crole.ApplyBuffs();
2024-12-12 23:03:57 +08:00
Debug.Log(this.role.gameObject.name+"<22><><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-12-18 20:43:54 +08:00
int direction=0;
2024-12-18 21:07:57 +08:00
if (collision.transform.position.x>transform.position.x)//<2F>ӵ<EFBFBD><D3B5>򵽵<EFBFBD><F2B5BDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>Ʈ<EFBFBD><C6AE><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ұ<EFBFBD>
2024-12-18 20:43:54 +08:00
{
direction = 1;
}
2024-12-19 19:01:39 +08:00
2024-12-23 16:57:40 +08:00
Crole.bloodLoss(new object[] { Crole, role.DamageCreate(), attackObj.damageTyp, role },direction);
2024-12-12 23:03:57 +08:00
attackObj.bulltes.Remove(this.gameObject);
2024-12-20 16:28:28 +08:00
2024-12-12 23:03:57 +08:00
if (myBulletType!=BulletType.Spraying)
{
2024-12-20 16:28:28 +08:00
if (animator == null)
{
attackObj.bulltes.Remove(this.gameObject);
Destroy(this.gameObject);
}
else
{
IsMove = false;//ֹͣ<CDA3>ƶ<EFBFBD>
Collider2D.enabled = false;//<2F>ر<EFBFBD><D8B1><EFBFBD>ײ<EFBFBD><D7B2>
transform.position = collision.transform.position;
animator.SetTrigger("Boom");
Debug.Log("<22><>ը<EFBFBD><D5A8><EFBFBD><EFBFBD>+++++++++++++++++++++++++++++++++");
}
}
if (bulletAttributes==BulletAttributes.Fire)
{
2024-12-24 14:10:25 +08:00
if (effectPres.Count==1)
{
GameObject go = Instantiate(effectPres[0], collision.transform);
2024-12-20 17:39:48 +08:00
go.transform.position = new Vector2(collision.transform.position.x, collision.transform.position.y + 0.2f);
2024-12-20 18:02:06 +08:00
go.GetComponent<Huo>().bullet = this;
2024-12-24 14:10:25 +08:00
2024-12-20 16:28:28 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-12-24 14:10:25 +08:00
}
2024-12-20 16:28:28 +08:00
}
}
2024-12-20 21:41:40 +08:00
if (bulletAttributes == BulletAttributes.Water)
{
2024-12-24 14:10:25 +08:00
if (effectPres.Count > 1)
2024-12-20 21:41:40 +08:00
{
2024-12-24 14:10:25 +08:00
GameObject go = Instantiate(effectPres[Random.Range(0,effectPres.Count-1)], collision.transform);
2024-12-20 21:41:40 +08:00
go.transform.position = new Vector2(collision.transform.position.x, collision.transform.position.y-0.2f);
collision.transform.GetComponent<enemy>().SlowDown(0.2f, 3f);
2024-12-20 16:28:28 +08:00
2024-12-20 21:41:40 +08:00
}
2024-12-18 20:43:54 +08:00
}
2024-12-20 21:41:40 +08:00
}
2024-12-24 14:10:25 +08:00
2024-12-18 20:43:54 +08:00
}
private float lastDamageTime = 0f;
private void OnTriggerStay2D(Collider2D collision)
{
Role Crole = collision.gameObject.GetComponent<Role>();
if (Crole)
{
if (Crole.camp != role.camp)
{
int direction = 0;
if (collision.transform.position.x >= transform.position.x)//<2F>ӵ<EFBFBD><D3B5>򵽵<EFBFBD><F2B5BDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>Ʈ<EFBFBD><C6AE><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ұ<EFBFBD>
{
direction = 1;
}
if (myBulletType == BulletType.Spraying)
{
if (Time.time - lastDamageTime > 1f) // ÿ<><C3BF><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ѫ
{
2024-12-19 19:01:39 +08:00
2024-12-23 16:57:40 +08:00
Crole.bloodLoss(new object[] { Crole, role.DamageCreate(), attackObj.damageTyp, role });
2024-12-18 20:43:54 +08:00
lastDamageTime = Time.time;
}
2024-12-05 16:25:46 +08:00
}
}
2024-12-18 20:43:54 +08:00
}
2024-12-05 16:25:46 +08:00
}
2024-12-12 23:03:57 +08:00
2024-12-20 16:28:28 +08:00
public void DesBullet()
{
attackObj.bulltes.Remove(this.gameObject);
Destroy(this.gameObject);
}
2024-12-18 20:43:54 +08:00
2024-12-12 23:03:57 +08:00
private void OnTriggerExit2D(Collider2D collision)
{
2024-12-14 23:20:36 +08:00
//Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ");
2024-12-12 23:03:57 +08:00
Role Crole = collision.gameObject.GetComponent<Role>();
if (Crole)
{
if (Crole.camp != role.camp)
{
2024-12-17 17:15:32 +08:00
2024-12-14 23:20:36 +08:00
//Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD>ѳɹ<D1B3><C9B9>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + attackObj.bulltes.Count);
2024-12-17 17:15:32 +08:00
if (myBulletType != BulletType.Spraying)
{
2024-12-20 16:28:28 +08:00
if (animator==null)
{
attackObj.bulltes.Remove(this.gameObject);
Destroy(this.gameObject);
}
2024-12-17 17:15:32 +08:00
}
2024-12-12 23:03:57 +08:00
}
}
}
2024-12-05 16:25:46 +08:00
}