12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using CommonAI.RTS;
- using CommonLang.Vector;
- using CommonLang;
- using CommonAI.Zone.Helper;
- using CommonAI.RTS.Manhattan;
- using CommonAI.Zone.ZoneEditor;
- namespace CommonAI.Zone.Instance
- {
- public abstract class InstanceFlag : GameEntity, IPositionObject
- {
- public abstract float Direction { get; }
- public abstract float RadiusSize { get; }
- public InstanceZone Parent { get { return mParent; } }
- public string Name { get { return mName; } }
- public string Alias { get { return mAlias; } }
- public Vector2 Pos { get { return mPos; } }
- public float X { get { return mPos.X; } }
- public float Y { get { return mPos.Y; } }
- private Vector2 mPos = new Vector2();
- private readonly string mName;
- private readonly string mAlias;
- private readonly string mSrcTag;
- private bool mEnableTriggerd = false;
- private bool mEnable = true;
- private InstanceZone mParent;
- private string mTag;
- public bool Enable
- {
- get { return mEnable; }
- set
- {
- if (!mEnableTriggerd || mEnable != value)
- {
- mEnableTriggerd = true;
- mEnable = value;
- if (mEnable)
- {
- if (mOnFlagEnabled != null)
- mOnFlagEnabled.Invoke(this);
- }
- else
- {
- if (mOnFlagDisabled != null)
- mOnFlagDisabled.Invoke(this);
- }
- }
- }
- }
- public string Tag
- {
- get { return mTag; }
- set
- {
- if (mTag != value)
- {
- mTag = value;
- Parent.queueEvent(new FlagTagChangedEvent(this.mName, this.mTag));
- }
- }
- }
- public string SrcTag
- {
- get { return mSrcTag; }
- }
- public InstanceFlag(InstanceZone zone, ZoneEditor.SceneObjectData data)
- {
- this.mParent = zone;
- this.mName = data.Name;
- this.mAlias = data.Alias;
- this.mPos = new Vector2(data.X, data.Y);
- this.mTag = data.Tag;
- }
- protected override void Disposing()
- {
- base.Disposing();
- this.clearEvents();
- }
- virtual internal void onAdded()
- {
- if (mOnInit != null)
- {
- mOnInit.Invoke(this);
- }
- }
- protected internal virtual void OnStart()
- {
- }
- virtual internal void update()
- {
- }
- abstract public TVector2 getRandomPos(Random random);
- //----------------------------------------------------------------------------
- #region IFlagAPI
- public bool get_enable()
- {
- return Enable;
- }
- public bool set_enable(bool e)
- {
- Enable = e;
- return e;
- }
- public string get_name()
- {
- return Name;
- }
- public float get_x()
- {
- return X;
- }
- public float get_y()
- {
- return Y;
- }
- /// <summary>
- /// Show Check Point
- /// </summary>
- /// <param name="target"></param>
- public void look_at_target(string target)
- {
- mParent.queueEvent(new LookAtEvent(target, 0, 0));
- }
- #endregion
- //----------------------------------------------------------------------------
- #region Delegate
- /// <summary>
- /// 触发器开始
- /// </summary>
- /// <param name="flag"></param>
- public delegate void InitHandler(InstanceFlag flag);
- /// <summary>
- /// 触发器被开启
- /// </summary>
- /// <param name="flag"></param>
- public delegate void FlagEnabledHandler(InstanceFlag flag);
- /// <summary>
- /// 触发器被关闭
- /// </summary>
- /// <param name="flag"></param>
- public delegate void FlagDisabledHandler(InstanceFlag flag);
- private InitHandler mOnInit;
- private FlagEnabledHandler mOnFlagEnabled;
- private FlagDisabledHandler mOnFlagDisabled;
- public event InitHandler OnInit { add { mOnInit += value; } remove { mOnInit -= value; } }
- public event FlagEnabledHandler OnFlagEnabled { add { mOnFlagEnabled += value; } remove { mOnFlagEnabled -= value; } }
- public event FlagDisabledHandler OnFlagDisabled { add { mOnFlagDisabled += value; } remove { mOnFlagDisabled -= value; } }
- protected virtual void clearEvents()
- {
- mOnInit = null;
- mOnFlagEnabled = null;
- mOnFlagDisabled = null;
- }
- #endregion
- //----------------------------------------------------------------------------
- }
- public class ZoneRegion : InstanceFlag, ViewTrigger.ViewTriggerListener
- {
- public ZoneEditor.RegionData.Shape RegionType { get { return mShape; } }
- /// <summary>
- /// Center of the position
- /// </summary>
- public override float RadiusSize { get { return R; } }
- public override float Direction { get { return 0; } }
- public float Width { get { return W; } }
- public float Height { get { return H; } }
- public float Radius { get { return R; } }
- readonly private ZoneEditor.RegionData.Shape mShape;
- private float W;
- private float H;
- private float R;
- private ViewTrigger mViewTrigger;
- private bool mAllTriggerOver = false;
- private bool mAllTriggerNoneAlive = false;
- private List<AbstractSpawnTrigger> mSpawnTriggers = new List<AbstractSpawnTrigger>();
- public ZoneRegion(InstanceZone zone, ZoneEditor.RegionData data)
- : base(zone, data)
- {
- this.mShape = data.RegionType;
- this.W = data.W;
- this.H = data.H;
- this.R = data.Radius;
- switch (mShape)
- {
- case ZoneEditor.RegionData.Shape.RECTANGLE:
- this.mViewTrigger = new ViewTriggerRectCenter(Parent, W, H);
- break;
- case ZoneEditor.RegionData.Shape.STRIP:
- this.mViewTrigger = new ViewTriggerStripCenter(Parent, W, H, data.StripDirection);
- break;
- case ZoneEditor.RegionData.Shape.ROUND:
- default:
- this.mViewTrigger = new ViewTriggerRoundCenter(Parent, R);
- break;
- }
- this.mViewTrigger.setListener(this);
- }
- public override TVector2 getRandomPos(Random random)
- {
- if (mShape == ZoneEditor.RegionData.Shape.ROUND)
- {
- float angle = (float)(random.NextDouble() * CMath.PI_MUL_2);
- float len = (float)(random.NextDouble() * R);
- float x = X + (float)(Math.Cos(angle) * len);
- float y = Y + (float)(Math.Sin(angle) * len);
- return new TVector2(x, y);
- }
- else
- {
- float x = X + (float)((-W / 2f) + random.NextDouble() * W);
- float y = Y + (float)((-H / 2f) + random.NextDouble() * H);
- return new TVector2(x, y);
- }
- }
- public bool IsUnitInRegion(InstanceZoneObject unit)
- {
- return Collider.Object_BlockBody_TouchRound(unit, this.get_x(), this.get_y(), this.RadiusSize); ;
- }
- public void keepInRegion(ref float x, ref float y)
- {
- switch (mShape)
- {
- case ZoneEditor.RegionData.Shape.RECTANGLE:
- x = CMath.getInRange(x, this.X - this.W / 2, this.X + this.W / 2);
- y = CMath.getInRange(y, this.Y - this.H / 2, this.Y + this.H / 2);
- break;
- case ZoneEditor.RegionData.Shape.ROUND:
- float d = MathVector.getDistance(x, y, this.X, this.Y);
- if (d > this.R)
- {
- float a = MathVector.getDegree(x, y, this.X, this.Y);
- MathVector.movePolar(ref x, ref y, a, d - this.R);
- }
- break;
- }
- }
- protected override void Disposing()
- {
- base.Disposing();
- mViewTrigger.Dispose();
- foreach (var e in mSpawnTriggers)
- {
- e.Dispose();
- }
- mSpawnTriggers.Clear();
- mOnUnitEnter = null;
- mOnUnitLeave = null;
- mOnZoneUpdate = null;
- mOnSpawnOver = null;
- }
- internal void AddSpawnTrigger(AbstractSpawnTrigger trigger)
- {
- trigger.OnLastObjectRemoved += onTriggerLastObjectRemoved;
- mSpawnTriggers.Add(trigger);
- mAllTriggerOver = false;
- }
- private void onTriggerLastObjectRemoved(ZoneRegion region, AbstractSpawnTrigger trigger, InstanceZoneObject unit)
- {
- if (mOnSpawnOver != null && IsSpawnOver)
- {
- mOnSpawnOver.Invoke(this);
- }
- }
- /// <summary>
- /// 获取此区域绑定的怪物刷新点
- /// </summary>
- /// <returns></returns>
- public IEnumerable<AbstractSpawnTrigger> GetSpawnTriggers()
- {
- return mSpawnTriggers;
- }
- /// <summary>
- /// 获取此区域绑定的怪物刷新点
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public AbstractSpawnTrigger GetSpawnTrigger(string name)
- {
- for (int i = mSpawnTriggers.Count - 1; i >= 0; --i)
- {
- AbstractSpawnTrigger tg = mSpawnTriggers[i];
- if (string.Equals(tg.Name, name))
- {
- return tg;
- }
- }
- return null;
- }
- /// <summary>
- /// 检测是否所有刷怪点都结束
- /// </summary>
- public bool IsSpawnOver
- {
- get
- {
- if (!mAllTriggerOver)
- {
- for (int i = mSpawnTriggers.Count - 1; i >= 0; --i)
- {
- AbstractSpawnTrigger tg = mSpawnTriggers[i];
- if (!tg.IsSpawnOver)
- {
- return false;
- }
- }
- mAllTriggerOver = true;
- }
- return true;
- }
- }
- /// <summary>
- /// 检测是否所有刷怪点都无存活
- /// </summary>
- public bool IsSpawnNoneAlive
- {
- get
- {
- if (!mAllTriggerNoneAlive)
- {
- for (int i = mSpawnTriggers.Count - 1; i >= 0; --i)
- {
- AbstractSpawnTrigger tg = mSpawnTriggers[i];
- if (!tg.IsSpawnOver)
- {
- return false;
- }
- if (tg.AliveCount > 0)
- {
- return false;
- }
- }
- mAllTriggerNoneAlive = true;
- }
- return true;
- }
- }
- internal override void update()
- {
- base.update();
- if (Enable && (mOnUnitEnter != null || mOnUnitLeave != null || mEnterOnceList.Count > 0 || mLeaveOnceList.Count > 0))
- {
- mViewTrigger.Enable = true;
- mViewTrigger.onLookUpdate(X, Y);
- }
- else
- {
- mViewTrigger.Enable = false;
- }
- if (Enable && mOnZoneUpdate != null)
- {
- mOnZoneUpdate.Invoke(this);
- }
- }
- void ViewTrigger.ViewTriggerListener.onObjectEnterView(ViewTrigger src, InstanceZoneObject obj)
- {
- if (mOnUnitEnter != null)
- {
- mOnUnitEnter.Invoke(this, obj as InstanceUnit);
- }
- mEnterOnceList.Invoke(this, obj as InstanceUnit);
- }
- void ViewTrigger.ViewTriggerListener.onObjectLeaveView(ViewTrigger src, InstanceZoneObject obj)
- {
- if (mOnUnitLeave != null)
- {
- mOnUnitLeave.Invoke(this, obj as InstanceUnit);
- }
- mLeaveOnceList.Invoke(this, obj as InstanceUnit);
- }
- bool ViewTrigger.ViewTriggerListener.select(ViewTrigger src, InstanceZoneObject obj)
- {
- return obj is InstanceUnit;
- }
- public void addInRegionViewed(InstanceUnit unit)
- {
- if (mViewTrigger != null)
- {
- mViewTrigger.addViewed(unit);
- }
- }
- /// <summary>
- /// 获得所有由此刷新点产生的单位
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="list"></param>
- public void getSpawnedObjectsInRegion<T>(List<T> list) where T : InstanceZoneObject
- {
- foreach (var spawn in mSpawnTriggers)
- {
- spawn.getSpawnedObjects<T>(list);
- }
- }
- //[Obsolete]
- //private List<T> getObjectsInRegion<T>(ObjectAoiStatus aoi) where T : InstanceZoneObject
- //{
- // if (this.RegionType == ZoneEditor.RegionData.Shape.RECTANGLE)
- // {
- // return Parent.getObjectsRectRange<T>(
- // Collider.Object_Pos_IncludeInRect,
- // this.X, this.Y, this.Width, this.Height , aoi);
- // }
- // else
- // {
- // return Parent.getObjectsRoundRange<T>(
- // Collider.Object_Pos_IncludeInRound,
- // this.X, this.Y, this.Radius, aoi);
- // }
- //}
- public void getObjectsInRegion<T>(List<T> list, ObjectAoiStatus aoi = null) where T : InstanceZoneObject
- {
- if (this.RegionType == ZoneEditor.RegionData.Shape.RECTANGLE)
- {
- Parent.getObjectsRectRange<T>(
- Collider.Object_Pos_IncludeInRect,
- this.X - this.Width/2, this.Y- this.Height/2, this.X + this.Width/2, this.Y + this.Height/2,
- list, aoi);
- }
- else
- {
- Parent.getObjectsRoundRange<T>(
- Collider.Object_Pos_IncludeInRound,
- this.X, this.Y, this.Radius,
- list, aoi);
- }
- }
- public int getObjectsCountInRegion<T>(ObjectAoiStatus aoi = null) where T : InstanceZoneObject
- {
- if (this.RegionType == ZoneEditor.RegionData.Shape.RECTANGLE)
- {
- return Parent.getObjectsCountRectRange<T>(Collider.Object_Pos_IncludeInRect,
- this.X - this.Width / 2, this.Y - this.Height / 2, this.X + this.Width / 2, this.Y + this.Height / 2, aoi);
- }
- else
- {
- return Parent.getObjectsCountRoundRange<T>(
- Collider.Object_Pos_IncludeInRound,
- this.X, this.Y, this.Radius, aoi);
- }
- }
- //[Obsolete]
- //private List<T> getObjectsInRegion<T>(Select<T> select, ObjectAoiStatus aoi) where T : InstanceZoneObject
- //{
- // List<T> list = getObjectsInRegion<T>();
- // for (int i = list.Count - 1; i >= 0; --i)
- // {
- // if (!select.Invoke(list[i]))
- // {
- // list.RemoveAt(i);
- // }
- // }
- // return list;
- //}
- public int getObjectsCountInRegion<T>(Select<T> select , ObjectAoiStatus aoi = null) where T : InstanceZoneObject
- {
- int ret = 0;
- using (var list = ListObjectPool<T>.AllocAutoRelease())
- {
- getObjectsInRegion<T>(list , aoi);
- for (int i = list.Count - 1; i >= 0; --i)
- {
- if (select.Invoke(list[i]))
- {
- ret++;
- }
- }
- }
- return ret;
- }
- //--------------------------------------------------------------------------------------------------------------
- #region Delegate
- /// <summary>
- /// 某单位进入此区域
- /// </summary>
- /// <param name="region"></param>
- /// <param name="obj"></param>
- public delegate void UnitEnterHandler(ZoneRegion region, InstanceUnit obj);
- /// <summary>
- /// 某单位离开此区域
- /// </summary>
- /// <param name="region"></param>
- /// <param name="obj"></param>
- public delegate void UnitLeaveHandler(ZoneRegion region, InstanceUnit obj);
- /// <summary>
- /// 区域更新
- /// </summary>
- /// <param name="region"></param>
- public delegate void ZoneUpdateHandler(ZoneRegion region);
- /// <summary>
- /// 刷新点刷新完毕
- /// </summary>
- /// <param name="region"></param>
- public delegate void SpawnOverHandler(ZoneRegion region);
- private UnitEnterHandler mOnUnitEnter;
- private UnitLeaveHandler mOnUnitLeave;
- private ZoneUpdateHandler mOnZoneUpdate;
- private SpawnOverHandler mOnSpawnOver;
- public event UnitEnterHandler OnUnitEnter { add { mOnUnitEnter += value; } remove { mOnUnitEnter -= value; } }
- public event UnitLeaveHandler OnUnitLeave { add { mOnUnitLeave += value; } remove { mOnUnitLeave -= value; } }
- public event ZoneUpdateHandler OnZoneUpdate { add { mOnZoneUpdate += value; } remove { mOnZoneUpdate -= value; } }
- public event SpawnOverHandler OnSpawnOver { add { mOnSpawnOver += value; } remove { mOnSpawnOver -= value; } }
- //--------------------------------------------------------------------------------------------------------------
- private struct OnceEvent : IOnceInvoke
- {
- public InstanceUnit unit { get; private set; }
- public bool IsDone { get { return done || !unit.Enable; } }
- private UnitEnterHandler enter_handler;
- private UnitLeaveHandler leave_handler;
- private bool done;
- public OnceEvent(InstanceUnit unit, UnitEnterHandler enter, UnitLeaveHandler leave)
- {
- this.unit = unit;
- this.enter_handler = enter;
- this.leave_handler = leave;
- this.done = false;
- }
- public void Invoke(ZoneRegion region)
- {
- if (enter_handler != null) this.enter_handler.Invoke(region, unit);
- if (leave_handler != null) this.leave_handler.Invoke(region, unit);
- this.enter_handler = null;
- this.leave_handler = null;
- this.done = true;
- }
- }
- private class OnceInvokeList : OnceInvokeList<OnceEvent>
- {
- public void Invoke(ZoneRegion region, InstanceUnit unit)
- {
- base.Invoke((e) =>
- {
- if (e.unit == unit)
- {
- e.Invoke(region);
- }
- });
- }
- }
- private OnceInvokeList mEnterOnceList = new OnceInvokeList();
- private OnceInvokeList mLeaveOnceList = new OnceInvokeList();
- /// <summary>
- /// 监听单位进入一次,触发器只触发一次
- /// </summary>
- /// <param name="unit"></param>
- /// <param name="handler"></param>
- public void ListenUnitEnterOnce(InstanceUnit unit, UnitEnterHandler handler)
- {
- mEnterOnceList.Add(new OnceEvent(unit, handler, null));
- }
- /// <summary>
- /// 监听单位离开一次,触发器只触发一次
- /// </summary>
- /// <param name="unit"></param>
- /// <param name="handler"></param>
- public void ListenUnitLeaveOnce(InstanceUnit unit, UnitLeaveHandler handler)
- {
- mLeaveOnceList.Add(new OnceEvent(unit, null, handler));
- }
- //--------------------------------------------------------------------------------------------------------------
- protected override void clearEvents()
- {
- base.clearEvents();
- mEnterOnceList.Clear();
- mLeaveOnceList.Clear();
- mOnUnitEnter = null;
- mOnUnitLeave = null;
- mOnZoneUpdate = null;
- mOnSpawnOver = null;
- }
- #endregion
- }
- public class ZoneWayPoint : InstanceFlag
- {
- public override float RadiusSize { get { return 1; } }
- public override float Direction { get { return 0; } }
- private IList<ZoneWayPoint> mNexts = new List<ZoneWayPoint>();
- public ZoneWayPoint(InstanceZone zone, ZoneEditor.PointData data)
- : base(zone, data)
- {
- }
- public override TVector2 getRandomPos(Random random)
- {
- return new TVector2(X, Y);
- }
- internal override void update()
- {
- base.update();
- }
- internal void AddNext(ZoneWayPoint next)
- {
- mNexts.Add(next);
- }
- public ZoneWayPoint GetRandomNext(ZoneWayPoint prev = null)
- {
- if (mNexts.Count > 0)
- {
- if (mNexts.Count == 1)
- {
- return mNexts[0];
- }
- int nexti = Parent.RandomN.Next(0, mNexts.Count);
- for (int i = 0; i < mNexts.Count; i++)
- {
- int idx = CMath.cycNum(nexti, i, mNexts.Count);
- if (mNexts[idx] != prev)
- {
- return mNexts[idx];
- }
- }
- return mNexts[nexti];
- }
- return null;
- }
- public ZoneWayPoint GetNext()
- {
- if (mNexts.Count > 0)
- {
- return mNexts[0];
- }
- return null;
- }
- public ZoneWayPoint GetTail()
- {
- if (mNexts.Count == 0)
- {
- return this;
- }
- ZoneWayPoint wp = GetNext();
- while (true)
- {
- ZoneWayPoint next = wp.GetNext();
- if (next != null)
- {
- wp = next;
- }
- else
- {
- break;
- }
- }
- return wp;
- }
- }
- public class ZoneDecoration : InstanceFlag
- {
- public override float RadiusSize { get { return this.R; } }
- public override float Direction { get { return this.StripDirection; } }
- readonly private ZoneEditor.DecorationData.Shape mShape;
- private float W;
- private float H;
- private float R;
- private float StripDirection;
- private bool Blockable;
- private int BlockValue;
- public ZoneDecoration(InstanceZone zone, ZoneEditor.DecorationData data)
- : base(zone, data)
- {
- this.mShape = data.RegionType;
- this.W = data.W;
- this.H = data.H;
- this.R = data.Radius;
- this.StripDirection = data.StripDirection;
- this.Blockable = data.Blockable;
- this.BlockValue = data.BlockValue;
- this.OnFlagEnabled += this.OnEnabled;
- this.OnFlagDisabled += this.OnDisabled;
- if (Blockable)
- {
- this.OnFlagEnabled += this.OnTerrainEnabled;
- this.OnFlagDisabled += this.OnTerrainDisabled;
- }
- }
- internal override void update()
- {
- base.update();
- }
- public override TVector2 getRandomPos(Random random)
- {
- if (mShape == ZoneEditor.DecorationData.Shape.ROUND)
- {
- float angle = (float)(random.NextDouble() * CMath.PI_MUL_2);
- float len = (float)(random.NextDouble() * R);
- float x = X + (float)(Math.Cos(angle) * len);
- float y = Y + (float)(Math.Sin(angle) * len);
- return new TVector2(x, y);
- }
- else
- {
- float x = X + (float)((-W / 2f) + random.NextDouble() * W);
- float y = Y + (float)((-H / 2f) + random.NextDouble() * H);
- return new TVector2(x, y);
- }
- }
- private void OnEnabled(InstanceFlag flag)
- {
- Parent.queueEvent(new DecorationChangedEvent(Name, true));
- }
- private void OnDisabled(InstanceFlag flag)
- {
- Parent.queueEvent(new DecorationChangedEvent(Name, false));
- }
- #region FillTerrain
- private void OnTerrainEnabled(InstanceFlag flag)
- {
- this.FillTerrain(fill_Terrain_value);
- }
- private void OnTerrainDisabled(InstanceFlag flag)
- {
- this.FillTerrain(fill_Terrain_reset);
- }
- private void fill_Terrain_value(int bx, int by)
- {
- Parent.PathFinder.TryFillTerrain(bx, by, BlockValue);
- }
- private void fill_Terrain_reset(int bx, int by)
- {
- Parent.PathFinder.TryFillTerrain(bx, by, Parent.TerrainSrc.TerrainMatrix[bx, by]);
- }
- protected void FillTerrain(AstarManhattan.ForEachTerrainAction fillAction)
- {
- var mmap = Parent.PathFinder.MMap;
- switch (this.mShape)
- {
- case ZoneEditor.DecorationData.Shape.RECTANGLE:
- AstarManhattan.ForEachTerrainRect(mmap, X - W / 2, Y - H / 2, W, H, fillAction);
- break;
- case ZoneEditor.DecorationData.Shape.ROUND:
- AstarManhattan.ForEachTerrainEllipse(mmap, X - W / 2, Y - H / 2, W, H, fillAction);
- break;
- case ZoneEditor.DecorationData.Shape.STRIP:
- TLine2 line = new TLine2(X, Y, X, Y);
- MathVector.movePolarExt(ref line.p, StripDirection, -H / 2);
- MathVector.movePolarExt(ref line.q, StripDirection, +H / 2);
- AstarManhattan.ForEachTerrainStripWidth(mmap, line.p.X, line.p.Y, line.q.X, line.q.Y, W / 2, fillAction);
- break;
- }
- Parent.PathFinder.BeginFindPath();
- }
- public bool TouchBlock(float x, float y)
- {
- if (Blockable && Enable && Parent.PathFinder.MMap.TestBlockValue(this.BlockValue))
- {
- int bx = (((int)x) / Parent.TerrainSrc.GridCellW);
- int by = (((int)y) / Parent.TerrainSrc.GridCellH);
- var mmap = Parent.PathFinder.MMap;
- switch (this.mShape)
- {
- case ZoneEditor.DecorationData.Shape.RECTANGLE:
- return AstarManhattan.ForEachTerrainRect(mmap, X - W / 2, Y - H / 2, W, H,
- (cx, cy) => { return cx == bx && cy == by; });
- case ZoneEditor.DecorationData.Shape.ROUND:
- return AstarManhattan.ForEachTerrainEllipse(mmap, X - W / 2, Y - H / 2, W, H,
- (cx, cy) => { return cx == bx && cy == by; });
- case ZoneEditor.DecorationData.Shape.STRIP:
- TLine2 line = new TLine2(X, Y, X, Y);
- MathVector.movePolarExt(ref line.p, StripDirection, -H / 2);
- MathVector.movePolarExt(ref line.q, StripDirection, +H / 2);
- return AstarManhattan.ForEachTerrainStripWidth(mmap, line.p.X, line.p.Y, line.q.X, line.q.Y, W / 2,
- (cx, cy) => { return cx == bx && cy == by; });
- }
- }
- return false;
- }
- #endregion
- }
- public class ZoneArea : InstanceFlag
- {
- public override float RadiusSize { get { return this.R; } }
- public override float Direction { get { return 0; } }
- public int CurrentMapNodeValue { get; private set; }
- private float W;
- private float H;
- private float R;
- public ZoneArea(InstanceZone zone, ZoneEditor.AreaData data)
- : base(zone, data)
- {
- this.W = data.W;
- this.H = data.H;
- this.R = data.W / 2;
- }
- internal void BindMapBlock(AreaData data)
- {
- //var current_node = Parent.PathFinder.GetMapNodeByPos(X, Y);
- //if (current_node != null)
- {
- this.CurrentMapNodeValue = data.Color;
- var list = Parent.TerrainAreaGenerator.GetContinuousMapNode(X, Y, W / 2, H / 2);
- foreach (var index in list)
- {
- var node = Parent.PathFinder.GetMapNode(index.BX, index.BY) as ZoneMapNode;
- if (node != null)
- {
- node.SetServerArea(this);
- }
- }
- }
- }
- public override TVector2 getRandomPos(Random random)
- {
- return new TVector2(X, Y);
- }
- internal void do_onUnitEnter(InstanceZoneObject obj)
- {
- if (obj is InstanceUnit)
- {
- if (mOnUnitEnter != null) mOnUnitEnter.Invoke(this, obj as InstanceUnit);
- if (mEnterOnceList.Count > 0) mEnterOnceList.Invoke(this, obj as InstanceUnit);
- }
- }
- internal void do_onUnitLeave(InstanceZoneObject obj)
- {
- if (obj is InstanceUnit)
- {
- if (mOnUnitLeave != null) mOnUnitLeave.Invoke(this, obj as InstanceUnit);
- if (mLeaveOnceList.Count > 0) mLeaveOnceList.Invoke(this, obj as InstanceUnit);
- }
- }
- //----------------------------------------------------------------------------
- #region Delegate
- /// <summary>
- /// 某单位进入此区域
- /// </summary>
- /// <param name="area"></param>
- /// <param name="obj"></param>
- public delegate void UnitEnterHandler(ZoneArea area, InstanceUnit obj);
- /// <summary>
- /// 某单位离开此区域
- /// </summary>
- /// <param name="area"></param>
- /// <param name="obj"></param>
- public delegate void UnitLeaveHandler(ZoneArea area, InstanceUnit obj);
- private UnitEnterHandler mOnUnitEnter;
- private UnitLeaveHandler mOnUnitLeave;
- public event UnitEnterHandler OnUnitEnter { add { mOnUnitEnter += value; } remove { mOnUnitEnter -= value; } }
- public event UnitLeaveHandler OnUnitLeave { add { mOnUnitLeave += value; } remove { mOnUnitLeave -= value; } }
- //----------------------------------------------------------------------------
- private struct OnceEvent : IOnceInvoke
- {
- public InstanceUnit unit { get; private set; }
- public bool IsDone { get { return done || !unit.Enable; } }
- private UnitEnterHandler enter_handler;
- private UnitLeaveHandler leave_handler;
- private bool done;
- public OnceEvent(InstanceUnit unit, UnitEnterHandler enter, UnitLeaveHandler leave)
- {
- this.unit = unit;
- this.enter_handler = enter;
- this.leave_handler = leave;
- this.done = false;
- }
- public void Invoke(ZoneArea area)
- {
- if (enter_handler != null) this.enter_handler.Invoke(area, unit);
- if (leave_handler != null) this.leave_handler.Invoke(area, unit);
- this.enter_handler = null;
- this.leave_handler = null;
- this.done = true;
- }
- }
- private class OnceInvokeList : OnceInvokeList<OnceEvent>
- {
- public void Invoke(ZoneArea area, InstanceUnit unit)
- {
- base.Invoke((e) =>
- {
- if (e.unit == unit)
- {
- e.Invoke(area);
- }
- });
- }
- }
- private OnceInvokeList mEnterOnceList = new OnceInvokeList();
- private OnceInvokeList mLeaveOnceList = new OnceInvokeList();
- /// <summary>
- /// 监听单位进入一次,触发器只触发一次
- /// </summary>
- /// <param name="unit"></param>
- /// <param name="handler"></param>
- public void ListenUnitEnterOnce(InstanceUnit unit, UnitEnterHandler handler)
- {
- mEnterOnceList.Add(new OnceEvent(unit, handler, null));
- }
- /// <summary>
- /// 监听单位离开一次,触发器只触发一次
- /// </summary>
- /// <param name="unit"></param>
- /// <param name="handler"></param>
- public void ListenUnitLeaveOnce(InstanceUnit unit, UnitLeaveHandler handler)
- {
- mLeaveOnceList.Add(new OnceEvent(unit, null, handler));
- }
- //----------------------------------------------------------------------------
- protected override void clearEvents()
- {
- base.clearEvents();
- mEnterOnceList.Clear();
- mLeaveOnceList.Clear();
- mOnUnitEnter = null;
- mOnUnitLeave = null;
- }
- #endregion
- //----------------------------------------------------------------------------
- }
- }
|