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
{
///
/// A class representing the home base that players must defend
///
public class PlayerHomeBase : DamageableBehaviour
{
///
/// The particle system when an attack is charging
///
public ParticleSystem chargePfx;
///
/// Sound to play when charge effect starts
///
public RandomAudioSource chargeSound;
///
/// The particle system for an attack
///
public ParticleSystem attackPfx;
///
/// Sound to play when attack effect starts
///
public RandomAudioSource attackSound;
///
/// 是否对手的HomeBase
///
public bool opponent = false;
///
/// 用于确认HomeBase血量的ImageList.
///
public List listHeart;
///
/// The current Agents within the home base attack zone
///
protected List m_CurrentAgentsInside = new List();
///
/// Subscribes to damaged event
///
protected virtual void Start()
{
configuration.damaged += OnDamaged;
}
///
/// 当前HomeBase对应的X索引,用于跟TowerPlacementGrid的塔位联系起来
///
public int homebaseIdx
{
get;set;
}
///
/// Unsubscribes to damaged event
///
protected virtual void OnDestroy()
{
configuration.damaged -= OnDamaged;
}
///
/// Plays if assigned
///
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();
}
}
///
/// Adds triggered Agent to tracked Agents, subscribes to Agent's
/// removed event and plays pfx
///
/// Triggered collider
void OnTriggerEnter(Collider other)
{
var homeBaseAttacker = other.GetComponent();
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();
}
///
/// If the entity that has entered the collider
/// has an component on it
///
void OnTriggerExit(Collider other)
{
var homeBaseAttacker = other.GetComponent();
if (homeBaseAttacker == null)
{
return;
}
RemoveTarget(homeBaseAttacker.agent);
}
///
/// Removes Agent from tracked
///
void OnAgentRemoved(DamageableBehaviour targetable)
{
targetable.removed -= OnAgentRemoved;
Agent attackingAgent = targetable as Agent;
RemoveTarget(attackingAgent);
}
///
/// Removes from and stops pfx
/// if there are no more s
///
///
/// The agent to remove
///
void RemoveTarget(Agent agent)
{
if (agent == null)
{
return;
}
m_CurrentAgentsInside.Remove(agent);
if (m_CurrentAgentsInside.Count == 0 && chargePfx != null)
{
chargePfx.Stop();
}
}
///
/// Fires kill events
///
protected override void OnDeath()
{
base.OnDeath();
// WORK START: 看看是否对手塔位可以被干掉.对手Wave的出兵规则,需要重新开始处理。
// 干掉当前塔位,显示成破坏状态
Debug.Log("当前HomeBase被干掉:" + homebaseIdx.ToString());
if (GameUI.instanceExists)
GameUI.instance.DestroyTowerGrid(homebaseIdx, this.opponent);
}
}
}