コード例 #1
0
        public override bool IsActive(Microsoft.Xna.Framework.GameTime gameTime, Levels.Level level)
        {
            if (Entity.IsDead)
            {
                return(false);
            }

            if (Following != null)
            {
                if (MaxDistance != null)
                {
                    var distance = new Vector2(Following.CenterPosition.X - Entity.CenterPosition.X, Following.CenterPosition.Y - Entity.CenterPosition.Y).Length();
                    if (distance > MaxDistance)
                    {
                        Following.GetBehavior <ChainBehavior <T> >().FollowedBy = null;
                        Following = null;
                    }
                }

                if (Following != null && (Following.IsDead || !level.ContainsEntity(Following)))
                {
                    Following.GetBehavior <ChainBehavior <T> >().FollowedBy = null;
                    Following = null;
                }
            }

            if (Following == null)
            {
                Following = (from ent in level.GetEntities <T>()
                             where ent != Entity && !ent.IsDead && level.ContainsEntity(ent)
                             let beh = ent.GetBehavior <ChainBehavior <T> >()
                                       where beh != null && beh.FollowedBy == null && !beh.IsFollowingEntity(Entity)
                                       let pos = new Vector2(Entity.CenterPosition.X - ent.CenterPosition.X,
                                                             Entity.CenterPosition.Y - ent.CenterPosition.Y)
                                                 let distance = pos.Length()
                                                                where MaxDistance == null || distance < MaxDistance
                                                                orderby distance
                                                                select ent).FirstOrDefault();
                if (Following != null)
                {
                    Following.GetBehavior <ChainBehavior <T> >().FollowedBy = Entity;
                }
            }

            return(Following != null);
        }