コード例 #1
0
    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
        }
    }
コード例 #2
0
    public void OnEndDrag(PointerEventData eventData)
    {
        GameObject hitObject  = eventData.pointerCurrentRaycast.gameObject;
        int        instanceId = Convert.ToInt32(gameObject.name);
        int        propId     = PlayerData.BagInfos[instanceId].propId;

        Debug.LogError(hitObject);

        if (hitObject == null)
        {
            ResetParent(father.gameObject);
        }
        else
        {
            BagBlock block = hitObject.GetComponent <BagBlock>();

            if (block != null)
            {
                if (block.type == BagBlock.BlockType.CharacterBag)
                {
                    if (hitObject.name == ConfigInfo.GetProp(info.propId.ToString()).Type)
                    {
                        ResetParent(hitObject, true);
                        SendPropEquipInfo(instanceId, propId, Convert.ToInt32(block.name));
                    }
                    else
                    {
                        ResetParent(father.gameObject);
                    }
                }
                else if (block.type == BagBlock.BlockType.Bag)
                {
                    ResetParent(hitObject, true);
                    SendPropEquipInfo(instanceId, propId, Convert.ToInt32(block.name));
                }
                else
                {
                    ResetParent(father.gameObject);
                }
            }
            else
            {
                BagProp bp = hitObject.GetComponent <BagProp>();
                if (bp != null)
                { ///替换装备
                    if (bp.transform.parent.GetComponent <BagBlock>().type == BagBlock.BlockType.CharacterBag)
                    {
                        ResetParent(hitObject.transform.parent.gameObject, true);
                        BagInfoEquipReq request = new BagInfoEquipReq();

                        PropInfo swapedInfo = PlayerData.BagInfos[Convert.ToInt32(hitObject.name)];
                        PropInfo dragInfo   = PlayerData.BagInfos[Convert.ToInt32(gameObject.name)];

                        EquipPropInfo reprop = new EquipPropInfo();
                        reprop.id     = swapedInfo.id;
                        reprop.propId = swapedInfo.propId;


                        EquipPropInfo prop = new EquipPropInfo();
                        prop.id     = dragInfo.id;
                        prop.propId = dragInfo.propId;

                        reprop.slot = dragInfo.slot;
                        int swapSlot = swapedInfo.slot;
                        prop.slot = swapSlot;


                        request.playerId = PlayerData.Me.info.id;
                        request.equipedProps.Add(reprop);
                        request.equipedProps.Add(prop);

                        MobaNetwork.Send((ushort)MessageId.EBagInfoEquipReq, request);
                    }
                    else
                    {
                        ResetParent(father.gameObject);
                    }
                }
                else
                {
                    ResetParent(father.gameObject);
                }
            }
        }
        icon.raycastTarget = true;
    }