EnemySpawnSystem.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #region File description
  2. // --------------------------------------------------------------------------------------------------------------------
  3. // <copyright file="EnemySpawnSystem.cs" company="GAMADU.COM">
  4. // Copyright � 2013 GAMADU.COM. All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without modification, are
  7. // permitted provided that the following conditions are met:
  8. //
  9. // 1. Redistributions of source code must retain the above copyright notice, this list of
  10. // conditions and the following disclaimer.
  11. //
  12. // 2. Redistributions in binary form must reproduce the above copyright notice, this list
  13. // of conditions and the following disclaimer in the documentation and/or other materials
  14. // provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY GAMADU.COM 'AS IS' AND ANY EXPRESS OR IMPLIED
  17. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GAMADU.COM OR
  19. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. //
  26. // The views and conclusions contained in the software and documentation are those of the
  27. // authors and should not be interpreted as representing official policies, either expressed
  28. // or implied, of GAMADU.COM.
  29. // </copyright>
  30. // <summary>
  31. // The enemy spawn system.
  32. // </summary>
  33. // --------------------------------------------------------------------------------------------------------------------
  34. #endregion File description
  35. namespace StarWarrior.Systems
  36. {
  37. #region Using statements
  38. using System;
  39. using System.Collections.Generic;
  40. using Artemis;
  41. using Artemis.Attributes;
  42. using Artemis.Manager;
  43. using Artemis.System;
  44. using Microsoft.Xna.Framework.Graphics;
  45. using StarWarrior.Components;
  46. using StarWarrior.Templates;
  47. #endregion
  48. /// <summary>The enemy spawn system.</summary>
  49. [ArtemisEntitySystem(GameLoopType = GameLoopType.Update, Layer = 1)]
  50. public class EnemySpawnSystem : IntervalEntitySystem
  51. {
  52. /// <summary>The random.</summary>
  53. private Random random;
  54. /// <summary>The sprite batch.</summary>
  55. private SpriteBatch spriteBatch;
  56. /// <summary>Initializes a new instance of the <see cref="EnemySpawnSystem" /> class.</summary>
  57. public EnemySpawnSystem()
  58. : base(
  59. new TimeSpan(0, 0, 0, 0, BlackBoard.GetEntry<int>("EnemyInterval")),
  60. Aspect.All(typeof(TransformComponent), typeof(VelocityComponent), typeof(EnemyComponent)))
  61. {
  62. }
  63. /// <summary>Override to implement code that gets executed when systems are initialized.</summary>
  64. public override void LoadContent()
  65. {
  66. this.spriteBatch = BlackBoard.GetEntry<SpriteBatch>("SpriteBatch");
  67. this.random = new Random();
  68. }
  69. /// <summary>Processes the entities.</summary>
  70. /// <param name="entities">The entities.</param>
  71. protected override void ProcessEntities(IDictionary<int, Entity> entities)
  72. {
  73. Entity entity = this.EntityWorld.CreateEntityFromTemplate(EnemyShipTemplate.Name);
  74. entity.GetComponent<TransformComponent>().X = this.random.Next(this.spriteBatch.GraphicsDevice.Viewport.Width);
  75. entity.GetComponent<TransformComponent>().Y = this.random.Next(400) + 50;
  76. entity.GetComponent<VelocityComponent>().Speed = 0.05f;
  77. entity.GetComponent<VelocityComponent>().Angle = this.random.Next() % 2 == 0 ? 0 : 180;
  78. entity.Refresh();
  79. }
  80. }
  81. }