Example #1
0
    // Update is called once per frame
    void Update()
    {
        // Get the active claim on each frame
        activeClaim = ClientAPI.ScriptObject.GetComponent<WorldBuilder>().ActiveClaim;

        if (GetBuildingState() == WorldBuildingState.SelectItem) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;
            // Casts the ray and get the first game object hit
            if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
                ClaimObject cObject = hit.transform.gameObject.GetComponent<ClaimObject>();
                if (cObject == null)
                    cObject = hit.transform.gameObject.GetComponentInChildren<ClaimObject>();
                if (cObject == null && hit.transform.parent != null)
                    cObject = hit.transform.parent.GetComponent<ClaimObject>();
                if (cObject != null) {
                    int objectID = cObject.ID;
                    if (activeClaim.claimObjects.ContainsKey(objectID)) {
                        if (hoverObject != cObject) {
                            if (hoverObject != null) {
                                hoverObject.ResetHighlight();
                            }
                            hoverObject = cObject;
                            cObject.Highlight();
                        }
                        if (Input.GetMouseButtonDown(0)) {
                            StopSelectObject();
                            SetBuildingState(WorldBuildingState.EditItem);
                            objectBeingEdited = activeClaim.claimObjects[objectID];
                            cObject.Selected = true;
                        }
                    }
                } else if (hoverObject != null) {
                    hoverObject.ResetHighlight();
                    hoverObject = null;
                }
            } else if (hoverObject != null) {
                hoverObject.ResetHighlight();
                hoverObject = null;
            }
        }

        if ((GetBuildingState() == WorldBuildingState.PlaceItem || GetBuildingState() == WorldBuildingState.MoveItem) && currentReticle != null) {
            GetHitLocation();
            float delta = Input.GetAxis ("Mouse ScrollWheel");
            if (mouseWheelBuildMode == MouseWheelBuildMode.Rotate) {
                if (delta > 0)
                    currentReticle.transform.Rotate (new Vector3 (0, -rotationAmount, 0));
                else if (delta < 0)
                    currentReticle.transform.Rotate (new Vector3 (0, rotationAmount, 0));
            } else if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical) {
                if (delta > 0) {
                    currentVerticalOffset += verticalMovementAmount;
                } else if (delta < 0) {
                    currentVerticalOffset -= verticalMovementAmount;
                }
            }

            if ((currentReticle != null) && (activeClaim != null) &&
                (ClientAPI.ScriptObject.GetComponent<WorldBuilder>().InsideClaimArea (activeClaim, hitPoint))) {
                if (snap) {
                    float newX = Mathf.Round(hitPoint.x * (1 / snapGap)) / (1 / snapGap);
                    float newZ = Mathf.Round(hitPoint.z * (1 / snapGap)) / (1 / snapGap);
                    hitPoint = new Vector3(newX, hitPoint.y + currentVerticalOffset, newZ);
                }
                currentReticle.transform.position = hitPoint;
            }

            if (Input.GetKeyDown(KeyCode.LeftShift)) {
                if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical) {
                    mouseWheelBuildMode = MouseWheelBuildMode.Rotate;
                    ClientAPI.Write("Changed to Rotate Mode");
                } else {
                    mouseWheelBuildMode = MouseWheelBuildMode.MoveVertical;
                    ClientAPI.Write("Changed to Move Vertical Mode");
                }
            }

            if (Input.GetMouseButtonDown (0) && !AtavismUiSystem.IsMouseOverFrame()) {
                if (GetBuildingState() == WorldBuildingState.PlaceItem) {
                    List<int> effectPositions = itemBeingPlaced.GetEffectPositionsOfTypes("ClaimObject");
                    Dictionary<string, object> props = new Dictionary<string, object>();
                    props.Add("claim", activeClaim.id);
                    props.Add("gameObject", itemBeingPlaced.itemEffectValues[effectPositions[0]]);
                    props.Add("loc", currentReticle.transform.position);
                    props.Add("orient", currentReticle.transform.rotation);
                    props.Add("itemID", itemBeingPlaced.templateId);
                    props.Add("itemOID", itemBeingPlaced.ItemId);
                    NetworkAPI.SendExtensionMessage(ClientAPI.GetPlayerOid(), false, "voxel.PLACE_CLAIM_OBJECT", props);
                    ClientAPI.Write("Send place claim object message");
                    ClearCurrentReticle(true);
                    itemBeingPlaced = null;
                    SetBuildingState(WorldBuildingState.Standard);
                    AtavismCursor.Instance.ChangeWorldBuilderState(false);
                } else if (GetBuildingState() == WorldBuildingState.MoveItem) {
                    Dictionary<string, object> props = new Dictionary<string, object>();
                    props.Add("action", "save");
                    props.Add("claimID", activeClaim.id);
                    props.Add("objectID", objectBeingEdited.GetComponent<ClaimObject>().ID);
                    props.Add("loc", objectBeingEdited.transform.position);
                    props.Add("orient", objectBeingEdited.transform.rotation);
                    NetworkAPI.SendExtensionMessage(ClientAPI.GetPlayerOid(), false, "voxel.EDIT_CLAIM_OBJECT", props);
                    //SetBuildingState(WorldBuildingState.EditItem);
                    SetBuildingState(WorldBuildingState.Standard);
                    ClearCurrentReticle(false);
                    itemBeingPlaced = null;
                    objectBeingEdited.GetComponent<ClaimObject>().Selected = false;
                    objectBeingEdited = null;
                }

            }
        }
    }
