using System;
|
using System.Collections.Generic;
|
using ActionGameFramework.Audio;
|
using Core.Health;
|
using DG.Tweening;
|
using MoreMountains.NiceVibrations;
|
using TowerDefense.Agents;
|
using TowerDefense.UI.HUD;
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
namespace TowerDefense.Level
|
{
|
/// <summary>
|
/// A class representing the home base that players must defend
|
/// </summary>
|
public class PlayerHomeBase : DamageableBehaviour
|
{
|
/// <summary>
|
/// The particle system when an attack is charging
|
/// </summary>
|
public ParticleSystem chargePfx;
|
|
/// <summary>
|
/// Sound to play when charge effect starts
|
/// </summary>
|
public RandomAudioSource chargeSound;
|
|
/// <summary>
|
/// The particle system for an attack
|
/// </summary>
|
public ParticleSystem attackPfx;
|
|
/// <summary>
|
/// Sound to play when attack effect starts
|
/// </summary>
|
public RandomAudioSource attackSound;
|
|
/// <summary>
|
/// 是否对手的HomeBase
|
/// </summary>
|
public bool opponent = false;
|
|
/// <summary>
|
/// 用于确认HomeBase血量的ImageList.
|
/// </summary>
|
public List<Image> listHeart;
|
|
|
/// <summary>
|
/// The current Agents within the home base attack zone
|
/// </summary>
|
protected List<Agent> m_CurrentAgentsInside = new List<Agent>();
|
|
/// <summary>
|
/// Subscribes to damaged event
|
/// </summary>
|
protected virtual void Start()
|
{
|
configuration.damaged += OnDamaged;
|
}
|
|
/// <summary>
|
/// 当前HomeBase对应的X索引,用于跟TowerPlacementGrid的塔位联系起来
|
/// </summary>
|
public int homebaseIdx
|
{
|
get;set;
|
}
|
|
/// <summary>
|
/// Unsubscribes to damaged event
|
/// </summary>
|
protected virtual void OnDestroy()
|
{
|
configuration.damaged -= OnDamaged;
|
}
|
|
/// <summary>
|
/// Plays <see cref="attackPfx"/> if assigned
|
/// </summary>
|
protected virtual void OnDamaged(HealthChangeInfo obj)
|
{
|
if ((attackPfx != null)&&(!opponent) )
|
{
|
ParticleSystem playParticle = Instantiate(attackPfx);
|
playParticle.transform.position = this.position;
|
playParticle.Simulate(0.0f);
|
playParticle.Play();
|
|
//
|
// 播放屏幕振动和手机振动:
|
ViewPortAdj.instance.cachedCamera.DOShakePosition(0.25f, 1.5f, 4);
|
MMVibrationManager.Haptic(HapticTypes.HeavyImpact);
|
|
}
|
if (attackSound != null)
|
{
|
attackSound.PlayRandomClip();
|
}
|
}
|
|
/// <summary>
|
/// Adds triggered Agent to tracked Agents, subscribes to Agent's
|
/// removed event and plays pfx
|
/// </summary>
|
/// <param name="other">Triggered collider</param>
|
void OnTriggerEnter(Collider other)
|
{
|
var homeBaseAttacker = other.GetComponent<HomeBaseAttacker>();
|
if (homeBaseAttacker == null)
|
{
|
// 场景内所有放置的Tower上的Targetter都会在这碰撞出现!
|
//if( other.transform.parent )
|
// Debug.Log("PlayerHomeBase Name is:" + other.transform.parent.name);
|
return;
|
}
|
//Debug.Log("Agent到达HomeBase的位置...");
|
m_CurrentAgentsInside.Add(homeBaseAttacker.agent);
|
homeBaseAttacker.agent.removed += OnAgentRemoved;
|
if (chargePfx != null)
|
{
|
chargePfx.Play();
|
}
|
if (chargeSound != null)
|
{
|
chargeSound.PlayRandomClip();
|
}
|
// 播放攻击动作:
|
//homeBaseAttacker.agent.PlayAttack();
|
|
}
|
|
/// <summary>
|
/// If the entity that has entered the collider
|
/// has an <see cref="Agent"/> component on it
|
/// </summary>
|
void OnTriggerExit(Collider other)
|
{
|
var homeBaseAttacker = other.GetComponent<HomeBaseAttacker>();
|
if (homeBaseAttacker == null)
|
{
|
return;
|
}
|
RemoveTarget(homeBaseAttacker.agent);
|
}
|
|
/// <summary>
|
/// Removes Agent from tracked <see cref="m_CurrentAgentsInside"/>
|
/// </summary>
|
void OnAgentRemoved(DamageableBehaviour targetable)
|
{
|
targetable.removed -= OnAgentRemoved;
|
Agent attackingAgent = targetable as Agent;
|
RemoveTarget(attackingAgent);
|
}
|
|
/// <summary>
|
/// Removes <paramref name="agent"/> from <see cref="m_CurrentAgentsInside"/> and stops pfx
|
/// if there are no more <see cref="Agent"/>s
|
/// </summary>
|
/// <param name="agent">
|
/// The agent to remove
|
/// </param>
|
void RemoveTarget(Agent agent)
|
{
|
if (agent == null)
|
{
|
return;
|
}
|
m_CurrentAgentsInside.Remove(agent);
|
if (m_CurrentAgentsInside.Count == 0 && chargePfx != null)
|
{
|
chargePfx.Stop();
|
}
|
}
|
|
/// <summary>
|
/// Fires kill events
|
/// </summary>
|
protected override void OnDeath()
|
{
|
base.OnDeath();
|
|
// WORK START: 看看是否对手塔位可以被干掉.对手Wave的出兵规则,需要重新开始处理。
|
// 干掉当前塔位,显示成破坏状态
|
Debug.Log("当前HomeBase被干掉:" + homebaseIdx.ToString());
|
if (GameUI.instanceExists)
|
GameUI.instance.DestroyTowerGrid(homebaseIdx, this.opponent);
|
else if (EndlessGameUI.instanceExists)
|
EndlessGameUI.instance.DestroyTowerGrid(homebaseIdx);
|
}
|
}
|
}
|