Esempio n. 1
0
    public void CarregaXmlLevel(string path)
    {
        string levelFileXML = LevelLoader.ReadXmlLevel(path);

        ABLevel levelFromFile = LevelLoader.LoadXmlLevel(levelFileXML);

        ELevel.instance.loadingLevelFromFile = true;
        Command addObject = null;
        int     index;

        foreach (BlockData block in levelFromFile.blocks)
        {
            index     = typePrefabIndex[block.type];
            addObject = new AddBlockCommand(block.type, block.rotation, block.x, block.y, block.material, blocks[index]);
            commandManager.ExecuteCommand(addObject);
        }

        foreach (OBjData pig in levelFromFile.pigs)
        {
            index     = typePrefabIndex[pig.type];
            addObject = new AddPigCommand(pig.type, pig.rotation, pig.x, pig.y, pigs[index]);
            commandManager.ExecuteCommand(addObject);
        }

        foreach (BirdData bird in levelFromFile.birds)
        {
            if (addBirdRef.ContainsKey(bird.type))
            {
                commandManager.ExecuteCommand(addBirdRef[bird.type]);
            }
        }

        ELevel.instance.loadingLevelFromFile = false;
    }
Esempio n. 2
0
        public void createBlock()
        {
            //get current material
            UInt32 mat = Terrain.MaterialManager.getMaterialIndex(myEditor.context.currentMaterial);

            //get block selection
            NodeLocation nl = myEditor.context.currentLocation;

            if (nl != null)
            {
                nl = nl.getNeighborLocation(myEditor.context.currentFace);

                //send create blocks cmd
                AddBlockCommand cmd = new AddBlockCommand(nl, mat);
                myEditor.world.dispatch(cmd);
            }
        }
Esempio n. 3
0
    void createInstance()
    {
        Command addObject = null;
        int     index     = typePrefabIndex[toInstantiate];

        if (toInstantiate.Contains("Basic"))
        {
            addObject = new AddPigCommand(toInstantiate, 0, 10, 10, pigs[index]);
        }
        else if (toInstantiate != null)
        {
            addObject = new AddBlockCommand(toInstantiate, 0, 10, 10, currentMaterial, blocks[index]);
        }
        if (addObject != null)
        {
            commandManager.ExecuteCommand(addObject);
        }
        //Debug.Log(toInstantiate);
    }
