public void Instantiate(RoadPiece prev) { m_previous = prev; next = new List <RoadPiece> (); if (transform.position.x < MAX_HEIGHT && transform.position.x >= 0 && transform.position.y < MAX_WIDTH && transform.position.y >= MIN_WIDTH) { m_locked = true; //Instantiate road pieces at all of this road's spawn points foreach (SpawnPoint spawnPoint in m_spawnPoints) { AddNext(spawnPoint.Spawn(this)); } StartCoroutine(Regenerate()); //Initial Spawn foreach (SpawnPoint spawnPoint in m_spawnPoints) { if (spawnPoint.IsLinked() == false) { spawnPoint.OneTimeSpawn(); } } } else { foreach (SpawnPoint spawnPoint in m_spawnPoints) { if (spawnPoint.IsLinked() == false) { spawnPoint.CreateCarSpawns(); } } m_terminates = true; } }
void OnCollisionEnter(Collision collision) { if (collision.collider.tag.Equals("Road")) { RoadPiece rp = collision.collider.GetComponent <RoadPiece> (); // print(collision.collider.tag + " " + rp.PieceNumber); if ((rp.PieceNumber != LastPiece) && (rp.PieceNumber == CurrentPiece + 1 || (MovingForward && rp.PieceNumber == 0))) { LastPiece = CurrentPiece; if (CurrentPiece == 10) { passedWaypoint = true; } CurrentPiece = rp.PieceNumber; MovingForward = true; } else { MovingForward = false; } if (rp.PieceNumber == 0) { CurrentPiece = 0; NewLap(); } } else if (collision.collider.tag.Equals("Wall")) { WallHits++; } }
bool OnRoadCheck() { Debug.DrawRay(rayDownPos.position, -transform.up * rayDownDist, Color.blue); Physics.Raycast(rayDownPos.position, -transform.up, out hitDown, rayDownDist); //print("pos: " + transform.position + " |pos + ofset: " + (transform.position + offsetDown) + " |down: " + -transform.up); //print("roadcheck: " + hitDown.transform.root.name); if (hitDown.transform.root.GetComponent <RoadPiece>() != null) { roadPoint = hitDown.transform.root.GetComponent <RoadPiece>(); if (hitDown.transform.root.GetComponent <RoadPiece>().roadType == RoadType.booster) { curSpeed = boosterSpeed; //print("booster speed"); } else { curSpeed = movespeedRoad; //print("normal speed"); } return(true); } else { curSpeed = movespeedMud; //print("mud speed"); return(false); } }
public void SetRoadTile(int x, int y, RoadPiece value) { if (x < 0 || x >= size.x || y < 0 || y >= size.y) { return; } roads[x, y] = value; }
// This is only to see if the cars are advancing from a road segment to another. For collisions // we want "OnCollisionStay" void OnCollisionEnter(Collision collision) { if (collision.collider.tag.Equals("Road")) { // We access the script of the new road piece (so we can access its ID) RoadPiece road_piece = collision.collider.GetComponent <RoadPiece>(); if (road_piece.PieceNumber > current_road_piece) { ++advanced; } else { --advanced; } // Special situation: from start (0) backwards (last piece, 35) or vice versa if (road_piece.PieceNumber == 0 && current_road_piece == last_piece) { // We are actually advancing! (Add 1 and correct the previous -1, // because 0 < last_piece) advanced += 2; } // From first to last (we check 0 AND 1 to prevent evolution from // spamming time-precision glitches) else if (road_piece.PieceNumber == last_piece && (current_road_piece == 0 || current_road_piece == 1)) { // We are actually going in reverse! Extra penalty if done at the start. advanced -= 3; } current_road_piece = road_piece.PieceNumber; // If we have to determine the result of a junction, and we are // outside of the junction... if (isDirectionPending && road_piece.roadType != DirectionTaken.isJunction) { if (road_piece.roadType != expectedDirection) { //Debug.Log("APPLY PENALTY"); // Take into account that in a simple track with 2 real // junctions and 2 easy ones (where two roads meet) a // car going "always right" would get 3 right and 1 wrong, // but we need this to be counted as a mistake, so 1 penalty // must, at least, make up for 3 rewards. junctions_count -= 8; } else { //Debug.Log("APPLY REWARD"); ++junctions_count; } isDirectionPending = false; } } }
// Use this for initialization public RoadPiece Spawn(RoadPiece previous) { if (IsClear()) { return(PlacePiece(previous)); } else { return(null); } }
private void ToggleObstacles(GameObject roadPiece) { RoadPiece piece = roadPiece.GetComponent <RoadPiece>(); int finalObstacleCount = ObstacleCount + Random.Range(-ObstacleCountLowerDeviation, ObstacleCountUpperDeviation); for (int i = 0; i < finalObstacleCount; i++) { int index = Random.Range(0, piece.Obstacles.Count); piece.Obstacles[index].SetActive(true); piece.Obstacles[index].transform.rotation = Quaternion.AngleAxis(Random.Range(0, 180), Vector3.up); } }
public bool IsColliding() { Collider2D [] cols = Physics2D.OverlapCircleAll(new Vector2(transform.position.x, transform.position.y), 1.0f); foreach (Collider2D cd in cols) { RoadPiece rp = cd.GetComponent <RoadPiece> (); if (rp != null) { return(true); } } return(false); }
void OnCollisionEnter(Collision collision) { if (collision.collider.tag.Equals("Road")) { RoadPiece rp = collision.collider.GetComponent <RoadPiece>(); // print(collision.collider.tag + " " + rp.PieceNumber); roadNumber = rp.PieceNumber; } if (collision.collider.tag.Equals("Wall")) { Destroy(gameObject); } }
public void Restart() { entryPiece.gameObject.SetActive(true); // Debug.Log (currentPieces.Count); for (int i = 0; i < currentPieces.Count; i++) { Debug.Log("destroy " + currentPieces [i].name); Destroy(currentPieces [i].gameObject); } currentPieces = new List <RoadPiece> (); entryPiece.roadMaker = this; currentPiece = entryPiece; nextPiece = Instantiate(availablePieces [0], currentPiece.nextPieceLocation.position, currentPiece.nextPieceLocation.rotation, transform); nextPiece.roadMaker = this; currentPieces.Add(nextPiece); }
public void MakeNewPiece() { if (previousPiece != null) { if (!previousPiece.entryPiece) { currentPieces.Remove(previousPiece); Destroy(previousPiece.gameObject); } else { previousPiece.gameObject.SetActive(false); } } previousPiece = currentPiece; currentPiece = nextPiece; nextPiece = Instantiate(availablePieces [Random.Range(0, availablePieces.Length)], nextPiece.nextPieceLocation.position, nextPiece.nextPieceLocation.rotation, transform); nextPiece.roadMaker = this; currentPieces.Add(nextPiece); // Debug.Log ("Creating new piece"); }
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 bool ZoneCheck(RoadPiece rp) { Collider2D [] cols = Physics2D.OverlapCircleAll(new Vector2(rp.transform.position.x, rp.transform.position.y + 10.2f), RADAR_CHECK_SIZE); rp.AddGizmo(new Vector3(rp.transform.position.x, rp.transform.position.y + 10.2f, 0)); RoadPiece TopRp = null; foreach (Collider2D cd in cols) { RoadPiece trp = cd.GetComponent <RoadPiece> (); if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponentInChildren <RoadPiece> (); } if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponent <RoadPiece> (); } if (trp != null) { TopRp = trp; } } if (TopRp != null) { if (TopRp.GetSpawnPoint(WaypointOrientation.Bottom) != null && rp.GetSpawnPoint(WaypointOrientation.Top) == null) { return(false); } else if (TopRp.GetSpawnPoint(WaypointOrientation.Bottom) == null && rp.GetSpawnPoint(WaypointOrientation.Top) != null) { return(false); } } cols = Physics2D.OverlapCircleAll(new Vector2(rp.transform.position.x - 10.2f, rp.transform.position.y), RADAR_CHECK_SIZE); rp.AddGizmo(new Vector3(rp.transform.position.x - 10.2f, rp.transform.position.y, 0)); RoadPiece LeftRp = null; foreach (Collider2D cd in cols) { RoadPiece trp = cd.GetComponent <RoadPiece> (); if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponentInChildren <RoadPiece> (); } if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponent <RoadPiece> (); } if (trp != null) { LeftRp = trp; } } if (LeftRp != null) { if (LeftRp.GetSpawnPoint(WaypointOrientation.Right) != null && rp.GetSpawnPoint(WaypointOrientation.Left) == null) { return(false); } else if (LeftRp.GetSpawnPoint(WaypointOrientation.Right) == null && rp.GetSpawnPoint(WaypointOrientation.Left) != null) { return(false); } } cols = Physics2D.OverlapCircleAll(new Vector2(rp.transform.position.x + 10.2f, rp.transform.position.y), RADAR_CHECK_SIZE); rp.AddGizmo(new Vector3(rp.transform.position.x + 10.2f, rp.transform.position.y, 0)); RoadPiece RightRp = null; foreach (Collider2D cd in cols) { RoadPiece trp = cd.GetComponent <RoadPiece> (); if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponentInChildren <RoadPiece> (); } if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponent <RoadPiece> (); } if (trp != null) { RightRp = trp; } } if (RightRp != null) { if (RightRp.GetSpawnPoint(WaypointOrientation.Left) != null && rp.GetSpawnPoint(WaypointOrientation.Right) == null) { return(false); } else if (RightRp.GetSpawnPoint(WaypointOrientation.Left) == null && rp.GetSpawnPoint(WaypointOrientation.Right) != null) { return(false); } } cols = Physics2D.OverlapCircleAll(new Vector2(rp.transform.position.x, rp.transform.position.y - 10.2f), RADAR_CHECK_SIZE); rp.AddGizmo(new Vector3(rp.transform.position.x, rp.transform.position.y - 10.2f, 0)); RoadPiece BottomRp = null; foreach (Collider2D cd in cols) { RoadPiece trp = cd.GetComponent <RoadPiece> (); if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponentInChildren <RoadPiece> (); } if (trp == null && cd.transform.parent != null) { trp = cd.transform.parent.GetComponent <RoadPiece> (); } if (trp != null) { BottomRp = trp; } } if (BottomRp != null) { if (BottomRp.GetSpawnPoint(WaypointOrientation.Top) != null && rp.GetSpawnPoint(WaypointOrientation.Bottom) == null) { return(false); } else if (BottomRp.GetSpawnPoint(WaypointOrientation.Top) == null && rp.GetSpawnPoint(WaypointOrientation.Bottom) != null) { return(false); } } 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); } }
public void AddNext(RoadPiece rp) { next.Add(rp); }
public void SetRoadTile(Coord c, RoadPiece value) { SetRoadTile(c.x, c.y, value); }