//----------------------------------------------------------------------------- // TrackCombiModels.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using Microsoft.Xna.Framework; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using RacingGame.Helpers; using RacingGame.Landscapes; #if NETFX_CORE using Serializable = System.Runtime.Serialization.DataContractAttribute; #endif namespace RacingGame.Tracks { /// /// Little helper class to load combinations of objects, which simplifies /// our level creation process. Also used for randomly generated objects /// near the track. /// public class TrackCombiModels { /// /// Directory for loading combi models. /// public const string Directory = "Content"; /// /// Extension we use for combi models. /// public const string Extension = "CombiModel"; /// /// CombiObject for every object in this combi model /// [Serializable] public class CombiObject { /// /// Model name /// public string modelName; /// /// Matrix /// public Matrix matrix; /// /// Create combi object /// public CombiObject() { } /// /// Create CombiObject /// /// Set model name /// Set matrix public CombiObject(string setModelName, Matrix setMatrix) { modelName = setModelName; matrix = setMatrix; } } /// /// List of combi objects used in this file. /// private List objects = new List(); /// /// Name of this combi model (extracted from filename). /// private string name = ""; /// /// Size of this combi model. /// private float size = 10; /// /// Name /// /// String public string Name { get { return name; } } /// /// Size /// public float Size { get { // Return size 10 for palms, stones and ruins, rest gets size = 50 return size; } } /// /// Load track combi models /// /// Filename public TrackCombiModels(string filename) { using (StreamReader file = new StreamReader(TitleContainer.OpenStream( Path.Combine(Directory, filename + "." + Extension)))) { // Load everything into this class with help of the XmlSerializer. objects = (List) new XmlSerializer(typeof(List)). Deserialize(file.BaseStream); name = Path.GetFileNameWithoutExtension(filename); // Return size 10 for palms, stones and ruins, rest gets size = 50 size = (Name == "CombiPalms" || Name == "CombiPalms2" || Name == "CombiRuins" || Name == "CombiRuins2" || Name == "CombiStones" || Name == "CombiStones2") ? 10 : 50; } } /// /// Add all models /// /// Landscape /// Parent matrix public void AddAllModels(Landscape landscape, Matrix parentMatrix) { // Just add all models in our combi foreach (CombiObject obj in objects) landscape.AddObjectToRender(obj.modelName, obj.matrix * parentMatrix, false); } } }