Ejemplo n.º 1
0
        public virtual void Update(GameTime gameTime, Level level)
        {
            _savedPositions.Enqueue(new EntitySavedPosition
            {
                Position      = CenterPosition,
                Level         = level.Id,
                TotalGameTime = gameTime.TotalGameTime
            });

            if (_savedPositions.Count > 5)
            {
                _savedPositions.Dequeue();
            }

            if (Behaviors != null)
            {
                var activeBehaviors = new List <EntityUpdateBehavior>();

                foreach (var behaviorGroup in Behaviors.ToList())
                {
                    if (behaviorGroup.Key == string.Empty)
                    {
                        foreach (var behavior in behaviorGroup.Value.Where(b => b.IsActive(gameTime, level)))
                        {
                            activeBehaviors.Add(behavior);
                        }
                    }
                    else
                    {
                        var bh = behaviorGroup.Value.Where(b => b.IsActive(gameTime, level)).FirstOrDefault();
                        if (bh != null)
                        {
                            activeBehaviors.Add(bh);
                        }
                    }
                }

                foreach (var bh in Behaviors.SelectMany(b => b.Value).ToList())
                {
                    if (activeBehaviors.Contains(bh))
                    {
                        bh.Update(gameTime, level);
                    }
                    else
                    {
                        bh.Deactivated(gameTime, level);
                    }
                }
            }

            var curAnimation = SelectAnimation();

            if (curAnimation != _lastAnimation)
            {
                _lastFrameStartTime = gameTime.TotalGameTime;
                _lastAnimation      = curAnimation;
                _currentFrameIndex  = 0;
            }
            else if (gameTime.TotalGameTime > _lastFrameStartTime + curAnimation.FrameDuration)
            {
                _currentFrameIndex++;
                if (_currentFrameIndex >= curAnimation.FrameIndexes.Length)
                {
                    if (AnimationEnded != null)
                    {
                        AnimationEnded(this, EventArgs.Empty);
                    }
                    _currentFrameIndex = curAnimation.ResetToFrame;
                }
                _lastFrameStartTime = gameTime.TotalGameTime;
            }

            if (Magic != null)
            {
                if (_timeSinceLastMpRegen > 1000)
                {
                    if (!Magic.IsFull)
                    {
                        Magic.Quantity++;
                    }
                    _timeSinceLastMpRegen = 0;
                }
                else
                {
                    _timeSinceLastMpRegen += gameTime.ElapsedGameTime.Milliseconds;
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Finds the behavior of the specified type.
 /// </summary>
 /// <typeparam name="TBehavior"></typeparam>
 /// <returns></returns>
 public TBehavior GetBehavior <TBehavior>(Func <TBehavior, bool> predicate = null) where TBehavior : EntityUpdateBehavior
 {
     return(Behaviors.SelectMany(b => b.Value.OfType <TBehavior>()).FirstOrDefault(b => predicate == null || predicate(b)));
 }