/// <summary> /// Get tiles batch by index. Creates a new batch if needed. /// </summary> /// <param name="index">Batch index to get.</param> /// <param name="createIfNeeded">If true and batch doesn't exist yet, create and return it. If false, may return null.</param> /// <returns>Tiles batch.</returns> private TilesBatch GetBatch(Point index, bool createIfNeeded = true) { // try to get batch try { return(_batches[index.X][index.Y]); } // not found? catch (KeyNotFoundException) { // create new batch if (createIfNeeded) { // create row if needed if (!_batches.ContainsKey(index.X)) { _batches[index.X] = new Dictionary <int, TilesBatch>(); } // create new batch TilesBatch newBatch = CreateBatch(index); _batches[index.X][index.Y] = newBatch; // return newly created batch return(newBatch); } } // if got here it means batch wasn't found and create-new was set to false. return null. return(null); }
/// <summary> /// Set the tile GameObject at a given index (will replace previous tile if exists). /// </summary> /// <param name="index">Tile index to set.</param> /// <param name="tile">GameObject to set as tile (note: will be cloned).</param> public void SetTile(Point index, GameObject tile) { // calc batch index and relative index CalcBatchIndexAndRelativeIndex(index); // get batch TilesBatch batch = GetBatch(batchIndex, true); // set tile tile = tile.Clone(); SetTileProperties(batch, tile, index); batch.Tiles[relativeIndex.X, relativeIndex.Y] = tile; }
/// <summary> /// Set the basic properties of a game object before pushing it into the tilemap. /// </summary> /// <param name="batch">Batch we are adding the tile to.</param> /// <param name="obj">GameObject to set.</param> /// <param name="index">Tile index.</param> private void SetTileProperties(TilesBatch batch, GameObject obj, Point index) { // set name and position obj.Name = "tile-" + index.ToString(); obj.SceneNode.Position = new Vector3(TileSize.X * index.X, 0, TileSize.Y * index.Y); // put index as attached data string intDataKey = InternalDataKey; obj.SetInternalData(ref intDataKey, index); // set to not update when not visible obj.UpdateWhenNotVisible = false; // add to root tile obj.Parent = batch.BatchRoot; }
/// <summary> /// Get tile at a given index (create new tile GameObject if doesn't exist). /// </summary> /// <param name="index">Tile index to get.</param> /// <param name="createIfNeeded">If true and tile doesn't exist yet, create and return it. If false, may return null.</param> /// <returns>Tile GameObject.</returns> public GameObject GetTile(Point index, bool createIfNeeded = true) { // make sure index is valid if (index.X < 0 || index.Y < 0) { throw new Exceptions.OutOfRangeException("Tile index must be positive!"); } // calc batch index and relative index CalcBatchIndexAndRelativeIndex(index); // get batch TilesBatch batch = GetBatch(batchIndex, createIfNeeded); // no batch? return null if (batch == null) { return(null); } // try to get tile GameObject ret = batch.Tiles[relativeIndex.X, relativeIndex.Y]; // check if need to create new tile if (ret == null && createIfNeeded) { // create the new game object and set its basic properties ret = new GameObject("", SceneNodeType.BoundingBoxCulling); SetTileProperties(batch, ret, index); // add to batch batch.Tiles[relativeIndex.X, relativeIndex.Y] = ret; } // return tile (or null, if not found and not creating new) return(ret); }
/// <summary> /// Destroy a specific tile. /// </summary> /// <param name="index">Tile index to destroy.</param> public void DestroyTile(Point index) { // calc batch index and relative index CalcBatchIndexAndRelativeIndex(index); // get batch TilesBatch batch = GetBatch(batchIndex, false); // no batch? return if (batch == null) { return; } // try to get tile GameObject toRemove = batch.Tiles[relativeIndex.X, relativeIndex.Y]; // if tile exists, destroy it if (toRemove != null) { toRemove.Parent = null; batch.Tiles[relativeIndex.X, relativeIndex.Y] = null; } }