Esempio n. 4
0
        public void createMultiBlocks()
        {
            //get current material
            UInt32 mat = MaterialManager.getMaterialIndex(myEditor.context.currentMaterial);
            List <NodeLocation> newNodes = new List <NodeLocation>();

            foreach (NodeLocation n in myEditor.context.selectedNodes)
            {
                NodeLocation nl = n;
                nl = nl.getNeighborLocation(myEditor.context.currentFace);
                newNodes.Add(nl);

                //send create blocks cmd
                AddBlockCommand cmd = new AddBlockCommand(nl, mat);
                myEditor.world.dispatch(cmd);
            }

            myEditor.context.selectedNodes.Clear();
            myEditor.context.selectedNodes.AddRange(newNodes);
        }
    // Update is called once per frame
    void Update()
    {
        if (itemsHaveChanged)
        {
            _loadItemsFromEquippedList();
            itemsHaveChanged = false;
        }


        bool raycastHit = false;

        RaycastHit hit;

        // only collide raycast with Blocks layer
        int layerMaskBlocksOnly = LayerMask.GetMask("Blocks");

        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, m_maxBlockPlacingRange, layerMaskBlocksOnly))
        {
            raycastHit = true;
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
        }
        else
        {
            Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 10, Color.white);
        }


        // Input Function
        if (Input.anyKeyDown)
        {
            int numberKey = 0;
            if (int.TryParse(Input.inputString, out numberKey))
            {
                if ((numberKey > 0) && (numberKey < 8))
                {
                    // remap from 1234567890 to 0123456789 (because of keyboard layout)
                    toolbarSlotSelection = (numberKey + 9) % 10;

                    if (activeItems[toolbarSlotSelection].transform.childCount > 0)
                    {
                        string itemName     = activeItems[toolbarSlotSelection].transform.GetChild(0).gameObject.name;
                        int    bracketIndex = itemName.IndexOf("(");
                        itemName           = itemName.Substring(0, bracketIndex);
                        blockTypeSelection = (BLOCK_ID)System.Enum.Parse(typeof(BLOCK_ID), itemName.ToUpper());
                        _hideSelectors();
                        selectors[toolbarSlotSelection].SetActive(true);
                    }
                }
            }
        }


        currentlySelectedBlockUI.text = blockDatabase.GetProperties((BLOCK_ID)blockTypeSelection).m_description;


        // if selection within range
        if (raycastHit)
        {
            // determine if block place position is too close to the player
            Vector3 blockPlacePosition   = new IntPos((hit.point) + (hit.normal * 0.5f) + new Vector3(0.5f, 0.5f, 0.5f)).Vec3();//(new IntPos(((hit.point) + (hit.normal * 0.1f))).Vec3() + new Vector3(0.5f, 0.5f, 0.5f));
            IntPos  integerPlacePosition = new IntPos(blockPlacePosition);

            // position of the block the raycast hit
            IntPos hitBlockPosition = new IntPos((hit.point) + (hit.normal * -0.5f) + new Vector3(0.5f, 0.5f, 0.5f));

            byte hitBlockType = world.GetBlockID(hitBlockPosition);

            // get all properties of the block the raycast hit
            BlockProperties hitBlockProperties = blockDatabase.GetProperties((BLOCK_ID)hitBlockType);

            // show description text for block
            blockDescriptionUI.text = hitBlockProperties.m_description;

            // put visible ghost block there and make it visible
            ghostBlock.transform.position = blockPlacePosition;
            ghostBlock.gameObject.SetActive(true);

            { // Debug block selection
                Debug.DrawRay(blockPlacePosition, new Vector3(0.5f, 0.0f, 0.0f), Color.red);
                Debug.DrawRay(blockPlacePosition, new Vector3(0.0f, 0.5f, 0.0f), Color.green);
                Debug.DrawRay(blockPlacePosition, new Vector3(0.0f, 0.0f, 0.5f), Color.blue);

                Debug.DrawRay(blockPlacePosition, new Vector3(0.0f, -0.5f, 0.0f), Color.yellow);
            }

            // Place Block
            if ((Input.GetButtonDown("PlaceBlock")))
            {
                // determine if block place position is too close to the player
                if (!ghostBlock.IsColliding() && hitBlockProperties.m_canBePlacedUpon)
                {
                    Debug.Log("Placing block!");
                    Command cmd = new AddBlockCommand((byte)blockTypeSelection, integerPlacePosition);

                    if (Execute(ref cmd))
                    {
                        // send notification that a Block was placed
                        NotifyAll(gameObject, OBSERVER_EVENT.PLACED_BLOCK);
                    }
                }   // endif ghostBlock colliding
                else
                {
                    Debug.Log("Cannot place block--Entity is in the way!");
                }
            }

            // Remove Block
            if (Input.GetButtonDown("RemoveBlock"))
            {
                Debug.Log("Removing block!");
                Command cmd = new RemoveBlockCommand(hitBlockType, hitBlockPosition);
                Execute(ref cmd);
            }
        }
        else // endif raycast hit
        {
            ghostBlock.gameObject.SetActive(false);
            blockDescriptionUI.text = "";
        }

        // Undo Last
        if (Input.GetButtonDown("Cancel") || Input.GetKeyDown(KeyCode.Backspace))
        {
            if (commandList.Count > 0)
            {
                var lastAction = commandList.Last;

                if (lastAction.Value.IsCompleted())
                {
                    lastAction.Value.Undo();
                }
                else
                {
                    Debug.Log("Could not Undo! Action not completed! Removing uncompleted action");
                }

                commandList.RemoveLast();
            }
            else
            {
                Debug.Log("Could not Undo! Nothing to Undo!");
            }
        }
    }