CollisionSystem.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #region File description
  2. // --------------------------------------------------------------------------------------------------------------------
  3. // <copyright file="CollisionSystem.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 collision system.
  32. // </summary>
  33. // --------------------------------------------------------------------------------------------------------------------
  34. #endregion File description
  35. namespace StarWarrior.Systems
  36. {
  37. #region Using statements
  38. using System.Collections.Generic;
  39. using Artemis;
  40. using Artemis.Attributes;
  41. using Artemis.Manager;
  42. using Artemis.System;
  43. using Artemis.Utils;
  44. using Microsoft.Xna.Framework;
  45. using StarWarrior.Components;
  46. using StarWarrior.Templates;
  47. #endregion
  48. /// <summary>The collision system.</summary>
  49. [ArtemisEntitySystem(GameLoopType = GameLoopType.Update, Layer = 1)]
  50. internal class CollisionSystem : EntitySystem
  51. {
  52. /// <summary>Initializes a new instance of the <see cref="CollisionSystem" /> class.</summary>
  53. public CollisionSystem()
  54. : base(Aspect.All(typeof(TransformComponent)))
  55. {
  56. }
  57. /// <summary>Override to implement code that gets executed when systems are initialized.</summary>
  58. public override void LoadContent()
  59. {
  60. }
  61. /// <summary>Processes the entities.</summary>
  62. /// <param name="entities">The entities.</param>
  63. protected override void ProcessEntities(IDictionary<int, Entity> entities)
  64. {
  65. Bag<Entity> bullets = this.EntityWorld.GroupManager.GetEntities("BULLETS");
  66. Bag<Entity> ships = this.EntityWorld.GroupManager.GetEntities("SHIPS");
  67. if (bullets != null && ships != null)
  68. {
  69. // being brutal !!!
  70. for (int shipIndex = 0; ships.Count > shipIndex; ++shipIndex)
  71. {
  72. Entity ship = ships.Get(shipIndex);
  73. for (int bulletIndex = 0; bullets.Count > bulletIndex; ++bulletIndex)
  74. {
  75. Entity bullet = bullets.Get(bulletIndex);
  76. if (this.CollisionExists(bullet, ship))
  77. {
  78. var bulletTransform = bullet.GetComponent<TransformComponent>();
  79. Entity bulletExplosion = this.EntityWorld.CreateEntityFromTemplate(BulletExplosionTemplate.Name);
  80. bulletExplosion.GetComponent<TransformComponent>().Position = bulletTransform.Position;
  81. bulletExplosion.Refresh();
  82. bullet.Delete();
  83. var healthComponent = ship.GetComponent<HealthComponent>();
  84. healthComponent.AddDamage(4);
  85. if (!healthComponent.IsAlive)
  86. {
  87. var shipTransform = ship.GetComponent<TransformComponent>();
  88. Entity shipExplosion = this.EntityWorld.CreateEntityFromTemplate(ShipExplosionTemplate.Name);
  89. shipExplosion.GetComponent<TransformComponent>().Position = shipTransform.Position;
  90. shipExplosion.Refresh();
  91. ship.Delete();
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. /// <summary>The collision exists.</summary>
  100. /// <param name="entity1">The entity 1.</param>
  101. /// <param name="entity2">The entity 2.</param>
  102. /// <returns>The <see cref="bool" />.</returns>
  103. private bool CollisionExists(Entity entity1, Entity entity2)
  104. {
  105. return Vector2.Distance(entity1.GetComponent<TransformComponent>().Position, entity2.GetComponent<TransformComponent>().Position) < 20;
  106. }
  107. }
  108. }