private static ShipTemplate workShipTemplate; //The current ShipTemplate we're working with. //Called to build all the game's crucial dictionaries from XML. public static void BuildDicts() { //Calls internal methods within the class for brevity. BuildStellarObjectTemplateDict(); BuildShipTemplateDict(); //Temporary UI texture set up. Will probably set up a UI material dictionary. workMat = new Material(Shader.Find("Sprites/Default")); workMat.mainTexture = Resources.Load("Textures/UI/Selection") as Texture2D; workMat.mainTexture.filterMode = FilterMode.Bilinear; MatDict.SetMaterial("Selection", workMat); }
//Called upon starting by the Unity engine. void Start() { Settings.LoadSettings(); //Loads the settings of the game. QualitySettings.vSyncCount = 60; //Sets the frame rate cap used by Unity. StellarObjectTemplateDict.Initialize(); //Starts up all the crucial dictionaries we need. ShipTemplateDict.Initialize(); MatDict.Initialize(); StellarPathfinderList.Initialize(); //Starts up the list of StellarPathfinders. TemplateManager.BuildDicts(); //Orders the TemplateManager to build all the template dictionaries. DisplayManager.CreateDrawMesh(); //Order the DisplayManager to draw the mesh that is used for rendering. Galaxy.RandomGenerate(1, 1); //Orders the Galaxy to randomly generate a 1 by 1 galaxy. //For testing purposes, add a simple ship object to the center of the only star system in the galaxy. Galaxy.galaxyArray[0, 0].systemObjects[15, 15].AddShip(new Ship("BasicShip", Galaxy.galaxyArray[0, 0], new IntVec2(15, 15))); }
//Called every frame to tell the GPU what meshes to render with what textures and where, essentially. public static void DrawPlayScreen() { //Currently, we're setting the star system to render to the only star system available in the galaxy right now, //later, the player will be able to move from system to system. renderStarSystem = Galaxy.galaxyArray[0, 0]; //Once the game knows what system to render, we loop through every possible position in the array. //While this might not be the fastest possible way, it's fast enough for now at least. for (int x = 0; x < Settings.systemRadius * 2 + 1; x++) { for (int y = 0; y < Settings.systemRadius * 2 + 1; y++) { //Is the position in the array null? If it is, it's meant to be that way, so do nothing. if (renderStarSystem.systemObjects[x, y] != null) { //Grab a temporary reference to the StellarObject contained at x, y position in the system. workStellarObject = renderStarSystem.systemObjects[x, y]; //If this StellarObject is the same as the one currently selected by the player... if (workStellarObject == selectedStellarObject) { //...draw the selection icon at the foremost z layer at its position. InternalDrawMesh(x - Settings.systemRadius + drawXOffset, y - Settings.systemRadius + drawYOffset, 0, MatDict.GetMaterial("Selection")); } //Check if ships are present. if (workStellarObject.ShipsPresent()) { //If they are, grab a reference to the top-most ship for display purposes, and order it to be rendered. workShip = workStellarObject.GetDisplayedShip(); InternalDrawMesh(x - Settings.systemRadius + drawXOffset, y - Settings.systemRadius + drawYOffset, 1, workShip.rotation, MatDict.GetMaterial(workShip.rootTemplate.graphicName)); } //Finally, draw whatever texture the StellarObject has, with an EmptySpace background behind it. Eventually, I'll use a single background image //spread across the whole screen instead of an ugly repeating texture. InternalDrawMesh(x - Settings.systemRadius + drawXOffset, y - Settings.systemRadius + drawYOffset, 2, MatDict.GetMaterial(renderStarSystem.systemObjects[x, y].rootTemplate.graphicName)); InternalDrawMesh(x - Settings.systemRadius + drawXOffset, y - Settings.systemRadius + drawYOffset, 3, MatDict.GetMaterial("StellarObject_EmptySpace")); } } } }
//Builds the StellarObjectTemplate dictionary from the XML database. public static void BuildStellarObjectTemplateDict() { //First check if the dictionary is already built. if (!stellarObjectTemplateDictBuilt) { //Try to open StellarObjectDefs.xml try { reader = new XmlTextReader("Assets/Resources/Defs/StellarObjectDefs.xml"); } catch { //Catch the file not being found. Debug.Log("Could not find StellarObjectDefs.xml"); return; } //Continue reading the file until the end. while (reader.Read()) { //Check if the read data is a start element. if (reader.IsStartElement()) { //If it is, determine what variable it's referring to, and assign it. switch (reader.Name.ToString()) { //Start building a new StellarObjectTemplate. case "StellarObjectTemplate": workStellarObjectTemplate = new StellarObjectTemplate(); break; //Assign the graphicName. case "graphicName": workStellarObjectTemplate.SetGraphicName(reader.ReadString()); break; //Build the template's material. case "graphicPath": workMat = new Material(Shader.Find("Sprites/Default")); workMat.mainTexture = Resources.Load(reader.ReadString()) as Texture2D; workMat.mainTexture.filterMode = FilterMode.Bilinear; MatDict.SetMaterial(workStellarObjectTemplate.graphicName, workMat); break; } } else { //Otherwise, check if the node is an end element. if (reader.NodeType == XmlNodeType.EndElement) { //Check if this is the end of the template. if (reader.Name.Equals("StellarObjectTemplate")) { //If it is, finalize the template and add it to the dictionary. workStellarObjectTemplate.Finalized(); StellarObjectTemplateDict.SetTemplate(workStellarObjectTemplate.graphicName, workStellarObjectTemplate); } } } } //Ensure the file is closed after reading, and mark the StellarObjectTemplate dictionary as built. reader.Close(); stellarObjectTemplateDictBuilt = true; } else { //If it's already built, something went wrong. Debug.Log("Attempting to build already built StellarObjectTemplateDict."); } }
//Builds the ShipTemplate dictionary from the XML database. public static void BuildShipTemplateDict() { //Check that the dictionary hasn't already been built. if (!shipTemplateDictBuilt) { //First try to read ShipDefs.xml try { reader = new XmlTextReader("Assets/Resources/Defs/ShipDefs.xml"); } catch { //Catch the file not being found. Debug.Log("Could not find ShipDefs.xml"); return; } //Read until the end of the file. while (reader.Read()) { //Check if the element is a start element. if (reader.IsStartElement()) { //If it is, determine what variable it's referring to, and assign it. switch (reader.Name.ToString()) { //Begin a new ShipTemplate. case "ShipTemplate": workShipTemplate = new ShipTemplate(); break; //Assign the graphicName to the template. case "graphicName": workShipTemplate.SetGraphicName(reader.ReadString()); break; //Build the material for the template and then add it to the material dictionary. case "graphicPath": workMat = new Material(Shader.Find("Sprites/Default")); workMat.mainTexture = Resources.Load(reader.ReadString()) as Texture2D; workMat.mainTexture.filterMode = FilterMode.Bilinear; MatDict.SetMaterial(workShipTemplate.graphicName, workMat); break; //Assign the movementSpeed to the template. case "movementSpeed": workShipTemplate.SetMovementSpeed(int.Parse(reader.ReadString())); break; } } else { //Check if the node type is an end element. if (reader.NodeType == XmlNodeType.EndElement) { //If it is, is this the end of a template? if (reader.Name.Equals("ShipTemplate")) { //Finalize the template and add it to the ShipTemplate dictionary. workShipTemplate.Finalized(); ShipTemplateDict.SetTemplate(workShipTemplate.graphicName, workShipTemplate); } } } } //Close the file after it's been read and mark the dictionary built. reader.Close(); shipTemplateDictBuilt = true; } else { //Otherwise, the dictionary has already been built and something went wrong. Debug.Log("Attempting to build already built ShipTemplateDict."); } }