// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit.collider != null) { PlatformSquare square = hit.collider.GetComponent <PlatformSquare> (); if (square != null) { int typeIndex = FindTypeIndex(square); if (typeIndex == -1) { Debug.Log("Couldn't find matching prefab for clicked on square '" + square.name + "' with type '" + square.GetType()); } else { int nextIndex = (typeIndex + 1 < m_squareTypes.Count ? typeIndex + 1 : 0); m_grid.ReplaceSquare(m_squareTypes [nextIndex], m_squareData [nextIndex], square.GridPosition.x, square.GridPosition.y, new Dictionary <string, string>()); Debug.Log(m_grid.AsLevelDefinition().ToString()); } } } } }
int FindTypeIndex(PlatformSquare square) { for (int i = 0; i < m_squareTypes.Count; ++i) { if (square.GetType() == m_squareTypes[i].GetType()) { return(i); } } return(-1); }
/// <summary> /// Replaces a grid square with a new one from a prefab. /// </summary> /// <param name="prefab">Prefab.</param> /// <param name="squareData">Square data.</param> /// <param name="x">The x coordinate.</param> /// <param name="y">The y coordinate.</param> /// <param name="attributes">Attributes.</param> public void ReplaceSquare(PlatformSquare prefab, PlatformSquareData squareData, int x, int y, Dictionary <string, string> attributes) { if (m_grid [x, y] != null) { m_grid [x, y].OnRemoveFromLevel(this, new GridCoord(x, y)); Destroy(m_grid [x, y].gameObject); } m_grid [x, y] = Instantiate <PlatformSquare> (prefab, this.transform); m_grid [x, y].InitializeSquareData(squareData); m_grid [x, y].name = "Grid (" + x + ", " + y + ")"; m_grid [x, y].Grid = this; m_grid [x, y].GridPosition = new GridCoord(x, y); m_grid [x, y].InitializeFromStringAttributes(attributes); m_grid [x, y].OnAddToLevel(this, new GridCoord(x, y)); }