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);
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, Levels.Level level)
        {
            if (_attackedBy == null || !level.ContainsEntity(_attackedBy) || _attackedBy.IsDead || _attackTime + _hitAttackerTime < gameTime.TotalGameTime)
            {
                foreach (var beh in Entity.Behaviors.SelectMany(l => l.Value.OfType <AttackBehavior>()))
                {
                    if (beh.OverrideTarget == _attackedBy)
                    {
                        beh.OverrideTarget = null;
                    }
                }

                _attackedBy = null;
            }
        }
Esempio n. 3
0
        public override void Update(GameTime gameTime, Levels.Level level)
        {
            if (CurrentDirection != Vector2.Zero)
            {
                Angle = (float)(Math.Atan2(-CurrentDirection.X, CurrentDirection.Y) + _spriteAngle);
            }

            _timeFromCreation += gameTime.ElapsedGameTime;

            if (level.Map.Collides(CollisionRect, false, true) && CurrentDirection != Vector2.Zero)
            {
                if (level.Map.IsOutsideBorders(CollisionRect))
                {
                    level.RemoveEntity(this);
                }
                else
                {
                    Dettach(level, removeFromLevel: Parent == null || Parent.Category != "Player");
                }
                return;
            }

            if (HitEntity != null)
            {
                if (gameTime.TotalGameTime > _entHitTime + _maxHitTime || !level.ContainsEntity(HitEntity))
                {
                    Dettach(level, HitEntity.Arrows == null || HitEntity.Health <= 0);
                }
                else
                {
                    Position = HitEntity.CenterPosition - _hitLocation;
                }

                return;
            }

            if (Parent != null)
            {
                var timeFactor = gameTime.ElapsedGameTime.TotalMilliseconds / 1000;
                Position += CurrentDirection * (float)timeFactor * Speed;
            }

            foreach (var ent in level.CollidesWith(CollisionRect).Distinct())
            {
                if (Parent == null)
                {
                    if (ent.Arrows != null && ent.Arrows.Quantity < ent.Arrows.Maximum)
                    {
                        ent.Arrows.Quantity += PickupCount;
                        level.RemoveEntity(this);
                    }
                    return;
                }

                if (ent != this && ent != Parent && ent.Health != null)
                {
                    _entHitTime = gameTime.TotalGameTime;
                    HitEntity   = ent;

                    var direction = VectorHelper.AngleToV2(Angle, 5);
                    direction = new Vector2(-direction.Y, direction.X);

                    ent.Hit(this, gameTime, level, direction);
                    _hitLocation = ent.CenterPosition - Position - CurrentDirection;
                    return;
                }
            }
        }