Пример #1
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        GUILayout.Space(30);
        GUILayout.Label("Functions for use in editor");
        Overlord overlord = (Overlord)target;

        overlord.EditorInit();

        MapSlice mSlice = overlord.environmentCore.mapSlice;

        if (GUILayout.Button("Generate map from file"))
        {
            mSlice.InitMap();
            Debug.LogError("[WARNING - Game Breaker] Don't forget to set center hub as uncapturable!");
        }

        if (GUILayout.Button("Clear Tiles"))
        {
            Transform[] res = mSlice.mapRoot.GetComponentsInChildren <Transform>();
            foreach (Transform t in res)
            {
                if (t != mSlice.mapRoot.transform && t != null)
                {
                    DestroyImmediate(t.gameObject);
                }
            }
        }

        if (GUILayout.Button("Toggle Hidden Tiles"))
        {
            mSlice.LoadTiles();
            for (int x = 0; x < 35; x++)
            {
                for (int y = 0; y < 35; y++)
                {
                    Tile t = mSlice.map.tMap[x, y];
                    if (t.hiddenBody)
                    {
                        if (t.bodyObj.activeInHierarchy)
                        {
                            t.bodyObj.SetActive(false);
                        }
                        else
                        {
                            t.bodyObj.SetActive(true);
                        }
                    }
                }
            }
        }
    }
