Skip to main content
Rollback to Revision 4
Source Link
Pimgd
  • 22.6k
  • 5
  • 68
  • 144
public abstract class Entity
{
    private List<Component> _components = new List<Component>();
    private List<Entity> _children = new List<Entity>();

    public void AddChild(Entity child)
    {
        _children.Add(child);
    }

    public void RemoveChild(Entity child)
    {
        _children.Remove(child);
    }

    public Entity[] GetChildren()
    {
        return _children.ToArray();
    }

    public void AddComponent<T>()
        where T : Component
    {
        if (_components.Find((c) => c is T) != null) {
            return; // each entity can only contains one type of component at the same time
        }

        var component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;
        _components.Add(component);
    }

    public void RemoveComponent<T>()
        where T : Component
    {
        var component = _components.Find((c) => c is T);
        if (component == null) {
            return;
        }
        
        component.Owner = null;
        _components.Remove(component);
    }

    public Component[] GetComponents()
    {
        return _components.ToArray();
    }
}
public class Scene : IRenderable, IUpdatable, IInputable
{
    private List<Entity> _entities = new List<Entity>();
    
    public bool Enabled { get; set; }
    public bool Visible { get; set; }

    public void Add(Entity entity)
    {
        _entities.Add(entity);
    }

    public void Remove(Entity entity)
    {
        _entities.Remove(entity);
    }

    // Render, Update and Input function in the Scene will automatically called at the game loop by game window
    public void Render(RenderTarget target, RenderStates states)
    {
        if (Visible)
        {
            foreach (var entity in _entities)
            {
                if (entity is IRenderable) {
                    ((IRenderable)entity).Render(target, states);
                }
            }
        }
    }

    public void Update(double delta)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IUpdatable) {
                    ((IUpdatable)entity).Update(delta);
                }
            }
        }
    }

    public void Input(InputEventArgs e)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IInputable) {
                    ((IInputable)entity).Input(e);
                }
            }
        }
    }

}
  1. At the shown code above, the Scene is not responsible to check the Entity children and it's components, and it also possible to add another Entity as a child while implementing interface that not implemented by it's parent. Consider following example:

     public class CustomEntity : Entity
     {
         // Some codes here...
     }
    
     public class Sprite : Entity, IRenderable
     {
         public bool Visible { get; set; }
    
         // ...
    
         public void Render(RenderTarget target, RenderStates states)
         {
             // If the sprite is not visible, it should not render itself and ignore the children
             if (!Visible) {
                 return;
             }
    
             // Render the sprite here...
    
             // Since scene does not check the Entity children, 
             // We need to render the child and components in case they implement IRenderable
             foreach (var child in GetChildren())
             {
                 if (child is IRenderable) 
                 {
                     if (((IRenderable)child).Visible) {
                         ((IRenderable)child).Render(target, states);
                     }
                 }
             }
    
             foreach (var component in GetComponents())
             {
                 if (component is IRenderable) 
                 {
                     if (((IRenderable)component).Visible) {
                         ((IRenderable)component).Render(target, states);
                     }
                 }
             }
         }
     }
    
public abstract class Entity
{
    private List<Component> _components = new List<Component>();
    private List<Entity> _children = new List<Entity>();

    public void AddChild(Entity child)
    {
        _children.Add(child);
    }

    public void RemoveChild(Entity child)
    {
        _children.Remove(child);
    }

    public Entity[] GetChildren()
    {
        return _children.ToArray();
    }

    public void AddComponent<T>()
        where T : Component
    {
        if (_components.Find((c) => c is T) != null) {
            return; // each entity can only contains one type of component at the same time
        }

        var component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;
        _components.Add(component);
    }

    public void RemoveComponent<T>()
        where T : Component
    {
        var component = _components.Find((c) => c is T);
        if (component == null) {
            return;
        }
        
        component.Owner = null;
        _components.Remove(component);
    }

    public Component[] GetComponents()
    {
        return _components.ToArray();
    }
}
public class Scene : IRenderable, IUpdatable, IInputable
{
    private List<Entity> _entities = new List<Entity>();
    
    public bool Enabled { get; set; }
    public bool Visible { get; set; }

    public void Add(Entity entity)
    {
        _entities.Add(entity);
    }

    public void Remove(Entity entity)
    {
        _entities.Remove(entity);
    }

    // Render, Update and Input function in the Scene will automatically called at the game loop by game window
    public void Render(RenderTarget target, RenderStates states)
    {
        if (Visible)
        {
            foreach (var entity in _entities)
            {
                if (entity is IRenderable) {
                    ((IRenderable)entity).Render(target, states);
                }
            }
        }
    }

