mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-24 12:06:37 +00:00
44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using SpaceGame.Entities;
|
|
|
|
namespace SpaceGame
|
|
{
|
|
public interface IEntityManager
|
|
{
|
|
T AddEntity<T>(T entity) where T : Entity;
|
|
}
|
|
|
|
public class EntityManager : IEntityManager
|
|
{
|
|
public EntityManager()
|
|
{
|
|
_entities = new List<Entity>();
|
|
}
|
|
|
|
private readonly List<Entity> _entities;
|
|
public IEnumerable<Entity> Entities => _entities;
|
|
|
|
public T AddEntity<T>(T entity) where T : Entity
|
|
{
|
|
_entities.Add(entity);
|
|
return entity;
|
|
}
|
|
|
|
public void Update(GameTime gameTime)
|
|
{
|
|
foreach (var entity in _entities.Where(e => !e.IsDestroyed))
|
|
entity.Update(gameTime);
|
|
|
|
_entities.RemoveAll(e => e.IsDestroyed);
|
|
}
|
|
|
|
public void Draw(SpriteBatch spriteBatch)
|
|
{
|
|
foreach (var entity in _entities.Where(e => !e.IsDestroyed))
|
|
entity.Draw(spriteBatch);
|
|
}
|
|
}
|
|
} |