453 lines
13 KiB
Plaintext
453 lines
13 KiB
Plaintext
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class PHomeWeaponBase : PHomeWeapon
|
|
{
|
|
[SerializeField]
|
|
protected float m_damage;
|
|
public float Damage
|
|
{
|
|
get
|
|
{
|
|
return m_damage;
|
|
}
|
|
}
|
|
|
|
#region CoolDown
|
|
|
|
private void Update_CoolDown()
|
|
{
|
|
if (m_coolDownTimer >= 0)
|
|
{
|
|
m_coolDownTimer -= Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Attack
|
|
|
|
private bool CanAttack()
|
|
{
|
|
if (DuringCoolDown)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return Firer.CanFire();
|
|
}
|
|
|
|
private void Update_Attack()
|
|
{
|
|
//UpdateCirclePos();
|
|
|
|
if (!CanAttack())
|
|
{
|
|
return;
|
|
}
|
|
|
|
OnWeaponStartFiring.Invoke();
|
|
|
|
StartCoroutine(NewFireRoutine());
|
|
EnterCooldown();
|
|
|
|
//switch (ShootType)
|
|
//{
|
|
// case ShootType.NearestTarget:
|
|
// AttackNearestTarget();
|
|
// break;
|
|
// case ShootType.NearbyRandomPoint:
|
|
// AttackNearbyRandomPoint();
|
|
// break;
|
|
// case ShootType.NearbyAllTarget:
|
|
// AttackNearbyAllTarget();
|
|
// break;
|
|
// case ShootType.NearbyRandomTarget:
|
|
// AttackNearbyRandomTarget();
|
|
// break;
|
|
// case ShootType.UnitPos:
|
|
// AttackUnitPos();
|
|
// break;
|
|
// default:
|
|
// break;
|
|
//}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private IEnumerator NewFireRoutine()
|
|
{
|
|
if (m_unit.PreAnimTime > 0)
|
|
{
|
|
yield return new WaitForSeconds(m_unit.PreAnimTime);
|
|
}
|
|
|
|
yield return Firer.Fire(ShootCount);
|
|
}
|
|
|
|
//void AttackUnitPos()
|
|
//{
|
|
// OnWeaponStartFiring.Invoke();
|
|
// AudioManager.instance.AttackAudio();
|
|
// StartCoroutine(FireRoutine(m_unit));
|
|
// EnterCooldown();
|
|
//}
|
|
|
|
//protected virtual void AttackNearestTarget()
|
|
//{
|
|
// PHomeMonster monster = PHomeGameMode.main.FindClosestMonster(m_unit.transform.position, Radius, m_unit);
|
|
// if (monster == null)
|
|
// {
|
|
// return;
|
|
// }
|
|
// OnWeaponStartFiring.Invoke();
|
|
// AudioManager.instance.AttackAudio();
|
|
// StartCoroutine(FireRoutine(monster));
|
|
|
|
// EnterCooldown();
|
|
//}
|
|
|
|
//void UpdateCirclePos()
|
|
//{
|
|
// if (circleReturn)
|
|
// {
|
|
// circleX += Time.deltaTime * Radius*0.5f;
|
|
// float currY = Mathf.Sqrt(Radius * Radius - circleX * circleX);
|
|
// if(currY<0.001){currY = 0.001f;}
|
|
// circleY = -currY;
|
|
// }
|
|
// else
|
|
// {
|
|
// circleX -= Time.deltaTime * Radius*0.5f;
|
|
// float currY = Mathf.Sqrt(Radius * Radius - circleX * circleX);
|
|
// if(currY<0.001){currY = 0.001f;}
|
|
// circleY = currY;
|
|
// }
|
|
// circlePos.x = circleX;
|
|
// circlePos.y = circleY;
|
|
// if (circleX <= -Radius)
|
|
// {
|
|
// circleReturn = true;
|
|
// }
|
|
// else if (circleX >= Radius)
|
|
// {
|
|
// circleReturn = false;
|
|
// }
|
|
//}
|
|
|
|
//float circleX;
|
|
//float circleY;
|
|
//Vector3 circlePos;
|
|
//bool circleReturn;
|
|
//List<Vector3> PosList;
|
|
//float startAngle;
|
|
//protected virtual void AttackNearbyRandomPoint()
|
|
//{
|
|
// OnWeaponStartFiring.Invoke();
|
|
// AudioManager.instance.AttackAudio();
|
|
// Vector2 randomPointInUnitCircle = Random.insideUnitCircle * Radius;
|
|
// if (CirclePos)
|
|
// {
|
|
// circleX = Random.Range(-Radius * 1f, Radius);
|
|
// circleY = Mathf.Sqrt(Radius * Radius - circleX * circleX);
|
|
// circleReturn = false;
|
|
// circlePos = new Vector3(circleX, circleY, 0);
|
|
// StartCoroutine(FireRoutine(m_unit.transform.position + circlePos));
|
|
// }
|
|
// else if (OnCircle)
|
|
// {
|
|
// PosList = new List<Vector3>();
|
|
// int perAngle = 360 / ProjectileCount;
|
|
// startAngle = Random.Range(0f, perAngle);
|
|
// for (int i = 0; i < ProjectileCount; i++)
|
|
// {
|
|
// float currAngle = startAngle + i * perAngle;
|
|
// float x = Radius * Mathf.Cos(currAngle * Mathf.Deg2Rad);
|
|
// float y = Radius * Mathf.Sin(currAngle * Mathf.Deg2Rad);
|
|
// PosList.Add(new Vector3(x, y, 0));
|
|
// }
|
|
// StartCoroutine(FireRoutine(m_unit.transform.position));
|
|
// }
|
|
// else
|
|
// {
|
|
// StartCoroutine(FireRoutine(m_unit.transform.position + new Vector3(randomPointInUnitCircle.x, randomPointInUnitCircle.y, 0)));
|
|
// }
|
|
// EnterCooldown();
|
|
//}
|
|
|
|
|
|
//public IEnumerator FireRoutine(PHomeUnit target)
|
|
//{
|
|
// if (m_unit.PreAnimTime > 0)
|
|
// {
|
|
// yield return new WaitForSeconds(m_unit.PreAnimTime);
|
|
// }
|
|
|
|
// float interval = 1 / ShootRate;
|
|
// for (int j = 0; j < ShootCount; j++)
|
|
// {
|
|
// for (int i = 0; i < ProjectileCount; i++)
|
|
// {
|
|
// if (ProjectileCountMax > 0 && ProjectileCountCurr >= ProjectileCountMax) yield break;
|
|
// PHomeProjectile proj = PHomeGameMode.main.SpawnProjectile(m_unit, m_unit.transform.position, m_projectilePrefab);
|
|
// ShootData currShootData = new ShootData();
|
|
// currShootData.SetTargetUnitWithPos(target);
|
|
// currShootData.StartPoint = m_unit.transform.position;
|
|
// //currShootData.Radius = Radius;
|
|
// currShootData.Weapon = this;
|
|
// //currShootData.ProjectileIndex = i;
|
|
// proj.Init(currShootData);
|
|
// ProjectileCountCurr++;
|
|
// }
|
|
// yield return new WaitForSeconds(interval);
|
|
// }
|
|
//}
|
|
|
|
//public IEnumerator FireRoutine(Vector3 randomPos)
|
|
//{
|
|
// if (m_unit.PreAnimTime > 0)
|
|
// {
|
|
// yield return new WaitForSeconds(m_unit.PreAnimTime);
|
|
// }
|
|
|
|
// float interval = 1 / ShootRate;
|
|
// for (int j = 0; j < ShootCount; j++)
|
|
// {
|
|
// for (int i = 0; i < ProjectileCount; i++)
|
|
// {
|
|
// Vector3 finalPos = randomPos;
|
|
// PHomeProjectile proj = PHomeGameMode.main.SpawnProjectile(m_unit, m_unit.transform.position, m_projectilePrefab);
|
|
// if (CirclePos)
|
|
// {
|
|
// finalPos = m_unit.transform.position + circlePos;
|
|
// Vector2 randomVec = Random.insideUnitCircle * 0.2f;
|
|
// finalPos += new Vector3(randomVec.x, randomVec.y, 0);
|
|
// }
|
|
// else if (OnCircle)
|
|
// {
|
|
// int perAngle = 360 / ProjectileCount;
|
|
// float currAngle = startAngle + i * perAngle;
|
|
// float x = Radius * Mathf.Cos(currAngle * Mathf.Deg2Rad);
|
|
// float y = Radius * Mathf.Sin(currAngle * Mathf.Deg2Rad);
|
|
// finalPos = m_unit.transform.position + new Vector3(x, y, 0);
|
|
// proj.transform.position = finalPos;
|
|
// }
|
|
// ShootData currShootData = new ShootData();
|
|
// currShootData.TargetUnit = m_unit;
|
|
// currShootData.StartPoint = m_unit.transform.position;
|
|
// currShootData.EndPoint = finalPos;
|
|
// //currShootData.Radius = Radius;
|
|
// currShootData.Weapon = this;
|
|
// if (OnCircle)
|
|
// {
|
|
// int perAngle = 360 / ProjectileCount;
|
|
// float currAngle = startAngle + i * perAngle;
|
|
// //currShootData.Angle = currAngle;
|
|
// }
|
|
// else if(CirclePos)
|
|
// {
|
|
// //currShootData.ProjectileIndex = j;
|
|
// }
|
|
// proj.Init(currShootData); ;
|
|
// }
|
|
// yield return new WaitForSeconds(interval);
|
|
// }
|
|
//}
|
|
|
|
//void AttackNearbyAllTarget()
|
|
//{
|
|
// OnWeaponStartFiring.Invoke();
|
|
// //StartCoroutine(FireRoutineForceField());
|
|
// EnterCooldown();
|
|
//}
|
|
|
|
//void AttackNearbyRandomTarget()
|
|
//{
|
|
// OnWeaponStartFiring.Invoke();
|
|
// StartCoroutine(FireNearByRandomTarget());
|
|
// EnterCooldown();
|
|
//}
|
|
|
|
#region Debug
|
|
|
|
|
|
#endregion
|
|
|
|
//private IEnumerator FireNearByRandomTarget()
|
|
//{
|
|
// if (m_unit.PreAnimTime > 0)
|
|
// {
|
|
// yield return new WaitForSeconds(m_unit.PreAnimTime);
|
|
// }
|
|
|
|
// float interval = 1 / ShootRate;
|
|
// for (int j = 0; j < ShootCount; j++)
|
|
// {
|
|
// Vector2 point = new Vector2()
|
|
// {
|
|
// x = m_unit.transform.position.x,
|
|
// y = m_unit.transform.position.y,
|
|
// };
|
|
// Collider2D[] colliders = Physics2D.OverlapCircleAll(point, Radius);
|
|
// List<PHomeUnit> List_PhomeUnit = new List<PHomeUnit>();
|
|
// for (int i = 0; i < colliders.Length; i++)
|
|
// {
|
|
// if (colliders[i].attachedRigidbody == null)
|
|
// {
|
|
// continue;
|
|
// }
|
|
|
|
// if (m_unit != null)
|
|
// {
|
|
// PHomeUnit currUnit = colliders[i].attachedRigidbody.GetComponent<PHomeUnit>();
|
|
// if (currUnit != null && PHomeGameMode.IsEnemy(m_unit, currUnit))
|
|
// {
|
|
// List_PhomeUnit.Add(currUnit);
|
|
// }
|
|
// }
|
|
// }
|
|
// if (List_PhomeUnit.Count > 0)
|
|
// {
|
|
// int currIndex = Random.Range(0, List_PhomeUnit.Count);
|
|
// PHomeProjectile proj = PHomeGameMode.main.SpawnProjectile(m_unit, List_PhomeUnit[currIndex].transform.position, m_projectilePrefab);
|
|
// ShootData currShootData = new ShootData();
|
|
// currShootData.TargetUnit = null;
|
|
// currShootData.StartPoint = List_PhomeUnit[currIndex].transform.position;
|
|
// currShootData.EndPoint = List_PhomeUnit[currIndex].transform.position;
|
|
// currShootData.Weapon = this;
|
|
// //currShootData.Radius = Radius;
|
|
// proj.Init(currShootData); ;
|
|
// }
|
|
// yield return new WaitForSeconds(interval);
|
|
// }
|
|
//}
|
|
|
|
protected override void Virtual_Update()
|
|
{
|
|
Update_CoolDown();
|
|
Update_Attack();
|
|
}
|
|
}
|
|
|
|
|
|
public enum ShootType
|
|
{
|
|
NearestTarget,//最近敌人射击
|
|
NearbyRandomPoint,// 周围随机点射击
|
|
NearbyRandomTarget,// 周围随机敌人射击
|
|
NearbyAllTarget,// 周围随机敌人射击
|
|
ScreenRandomPoint,// 玩家屏幕随机点射击
|
|
UnitPos,//所有者坐标点生成
|
|
None,
|
|
}
|
|
|
|
public enum ShootDataDirections
|
|
{
|
|
ToEnd,
|
|
StartToEnd,
|
|
Direction,
|
|
}
|
|
|
|
public enum ShootDataPoints
|
|
{
|
|
StartPoint,
|
|
Unit,
|
|
EndPoint,
|
|
TargetUnit,
|
|
}
|
|
|
|
public struct ShootData
|
|
{
|
|
public PHomeUnit Unit;
|
|
public PHomeWeaponBase Weapon;
|
|
|
|
public Vector2 StartPoint;
|
|
|
|
public Vector2? EndPoint;
|
|
public Vector2? Direction;
|
|
public PHomeUnit TargetUnit;
|
|
|
|
public void SetTargetUnitWithPos(PHomeUnit targetUnit)
|
|
{
|
|
if (targetUnit == null)
|
|
{
|
|
TargetUnit = null;
|
|
}
|
|
else
|
|
{
|
|
TargetUnit = targetUnit;
|
|
EndPoint = targetUnit.transform.position;
|
|
}
|
|
}
|
|
|
|
public Vector2? GetPoint(ShootDataPoints point)
|
|
{
|
|
switch (point)
|
|
{
|
|
case ShootDataPoints.StartPoint:
|
|
return StartPoint;
|
|
case ShootDataPoints.Unit:
|
|
return Unit.transform.position;
|
|
case ShootDataPoints.EndPoint:
|
|
return EndPoint;
|
|
case ShootDataPoints.TargetUnit:
|
|
return TargetUnit.transform.position;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public Vector2? GetDirection(ShootDataDirections direction, Vector2 position, bool reverse = false)
|
|
{
|
|
Vector2? result = null;
|
|
switch (direction)
|
|
{
|
|
case ShootDataDirections.ToEnd:
|
|
result = EndPoint - position;
|
|
break;
|
|
case ShootDataDirections.StartToEnd:
|
|
result = EndPoint - StartPoint;
|
|
break;
|
|
case ShootDataDirections.Direction:
|
|
result = Direction;
|
|
break;
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
result = result.Value.normalized;
|
|
float flag = reverse ? -1.0f : 1.0f;
|
|
return result * flag;
|
|
}
|
|
|
|
public Vector2? GetTargetPoint()
|
|
{
|
|
if (TargetUnit != null)
|
|
{
|
|
return TargetUnit.transform.position;
|
|
}
|
|
|
|
if (EndPoint != null)
|
|
{
|
|
return EndPoint;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class WeaponLevelConfig
|
|
{
|
|
public int Level;
|
|
public float FireRate;
|
|
public int ShootCount;
|
|
public GameObject m_projectilePrefab;
|
|
} |