    public void Update(double delta)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IUpdatable) {
                    ((IUpdatable)entity).Update(delta);
                }
            }
        }
    }

    public void Input(InputEventArgs e)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IInputable) {
                    ((IInputable)entity).Input(e);
                }
            }
        }
    }

}
  1. At the shown code above, the Scene is not responsible to check the Entity children and it's components, and it also possible to add another Entity as a child while implementing interface that not implemented by it's parent. Consider following example:

     public class CustomEntity : Entity
     {
         // Some codes here...
     }
    
     public class Sprite : Entity, IRenderable
     {
         public bool Visible { get; set; }
    
         // ...
    
         public void Render(RenderTarget target, RenderStates states)
         {
             // If the sprite is not visible, it should not render itself and ignore the children
             if (!Visible) {
                 return;
             }
    
             // Render the sprite here...
    
             // Since scene does not check the Entity children, 
             // We need to render the child and components in case they implement IRenderable
             foreach (var child in GetChildren())
             {
                 if (child is IRenderable) 
                 {
                     if (((IRenderable)child).Visible) {
                         ((IRenderable)child).Render(target, states);
                     }
                 }
             }
    
             foreach (var component in GetComponents())
             {
                 if (component is IRenderable) 
                 {
                     if (((IRenderable)component).Visible) {
                         ((IRenderable)component).Render(target, states);
                     }
                 }
             }
         }
     }
    
public abstract class Entity
{
    private List<Component> _components = new List<Component>();
    private List<Entity> _children = new List<Entity>();

    public void AddChild(Entity child)
    {
        _children.Add(child);
    }

    public void RemoveChild(Entity child)
    {
        _children.Remove(child);
    }

    public Entity[] GetChildren()
    {
        return _children.ToArray();
    }

    public void AddComponent<T>()
        where T : Component
    {
        if (_components.Find((c) => c is T) != null)
            return; // each entity can only contains one type of component at the same time

        var component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;
        _components.Add(component);
    }

    public void RemoveComponent<T>()
        where T : Component
    {
        var component = _components.Find((c) => c is T);
        if (component == null)
            return;
        
        component.Owner = null;
        _components.Remove(component);
    }

    public Component[] GetComponents()
    {
        return _components.ToArray();
    }
}
public class Scene : IRenderable, IUpdatable, IInputable
{
    private List<Entity> _entities = new List<Entity>();
    
    public bool Enabled { get; set; }
    public bool Visible { get; set; }

    public void Add(Entity entity)
    {
        _entities.Add(entity);
    }

    public void Remove(Entity entity)
    {
        _entities.Remove(entity);
    }

    // Render, Update and Input function in the Scene will automatically called at the game loop by game window
    public void Render(RenderTarget target, RenderStates states)
    {
        if (Visible)
        {
            foreach (var entity in _entities)
            {
                if (entity is IRenderable)
                    ((IRenderable)entity).Render(target, states);
            }
        }
    }

    public void Update(double delta)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IUpdatable)
                    ((IUpdatable)entity).Update(delta);
            }
        }
    }

    public void Input(InputEventArgs e)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IInputable)
                    ((IInputable)entity).Input(e);
            }
        }
    }

}
  1. At the shown code above, the Scene is not responsible to check the Entity children and it's components, and it also possible to add another Entity as a child while implementing interface that not implemented by it's parent. Consider following example:

     public class CustomEntity : Entity
     {
         // Some codes here...
     }
    
     public class Sprite : Entity, IRenderable
     {
         public bool Visible { get; set; }
    
         // ...
    
         public void Render(RenderTarget target, RenderStates states)
         {
             // If the sprite is not visible, it should not render itself and ignore the children
             if (!Visible)
                 return;
    
             // Render the sprite here...
    
             // Since scene does not check the Entity children, 
             // We need to render the child and components in case they implement IRenderable
             foreach (var child in GetChildren())
             {
                 if (child is IRenderable) 
                 {
                     if (((IRenderable)child).Visible)
                         ((IRenderable)child).Render(target, states);
                 }
             }
    
             foreach (var component in GetComponents())
             {
                 if (component is IRenderable) 
                 {
                     if (((IRenderable)component).Visible)
                         ((IRenderable)component).Render(target, states);
                 }
             }
         }
     }
    
added 188 characters in body
Source Link
CXO2
  • 153
  • 5
public abstract class Entity
{
    private List<Component> _components = new List<Component>();
    private List<Entity> _children = new List<Entity>();

    public void AddChild(Entity child)
    {
        _children.Add(child);
    }

    public void RemoveChild(Entity child)
    {
        _children.Remove(child);
    }

