public bool SetConnections() { Collider2D [] cols = Physics2D.OverlapCircleAll(new Vector2(transform.position.x, transform.position.y), 1.0f); RoadPiece rp = null; foreach (Collider2D cd in cols) { RoadPiece trp = cd.GetComponent <RoadPiece> (); if (trp != null) { rp = trp; } } if (rp == null) { return(true); } WaypointOrientation oppositeOrientation = WaypointOrientation.Bottom; switch (m_orientation) { case WaypointOrientation.Bottom: oppositeOrientation = WaypointOrientation.Top; break; case WaypointOrientation.Left: oppositeOrientation = WaypointOrientation.Right; break; case WaypointOrientation.Right: oppositeOrientation = WaypointOrientation.Left; break; case WaypointOrientation.Top: oppositeOrientation = WaypointOrientation.Bottom; break; } //TODO: fix this to accomodate placing pieces with multiple connections SpawnPoint sp = rp.GetSpawnPoint(oppositeOrientation); if (sp == null) { return(false); } if (sp.IsLinked()) { return(true); } Waypoint[] targetInput; targetInput = sp.getInput(); Waypoint[] targetOutput; targetOutput = sp.getOutput(); foreach (Waypoint wp in m_output) { wp.SetNext(targetInput); } foreach (Waypoint wp in targetOutput) { wp.SetNext(m_input); } Link(sp); sp.Link(this); return(true); }
public RoadPiece PlacePiece(RoadPiece previous) { if (m_OriginalViableRoadPieces == null) { m_OriginalViableRoadPieces = m_viableRoadPieces; } m_viableRoadPieces = m_OriginalViableRoadPieces; List <GameObject> instantiables = AvailablePieceThatFits(); if (instantiables != null) { bool pieceNotPlaced = true; while (pieceNotPlaced && instantiables.Count > 0) { //TODO, prevent double selection int choice = Random.Range(0, instantiables.Count); GameObject go = GameObject.Instantiate(instantiables[choice]); instantiables.RemoveAt(choice); go.transform.position = transform.position; WaypointOrientation oppositeOrientation = WaypointOrientation.Bottom; switch (m_orientation) { case WaypointOrientation.Bottom: oppositeOrientation = WaypointOrientation.Top; break; case WaypointOrientation.Left: oppositeOrientation = WaypointOrientation.Right; break; case WaypointOrientation.Right: oppositeOrientation = WaypointOrientation.Left; break; case WaypointOrientation.Top: oppositeOrientation = WaypointOrientation.Bottom; break; } //TODO: fix this to accomodate placing pieces with multiple connections RoadPiece rp = go.GetComponent <RoadPiece> (); SpawnPoint sp = rp.GetSpawnPoint(oppositeOrientation); //TODO: link up all of the road pieces Waypoint[] targetInput; targetInput = sp.getInput(); Waypoint[] targetOutput; targetOutput = sp.getOutput(); Link(sp); sp.Link(this); foreach (Waypoint wp in m_output) { wp.SetNext(targetInput); } foreach (Waypoint wp in targetOutput) { wp.SetNext(m_input); } go.GetComponent <RoadPiece> ().Instantiate(previous); bool works = true; foreach (SpawnPoint targetSpawnPoints in rp.SpawnPoints()) { if (targetSpawnPoints.IsColliding()) { if (targetSpawnPoints.SetConnections() == false) { works = false; } } } if (ZoneCheck(rp) == false) { works = false; } if (works == false) { rp.KillMe(); } else { pieceNotPlaced = false; return(rp); } } return(null); } else { return(null); } }