Beispiel #1
0
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (DynamicEntities.Count > 1)
            {
                DynamicEntities.Sort(new PositionYComparer());
            }

            int j = 0;
            var dynamicYposition = int.MaxValue;

            if (DynamicEntities.Count > 0)
            {
                dynamicYposition = DynamicEntities[j].GetPosition().Y + DynamicEntities[j].GetPosition().Height;
            }

            while (dynamicYposition < 0)
            {
                DynamicEntities[j].Draw(spriteBatch);
                j++;
                if (j < DynamicEntities.Count)
                {
                    dynamicYposition = DynamicEntities[j].GetPosition().Y + DynamicEntities[j].GetPosition().Height;
                }
                else
                {
                    dynamicYposition = int.MaxValue;
                }
            }

            for (var i = 0; i < StaticEntities.Length; i++)
            {
                if (StaticEntities[i] != null)
                {
                    StaticEntities[i].ForEach(entity => entity.Draw(spriteBatch));
                }

                while (i == dynamicYposition)
                {
                    DynamicEntities[j].Draw(spriteBatch);
                    j++;

                    if (j < DynamicEntities.Count)
                    {
                        dynamicYposition = DynamicEntities[j].GetPosition().Y + DynamicEntities[j].GetPosition().Height;
                    }
                    else
                    {
                        dynamicYposition = int.MaxValue;
                    }
                }
            }

            while (j < DynamicEntities.Count)
            {
                DynamicEntities[j].Draw(spriteBatch);
                j++;
            }
        }
Beispiel #2
0
        public virtual void Update(GameTime gameTime)
        {
            foreach (var item in StaticEntities)
            {
                item?.ForEach(entity => entity.Update(gameTime));
            }
            DynamicEntities.ForEach(entity => entity.Update(gameTime));

            this.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalSeconds, (1f / 30f)));
        }
Beispiel #3
0
        public void RemoveDynamicEntity(Entity dynamicEntity)
        {
            if (DynamicEntities.Contains(dynamicEntity))
            {
                DynamicEntities.Remove(dynamicEntity);

                RemoveBody(dynamicEntity.Body);

                if (dynamicEntity is IInteractable && InteractablesDictionary.ContainsKey(dynamicEntity.BodyId))
                {
                    InteractablesDictionary.Remove(dynamicEntity.BodyId);
                }
            }
        }
Beispiel #4
0
        public IEnumerable <Entity> GetEntitiesAtPosition(int x, int y)
        {
            if (!IsInBounds(x, y))
            {
                return(null);
            }

            var position = new IntVector2(x, y);
            var entities = new List <Entity>();

            if (StaticEntities.Contains(position))
            {
                entities.Add(StaticEntities[position]);
            }
            entities.AddRange(DynamicEntities.Where(e => e.Position == position));
            entities.AddRange(PlayerEntities.Where(e => e.Position == position));
            return(entities);
        }
Beispiel #5
0
 /// <summary>
 /// Saves the state of this object and all <see cref="IServerSaveable"/> objects under it to the database.
 /// </summary>
 public void ServerSave()
 {
     // If possible, check only DynamicEntities. See the notes on _checkDynamicEntitiesForIServerSaveableOnly
     // for more details.
     if (_checkDynamicEntitiesForIServerSaveableOnly)
     {
         // Check only the DynamicEntities (fast)
         foreach (var saveable in DynamicEntities.OfType <IServerSaveable>())
         {
             saveable.ServerSave();
         }
     }
     else
     {
         // Check all Entities (slow)
         foreach (var saveable in Entities.OfType <IServerSaveable>())
         {
             saveable.ServerSave();
         }
     }
 }
Beispiel #6
0
        public void AddDynamicEntity(Entity dynamicEntity)
        {
            if (DynamicEntities == null)
            {
                DynamicEntities = new List <Entity>()
                {
                    dynamicEntity
                }
            }
            ;
            else
            {
                DynamicEntities.Add(dynamicEntity);
            }

            if (dynamicEntity is IInteractable)
            {
                InteractablesDictionary.Add(dynamicEntity.BodyId, dynamicEntity);
            }
            if (dynamicEntity is IAttackable)
            {
                AttackableDictionary.Add(dynamicEntity.BodyId, dynamicEntity);
            }
        }