using System;
using CommonAI.Zone.Helper;
using CommonAI.Zone.Instance;
using CommonLang;
using XmdsCommon.Plugin;
namespace XmdsCommonServer.Plugin.Scene
{
///
/// 由玩家创建的私有位面
///
public class XmdsPlayerAOI : ObjectAoiStatus
{
private readonly InstancePlayer m_owner;
private readonly InstanceZone m_zone;
///
/// 玩家自己
///
public InstancePlayer Owner { get { return m_owner; } }
bool _canSeeMe = false;
public override bool CanSeeMe
{
get
{
return _canSeeMe;
}
}
bool _canSeeOther = false;
public override bool CanSeeOther
{
get
{
return _canSeeOther;
}
}
public override bool IsNeedNotifyClient
{
get
{
return true;
}
}
///
/// 是否在死亡后退出位面
///
public bool DeadExit { get; set; }
///
/// 此次aoi名称
///
public string AoiName { get; set; }
///
/// 没有怪物则销毁位面
///
public bool isDestoryedOnMonsterDead
{
get;
private set;
}
///
/// 任务完成则自动退出位面
///
public string QuestId
{
get;
private set;
}
private void onOwnerDead(InstanceUnit unit, InstanceUnit attacker)
{
if (DeadExit)
{
unit.setAoiStatus(null);
}
}
private void onQuestStatusChanged(InstancePlayer player, string quest, string key, string value)
{
if(player == m_owner && quest == QuestId && key == XmdsQuestData.Attribute_SubState && value == "1")
{
player.setAoiStatus(null);
}
}
public XmdsPlayerAOI(InstancePlayer player,
bool can_see_me = false,
bool can_see_other = false ,
bool isDestoryedOnMonsterDead = false,
bool DeadExit = false,
string QuestId = null)
{
this.m_owner = player;
this.m_zone = player.Parent;
this._canSeeMe = can_see_me;
this._canSeeOther = can_see_other;
this.isDestoryedOnMonsterDead = isDestoryedOnMonsterDead;
this.DeadExit = DeadExit;
this.QuestId = QuestId;
if(!string.IsNullOrEmpty(this.QuestId))
{
this.m_zone.OnQuestStatusChanged += onQuestStatusChanged;
}
player.OnDead += onOwnerDead;
}
protected override void onObjectEnter(InstanceZoneObject o)
{
base.onObjectEnter(o);
}
protected override void onObjectLeave(InstanceZoneObject o)
{
base.onObjectLeave(o);
if (o == m_owner)
{
(o as InstancePlayer).OnDead -= onOwnerDead;
this.Cleanup();
}
if (isDestoryedOnMonsterDead && GetUnitCountByForce(1) == 0)
{
m_owner.setAoiStatus(null);
}
}
///
/// 玩家掉线后,干掉所有私有AOI单位
///
public void Cleanup()
{
using(var list = ListObjectPool.AllocAutoRelease(base.Objects))
{
foreach (var obj in list)
{
if (obj != m_owner)
{
if(obj is InstancePlayer)
{
(obj as InstancePlayer).setAoiStatus(null);
}
else
{
m_zone.RemoveObjectByID(obj.ID);
}
}
}
}
if (!string.IsNullOrEmpty(this.QuestId))
{
this.m_zone.OnQuestStatusChanged -= onQuestStatusChanged;
}
}
}
}