Пример #2
0
        /// <summary>
        /// loads map for use in game
        /// </summary>
        public void LoadAndInitialize()
        {
            try
            {
                #region load map data

                XDocument doc = XDocument.Load(Parameters.FileName);

                XElement root = doc.Element("MapFile");
                if (root == null)
                    return;

                //sectors
                XElement sectors = root.Element("Sectors");
                if (sectors == null)
                    return;
                foreach (XElement sector in sectors.Elements("Sector"))
                {
                    try
                    {
                        Sectors.Add(new MapSector(sector.Attribute("name").Value,
                                                  new Vector2(float.Parse(sector.Attribute("x").Value.Replace('.', ',')), float.Parse(sector.Attribute("y").Value.Replace('.', ','))),
                                                  float.Parse(sector.Attribute("range").Value.Replace('.', ','))));
                        foreach (XElement item in sector.Elements())
                        {
                            try
                            {
                                switch (item.Name.LocalName)
                                {
                                    case "Edge": Sectors[Sectors.Count - 1].Edges.Add(item.Attribute("name").Value); break;
                                    case "Neighbour": Sectors[Sectors.Count - 1].Neighbours.Add(item.Attribute("name").Value); break;
                                    case "Spawn": Sectors[Sectors.Count - 1].SpawnPoints.Add(item.Attribute("name").Value); break;
                                }
                            }
                            catch { }
                        }
                    }
                    catch { }
                }

                //points
                XElement points = root.Element("Points");
                if (points == null)
                    return;
                foreach (XElement point in points.Elements("Point"))
                {
                    try
                    {
                        Points.Add(new MapPoint(point.Attribute("name").Value, new Vector2(float.Parse(point.Attribute("x").Value.Replace('.', ',')), float.Parse(point.Attribute("y").Value.Replace('.', ',')))));
                    }
                    catch { }
                }

                //edges
                XElement edges = root.Element("Edges");
                if (edges == null)
                    return;
                foreach (XElement edge in edges.Elements("Edge"))
                {
                    try
                    {
                        Edges.Add(new MapEdge(edge.Attribute("name").Value, Points.Find(p => p.Name == edge.Attribute("start").Value), Points.Find(p => p.Name == edge.Attribute("end").Value)));
                    }
                    catch { }
                }

                //spawn points
                XElement spawns = root.Element("Spawns");
                if (spawns == null)
                    return;
                foreach (XElement spawn in spawns.Elements("Spawn"))
                {
                    try
                    {
                        SpawnPoints.Add(new MapSpawnPoint(spawn.Attribute("name").Value,
                                        new Vector2(float.Parse(spawn.Attribute("x").Value.Replace('.', ',')), float.Parse(spawn.Attribute("y").Value.Replace('.', ','))),
                                        float.Parse(spawn.Attribute("rot").Value.Replace('.', ',')),
                                        int.Parse(spawn.Attribute("team").Value)));
                        SpawnInUse.Add(false);
                    }
                    catch { }
                }

                //rectangles
                XElement rects = root.Element("Rects");
                if (rects == null)
                    return;
                foreach (XElement rect in rects.Elements("Rect"))
                {
                    try
                    {
                        Rects.Add(new MapRect(rect.Attribute("name").Value,
                                  new Rectangle(int.Parse(rect.Attribute("x").Value), int.Parse(rect.Attribute("y").Value), int.Parse(rect.Attribute("w").Value), int.Parse(rect.Attribute("h").Value)),
                                  float.Parse(rect.Attribute("rot").Value.Replace('.', ',')),
                                  int.Parse(rect.Attribute("originX").Value), int.Parse(rect.Attribute("originY").Value)));
                    }
                    catch { }
                }

                //clean up
                //erase edges not assigned to any sector
                for (int i = 0; i < Edges.Count; )
                {
                    if (Sectors.FindIndex(s => s.Edges.FindIndex(e => Edges[i].Name == e) != -1) == -1)
                        Edges.RemoveAt(i);
                    else
                        i++;
                }
                //erase spawns outside of every sector (spawn need to be inside of some sector for motor initialization)
                for (int i = 0; i < SpawnPoints.Count; )
                {
                    if (Sectors.FindIndex(s => s.IsInSector(SpawnPoints[i].Coords)) == -1)
                        SpawnPoints.RemoveAt(i);
                    else
                        i++;
                }

                #endregion

                #region draw map and slice it

                //draw to memory
                Texture = UIParent.defaultTextures;

                RenderTarget2D mappicture = new RenderTarget2D(game.GraphicsDevice, (int)Parameters.Size.X, (int)Parameters.Size.Y, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
                SpriteBatch sb = new SpriteBatch(game.GraphicsDevice);
                game.GraphicsDevice.SetRenderTarget(mappicture);
                game.GraphicsDevice.Clear(Parameters.BackColor);
                sb.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
                for (int x = 0; x < (int)(Parameters.Size.X / BackCrateTexture.Width + (Parameters.Size.X % BackCrateTexture.Width != 0 ? 1 : 0)); x++)
                    for (int y = 0; y < (int)(Parameters.Size.Y / BackCrateTexture.Height + (Parameters.Size.Y % BackCrateTexture.Height != 0 ? 1 : 0)); y++)
                        sb.Draw(Texture, new Vector2(x * BackCrateTexture.Width, y * BackCrateTexture.Height), BackCrateTexture, Parameters.BackCrateColor);
                for (int i = 0; i < Rects.Count; i++)
                    Rects[i].Draw(ref sb, Parameters.BlockingColor);
                sb.End();

                //slice map
                for (int x = 0; x < Slices.GetLength(0); x++)
                    for (int y = 0; y < Slices.GetLength(1); y++)
                    {
                        Slices[x, y] = new MapSlice(game, new Rectangle((int)(x * Parameters.Slicing.X), (int)(y * Parameters.Slicing.Y), (int)Parameters.Slicing.X, (int)Parameters.Slicing.Y));
                        game.GraphicsDevice.SetRenderTarget(Slices[x, y].picture);
                        game.GraphicsDevice.Clear(Color.Transparent);
                        sb.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
                        sb.Draw(mappicture, Vector2.Zero, Slices[x, y].PositionAndSize, Color.White);
                        sb.End();
                    }

                game.GraphicsDevice.SetRenderTarget(null);

                mappicture.Dispose();
                sb.Dispose();
                Rects.Clear();

                #endregion
            }
            catch { }
        }