Example #2
0
        // Update is called once per frame
        void Update()
        {
            if (AtavismCursor.Instance == null || AtavismCursor.Instance.IsMouseOverUI())
            {
                return;
            }
            if (WorldBuilder.Instance.ActiveClaim != null)
            {
                if (WorldBuilder.Instance.ActiveClaim.permissionlevel < 1 && !WorldBuilder.Instance.ActiveClaim.playerOwned)
                {
                    return;
                }
            }
            if (GetBuildingState() == WorldBuildingState.SelectItem)
            {
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                // Casts the ray and get the first game object hit
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, objectSelectLayers))
                {
                    ClaimObject cObject = hit.transform.gameObject.GetComponent <ClaimObject>();
                    if (cObject == null)
                    {
                        cObject = hit.transform.gameObject.GetComponentInChildren <ClaimObject>();
                    }
                    if (cObject == null && hit.transform.parent != null)
                    {
                        cObject = hit.transform.parent.GetComponent <ClaimObject>();
                    }
                    if (cObject != null)
                    {
                        int objectID = cObject.ID;
                        if (WorldBuilder.Instance.ActiveClaim.claimObjects.ContainsKey(objectID))
                        {
                            if (hoverObject != cObject)
                            {
                                if (hoverObject != null)
                                {
                                    hoverObject.ResetHighlight();
                                }
                                hoverObject = cObject;
                                cObject.Highlight();
                            }
                            if (Input.GetMouseButtonDown(0))
                            {
                                StopSelectObject();
                                //SetBuildingState(WorldBuildingState.EditItem);
                                //objectBeingEdited = activeClaim.claimObjects[objectID].gameObject;
                                WorldBuilder.Instance.RequestClaimObjectInfo(objectID);
                                cObject.Highlight();
                                WorldBuilder.Instance.SelectedObject = cObject;
                            }
                        }
                    }
                    else if (hoverObject != null)
                    {
                        hoverObject.ResetHighlight();
                        hoverObject = null;
                    }
                }
                else if (hoverObject != null)
                {
                    hoverObject.ResetHighlight();
                    hoverObject = null;
                }
            }

            if ((GetBuildingState() == WorldBuildingState.PlaceItem || GetBuildingState() == WorldBuildingState.MoveItem) && currentReticle != null)
            {
                if (Input.GetMouseButtonDown(1))
                {
                    ClearCurrentReticle(true);
                    WorldBuilder.Instance.BuildingState = WorldBuildingState.None;
                    return;
                }

                GetHitLocation();
                bool legalPlacement = IsLegalClaimObjectPlacement();
                if (!legalPlacement)
                {
                    currentReticle.GetComponentInChildren <ClaimObject>().HighlightError();
                }
                else
                {
                    currentReticle.GetComponentInChildren <ClaimObject>().ResetHighlight();
                }

                float delta = Input.GetAxis("Mouse ScrollWheel");
                if (mouseWheelBuildMode == MouseWheelBuildMode.Rotate)
                {
                    Vector3 currentRotation = currentReticle.transform.rotation.eulerAngles;
                    if (delta > 0)
                    {
                        //currentReticle.transform.Rotate (new Vector3 (0, -rotationAmount, 0));
                        currentRotation.y -= 90;
                    }
                    else if (delta < 0)
                    {
                        //currentReticle.transform.Rotate (new Vector3 (0, rotationAmount, 0));
                        currentRotation.y += 90;
                    }
                    Quaternion rotation = Quaternion.Euler(currentRotation);
                    currentReticle.transform.rotation = rotation;
                }
                else if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical)
                {
                    if (delta > 0)
                    {
                        if (currentClaimObject.verticalSnapDistance > 0)
                        {
                            currentVerticalOffset += currentClaimObject.verticalSnapDistance;
                        }
                        else
                        {
                            currentVerticalOffset += verticalMovementAmount;
                        }
                    }
                    else if (delta < 0)
                    {
                        if (currentClaimObject.verticalSnapDistance > 0)
                        {
                            currentVerticalOffset -= currentClaimObject.verticalSnapDistance;
                        }
                        else
                        {
                            currentVerticalOffset -= verticalMovementAmount;
                        }
                    }
                }

                //snapped = false;
                if ((currentReticle != null) && (WorldBuilder.Instance.ActiveClaim != null) &&
                    (WorldBuilder.Instance.InsideClaimArea(WorldBuilder.Instance.ActiveClaim, hitPoint)))
                {
                    if (snap)
                    {
                        float newX = Mathf.Round(hitPoint.x * (1 / snapGap)) / (1 / snapGap);
                        float newZ = Mathf.Round(hitPoint.z * (1 / snapGap)) / (1 / snapGap);
                        float newY = hitPoint.y + currentVerticalOffset;
                        // If the claimobject has a horizontal snap value, override default snap values
                        if (currentClaimObject.horizontalSnapDistance > 0)
                        {
                            newX = Mathf.Round(hitPoint.x * (1 / currentClaimObject.horizontalSnapDistance)) / (1 / currentClaimObject.horizontalSnapDistance);
                            newZ = Mathf.Round(hitPoint.z * (1 / currentClaimObject.horizontalSnapDistance)) / (1 / currentClaimObject.horizontalSnapDistance);
                        }
                        // If the claimobject has a vertical snap value, set that as well
                        if (currentClaimObject.verticalSnapDistance > 0)
                        {
                            newY = Mathf.Round((hitPoint.y + currentVerticalOffset) * (1 / currentClaimObject.verticalSnapDistance)) / (1 / currentClaimObject.verticalSnapDistance);
                        }
                        hitPoint = new Vector3(newX, newY, newZ);
                        //	snapped = true;
                    }

                    if (currentClaimObject.placementType == BuildObjectPlacementType.Terrain && hitObject != null &&
                        (hitObject.GetComponent <ClaimObject>() != null || hitObject.GetComponentInParent <ClaimObject>() != null))
                    {
                        // Custom snap system to line them up with each other
                        // Get size of block
                        Vector3 baseSize = GetSizeOfBase(currentReticle).size;
                        // Move block along z axis by size / 2, set y to same as hit object
                        if (hitNormal == Vector3.back)
                        {
                            hitPoint    = hitObject.transform.position;
                            hitPoint.z -= baseSize.z;
                            //	snapped = true;
                        }
                        else if (hitNormal == Vector3.forward)
                        {
                            hitPoint    = hitObject.transform.position;
                            hitPoint.z += baseSize.z;
                            //	snapped = true;
                        }
                        else if (hitNormal == Vector3.right)
                        {
                            hitPoint    = hitObject.transform.position;
                            hitPoint.x += baseSize.x;
                            //	snapped = true;
                        }
                        else if (hitNormal == Vector3.left)
                        {
                            hitPoint    = hitObject.transform.position;
                            hitPoint.x -= baseSize.x;
                            //	snapped = true;
                        }

                        // Check terrain height
                        if (baseOverTerrainThreshold > 0)
                        {
                            float height = Terrain.activeTerrain.SampleHeight(hitPoint) + Terrain.activeTerrain.GetPosition().y;
                            //Debug.Log("Hitpoint y = " + hitPoint.y + " with terrain height: " + height);
                            if (hitPoint.y - baseOverTerrainThreshold > height)
                            {
                                legalPlacement = false;
                                currentClaimObject.HighlightError();
                                Debug.Log("Too high above terrain");
                            }
                        }
                    }
                    currentReticle.transform.position = hitPoint;

                    if (currentClaimObject.autoRotateToHitNormal)
                    {
                        //Rotate the object to match normals
                        Vector3 currentRotation = currentReticle.transform.rotation.eulerAngles;
                        if (hitNormal == Vector3.back)
                        {
                            currentRotation.y = 180;
                            //currentReticle.transform.rotation = Quaternion.AngleAxis(180, Vector3.up);
                            //hitPoint = new Vector3(newX, newY, hitPoint.z);
                        }
                        else if (hitNormal == Vector3.forward)
                        {
                            currentRotation.y = 0;
                        }
                        else if (hitNormal == Vector3.right)
                        {
                            currentRotation.y = 90;
                        }
                        else if (hitNormal == Vector3.left)
                        {
                            currentRotation.y = -90;
                        }
                        Quaternion rotation = Quaternion.Euler(currentRotation);
                        currentReticle.transform.rotation = rotation;
                    }
                }

                if (Input.GetKeyDown(KeyCode.LeftShift))
                {
                    if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical)
                    {
                        mouseWheelBuildMode = MouseWheelBuildMode.Rotate;
#if AT_I2LOC_PRESET
                        ClientAPI.Write(I2.Loc.LocalizationManager.GetTranslation("Changed to Rotate Mode"));
#else
                        ClientAPI.Write("Changed to Rotate Mode");
#endif
                    }
                    else
                    {
                        mouseWheelBuildMode = MouseWheelBuildMode.MoveVertical;
#if AT_I2LOC_PRESET
                        ClientAPI.Write(I2.Loc.LocalizationManager.GetTranslation("Changed to Move Vertical Mode"));
#else
                        ClientAPI.Write("Changed to Move Vertical Mode");
#endif
                    }
                }

                if (Input.GetMouseButtonDown(0) && !AtavismUiSystem.IsMouseOverFrame())
                {
                    if (!legalPlacement)
                    {
                        string[] args = new string[1];
#if AT_I2LOC_PRESET
                        args[0] = I2.Loc.LocalizationManager.GetTranslation("That object cannot be placed there");
#else
                        args[0] = "That object cannot be placed there";
#endif
                        AtavismEventSystem.DispatchEvent("ERROR_MESSAGE", args);
                    }
                    else if (GetBuildingState() == WorldBuildingState.PlaceItem)
                    {
                        int parent = -1;
                        if (currentClaimObject.canHaveParent && hitObject.GetComponentInParent <ClaimObject>() != null)
                        {
                            parent = hitObject.GetComponentInParent <ClaimObject>().ID;
                        }

                        WorldBuilder.Instance.SendPlaceClaimObject(buildTemplate.id, itemBeingPlaced, currentReticle.transform, parent);
                        // Play a coord effect
                        if (placementCoordEffect != null)
                        {
                            Dictionary <string, object> props = new Dictionary <string, object>();
                            props["gameObject"] = ClientAPI.GetPlayerObject().GameObject;
                            CoordinatedEffectSystem.ExecuteCoordinatedEffect(placementCoordEffect.name, props);
                        }

#if AT_I2LOC_PRESET
                        ClientAPI.Write(I2.Loc.LocalizationManager.GetTranslation("Send place claim object message"));
#else
                        ClientAPI.Write("Send place claim object message");
#endif
                        ClearCurrentReticle(true);
                        itemBeingPlaced = null;
                        SetBuildingState(WorldBuildingState.Standard);
                        AtavismCursor.Instance.ClearItemClickedOverride(WorldBuilder.Instance.StartPlaceClaimObject);
                    }
                    else if (GetBuildingState() == WorldBuildingState.MoveItem)
                    {
                        int parent = -1;
                        if (hitObject.GetComponent <ClaimObject>() != null)
                        {
                            parent = hitObject.GetComponent <ClaimObject>().ID;
                        }
                        WorldBuilder.Instance.SendEditObjectPosition(WorldBuilder.Instance.SelectedObject, parent);
                        //SetBuildingState(WorldBuildingState.EditItem);
                        SetBuildingState(WorldBuildingState.Standard);
                        ClearCurrentReticle(false);
                        itemBeingPlaced = null;
                        //objectBeingEdited.GetComponent<ClaimObject>().ResetHighlight();
                        //objectBeingEdited = null;
                    }
                }
            }
        }
