using KTGMGemClient;
using ActionGameFramework.Health;
using Core.Health;
using Core.Utilities;
using TowerDefense.Level;
using TowerDefense.Nodes;
using TowerDefense.UI.HUD;
using UnityEngine;
namespace TowerDefense.Agents
{
///
/// A component that attacks a home base when an agent reaches it
///
[RequireComponent(typeof(Agent))]
public class HomeBaseAttacker : MonoBehaviour
{
///
/// How long the agent charges for before it attacks
/// the home base
///
public float homeBaseAttackChargeTime = 0.5f;
///
/// Timer used to stall attack to the home base
///
protected Timer m_HomeBaseAttackTimer;
///
/// If the agent has reached the Player Home Base and is charging an attack
///
protected bool m_IsChargingHomeBaseAttack;
///
/// The DamageableBehaviour on the home base
///
protected DamageableBehaviour m_FinalDestinationDamageableBehaviour;
///
/// The agent component attached to this gameObject
///
public Agent agent { get; protected set; }
///
/// Fired on completion of
/// Applies damage to the homebase
///
protected void AttackHomeBase()
{
m_IsChargingHomeBaseAttack = false;
var damager = GetComponent();
if (damager != null)
{
if (!EndlessLevelManager.instanceExists)
{
m_FinalDestinationDamageableBehaviour.TakeDamage(damager.finalDamage, transform.position, agent.configuration.alignmentProvider);
PlayerHomeBase homebase = m_FinalDestinationDamageableBehaviour.GetComponent();
if (homebase && (!homebase.isDead))
{
// 显示塔位血条
float hscale = homebase.healthVal / homebase.configuration.maxHealth;
if (hscale < 1.0)
{
if (homebase.opponent)
OpponentMgr.instance.m_CurrentArea.setTowerPosHealth(homebase.homebaseIdx, hscale);
else
{
if (null != GameUI.instance.selfTowerPlaceArea)
GameUI.instance.selfTowerPlaceArea.setTowerPosHealth(homebase.homebaseIdx, hscale);
}
}
}
}
else
{
if (GameConfig.IsNewbie)
{
if (GameConfig.TowerFirstTakeDamage)
{
GameConfig.TowerFirstTakeDamage = false;
EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessLoseHeart, 1);
EndlessLevelManager.instance.StopSecondWave();
}
else
{
if (GameConfig.TowerCanTakeDamage)
EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessLoseHeart, 1);
}
}
else
EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessLoseHeart, 1);
}
}
EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentTaskDamage, agent.configuration.currentHealth);
agent.Remove();
}
///
/// Ticks the attack timer
///
protected virtual void Update()
{
// Update HomeBaseAttack Timer
if (m_IsChargingHomeBaseAttack)
{
m_HomeBaseAttackTimer.Tick(Time.deltaTime);
}
}
///
/// Caches the attached Agent and subscribes to the destinationReached event
///
protected virtual void Awake()
{
agent = GetComponent();
agent.destinationReached += OnDestinationReached;
agent.died += OnDied;
}
///
/// Unsubscribes from the destinationReached event
///
protected virtual void OnDestroy()
{
if (agent != null)
{
agent.destinationReached -= OnDestinationReached;
agent.died -= OnDied;
}
}
///
/// Stops the attack on the home base
///
void OnDied(DamageableBehaviour damageableBehaviour)
{
m_IsChargingHomeBaseAttack = false;
}
///
/// Fired then the agent reached its final node,
/// Starts the attack timer
///
///
void OnDestinationReached(Node homeBase)
{
if (agent.bInDeathState) return;
m_FinalDestinationDamageableBehaviour = homeBase.GetComponent();
// start timer
if (m_HomeBaseAttackTimer == null)
{
m_HomeBaseAttackTimer = new Timer(homeBaseAttackChargeTime, AttackHomeBase);
//Debug.Log("HomeBase Timer 设置起来...");
agent.PlayAttack();
}
else
{
//m_HomeBaseAttackTimer.Reset();
// Debug.Log("重复设置导致有可能怪物不会消失.");
}
m_IsChargingHomeBaseAttack = true;
}
}
}