using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Collections.Generic; public class GameObjectList : GameObject { protected List gameObjects; public GameObjectList(int layer = 0, string id = "") : base(layer, id) { gameObjects = new List(); } public void Add(GameObject obj) { obj.Parent = this; for (int i = 0; i < gameObjects.Count; i++) { if (gameObjects[i].Layer > obj.Layer) { gameObjects.Insert(i, obj); return; } } gameObjects.Add(obj); } public void Remove(GameObject obj) { gameObjects.Remove(obj); obj.Parent = null; } public GameObject Find(string id) { foreach (GameObject obj in gameObjects) { if (obj.ID == id) return obj; if (obj is GameObjectList) { GameObjectList objlist = obj as GameObjectList; GameObject subobj = objlist.Find(id); if (subobj != null) return subobj; } } return null; } public List Objects { get { return gameObjects; } } public override void HandleInput(InputHelper inputHelper, GameWorld gameWorld) { foreach (GameObject obj in gameObjects) obj.HandleInput(inputHelper, gameWorld); } public override void Update(GameTime gameTime, GameWorld gameWorld) { foreach (GameObject obj in gameObjects) obj.Update(gameTime, gameWorld); } public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { List.Enumerator e = gameObjects.GetEnumerator(); while (e.MoveNext()) e.Current.Draw(gameTime, spriteBatch); } }