Example #3
0
    void DrawEditItemWindow()
    {
        GUI.Box(uiRect, "");
        GUILayout.BeginArea (uiRect, skin.GetStyle ("Window"));
        GUILayout.BeginHorizontal ();
        GUILayout.Label ("Edit Claim Object");
        if (GUILayout.Button("x", GUILayout.Width(20))) {
            SetBuildingState(WorldBuildingState.Standard);
        }
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        GUILayout.Label("Name: " + objectBeingEdited.name);
        GUILayout.EndHorizontal();

        if(GetBuildingState() == WorldBuildingState.MoveItem) {
            if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical) {
                if (GUILayout.Button("Rotate Object", GUILayout.Width(120))) {
                    mouseWheelBuildMode = MouseWheelBuildMode.Rotate;
                }
            } else {
                if (GUILayout.Button("Move Vertically", GUILayout.Width(120))) {
                    mouseWheelBuildMode = MouseWheelBuildMode.MoveVertical;
                }
            }
        } else {
            if (GUILayout.Button("Move", GUILayout.Width(120))) {
                SetCurrentReticle(objectBeingEdited);
                SetBuildingState(WorldBuildingState.MoveItem);
            }
        }
        if (GUILayout.Button("Convert to Item", GUILayout.Width(120))) {
            Dictionary<string, object> props = new Dictionary<string, object>();
            props.Add("action", "convert");
            props.Add("claimID", activeClaim.id);
            props.Add("objectID", objectBeingEdited.GetComponent<ClaimObject>().ID);
            NetworkAPI.SendExtensionMessage(ClientAPI.GetPlayerOid(), false, "voxel.EDIT_CLAIM_OBJECT", props);
            SetBuildingState(WorldBuildingState.Standard);
            ClearCurrentReticle(false);
        }
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Save", GUILayout.Width(120))) {
            Dictionary<string, object> props = new Dictionary<string, object>();
            props.Add("action", "save");
            props.Add("claimID", activeClaim.id);
            props.Add("objectID", objectBeingEdited.GetComponent<ClaimObject>().ID);
            props.Add("loc", objectBeingEdited.transform.position);
            props.Add("orient", objectBeingEdited.transform.rotation);
            NetworkAPI.SendExtensionMessage(ClientAPI.GetPlayerOid(), false, "voxel.EDIT_CLAIM_OBJECT", props);
            SetBuildingState(WorldBuildingState.Standard);
            ClearCurrentReticle(false);
        }

        GUILayout.EndArea();
    }