public void Fill(TileData.TileType newType) { TileData.TileType previousType = GetTileType(); if (previousType != newType) { // Before we actually change the tile type, check and handle special cases. if (newType == TileData.TileType.Unoccupied) { // Create the fading visual effects if the Tile hasn't already been cleared. if (previousType != TileData.TileType.Unoccupied && previousType != TileData.TileType.Uninitialized) { CreateVanishVisualEffects(); } } data.Fill(newType); SetSprite(newType); trueSprite = spriteRenderer.sprite; OnChanged(newType); } }
public void SpawnRandomBlock() { if (!enabled) { // If the component is disabled, don't spawn a block. return; } if (blocksQueue.Count == maxBlocksInQueue) { //if the # of elements in queue already reaches //max, game over gameFlow.GameOver(GameFlow.GameOverCause.QueueOverflow); return; } else { //otherwise we select a random block from the possible list, //then instantiate the draggable block and add it into the queue. Block toSpawn = null; if (isSpawningOneTileBlocks) { Block oneTile = new Block(1, 1); oneTile.Fill(0, 0, TileData.TileType.Regular); toSpawn = oneTile; } else if (isSpawningSameBlocks) { if (currentBlock != null) { toSpawn = currentBlock; } } else { // The index of the block to choose from the bag. int indexInBagToChoose = -1; if (bag.Count == 0) { // If the bag is empty, repopulate it. List <BagBlock> viableBlocks = GetBagBlocksOfTier(tierCurrent); for (int j = 0; j < viableBlocks.Count; ++j) { //Block bagBlock = possibleBlocks[tierCurrent][j]; //Block bagBlock = new Block(possibleBlocks[tierCurrent][j]); BagBlock viableBlock = viableBlocks[j]; bag.Add(viableBlock); } // If a Junkyard event is starting, choose a junkyard-specific block. if (junkyardEventIsStarting) { junkyardEventIsStarting = false; List <BagBlock> junkyardBlocks = GetBagBlocksJunkyardOnlyOfTier(tierCurrent); int indexInJunkyardBlocks = Random.Range(0, junkyardBlocks.Count); BagBlock junkyardBlock = junkyardBlocks[indexInJunkyardBlocks]; for (int j = 0; j < bag.Count; ++j) { BagBlock bagBlock = bag[j]; if (bagBlock == junkyardBlock) { indexInBagToChoose = j; break; } } /* * Debug.Log("Junkyard index / Bag index: " + indexInJunkyardBlocks + * " / " + indexInBagToChoose); */ } else { indexInBagToChoose = Random.Range(0, bag.Count); } } else { indexInBagToChoose = Random.Range(0, bag.Count); } toSpawn = bag[indexInBagToChoose].CreateBlock(); // Remove each chosen element from the bag. bag.RemoveAt(indexInBagToChoose); // Add vestiges to the block, if applicable. if (isContaminationBlock) { int vestigesAdded = 0; List <TileData> refs = toSpawn.GetReferencesToType(TileData.TileType.Regular); // Stop adding vestiges when there are no regular tiles left. //Debug.Log("Vestige generation begin."); while (vestigesAdded < vestigesPerBlock && refs.Count != 0) { int index = Random.Range(0, refs.Count); TileData v = refs[index]; v.Fill(TileData.TileType.Vestige); v.SetVestigeLevel(vestigeLevel); refs.RemoveAt(index); ++vestigesAdded; //Debug.Log("vestigesAdded / vestigesPerBlock: " + vestigesAdded + " / " + vestigesPerBlock); //Debug.Log("BlockSpawner: vestigeLevel: " + vestigeLevel); } } //Debug.Log("Vestige generation end."); if (doContaminationBlocksAlternate) { isContaminationBlock = !isContaminationBlock; } } // Instantiate the actual block. GameObject newBlock = Instantiate(prefabDraggableBlock, transform, false); // Initialize the DraggableBlock component. DraggableBlock newDraggable = newBlock.GetComponent <DraggableBlock>(); //newDraggable.Init(toSpawn, grid, canvas); newDraggable.Init(toSpawn, grid, GetComponent <RectTransform>(), consoleGrid); newDraggable.SetScreenTapping(screenTapping);//Pass screenTapping to DraggableObject currentBlock = new Block(newDraggable.GetBlock()); // Add it to the queue. blocksQueue.Enqueue(newDraggable); // If this block is the only block in queue, enable it. if (blocksQueue.Count == 1) { EnableFrontBlock(); } else { // This block is not the only block in the queue. // Force the front block to be the last sibling so // that it is drawn above all of the other blocks. DraggableBlock frontBlock = blocksQueue.Peek(); frontBlock.transform.SetAsLastSibling(); } // Position new block at the next available blockPosition. int closestIndex = blocksQueue.Count - 1; PositionBlockAt(newDraggable, closestIndex); newDraggable.SetDefaultPosition(newBlock.transform.localPosition); consoleGrid.SetDraggableBlock(newDraggable); //Insert the block into the console grid } }