EntityMap.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using OpenTK.Graphics.OpenGL;
  8. namespace RaahnSimulation
  9. {
  10. [XmlRoot("Map")]
  11. public class MapConfig
  12. {
  13. [XmlElement("Robot")]
  14. public CarConfig robotConfig;
  15. [XmlElement("Entity")]
  16. public EntityConfig[] entites;
  17. }
  18. public class EntityMap
  19. {
  20. public const int UNIQUE_ROAD_COUNT = 2;
  21. //Textures
  22. private const int STRAIGHT = 0;
  23. private const int TURN = 1;
  24. private const int ENTITY_DEFAULT_TEXTURE_VARIATION = 0;
  25. private const double ENTITY_DEFAULT_X = 0.0;
  26. private const double ENTITY_DEFAULT_Y = 0.0;
  27. private const double ENTITY_DEFAULT_ANGLE = 0.0;
  28. private uint layer;
  29. private bool loaded;
  30. private double defaultCarX;
  31. private double defaultCarY;
  32. private double defaultCarAngle;
  33. private List<Entity> entities;
  34. private Simulator context;
  35. private State currentState;
  36. private QuadTree quadTree;
  37. private Car raahnCar;
  38. public EntityMap(Simulator sim, uint layerIndex, Car car, QuadTree tree)
  39. {
  40. Construct(sim, layerIndex, car, tree);
  41. }
  42. public EntityMap(Simulator sim, uint layerIndex, Car car, QuadTree tree, string fileName)
  43. {
  44. Construct(sim, layerIndex, car, tree);
  45. Load(fileName);
  46. }
  47. ~EntityMap()
  48. {
  49. while (entities.Count > 0)
  50. {
  51. entities.RemoveAt(entities.Count - 1);
  52. }
  53. }
  54. public void Update()
  55. {
  56. }
  57. public void UpdateEvent(Event e)
  58. {
  59. }
  60. public void SetQuadTree(QuadTree tree)
  61. {
  62. quadTree = tree;
  63. }
  64. public bool Load(string fileName)
  65. {
  66. if (loaded)
  67. {
  68. Console.WriteLine(Utils.MAP_ALREADY_LOADED);
  69. return false;
  70. }
  71. if (!File.Exists(fileName))
  72. {
  73. Console.WriteLine(string.Format(Utils.FILE_NOT_FOUND, fileName));
  74. return false;
  75. }
  76. TextReader configReader = new StreamReader(fileName);
  77. MapConfig mapConfig = null;
  78. try
  79. {
  80. XmlSerializer deserializer = new XmlSerializer(typeof(MapConfig));
  81. mapConfig = (MapConfig)deserializer.Deserialize(configReader);
  82. }
  83. catch (Exception e)
  84. {
  85. Console.WriteLine(Utils.XML_READ_ERROR);
  86. Console.WriteLine(Utils.MAP_LOAD_ERROR);
  87. Console.WriteLine(e.Message);
  88. return false;
  89. }
  90. finally
  91. {
  92. configReader.Close();
  93. }
  94. if (mapConfig.robotConfig != null)
  95. {
  96. defaultCarX = mapConfig.robotConfig.x;
  97. defaultCarY = mapConfig.robotConfig.y;
  98. defaultCarAngle = mapConfig.robotConfig.angle;
  99. raahnCar.SetPosition(defaultCarX, defaultCarY);
  100. raahnCar.angle = defaultCarAngle;
  101. }
  102. if (mapConfig.entites != null)
  103. {
  104. for (int i = 0; i < mapConfig.entites.Length; i++)
  105. {
  106. Entity newEntity = null;
  107. Entity.EntityType type = Entity.EntityType.NONE;
  108. if (!string.IsNullOrEmpty(mapConfig.entites[i].type))
  109. type = Entity.GetTypeFromString(mapConfig.entites[i].type);
  110. switch (type)
  111. {
  112. case Entity.EntityType.WALL:
  113. {
  114. newEntity = new Wall(context);
  115. Wall newWall = (Wall)newEntity;
  116. newWall.SetRelativeEndPoint(mapConfig.entites[i].relX, mapConfig.entites[i].relY);
  117. break;
  118. }
  119. case Entity.EntityType.POINT:
  120. {
  121. newEntity = new Point(context);
  122. break;
  123. }
  124. }
  125. if (newEntity == null)
  126. continue;
  127. newEntity.SetPosition(mapConfig.entites[i].x, mapConfig.entites[i].y);
  128. newEntity.angle = mapConfig.entites[i].angle;
  129. entities.Add(newEntity);
  130. currentState.AddEntity(newEntity, layer);
  131. if (quadTree != null)
  132. quadTree.AddEntity(newEntity);
  133. }
  134. }
  135. loaded = true;
  136. return true;
  137. }
  138. public double GetDefaultCarX()
  139. {
  140. return defaultCarX;
  141. }
  142. public double GetDefaultAngle()
  143. {
  144. return defaultCarAngle;
  145. }
  146. public double GetDefaultCarY()
  147. {
  148. return defaultCarY;
  149. }
  150. public List<Point> GetPOIs()
  151. {
  152. List<Point> pois = new List<Point>();
  153. for (int i = 0; i < entities.Count; i++)
  154. {
  155. if (entities[i].GetEntityType() == Entity.EntityType.POINT)
  156. pois.Add((Point)entities[i]);
  157. }
  158. return pois;
  159. }
  160. private void Construct(Simulator sim, uint layerIndex, Car car, QuadTree tree)
  161. {
  162. loaded = false;
  163. defaultCarX = 0.0;
  164. defaultCarY = 0.0;
  165. defaultCarAngle = 0.0;
  166. entities = new List<Entity>();
  167. context = sim;
  168. currentState = context.GetState();
  169. layer = layerIndex;
  170. raahnCar = car;
  171. quadTree = tree;
  172. }
  173. }
  174. }