public void ResizeMap(Vector2 newSize) { if (tileMap.TileLayers == null) { return; } Undo.RecordObject(tileMap, null); foreach (TileLayer layer in tileMap.TileLayers) { if (layer == null) { continue; } Tile[] newTiles = new Tile[(int)(newSize.x * newSize.y)]; for (int x = 0; x < tileMap.MapSize.x; ++x) { for (int y = 0; y < tileMap.MapSize.y; ++y) { if (x < newSize.x && y < newSize.y) { newTiles[(int)(x + y * newSize.x)] = layer.Tiles[(int)(x + y * tileMap.MapSize.x)]; } else { Tile tile = layer.Tiles[(int)(x + y * tileMap.MapSize.x)]; if (tile != null) { EditorApplication.delayCall += () => Undo.DestroyObjectImmediate(tile.gameObject); } } } } Undo.RecordObject(layer, null); layer.Tiles = newTiles; } tileMap.MapSize = newSize; }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory(); if (DialogResult.OK == dlg.ShowDialog()) { ListofObjects = new List<Object>(); navGraph = new List<Node>(); collisionObjects = new List<Collidable>(); PathNodes.Items.Clear(); Objects.Items.Clear(); listBoxCollisionObjects.Items.Clear(); numericMapCol.Value = 0; numericMapCol.Value = 0; numericMapRows.Value = 0; numericSheetWidth.Value = 0; XElement xRoot = XElement.Load(dlg.FileName); //XElement xLevel = xRoot.Element("Level"); XElement xWorldInfo = xRoot.Element("world_info"); XAttribute xTWidth = xWorldInfo.Attribute("width"); tileSize.Width = int.Parse(xTWidth.Value); XAttribute xTHeight = xWorldInfo.Attribute("height"); tileSize.Height = int.Parse(xTHeight.Value); XAttribute xWWidth = xWorldInfo.Attribute("worldWidth"); mapSize.Width = int.Parse(xWWidth.Value); XAttribute xWHeight = xWorldInfo.Attribute("worldHeight"); mapSize.Height = int.Parse(xWHeight.Value); XAttribute xTSWidth = xWorldInfo.Attribute("tileSheetWidth"); tSetSize.Width = int.Parse(xTSWidth.Value); XAttribute xTSHeight = xWorldInfo.Attribute("tileSheetHeight"); tSetSize.Height = int.Parse(xTSHeight.Value); filePath = xWorldInfo.Value; numericMapCol.Value = mapSize.Width; numericMapRows.Value = mapSize.Height; TileMap = new Tile[mapSize.Width, mapSize.Height]; for (int i = 0; i < mapSize.Width; ++i) { for (int j = 0; j < mapSize.Height; ++j) { TileMap[i, j] = new Tile(); TileMap[i, j].X = 0; TileMap[i, j].Y = 0; TileMap[i, j].sRect = new Rectangle(0, 0, tileSize.Width, tileSize.Height); } } if (D3DTileId != -1) { m_TM.UnloadTexture(D3DTileId); D3DTileId = -1; } D3DTileId = m_TM.LoadTexture(filePath); XElement xPlayer = xRoot.Element("player_info"); if (xPlayer != null) { XAttribute xId = xPlayer.Attribute("id"); XAttribute xPosX = xPlayer.Attribute("posX"); XAttribute xPosY = xPlayer.Attribute("posY"); XAttribute xNumEvents = xPlayer.Attribute("numEvents"); XAttribute xNumWaypoints = xPlayer.Attribute("numWaypoints"); XAttribute xNumEnemies = xPlayer.Attribute("numEnemies"); XAttribute xState = xPlayer.Attribute("startState"); XAttribute xSRectLeft = xPlayer.Attribute("sRectleft"); XAttribute xSRectTop = xPlayer.Attribute("sRecttop"); XAttribute xSRectWidth = xPlayer.Attribute("sRectwidth"); XAttribute xSRectHeight = xPlayer.Attribute("sRectheight"); Object tObject = new Object(int.Parse(xId.Value)); tObject.Position = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value)); tObject.NumEnemies = int.Parse(xNumEnemies.Value); tObject.sRect = new Rectangle(int.Parse(xSRectLeft.Value), int.Parse(xSRectTop.Value), int.Parse(xSRectWidth.Value), int.Parse(xSRectHeight.Value)); if (xState != null) { tObject.State = int.Parse(xState.Value); } string NameandEvents = xPlayer.Value; string[] info = NameandEvents.Split('/'); tObject.Name = info[0]; for (int i = 1; i < info.Length; i++) { tObject.events.Add(info[i]); } int numWaypoints = int.Parse(xNumWaypoints.Value); for (int currWP = 0; currWP < numWaypoints; currWP++) { string waypoint = "waypoint"; waypoint += currWP; XAttribute xWaypoint = xPlayer.Attribute(waypoint); tObject.waypoints.Add(int.Parse(xWaypoint.Value)); } ListofObjects.Add(tObject); Objects.Items.Add(tObject.Name + " " + tObject.Type); names[tObject.Type] = tObject.Name; } // D3DTileId = m_TM.LoadTexture(filePath); XElement xTileList = xRoot.Element("tile_list"); IEnumerable<XElement> xTiles = xTileList.Elements(); int count = 0; //int rowcount = 0; foreach (XElement xTile in xTiles) { Tile tTile = new Tile(); Rectangle sourceRect = new Rectangle(); XAttribute xSourceRectLeft = xTile.Attribute("rposx"); sourceRect.X = int.Parse(xSourceRectLeft.Value); XAttribute xSourceRectTop = xTile.Attribute("rposy"); sourceRect.Y = int.Parse(xSourceRectTop.Value); XAttribute xPositionX = xTile.Attribute("posX"); tTile.X = int.Parse(xPositionX.Value); XAttribute xPositionY = xTile.Attribute("posY"); tTile.Y = int.Parse(xPositionY.Value); sourceRect.Width = tileSize.Width; sourceRect.Height = tileSize.Height; tTile.sRect = sourceRect; //tTile.Y = (count / mapSize.Width) * tileSize.Width; //tTile.X = count % mapSize.Height * tileSize.Height; TileMap[tTile.X / tileSize.Width, tTile.Y / tileSize.Width] = tTile; //TileMap[(count / mapSize.Width) * tileSize.Width, (count % mapSize.Width) * tileSize.Height] = tTile; count++; } XElement xCollsionList = xRoot.Element("collsion_list"); if (xCollsionList != null) { IEnumerable<XElement> xColliders = xCollsionList.Elements(); foreach (XElement xCollider in xColliders) { Collidable tCollider = new Collidable(); XAttribute xPosX = xCollider.Attribute("posX"); XAttribute xPosY = xCollider.Attribute("posY"); XAttribute xType = xCollider.Attribute("type"); XAttribute xIndex = xCollider.Attribute("index"); string eventTrigger = xCollider.Value; string[] info = eventTrigger.Split('/'); for (int i = 1; i < info.Length; i++) { tCollider.eventTrigger.Add(info[i]); } if (eventTrigger.Length > 0) tCollider.eventTrigger.Add(eventTrigger.Remove(eventTrigger.Length - 1)); tCollider.Id = int.Parse(xType.Value); tCollider.PtPostion = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value)); tCollider.Index = int.Parse(xIndex.Value); collisionObjects.Add(tCollider); if (tCollider.Id == 0) listBoxCollisionObjects.Items.Add("Collider" + tCollider.Index); else listBoxCollisionObjects.Items.Add("Trigger" + tCollider.Index); } } XElement xNavLayer = xRoot.Element("graph_list"); if (xNavLayer != null) { IEnumerable<XElement> xNodes = xNavLayer.Elements(); foreach (XElement xNode in xNodes) { Node tNode = new Node(); XAttribute xPosX = xNode.Attribute("posX"); XAttribute xPosY = xNode.Attribute("posY"); XAttribute xIndex = xNode.Attribute("index"); XAttribute xNumEdges = xNode.Attribute("numEdges"); tNode.Position = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value)); tNode.Index = int.Parse(xIndex.Value); int numEdges = int.Parse(xNumEdges.Value); tNode.Tag = xNode.Value; for (int currEdge = 0; currEdge < numEdges; currEdge++) { string name = "edge"; name += currEdge; XName xname = name; XAttribute xEdge = xNode.Attribute(xname); tNode.edges.Add(int.Parse(xEdge.Value)); } navGraph.Add(tNode); PathNodes.Items.Add("Node" + tNode.Index.ToString()); } XElement xObjectLayer = xRoot.Element("objects_list"); if (xObjectLayer != null) { IEnumerable<XElement> xObjects = xObjectLayer.Elements(); foreach (XElement xObject in xObjects) { XAttribute xId = xObject.Attribute("id"); XAttribute xPosX = xObject.Attribute("posX"); XAttribute xPosY = xObject.Attribute("posY"); XAttribute xNumEvents = xObject.Attribute("numEvents"); XAttribute xNumWaypoints = xObject.Attribute("numWaypoints"); XAttribute xNumEnemies = xObject.Attribute("numEnemies"); XAttribute xSRectLeft = xObject.Attribute("sRectleft"); XAttribute xSRectTop = xObject.Attribute("sRecttop"); XAttribute xSRectWidth = xObject.Attribute("sRectwidth"); XAttribute xSRectHeight = xObject.Attribute("sRectheight"); Object tObject = new Object(int.Parse(xId.Value)); tObject.Position = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value)); tObject.NumEnemies = int.Parse(xNumEnemies.Value); tObject.sRect = new Rectangle(int.Parse(xSRectLeft.Value), int.Parse(xSRectTop.Value), int.Parse(xSRectWidth.Value), int.Parse(xSRectHeight.Value)); string NameandEvents = xObject.Value; string[] info = NameandEvents.Split('/'); tObject.Name = info[0]; for (int i = 1; i < info.Length; i++) { tObject.events.Add(info[i]); } int numWaypoints = int.Parse(xNumWaypoints.Value); for (int currWP = 0; currWP < numWaypoints; currWP++) { string waypoint = "waypoint"; waypoint += currWP; XAttribute xWaypoint = xObject.Attribute(waypoint); tObject.waypoints.Add(int.Parse(xWaypoint.Value)); } ListofObjects.Add(tObject); Objects.Items.Add(tObject.Name +" "+ tObject.Type); names[tObject.Type] = tObject.Name; } } } worldPanel.AutoScrollMinSize = new Size(tileSize.Width * mapSize.Width, tileSize.Height * mapSize.Height); } }
private void ApplyMovedTiles() { if (selection.hasSelection && selection.selectingState == Selection.SelectionState.Selected && selection.selectionToolMode == Selection.SelectionToolMode.Move) { TileLayer tileLayer = GetSelectedTileLayer(); if (tileLayer == null) { return; } int startX = (int)selection.GetBottomLeft().x; int endX = (int)selection.GetTopRight().x; int startY = (int)selection.GetBottomLeft().y; int endY = (int)selection.GetTopRight().y; int w = endX - startX + 1; int h = endY - startY + 1; Undo.IncrementCurrentGroup(); Undo.RecordObject(tileLayer, null); Tile[] buffer = new Tile[w * h]; for (int y = startY; y <= endY; ++y) { for (int x = startX; x <= endX; ++x) { int key = (int)((int)tileMap.MapSize.x * y + x); if (key < 0 || key > tileLayer.Tiles.Length) { continue; } buffer[(y - startY) * w + (x - startX)] = tileLayer.Tiles[key]; tileLayer.Tiles[key] = null; } } for (int y = startY; y <= endY; ++y) { for (int x = startX; x <= endX; ++x) { int key = (int)((int)tileMap.MapSize.x * y + x); if (key < 0 || key > tileLayer.Tiles.Length) { continue; } Tile tile = buffer[(y - startY) * w + (x - startX)]; if (tile != null) { GameObject go = tile.gameObject; Vector2 gridIndex = tileMap.TransformPositionToGridIndex(go.transform.position); int newKey = (int)((int)tileMap.MapSize.x * gridIndex.y + gridIndex.x); gridIndex = tileMap.ClampGridIndex(gridIndex); if (newKey < 0 || newKey > tileLayer.Tiles.Length) { continue; } if (tileLayer.Tiles[newKey] != null) { GameObject gameObjectToDelete = tileLayer.Tiles[newKey].gameObject; EditorApplication.delayCall += () => Undo.DestroyObjectImmediate(gameObjectToDelete); } Vector3 savedPosition = go.transform.position; go.transform.position = tileMap.GetGridIndexPosInWorldSpace(new Vector2(x, y)); Undo.RecordObject(go.transform, null); go.transform.position = savedPosition; tileLayer.Tiles[newKey] = tile; } } } Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); } }
//////////////////////////////////////////////////////////////////////////////// // Initialize // - initialize the SGP wrappers // - load assets //////////////////////////////////////////////////////////////////////////////// public void Initialize() { // Access the SGP Wrapper singletons m_D3D.Initialize(splitContainer1.Panel1, true); // m_D3D.AddRenderTarget(splitContainer1.Panel2); m_TM.Initialize(m_D3D.Device, m_D3D.Sprite); comboBoxAIStates.DataSource = Enum.GetValues(typeof(AIStates)); // Load assets // m_nTileSheetID = m_TM.LoadTexture("resource/graphics/SGD_Tank.png", Color.FromArgb(255, 0, 0, 0).ToArgb()); // w/o color key // tilesDraw = m_TM.LoadTexture("../../resource/graphics/tset.png"); //filePath = "resource\\graphics; //string fullPath = System.IO.Directory.GetCurrentDirectory(); //string[] split = fullPath.Split(new Char[] { '\\', '\n' }); //string tempString = ""; //for (int i = 0; i < split.Length - 2; i++) //{ // tempString += split[i] + "\\"; //} //tempString += filePath; //D3DTileId = m_TM.LoadTexture(tempString); objFilePath = "Resources\\ObjectImage.png"; D3DObjID = m_TM.LoadTexture(objFilePath); //m_TileSet = new TileSet[mapSize.Width, mapSize.Height]; //r_TileSet = new TileSet[renderTiles.Width, renderTiles.Height]; names.Add("PSpwn"); names.Add("ESpwn"); names.Add("Goal"); names.Add("ChkPnt"); names.Add("Door"); names.Add("Door\nOpener"); for (int i = 6; i < objectSetSize.Width * objectSetSize.Height; i++) { names.Add("Empty"); } numericMapRows.Value = mapSize.Height; numericMapCol.Value = mapSize.Width; for (int i = 0; i < mapSize.Width; i++) { for (int j = 0; j < mapSize.Height; j++) { TileMap[i, j] = new Tile(); TileMap[i, j].X = tileSize.Width * i; TileMap[i, j].Y = tileSize.Height * j; TileMap[i, j].sRect = new Rectangle(0, 0, tileSize.Width, tileSize.Height); } } // Anything else tilePanel.AutoScrollMinSize = new Size(tSetSize.Width * tileSize.Width, tSetSize.Height * tileSize.Height); objectPanel.AutoScrollMinSize = new Size(objectSetSize.Width * tileSize.Width, objectSetSize.Height * tileSize.Height); splitContainer1.Panel2.AutoScrollMinSize = new Size(mapSize.Width * tileSize.Width, mapSize.Height * tileSize.Height); // success Running = true; }
private void buttonSetMapSize_Click(object sender, EventArgs e) { Tile[,] currMap = TileMap; Size currSize = mapSize; mapSize.Width = (int)numericMapCol.Value; mapSize.Height = (int)numericMapRows.Value; TileMap = new Tile[(int)numericMapCol.Value, (int)numericMapRows.Value]; for (int x = 0; x < (int)numericMapCol.Value; x++) { for (int y = 0; y < (int)numericMapRows.Value; y++) { TileMap[x, y] = new Tile(); TileMap[x, y].sRect.Width = tileSize.Width; TileMap[x, y].sRect.Height = tileSize.Height; TileMap[x, y].X = x * tileSize.Width; TileMap[x, y].Y = y * tileSize.Height; } } // Copy old map into new map int minX = (mapSize.Width <= currSize.Width ? mapSize.Width : currSize.Width); int minY = (mapSize.Height <= currSize.Height ? mapSize.Height : currSize.Height); for (int x = 0; x < minX; x++) { for (int y = 0; y < minY; y++) { TileMap[x, y] = currMap[x, y]; } } worldPanel.AutoScrollMinSize = new Size(tileSize.Width * mapSize.Width, tileSize.Height * mapSize.Height); }
public static bool IsTileColliderEqualsToColliderInfo(Tile tile, ColliderInfo colliderInfo) { if (tile == null || colliderInfo == null) { return false; } switch (colliderInfo.CollisionType) { case CollisionType.Box: { BoxCollider2D collider = tile.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D; BoxCollider2DInfo info = colliderInfo as BoxCollider2DInfo; if (collider == null || info == null) { return false; } #if UNITY_5_0 if (info.Center == collider.offset && info.Size == collider.size) #else if (info.Center == collider.offset && info.Size == collider.size) #endif { return true; } else { return false; } } case CollisionType.Circle: { CircleCollider2D collider = tile.GetComponent(typeof(CircleCollider2D)) as CircleCollider2D; CircleCollider2DInfo info = colliderInfo as CircleCollider2DInfo; if (collider == null || info == null) { return false; } #if UNITY_5_0 if (collider.offset == info.Center && collider.radius == info.Radius) #else if (collider.offset == info.Center && collider.radius == info.Radius) #endif { return true; } else { return false; } } case CollisionType.Polygon: { PolygonCollider2D collider = tile.GetComponent(typeof(PolygonCollider2D)) as PolygonCollider2D; PolygonCollider2DInfo info = colliderInfo as PolygonCollider2DInfo; if (collider == null || info == null) { return false; } if (collider.pathCount == info.PathCount) { for (int i = 0; i < collider.pathCount; ++i) { if (collider.points[i] != info.Points[i]) { return false; } } return true; } else { return false; } } default: return true; } }
public static TileData GetTileDataFromTile(Tile tile) { if (tile == null) { return null; } TileData tileData = new TileData(); SpriteRenderer renderer = tile.GetComponent<SpriteRenderer>(); Collider2D collider = tile.GetComponent<Collider2D>(); Vector3 localScale = tile.transform.localScale; tileData.FlipHorizontally = localScale.x == -1; tileData.FlipVertically = localScale.y == -1; tileData.Sprite = renderer.sprite; tileData.OrderInLayer = renderer.sortingOrder; tileData.Rotation = tile.transform.rotation.eulerAngles.z; tileData.Collision = tile.Collision; tileData.Tag = tile.tag; if (collider != null) { tileData.IsTrigger = collider.isTrigger; tileData.PhysicsMaterial = collider.sharedMaterial; } return tileData; }
public static bool IsTheSameTile(Tile tile, TileData tileData) { Vector3 localScale = tile.transform.localScale; SpriteRenderer renderer = tile.GetComponent<SpriteRenderer>(); if (tile.tag != tileData.Tag) { return false; } if (renderer.sortingOrder != tileData.OrderInLayer) { return false; } if ((tileData.FlipHorizontally && localScale.x != -1) || (!tileData.FlipHorizontally && localScale.x != 1)) { return false; } if ((tileData.FlipVertically && localScale.y != -1) || (!tileData.FlipVertically && localScale.y != 1)) { return false; } if (renderer.sprite != tileData.Sprite) { return false; } if (tile.Collision != tileData.Collision) { return false; } if (Mathf.Abs(tile.transform.eulerAngles.z - tileData.Rotation) > 0.0001f) { return false; } Collider2D collider = tile.GetComponent<Collider2D>(); if (collider != null) { if (collider.isTrigger != tileData.IsTrigger || collider.sharedMaterial != tileData.PhysicsMaterial) { return false; } } return true; }
public static void ApplyColliderInfoToTile(Tile tile, ColliderInfo colliderInfo) { if (tile == null || colliderInfo == null) { return; } CollisionType collisionType = colliderInfo.CollisionType; switch (collisionType) { case CollisionType.Box: { BoxCollider2DInfo info = colliderInfo as BoxCollider2DInfo; BoxCollider2D collider = tile.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D; if (info != null && collider != null) { collider.size = info.Size; #if UNITY_5_0 collider.offset = info.Center; #else collider.offset = info.Center; #endif } break; } case CollisionType.Circle: { CircleCollider2DInfo info = colliderInfo as CircleCollider2DInfo; CircleCollider2D collider = tile.GetComponent(typeof(CircleCollider2D)) as CircleCollider2D; if (info != null && collider != null) { #if UNITY_5_0 collider.offset = info.Center; #else collider.offset = info.Center; #endif collider.radius = info.Radius; } break; } case CollisionType.Polygon: { PolygonCollider2DInfo info = colliderInfo as PolygonCollider2DInfo; PolygonCollider2D collider = tile.GetComponent(typeof(PolygonCollider2D)) as PolygonCollider2D; if (info != null && collider != null) { collider.pathCount = info.PathCount; collider.points = info.Points; } break; } default: break; } }
public static ColliderInfo GetColliderInfoFromTile(Tile tile) { if (tile == null) { return null; } ColliderInfo info = null; CollisionType collisionType = tile.Collision; switch (collisionType) { case CollisionType.Box: { BoxCollider2D collider = tile.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D; if (collider) { #if UNITY_5_0 info = new BoxCollider2DInfo(collider.offset, collider.size); #else info = new BoxCollider2DInfo(collider.offset, collider.size); #endif } break; } case CollisionType.Circle: { CircleCollider2D collider = tile.GetComponent(typeof(CircleCollider2D)) as CircleCollider2D; if (collider) { #if UNITY_5_0 info = new CircleCollider2DInfo(collider.offset, collider.radius); #else info = new CircleCollider2DInfo(collider.offset, collider.radius); #endif } break; } case CollisionType.Polygon: { PolygonCollider2D collider = tile.GetComponent(typeof(PolygonCollider2D)) as PolygonCollider2D; if (collider) { info = new PolygonCollider2DInfo(collider.pathCount, collider.points); } break; } default: break; } return info; }