Environment.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. namespace fun.Core
  4. {
  5. /// <summary>
  6. /// Describes the environment of the game and manages communication between entities.
  7. /// </summary>
  8. public sealed class Environment
  9. {
  10. private readonly List<Entity> entities;
  11. /// <summary>
  12. /// entities available for communication.
  13. /// </summary>
  14. public Entity[] Entities { get { return entities.ToArray(); } }
  15. /// <summary>
  16. /// Creates an environment-Object.
  17. /// </summary>
  18. public Environment()
  19. {
  20. entities = new List<Entity>();
  21. }
  22. /// <summary>
  23. /// Adds an entity to the environment.
  24. /// </summary>
  25. /// <param name="entity">our needed entity-Object</param>
  26. public void AddEntity(Entity entity)
  27. {
  28. // Make sure the same entity doesn't get added again
  29. foreach (var singleEntity in entities)
  30. if (singleEntity.Name == entity.Name)
  31. // Throw expection if, for whatever reason, the same entity would be going to be added again
  32. throw new ArgumentException("Entity " + entity.Name + " already exists.");
  33. // Add entity to entity-List
  34. entities.Add(entity);
  35. }
  36. /// <summary>
  37. /// Returns an entity of the given type.
  38. /// </summary>
  39. /// <param name="name">name of the wanted entity</param>
  40. /// <returns>returns the wanted entity, if successful</returns>
  41. public Entity GetEntity(string name)
  42. {
  43. // Search
  44. foreach (var entity in entities)
  45. if (entity.Name == name)
  46. return entity;
  47. // 404 not found
  48. throw new ArgumentException("Entity not found");
  49. }
  50. public void Initialize()
  51. {
  52. foreach (var entity in entities)
  53. entity.Initialize();
  54. }
  55. public void Update(double time)
  56. {
  57. foreach (var entity in entities)
  58. entity.Update(time);
  59. }
  60. }
  61. }