Ejemplo n.º 1
0
        void HandleMouseDrag(ref ToolBase.RaycastOutput raycastOutput, ref ToolError error, bool test, ref NetInfo newRoadPrefab, ref int newSegmentIndex)
        {
            Vector3 hitPosDelta = Vector3.zero;

            if (!test)
            {
                if (currentTime - prevRebuildTime < 0.1f)
                {
                    return;
                }

                if (dragging)
                {
                    hitPosDelta = raycastOutput.m_hitPos - prevHitPos;
                    if (hitPosDelta.magnitude < 12.0f)
                    {
                        return;
                    }
                }
                else
                {
                    prevHitPos = raycastOutput.m_hitPos;
                    dragging   = true;
                    if (toolMode == ToolMode.Oneway)
                    {
                        return;
                    }
                }

                prevHitPos = raycastOutput.m_hitPos;
            }

            int segmentIndex = raycastOutput.m_netSegment;

            if (segmentIndex != 0)
            {
                NetManager net    = Singleton <NetManager> .instance;
                NetInfo    prefab = net.m_segments.m_buffer[segmentIndex].Info;

                bool isOneway = !prefab.m_hasForwardVehicleLanes || !prefab.m_hasBackwardVehicleLanes;


                string prefabName = null;
                if (!roadPrefabNames.TryGetValue(prefab.GetInstanceID(), out prefabName) || prefabName == null)
                {
                    ModDebug.Error("Prefab name not found");
                    error = ToolError.Unknown;
                    return;
                }

                string newPrefabName = null;
                if (toolMode == ToolMode.Oneway)
                {
                    if (!isOneway)
                    {
                        newPrefabName = FindMatchingName(prefabName, twowayNames, onewayNames);
                    }
                    else
                    {
                        newPrefabName = prefabName;
                    }
                }
                else
                {
                    if (isOneway)
                    {
                        newPrefabName = FindMatchingName(prefabName, onewayNames, twowayNames);
                    }
                    else
                    {
                        toolError = ToolError.AlreadyTwoway;
                        return;
                    }
                }

                if (newPrefabName != null)
                {
                    if (test)
                    {
                        toolError = ToolError.None;
                        return;
                    }

                    NetInfo newPrefab;
                    if (!roadPrefabs.TryGetValue(newPrefabName, out newPrefab) || newPrefab == null)
                    {
                        ModDebug.Error("Prefab not found: " + newPrefabName);
                        error = ToolError.Unknown;
                        return;
                    }

                    newRoadPrefab = newPrefab;

                    int newIndex = RebuildSegment(segmentIndex, newPrefab, toolMode == ToolMode.Oneway, raycastOutput.m_hitPos, hitPosDelta, ref error);

                    if (newIndex != 0)
                    {
                        if (error != ToolError.None)
                        {
                            return;
                        }

                        prevBuiltSegmentIndex = newSegmentIndex;
                        prevRebuildTime       = currentTime;
                        newSegmentIndex       = newIndex;
                    }
                }
                else
                {
                    toolError = ToolError.CannotUpgradeThisType;
                    return;
                }
            }
        }
        public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
        {
            ModDebug.LogClassAndMethodName(this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name);
            if (loadingLevel)
            {
                return;
            }

            if (Input.GetKeyDown(KeyCode.Return))
            {
                ModDebug.Log(netTool.m_prefab + " " + netTool.m_mode);
            }

            if (roadsPanel == null)
            {
                roadsPanel = UIView.Find <UIPanel>("RoadsPanel");
            }

            if (roadsPanel == null || !roadsPanel.isVisible)
            {
                if (toolMode != ToolMode.None)
                {
                    ModDebug.Log("Roads panel no longer visible");
                    SetToolMode(ToolMode.None, true);
                }
                return;
            }

            if (netTool == null)
            {
                foreach (var tool in ToolsModifierControl.toolController.Tools)
                {
                    NetTool nt = tool as NetTool;
                    if (nt != null && nt.m_prefab != null)
                    {
                        ModDebug.Log("NetTool found: " + nt.name);
                        netTool = nt;
                        break;
                    }
                }

                if (netTool == null)
                {
                    return;
                }

                raycastService = new ToolBase.RaycastService(netTool.m_prefab.m_class.m_service, netTool.m_prefab.m_class.m_subService, netTool.m_prefab.m_class.m_layer);

                ModDebug.Log("UI visible: " + ui.isVisible);
            }

            if (!ui.isVisible)
            {
                ui.Show();
            }

            if (toolMode != ToolMode.None)
            {
                mouseDown      = Input.GetMouseButton(0);
                mouseRayValid  = !ToolsModifierControl.toolController.IsInsideUI && Cursor.visible;
                mouseRay       = Camera.main.ScreenPointToRay(Input.mousePosition);
                mouseRayLength = Camera.main.farClipPlane;
                currentTime    = Time.time;

                if (ToolsModifierControl.toolController.CurrentTool != buildTool)
                {
                    ModDebug.Log("Another tool selected");
                    SetToolMode(ToolMode.None);
                }
            }
            else
            {
                ui.toolMode = ToolMode.None;

                if (ToolsModifierControl.toolController.CurrentTool == buildTool)
                {
                    ToolsModifierControl.toolController.CurrentTool = netTool;
                }
            }

            //ModDebug.LogClassAndMethodName(this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name);

            if (toolMode == ToolMode.None)
            {
                return;
            }

            if (!mouseDown)
            {
                prevBuiltSegmentIndex = 0;
            }

            if (buildTool != null)
            {
                buildTool.isHoveringSegment = false;
                buildTool.toolMode          = toolMode;
                buildTool.ToolCursor        = netTool.m_upgradeCursor;
            }

            if (!mouseRayValid)
            {
                return;
            }

            ToolBase.RaycastInput raycastInput = new ToolBase.RaycastInput(mouseRay, mouseRayLength);
            raycastInput.m_netService         = raycastService;
            raycastInput.m_ignoreTerrain      = true;
            raycastInput.m_ignoreNodeFlags    = NetNode.Flags.All;
            raycastInput.m_ignoreSegmentFlags = NetSegment.Flags.Untouchable;

            ToolBase.RaycastOutput raycastOutput;

            if (BuildTool21.RayCast(raycastInput, out raycastOutput))
            {
                int segmentIndex = raycastOutput.m_netSegment;
                if (segmentIndex != 0)
                {
                    NetManager net           = Singleton <NetManager> .instance;
                    NetInfo    newRoadPrefab = null;
                    NetInfo    prefab        = net.m_segments.m_buffer[segmentIndex].Info;

                    NetTool.ControlPoint startPoint;
                    NetTool.ControlPoint middlePoint;
                    NetTool.ControlPoint endPoint;

                    GetSegmentControlPoints(segmentIndex, out startPoint, out middlePoint, out endPoint);

                    ushort node;
                    ushort outSegment;
                    int    cost;
                    int    productionRate;
                    // Check for out-of-area error and initialized collide arrays for visualization
                    ToolBase.ToolErrors errors = NetTool.CreateNode(net.m_segments.m_buffer[segmentIndex].Info,
                                                                    startPoint, middlePoint, endPoint, NetTool.m_nodePositionsSimulation, 1000,
                                                                    true, false, true, false, false, false, (ushort)0, out node, out outSegment, out cost, out productionRate);

                    if ((errors & ToolBase.ToolErrors.OutOfArea) != 0)
                    {
                        toolError = ToolError.OutOfArea;
                    }

                    string prefabName = null;
                    if (!roadPrefabNames.TryGetValue(prefab.GetInstanceID(), out prefabName) || prefabName == null)
                    {
                        ModDebug.Error("Prefab name not found");
                        toolError = ToolError.Unknown;
                        return;
                    }

                    NetInfo newPrefab;
                    if (!roadPrefabs.TryGetValue(prefabName, out newPrefab) || newPrefab == null)
                    {
                        ModDebug.Error("Prefab not found: " + prefabName);
                        toolError = ToolError.Unknown;
                        return;
                    }

                    newRoadPrefab = newPrefab;

                    //  ModDebug.Log("Going to rebuild segment");
                    int newIndex = RebuildSegment(segmentIndex, newPrefab, raycastOutput.m_hitPos, hitPosDelta, ref toolError);

                    if (newIndex != 0)
                    {
                        //    ModDebug.Log("newIndex: " + newIndex);
                        if (toolError != ToolError.None)
                        {
                            return;
                        }

                        prevBuiltSegmentIndex = segmentIndex;
                        prevRebuildTime       = currentTime;
                        segmentIndex          = newIndex;
                    }

                    if (buildTool != null)
                    {
                        // ModDebug.Log("Using segment from buffer");
                        buildTool.segment           = net.m_segments.m_buffer[segmentIndex];
                        buildTool.segmentIndex      = segmentIndex;
                        buildTool.isHoveringSegment = toolError != ToolError.Unknown;
                        if (newRoadPrefab != null)
                        {
                            buildTool.newPrefab = newRoadPrefab;
                        }
                        GetSegmentControlPoints(segmentIndex, out buildTool.startPoint, out buildTool.middlePoint, out buildTool.endPoint);
                    }
                }
            }

            if (buildTool != null)
            {
                buildTool.toolError = toolError;
            }
        }