Ejemplo n.º 1
0
    void LineEndFollowMouse(LineRenderer lineR)
    {
        var distance = (transform.position - Mouse_Controller.instance.mousePosition).sqrMagnitude;

        // Set the second position to the mouse but limited to the max wire length
        if (distance <= maxWireLength)
        {
            lineR.SetPosition(1, Mouse_Controller.instance.mousePosition);
        }


        if (Input.GetKeyDown(KeyCode.Space))
        {
            // This cancels a connection by pooling the connection game object (lineR)
            isConnecting = false;
            ObjectPool.instance.PoolObject(lineR.gameObject);
        }

        if (Input.GetMouseButtonDown(0) && mouse.GetTileUnderMouse().tileType == TileType.CITY)
        {
            // CONNECTION HAS BEEN MADE!
            // Charge the Money Manager for its wire purchase. Later I will add a wire type (basic, advanced, etc.) that will have a different cost and different features.

            if (Money_Manager.instance.Purchase("Basic Connection"))
            {
                Tile curTile = mouse.GetTileUnderMouse();
                if (curTile != null)
                {
                    if (curTile.tileType == TileType.CITY)
                    {
                        // Connecting to a City Behavior:
                        Connection newConnect = new Connection(Mathf.RoundToInt(mouse.mousePosition.x), curChoseConnectionType,
                                                               curChosenConnectionMode, Connector.City, myPole, 1, 30f);

                        // All input connections should know their source! and all Output connections should know their input's source!
                        if (newConnect.connectionMode == ConnectionMode.INPUT)
                        {
                            newConnect.sourceCity = world.GetCityFromTilePosX(curTile.posX);
                            //Debug.Log("POLE: Source city set to " + newConnect.sourceCity.name);
                        }
                        else
                        {
                            newConnect.outputCity = world.GetCityFromTilePosX(curTile.posX);
                            //Debug.Log("POLE: Output city set to " + newConnect.outputCity.name);
                        }

                        //Debug.Log("POLE: Made a new " + newConnect.connectionMode + " connection of type " + newConnect.connectionType);


                        isConnecting = false;

                        // Here we have to tell the city handler at the end of the connection that it's receiving a new connnection of this type from this pole
                        Guid id = Guid.NewGuid();

                        //Debug.Log("New ID = " + id);

                        world.GameObjectFromTileXCoord(curTile.posX).GetComponent <City_Handler>().AddNewConnection(id, newConnect);

                        // Register the connection with this pole to keep track of its current connections
                        RegisterConnectionFromMe(id, newConnect);

                        // Set this new connection's Connection Handler
                        connectionGObj.GetComponent <Connection_Handler>().Init(id, newConnect);

                        /*
                         * // THIS LINE BELOW, will cause the connection to stick to a position in the center of the city sprite.
                         * // Could FIX THIS to make the connections look tidier but they can't all connect to the same Vector3, if they do they look like one
                         * // continous line instead of multiple connections.
                         *
                         * lineR.SetPosition(1, world.GetCityFromTilePosX(curTile.posX).connectorPosition);
                         */
                    }
                    else if (curTile.tileType == TileType.POLE)
                    {
                        // Connecting to a Pole Behavior:
                        Connection newConnect = new Connection(Mathf.RoundToInt(mouse.mousePosition.x), curChoseConnectionType,
                                                               curChosenConnectionMode, Connector.Pole, myPole, 1, 30f);

                        // All input connections need its source. In this case, with a new Pole to Pole connection, find the corresponding input connection on this Pole
                        if (newConnect.connectionMode == ConnectionMode.INPUT)
                        {
                            if (inputConnections.ContainsKey(newConnect.connectionType))
                            {
                                newConnect.sourceCity = inputConnections[newConnect.connectionType].sourceCity;
                            }
                        }
                        else
                        {
                            // check this pole for outputs. There's a big chance that the player hasn't set any outputs yet of this type so this will need to be check again!
                            // Check the connector poles outputs
                            if (world.GameObjectFromTileXCoord(curTile.posX).GetComponent <Pole_Handler>().outputConnections.
                                ContainsKey(newConnect.connectionType))
                            {
                                newConnect.outputCity = world.GameObjectFromTileXCoord(curTile.posX).GetComponent <Pole_Handler>().
                                                        outputConnections[newConnect.connectionType].outputCity;
                            }
                        }

                        //Debug.Log("POLE: Made a new " + newConnect.connectionMode + " connection of type " + newConnect.connectionType);

                        isConnecting = false;

                        Guid id = Guid.NewGuid();


                        // Register the connection with this pole to keep track of its current connections
                        RegisterConnectionFromMe(id, newConnect);

                        // Set this new connection's Connection Handler
                        connectionGObj.GetComponent <Connection_Handler>().Init(id, newConnect);

                        // Register the connection on the pole on the end of this connection (reverse the connection mode!)
                        world.GameObjectFromTileXCoord(curTile.posX).GetComponent <Pole_Handler>().RegisterConnectionToMe(id, newConnect);
                    }
                }
            }
            else
            {
                // Money Manager doesn't have enough capital to make this connection purchase
                isConnecting = false;
                // Pool the connection since it was a fail
                ObjectPool.instance.PoolObject(lineR.gameObject);
            }
        }
    }
Ejemplo n.º 2
0
    void Update()
    {
        TrackMousePosition();

        TrackMiddleClickDrag();

        lastFramePosition   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        lastFramePosition.z = 0;

        if (Input.GetMouseButtonDown(1))
        {
            Tile curTile = GetTileUnderMouse();

            if (curTile != null)
            {
                if (buildMode == BuildMode.BUILD_POLE)
                {
                    if (curTile.tileType == TileType.EMPTY)
                    {
                        PlacePole();
                    }
                    else if (curTile.tileType == TileType.POLE)
                    {
                        ActivatePole(curTile.posX);
                    }
                }
                else if (buildMode == BuildMode.NONE)
                {
                    if (curTile.tileType == TileType.CITY)
                    {
                        Cities_Manager.instance.DisplayCityInfo(curTile.posX);
                        // Center the camera on this city that we are displaying info for
                        //Vector3 center = lastFramePosition - curMousePosition;
                        //center.y = 0;
                        //Camera.main.transform.Translate(center);
                    }
                }
                else
                {
                    // DESTROY Poles: ( By pooling them )
                    if (curTile.tileType == TileType.POLE)
                    {
                        if (world.GameObjectFromTileXCoord(curTile.posX) != null)
                        {
                            ObjectPool.instance.PoolObject(world.GameObjectFromTileXCoord(curTile.posX));
                        }

                        // Remove from tiles grid
                        world.RemoveFromGrid(curTile.posX);
                    }

                    // Always after attempting any Destruction, reset the build mode to None
                    buildMode = BuildMode.NONE;
                }
            }
            else
            {
                Debug.Log("MOUSE: Can't place a pole! There's NO TILE! Are you checking a negative coordinate?");
            }
        }


        mousePosition = curMousePosition;
    }