Beispiel #1
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;
                    }
                }
            }
        }