RenderSystem.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #region File description
  2. // --------------------------------------------------------------------------------------------------------------------
  3. // <copyright file="RenderSystem.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 render system.
  32. // </summary>
  33. // --------------------------------------------------------------------------------------------------------------------
  34. #endregion File description
  35. namespace StarWarrior.Systems
  36. {
  37. #region Using statements
  38. using System;
  39. using Artemis;
  40. using Artemis.Attributes;
  41. using Artemis.Manager;
  42. using Artemis.System;
  43. using Microsoft.Xna.Framework;
  44. using Microsoft.Xna.Framework.Content;
  45. using Microsoft.Xna.Framework.Graphics;
  46. using StarWarrior.Components;
  47. using StarWarrior.Spatials;
  48. #endregion
  49. /// <summary>The render system.</summary>
  50. [ArtemisEntitySystem(GameLoopType = GameLoopType.Draw, Layer = 0)]
  51. public class RenderSystem : EntityProcessingSystem
  52. {
  53. /// <summary>The content manager.</summary>
  54. private ContentManager contentManager;
  55. /// <summary>The spatial name.</summary>
  56. private string spatialName;
  57. /// <summary>The sprite batch.</summary>
  58. private SpriteBatch spriteBatch;
  59. /// <summary>Initializes a new instance of the <see cref="RenderSystem" /> class.</summary>
  60. public RenderSystem()
  61. : base(Aspect.All(typeof(TransformComponent), typeof(SpatialFormComponent)))
  62. {
  63. }
  64. /// <summary>Override to implement code that gets executed when systems are initialized.</summary>
  65. public override void LoadContent()
  66. {
  67. this.spriteBatch = BlackBoard.GetEntry<SpriteBatch>("SpriteBatch");
  68. this.contentManager = BlackBoard.GetEntry<ContentManager>("ContentManager");
  69. }
  70. /// <summary>Processes the specified entity.</summary>
  71. /// <param name="entity">The entity.</param>
  72. public override void Process(Entity entity)
  73. {
  74. var transformComponent = entity.GetComponent<TransformComponent>();
  75. var spatialFormComponent = entity.GetComponent<SpatialFormComponent>();
  76. if (spatialFormComponent != null)
  77. {
  78. this.spatialName = spatialFormComponent.SpatialFormFile;
  79. if (transformComponent.X >= 0 &&
  80. transformComponent.Y >= 0 &&
  81. transformComponent.X < this.spriteBatch.GraphicsDevice.Viewport.Width &&
  82. transformComponent.Y < this.spriteBatch.GraphicsDevice.Viewport.Height)
  83. {
  84. // very naive render ...
  85. if (string.Compare("PlayerShip", this.spatialName, StringComparison.InvariantCultureIgnoreCase) == 0)
  86. {
  87. PlayerShip.Render(this.spriteBatch, this.contentManager, transformComponent);
  88. }
  89. else if (string.Compare("Missile", this.spatialName, StringComparison.InvariantCultureIgnoreCase) == 0)
  90. {
  91. Missile.Render(this.spriteBatch, this.contentManager, transformComponent);
  92. }
  93. else if (string.Compare("EnemyShip", this.spatialName, StringComparison.InvariantCultureIgnoreCase) == 0)
  94. {
  95. EnemyShip.Render(this.spriteBatch, this.contentManager, transformComponent);
  96. }
  97. else if (string.Compare("BulletExplosion", this.spatialName, StringComparison.InvariantCultureIgnoreCase) == 0)
  98. {
  99. Explosion.Render(this.spriteBatch, this.contentManager, transformComponent, Color.Red, 10);
  100. }
  101. else if (string.Compare("ShipExplosion", this.spatialName, StringComparison.InvariantCultureIgnoreCase) == 0)
  102. {
  103. ShipExplosion.Render(this.spriteBatch, this.contentManager, transformComponent, Color.Yellow, 30);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }