Ejemplo n.º 1
0
    public bool ConnectPartOnGrid(GrabbablePart otherPart, PhysicalConnectionType connectionType)
    {
        if (connectionType == PhysicalConnectionType.None)
        {
//			Debug.Log("Not Connecting: "+this.idNumber+" : "+otherPart.idNumber);
            return(false);
        }

//		Debug.Log("Connecting: "+this.idNumber+" : "+otherPart.idNumber);

//		HexMetrics.Direction directionToOther;
        for (int i = 0; i < 6; i++)
        {
            HexMetrics.Direction iDir             = (HexMetrics.Direction)i;
            IntVector2           relativeLocation = HexMetrics.GetGridOffset(iDir);

//			Debug.Log (GetGridLocationFromPosition().ToString() +" : "+otherPart.GetGridLocationFromPosition().ToString() + " + "+relativeLocation.ToString() + " = "+ (otherPart.GetGridLocationFromPosition() + relativeLocation).ToString());
            if (GetGridLocationFromPosition().IsEqualTo(otherPart.GetGridLocationFromPosition() - relativeLocation))
            {
                HexMetrics.Direction relativeDirection = Relative(iDir);
                int r = (int)relativeDirection;
//				Debug.Log ("Found Direction A: "+iDir+", R: "+relativeDirection);
                if (_connectedParts[r] != null && _connectedParts[r].connectedPart != null)
                {
                    // if this happens twice in a step, remember that it is actually happening over 2 steps.
                    // It weld at the beginning of each step, i.e. as it comes into the welder and as it leaves
//					Debug.Log("Connecting part "+otherPart.idNumber+" to a direction that is already connected (connected to "+_connectedParts[r].connectedPart.idNumber+")");
                    return(false);
                }
//				otherPart.gameObject.transform.parent = gameObject.transform;

                if (IsWeldable(relativeDirection, otherPart))
                {
                    if (ParentConstruction != null)
                    {
                        ParentConstruction.AddToConstruction(otherPart);
                    }

                    _connectedParts[r].Reset();
                    _connectedParts[r].connectedPart = otherPart;
                    HexMetrics.Direction oppositeDirection = ConnectedsOpposite(relativeDirection);
                    otherPart._connectedParts[(int)oppositeDirection].Reset();
                    otherPart._connectedParts[(int)oppositeDirection].connectedPart = this;
                    SetPhysicalConnection(relativeDirection, connectionType);
                    return(true);
                }
            }
        }

        return(false);
    }
Ejemplo n.º 2
0
    public HashSet <Construction> SetSimulationOrientation(HexMetrics.Direction orientation)
    {
        for (int i = 0; i < 6; i++)
        {
            if (_connectedParts[i] == null)
            {
                _connectedParts[i] = new ConnectionDescription();
            }
        }

//			adjacent.ForEach((obj) => obj.parent = null);
        int directionChange = ((int)orientation - (int)SimulationOrientation + 6) % 6;

        transform.rotation = Quaternion.Euler(0, 0, (int)orientation * -60);

        ConnectionDescription []  newParts            = new ConnectionDescription [6];
        PhysicalConnectionType [] physicalConnections = new PhysicalConnectionType [6];
        int [] auxilaryConnections = new int [6];

//			adjacent.ForEach((obj) => obj.parent = transform);

        for (int i = 0; i < 6; i++)
        {
            int newDirection = (i + directionChange) % 6;
            newParts[i] = _connectedParts[newDirection];

            physicalConnections[i] = _connectedParts[newDirection].connectionType;
            auxilaryConnections[i] = _connectedParts[newDirection].auxConnectionTypes;
        }
        for (int i = 0; i < 6; i++)
        {
            int newDirection = (i + directionChange) % 6;
            _connectedParts[i] = newParts[i];

            if (_connectedParts[i].connectedPart == null)
            {
                physicalConnections[i] = PhysicalConnectionType.None;
                auxilaryConnections[i] = 0;
            }
//			Debug.Log ("Replacing Connection ("+(HexMetrics.Direction)newDirection+") "+ _connectedParts[i].connectionType +" -> ("+(HexMetrics.Direction)i+") " + physicalConnections[i]);
            SetPhysicalConnection((HexMetrics.Direction)i, physicalConnections[i], SplitOptions.DoNotSplit);
            SetAuxilaryConnections((HexMetrics.Direction)i, auxilaryConnections[i]);
        }

        if (ParentConstruction != null)
        {
            return(ParentConstruction.CheckForSplitsOrJoins());
        }
        return(new HashSet <Construction>());
    }
