TrackCombiModels.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //-----------------------------------------------------------------------------
  2. // TrackCombiModels.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Text;
  12. using System.Xml;
  13. using System.Xml.Serialization;
  14. using RacingGame.Helpers;
  15. using RacingGame.Landscapes;
  16. #if NETFX_CORE
  17. using Serializable = System.Runtime.Serialization.DataContractAttribute;
  18. #endif
  19. namespace RacingGame.Tracks
  20. {
  21. /// <summary>
  22. /// Little helper class to load combinations of objects, which simplifies
  23. /// our level creation process. Also used for randomly generated objects
  24. /// near the track.
  25. /// </summary>
  26. public class TrackCombiModels
  27. {
  28. /// <summary>
  29. /// Directory for loading combi models.
  30. /// </summary>
  31. public const string Directory = "Content";
  32. /// <summary>
  33. /// Extension we use for combi models.
  34. /// </summary>
  35. public const string Extension = "CombiModel";
  36. /// <summary>
  37. /// CombiObject for every object in this combi model
  38. /// </summary>
  39. [Serializable]
  40. public class CombiObject
  41. {
  42. /// <summary>
  43. /// Model name
  44. /// </summary>
  45. public string modelName;
  46. /// <summary>
  47. /// Matrix
  48. /// </summary>
  49. public Matrix matrix;
  50. /// <summary>
  51. /// Create combi object
  52. /// </summary>
  53. public CombiObject()
  54. {
  55. }
  56. /// <summary>
  57. /// Create CombiObject
  58. /// </summary>
  59. /// <param name="setModelName">Set model name</param>
  60. /// <param name="setMatrix">Set matrix</param>
  61. public CombiObject(string setModelName, Matrix setMatrix)
  62. {
  63. modelName = setModelName;
  64. matrix = setMatrix;
  65. }
  66. }
  67. /// <summary>
  68. /// List of combi objects used in this file.
  69. /// </summary>
  70. private List<CombiObject> objects = new List<CombiObject>();
  71. /// <summary>
  72. /// Name of this combi model (extracted from filename).
  73. /// </summary>
  74. private string name = "";
  75. /// <summary>
  76. /// Size of this combi model.
  77. /// </summary>
  78. private float size = 10;
  79. /// <summary>
  80. /// Name
  81. /// </summary>
  82. /// <returns>String</returns>
  83. public string Name
  84. {
  85. get
  86. {
  87. return name;
  88. }
  89. }
  90. /// <summary>
  91. /// Size
  92. /// </summary>
  93. public float Size
  94. {
  95. get
  96. {
  97. // Return size 10 for palms, stones and ruins, rest gets size = 50
  98. return size;
  99. }
  100. }
  101. /// <summary>
  102. /// Load track combi models
  103. /// </summary>
  104. /// <param name="filename">Filename</param>
  105. public TrackCombiModels(string filename)
  106. {
  107. using (StreamReader file = new StreamReader(TitleContainer.OpenStream(
  108. Path.Combine(Directory, filename + "." + Extension))))
  109. {
  110. // Load everything into this class with help of the XmlSerializer.
  111. objects = (List<TrackCombiModels.CombiObject>)
  112. new XmlSerializer(typeof(List<TrackCombiModels.CombiObject>)).
  113. Deserialize(file.BaseStream);
  114. name = Path.GetFileNameWithoutExtension(filename);
  115. // Return size 10 for palms, stones and ruins, rest gets size = 50
  116. size = (Name == "CombiPalms" ||
  117. Name == "CombiPalms2" ||
  118. Name == "CombiRuins" ||
  119. Name == "CombiRuins2" ||
  120. Name == "CombiStones" ||
  121. Name == "CombiStones2") ? 10 : 50;
  122. }
  123. }
  124. /// <summary>
  125. /// Add all models
  126. /// </summary>
  127. /// <param name="landscape">Landscape</param>
  128. /// <param name="parentMatrix">Parent matrix</param>
  129. public void AddAllModels(Landscape landscape, Matrix parentMatrix)
  130. {
  131. // Just add all models in our combi
  132. foreach (CombiObject obj in objects)
  133. landscape.AddObjectToRender(obj.modelName,
  134. obj.matrix * parentMatrix, false);
  135. }
  136. }
  137. }