Example #1
0
    private IEnumerable <LocatedPart> GetAllConnectedPartsWithLocation(IntVector2 thisLocation, HashSet <GrabbablePart> exploredParts)
    {
        if (exploredParts.Contains(this))
        {
            yield break;
        }
        exploredParts.Add(this);

        yield return(new LocatedPart(this, thisLocation));

//		Debug.Log ("adding "+thisLocation.x+":"+thisLocation.y);

        for (int i = 0; i < 6; i++)
        {
            GrabbablePart connectedPart = _connectedParts[i].connectedPart;
            if (connectedPart != null && _connectedParts[i].connectionType != PhysicalConnectionType.None)
            {
                HexMetrics.Direction relativeDirection = (HexMetrics.Direction)i;
                HexMetrics.Direction absoluteDirection = AbsoluteDirectionFromRelative(relativeDirection);
                IntVector2           newLocation       = HexMetrics.GetGridOffset(absoluteDirection) + thisLocation;
//				Debug.Log (thisLocation.x+":"+thisLocation.y+" -> "+newLocation.x+":"+newLocation.y+" (A:"+absoluteDirection+", R:"+relativeDirection+")");
                foreach (LocatedPart loactedPart in connectedPart.GetAllConnectedPartsWithLocation(newLocation, exploredParts))
                {
                    yield return(loactedPart);
                }
            }
        }
    }
    public override bool PlaceAtLocation(IntVector2 location)
    {
        Location = location;
        if (location != null)
        {
            hexCell     = GridManager.instance.GetHexCell(location);
            leftBelow   = GridManager.instance.GetHexCell(location + HexMetrics.GetGridOffset(HexMetrics.Direction.LeftDown));
            centerBelow = GridManager.instance.GetHexCell(location + HexMetrics.GetGridOffset(HexMetrics.Direction.Down));
            rightBelow  = GridManager.instance.GetHexCell(location + HexMetrics.GetGridOffset(HexMetrics.Direction.RightDown));

            if (hexCell != null && leftBelow != null && centerBelow != null && rightBelow != null)
            {
                foreach (HexCell hc in new HexCell [4] {
                    hexCell, leftBelow, centerBelow, rightBelow
                })
                {
                    if (hc.placedPlaceable != null)
                    {
                        return(false);
                    }
                }

                transform.position = hexCell.transform.position;
                foreach (HexCell hc in new HexCell [4] {
                    hexCell, leftBelow, centerBelow, rightBelow
                })
                {
                    hc.placedPlaceable = this;
                }

                return(true);
            }
        }
        else
        {
            if (hexCell != null && leftBelow != null && centerBelow != null && rightBelow != null)
            {
//				if (hexCell.placedPlaceable == this)
//				{
//					hexCell.placedPlaceable = null;
//				}
                foreach (HexCell hc in new HexCell [4] {
                    hexCell, leftBelow, centerBelow, rightBelow
                })
                {
                    if (hc.placedPlaceable == this)
                    {
                        hc.placedPlaceable = null;
                    }
                }
                hexCell     = null;
                leftBelow   = null;
                centerBelow = null;
                rightBelow  = null;
            }
        }

        return(false);
    }
Example #3
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);
    }
Example #4
0
 IntVector2 LocationAtClamp()
 {
     return(Location + HexMetrics.GetGridOffset(_startStepState.direction) * (_startStepState.extention + 1));
 }