public void DeleteObject() { Command cmd = null; if (type.Equals("block")) { cmd = new RemoveBlockCommand(objectIndex); } else if (type.Equals("pig")) { cmd = new RemovePigCommand(objectIndex); } if (cmd != null) { LevelEditorManager.commandManager.ExecuteCommand(cmd); } else { Debug.Log("MenuObject : delete object error"); } HideMenu(); }
// 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!"); } } }