Ejemplo n.º 3
0
//	public void ConnectPartAndPlaceAtAbsoluteDirection(GrabbablePart otherPart, PhysicalConnectionType connectionType, HexMetrics.Direction absoluteDirection)
//	{
//		ConnectPartAndPlaceAtRelativeDirection(otherPart, connectionType, Relative(absoluteDirection));
//	}

    public void ConnectPartAndPlaceAtRelativeDirection(GrabbablePart otherPart, PhysicalConnectionType connectionType, HexMetrics.Direction relativeDirection)
    {
        HexMetrics.Direction  absoluteDirection = Absolute(relativeDirection);
        ConnectionDescription connDesc          = _connectedParts[(int)relativeDirection];

        connDesc.connectedPart = otherPart;                                                                          // connect the part

        ConnectionDescription otherConnDesc = otherPart._connectedParts[(int)ConnectedsOpposite(relativeDirection)]; // get the other parts opposite side that is connected to this

        otherConnDesc.connectedPart = this;                                                                          // connect that side

        connDesc.connectedPart.transform.position = transform.position + (Vector3)GameSettings.instance.hexCellPrefab.GetDirection(absoluteDirection);

        SetPhysicalConnection(relativeDirection, connectionType);

        if (ParentConstruction != null)
        {
//			Debug.Log("Adding to construction "+ParentConstruction.name);
            ParentConstruction.AddToConstruction(otherPart);
        }
    }
Ejemplo n.º 4
0
    public HashSet <Construction> SetPhysicalConnection(HexMetrics.Direction relativeDirection,
                                                        PhysicalConnectionType newConnectionType,
                                                        SplitOptions splitOption)
    {
        ConnectionDescription connDesc = _connectedParts[(int)relativeDirection];

//		PhysicalConnectionType originalConnection = connDesc.connectionType;
        if (connDesc.connectedPart == null)
        {
            // disconnecting part
            connDesc.connectionType = PhysicalConnectionType.None;
            SetWeldSprite(relativeDirection, false);

            if (ParentConstruction != null)
            {
                if (splitOption == SplitOptions.SplitIfNecessary)
                {
                    return(ParentConstruction.CheckForSplitsOrJoins());
                }
                return(new HashSet <Construction> {
                    ParentConstruction
                });
            }

            return(new HashSet <Construction> ());
        }

        HexMetrics.Direction  oppositeDirection = ConnectedsOpposite(relativeDirection);
        ConnectionDescription otherConnDesc     = connDesc.connectedPart._connectedParts[(int)oppositeDirection];

        bool weldableHere  = Weldable(relativeDirection);
        bool weldableThere = connDesc.connectedPart.Weldable(oppositeDirection);

//		Debug.Log("Checking weldability: "+direction+"("+weldableHere+") <-> "+oppositeDirection+"("+weldabelThere+")");
        if (weldableHere && weldableThere)
        {
            connDesc.connectionType      = newConnectionType;
            otherConnDesc.connectionType = newConnectionType;
        }
        else
        {
            connDesc.connectionType      = PhysicalConnectionType.None;
            otherConnDesc.connectionType = PhysicalConnectionType.None;
        }

        // update the weld sprites
        SetWeldSprite(relativeDirection, connDesc.connectionType != PhysicalConnectionType.None);
        connDesc.connectedPart.SetWeldSprite(oppositeDirection, connDesc.connectionType != PhysicalConnectionType.None);


        //if we are disconnecting the side (None) or if it's not weldable, make sure that the parts are not connected
        if (connDesc.connectionType == PhysicalConnectionType.None)
        {
            otherConnDesc.connectedPart = null;
            connDesc.connectedPart      = null;

            if (ParentConstruction == null)
            {
                Debug.LogWarning("Parent is null!");
            }
            else
            {
                // check that by disconnecting a side, we have not split the construction up
                if (splitOption == SplitOptions.SplitIfNecessary)
                {
                    return(ParentConstruction.CheckForSplitsOrJoins());
                }
                return(new HashSet <Construction> {
                    ParentConstruction
                });
            }
        }
        if (ParentConstruction != null)
        {
            return(new HashSet <Construction> {
                ParentConstruction
            });
        }
        return(new HashSet <Construction>());
    }