using CommonAI.Zone;
using CommonAI.Zone.Instance;
using CommonAI.Zone.ZoneEditor;
using CommonLang;
using CommonLang.Log;
using CommonLang.Vector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XmdsCommonServer.Plugin.Scene;
using XmdsCommonServer.Plugin.Units;
namespace XmdsCommonServer.Plugin.Quest
{
///
/// 任务脚本子类
///
public abstract class QuestScript
{
protected readonly static Logger log = LoggerFactory.GetLogger("QuestScript");
private string m_QuestID;
//private int m_QuestType;
private XmdsInstancePlayer m_Player;
private XmdsServerScene m_Zone;
private XmdsVirtual_Player m_Virtual;
///
/// 当前场景
///
public XmdsServerScene Zone { get { return m_Zone; } }
///
/// 触发的任务ID
///
public string QuestID { get { return m_QuestID; } }
///
/// 触发的任务类型
///
//public int QuestType { get { return m_QuestType; } }
///
/// 触发任务的玩家实体
///
public XmdsInstancePlayer Player { get { return m_Player; } }
///
/// 触发任务的玩家虚体
///
public XmdsVirtual_Player Virtual { get { return m_Virtual; } }
///
/// 当前AOI
///
public XmdsPlayerAOI AOI { get { return m_Player.AoiStatus as XmdsPlayerAOI; } }
Action mOnStart;
Action mOnDispose;
public event Action DisposeEvent { add { mOnDispose += value; } remove { mOnDispose -= value; } }
public event Action StartEvent { add { mOnStart += value; } remove { mOnStart -= value; } }
//---------------------------------------------------------------------------------------------------
internal void Init(string questID, int questType, XmdsInstancePlayer player)
{
this.m_QuestID = questID;
this.m_Player = player;
this.m_Zone = player.Parent as XmdsServerScene;
this.m_Virtual = player.Virtual as XmdsVirtual_Player;
}
internal void Dispose()
{
this.OnDispose();
if (mOnDispose != null)
{
mOnDispose.Invoke(this);
}
this.m_QuestID = null;
this.m_Player = null;
this.m_Zone = null;
this.m_Virtual = null;
}
internal void Start()
{
this.OnStart();
if (mOnStart != null)
{
mOnStart.Invoke(this);
}
}
//---------------------------------------------------------------------------------------------------
///
/// 脚本开始回调
///
protected abstract void OnStart();
///
/// 脚本结束回调
///
protected abstract void OnDispose();
///
/// 返回true表示处理完成后不传递到其他QuestScrpt中
///
///
public virtual bool TryDoAction(ObjectAction act)
{
return false;
}
//---------------------------------------------------------------------------------------------------
#region API
///
/// 任务失败
///
/// 原因
public void QuestFailed(string reason = "")
{
Zone.QuestAdapter.DoDropQuest(Player.PlayerUUID, QuestID, reason);
}
///
/// 任务完成
///
/// 原因
public void QuestAccomplish(string reason = "")
{
Zone.QuestAdapter.DoCommitQuest(Player.PlayerUUID, QuestID, reason);
}
public virtual void OnStatusChanged(string key, string v)
{
}
///
/// 玩家进入AOI
///
///
///
public void PlayerEnterAOI(bool can_see_me = false, bool can_see_other = false)
{
XmdsPlayerAOI aoi = new XmdsPlayerAOI(Player, can_see_me, can_see_other);
Player.setAoiStatus(aoi);
}
///
/// 玩家离开AOI
///
public void PlayerLeaveAOI()
{
Player.setAoiStatus(null);
}
///
/// 添加若干单位在区域范围内
///
/// 区域名字
/// 模板ID
/// 数量
/// Force
/// 是否在AOI
/// 名字
/// 等级
/// 出生后A到此处
///
public InstanceUnit[] AddUnitsInRegion(string region_name, int unit_template_id, int count, int force, bool aoi = true, string unit_name = null, int level = 0, string attack_to = null)
{
InstanceUnit[] ret = new InstanceUnit[count];
var region = Zone.GetEditFlag(region_name) as ZoneRegion;
if (region != null)
{
for (int i = count - 1; i >= 0; --i)
{
var pos = region.getRandomPos(Zone.RandomN);
var unit = AddUnit(pos, unit_template_id, force, aoi, unit_name, level, attack_to);
ret[i] = unit;
}
}
else
{
log.Error("Region not exist : " + region_name);
}
return ret;
}
///
/// 添加一个单位
///
/// 区域名字
/// 模板ID
/// Force
/// 是否在AOI
/// 名字
/// 等级
/// 出生后A到此处
///
public InstanceUnit AddUnitInRegion(string region_name, int unit_template_id, int force, bool aoi = true, string unit_name = null, int level = 0, string attack_to = null)
{
var region = Zone.GetEditFlag(region_name) as ZoneRegion;
if (region != null)
{
return AddUnit(region.Pos, unit_template_id, force, aoi, unit_name, level, attack_to);
}
else
{
log.Error("Region not exist : " + region_name);
return null;
}
}
///
/// 添加一个单位
///
/// 坐标
/// 模板ID
/// Force
/// 是否在AOI
/// 名字
/// 等级
/// 出生后A到此处
///
public InstanceUnit AddUnit(IVector2 pos, int unit_template_id, int force, bool aoi = true, string unit_name = null, int level = 0, string attack_to = null)
{
InstanceUnit unit = Zone.AddUnit(unit_template_id, "", (byte)force, level, pos.X, pos.Y, CMath.RandomAngle(Zone.RandomN));
if (unit != null)
{
if (aoi) unit.setAoiStatus(Player.AoiStatus);
if (unit is InstanceGuard && attack_to != null)
{
InstanceGuard guard = unit as InstanceGuard;
ZoneWayPoint flag = Zone.GetEditFlag(attack_to) as ZoneWayPoint;
if (flag != null)
{
guard.attackTo(flag);
}
}
}
else
{
log.Error("Create unit error : " + unit_template_id);
}
return unit;
}
#endregion
//---------------------------------------------------------------------------------------------------
}
public class Quest_Simple : QuestScript
{
protected override void OnDispose() {}
protected override void OnStart() {}
}
}