    public Entity[] GetChildren()
    {
        return _children.ToArray();
    }

    public void AddComponent<T>()
        where T : Component
    {
        if (_components.Find((c) => c is T) != null) {
            return; // each entity can only contains one type of component at the same time
        }

        var component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;
        _components.Add(component);
    }

    public void RemoveComponent<T>()
        where T : Component
    {
        var component = _components.Find((c) => c is T);
        if (component == null) {
            return;
        }
        
        component.Owner = null;
        _components.Remove(component);
    }

    public Component[] GetComponents()
    {
        return _components.ToArray();
    }
}
public class Scene : IRenderable, IUpdatable, IInputable
{
    private List<Entity> _entities = new List<Entity>();
    
    public bool Enabled { get; set; }
    public bool Visible { get; set; }

    public void Add(Entity entity)
    {
        _entities.Add(entity);
    }

    public void Remove(Entity entity)
    {
        _entities.Remove(entity);
    }

    // Render, Update and Input function in the Scene will automatically called at the game loop by game window
    public void Render(RenderTarget target, RenderStates states)
    {
        if (Visible)
        {
            foreach (var entity in _entities)
            {
                if (entity is IRenderable) {
                    ((IRenderable)entity).Render(target, states);
                }
            }
        }
    }

    public void Update(double delta)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IUpdatable) {
                    ((IUpdatable)entity).Update(delta);
                }
            }
        }
    }

    public void Input(InputEventArgs e)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IInputable) {
                    ((IInputable)entity).Input(e);
                }
            }
        }
    }

}
  1. At the shown code above, the Scene is not responsible to check the Entity children and it's components, and it also possible to add another Entity as a child while implementing interface that not implemented by it's parent. Consider following example:

     public class CustomEntity : Entity
     {
         // Some codes here...
     }
    
     public class Sprite : Entity, IRenderable
     {
         public bool Visible { get; set; }
    
         // ...
    
         public void Render(RenderTarget target, RenderStates states)
         {
             // If the sprite is not visible, it should not render itself and ignore the children
             if (!Visible) {
                 return;
             }
    
             // Render the sprite here...
    
             // Since scene does not check the Entity children, 
             // We need to render the child and components in case they implement IRenderable
             foreach (var child in GetChildren())
             {
                 if (child is IRenderable) 
                 {
                     if (((IRenderable)child).Visible) {
                         ((IRenderable)child).Render(target, states);
                     }
                 }
             }
    
             foreach (var component in GetComponents())
             {
                 if (component is IRenderable) 
                 {
                     if (((IRenderable)component).Visible) {
                         ((IRenderable)component).Render(target, states);
                     }
                 }
             }
         }
     }
    
public abstract class Entity
{
    private List<Component> _components = new List<Component>();
    private List<Entity> _children = new List<Entity>();

    public void AddChild(Entity child)
    {
        _children.Add(child);
    }

    public void RemoveChild(Entity child)
    {
        _children.Remove(child);
    }

    public Entity[] GetChildren()
    {
        return _children.ToArray();
    }

    public void AddComponent<T>()
        where T : Component
    {
        if (_components.Find((c) => c is T) != null)
            return; // each entity can only contains one type of component at the same time

        var component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;
        _components.Add(component);
    }

    public void RemoveComponent<T>()
        where T : Component
    {
        var component = _components.Find((c) => c is T);
        if (component == null)
            return;
        
        component.Owner = null;
        _components.Remove(component);
    }

    public Component[] GetComponents()
    {
        return _components.ToArray();
    }
}
public class Scene : IRenderable, IUpdatable, IInputable
{
    private List<Entity> _entities = new List<Entity>();
    
    public bool Enabled { get; set; }
    public bool Visible { get; set; }

    public void Add(Entity entity)
    {
        _entities.Add(entity);
    }

    public void Remove(Entity entity)
    {
        _entities.Remove(entity);
    }

    // Render, Update and Input function in the Scene will automatically called at the game loop by game window
    public void Render(RenderTarget target, RenderStates states)
    {
        if (Visible)
        {
            foreach (var entity in _entities)
            {
                if (entity is IRenderable)
                    ((IRenderable)entity).Render(target, states);
            }
        }
    }

    public void Update(double delta)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IUpdatable)
                    ((IUpdatable)entity).Update(delta);
            }
        }
    }

    public void Input(InputEventArgs e)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IInputable)
                    ((IInputable)entity).Input(e);
            }
        }
    }

}
  1. At the shown code above, the Scene is not responsible to check the Entity children and it's components, and it also possible to add another Entity as a child while implementing interface that not implemented by it's parent. Consider following example:

     public class CustomEntity : Entity
     {
         // Some codes here...
     }
    
     public class Sprite : Entity, IRenderable
     {
         public bool Visible { get; set; }
    
         // ...
    
         public void Render(RenderTarget target, RenderStates states)
         {
             // If the sprite is not visible, it should not render itself and ignore the children
             if (!Visible)
                 return;
    
             // Render the sprite here...
    
             // Since scene does not check the Entity children, 
             // We need to render the child and components in case they implement IRenderable
             foreach (var child in GetChildren())
             {
                 if (child is IRenderable) 
                 {
                     if (((IRenderable)child).Visible)
                         ((IRenderable)child).Render(target, states);
                 }
             }
    
             foreach (var component in GetComponents())
             {
                 if (component is IRenderable) 
                 {
                     if (((IRenderable)component).Visible)
                         ((IRenderable)component).Render(target, states);
                 }
             }
         }
     }
    
public abstract class Entity
{
    private List<Component> _components = new List<Component>();
    private List<Entity> _children = new List<Entity>();

    public void AddChild(Entity child)
    {
        _children.Add(child);
    }

    public void RemoveChild(Entity child)
    {
        _children.Remove(child);
    }

    public Entity[] GetChildren()
    {
        return _children.ToArray();
    }

    public void AddComponent<T>()
        where T : Component
    {
        if (_components.Find((c) => c is T) != null) {
            return; // each entity can only contains one type of component at the same time
        }

        var component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;
        _components.Add(component);
    }

    public void RemoveComponent<T>()
        where T : Component
    {
        var component = _components.Find((c) => c is T);
        if (component == null) {
            return;
        }
        
        component.Owner = null;
        _components.Remove(component);
    }

    public Component[] GetComponents()
    {
        return _components.ToArray();
    }
}
public class Scene : IRenderable, IUpdatable, IInputable
{
    private List<Entity> _entities = new List<Entity>();
    
    public bool Enabled { get; set; }
    public bool Visible { get; set; }

    public void Add(Entity entity)
    {
        _entities.Add(entity);
    }

    public void Remove(Entity entity)
    {
        _entities.Remove(entity);
    }

    // Render, Update and Input function in the Scene will automatically called at the game loop by game window
    public void Render(RenderTarget target, RenderStates states)
    {
        if (Visible)
        {
            foreach (var entity in _entities)
            {
                if (entity is IRenderable) {
                    ((IRenderable)entity).Render(target, states);
                }
            }
        }
    }

    public void Update(double delta)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IUpdatable) {
                    ((IUpdatable)entity).Update(delta);
                }
            }
        }
    }

    public void Input(InputEventArgs e)
    {
        if (Enabled)
        {
            foreach (var entity in _entities)
            {
                if (entity is IInputable) {
                    ((IInputable)entity).Input(e);
                }
            }
        }
    }

}
  1. At the shown code above, the Scene is not responsible to check the Entity children and it's components, and it also possible to add another Entity as a child while implementing interface that not implemented by it's parent. Consider following example:

     public class CustomEntity : Entity
     {
         // Some codes here...
     }
    
     public class Sprite : Entity, IRenderable
     {
         public bool Visible { get; set; }
    
         // ...
    
         public void Render(RenderTarget target, RenderStates states)
         {
             // If the sprite is not visible, it should not render itself and ignore the children
             if (!Visible) {
                 return;
             }
    
             // Render the sprite here...
    
             // Since scene does not check the Entity children, 
             // We need to render the child and components in case they implement IRenderable
             foreach (var child in GetChildren())
             {
                 if (child is IRenderable) 
                 {
                     if (((IRenderable)child).Visible) {
                         ((IRenderable)child).Render(target, states);
                     }
                 }
             }
    
             foreach (var component in GetComponents())
             {
                 if (component is IRenderable) 
                 {
                     if (((IRenderable)component).Visible) {
                         ((IRenderable)component).Render(target, states);
                     }
                 }
             }
         }
     }
    
deleted 22 characters in body
Source Link
BCdotWEB
  • 11.4k
  • 2
  • 28
  • 45

That's all for now, in case you find something weird with the design, I'm open for suggestion. I'll update the question if iI've got something more to ask.

Thanks in advance

That's all for now, in case you find something weird with the design, I'm open for suggestion. I'll update the question if i got something more to ask.

Thanks in advance

That's all for now, in case you find something weird with the design, I'm open for suggestion. I'll update the question if I've got something more to ask.

added 90 characters in body
Source Link
CXO2
  • 153
  • 5
Loading
added 15 characters in body
Source Link
CXO2
  • 153
  • 5
Loading
Source Link
CXO2
  • 153
  • 5
Loading