private bool dfs(Vector2 position, Vector2 dir, int length, bool fat) { if (length == 0) { if (getMatrix(chunks, position) != null) { return(false); } return(completeTrack(position, dir)); } if (chunks [(int)position.x, (int)position.y] == null) { int[] roadChunkIdx = getRandomChunkIndex(fat); RoadChunk[] roadChunks = fat ? roadChunksStartBig : roadChunksStartSmall; for (int i = 0; i < roadChunkIdx.Length; i++) { RoadChunk newGO = UnityEngine.Object.Instantiate(roadChunks [roadChunkIdx[i]]) as RoadChunk; Vector2 newDir = setupNewRoadChunk(newGO, position, dir); //Debug.Log (dir); if (dfs(position + newDir, newDir, length - 1, newGO.endFat)) { return(true); } DestroyImmediate(newGO.gameObject); setChunk(position, null); } } return(false); }
protected void updateRoadState() { RaycastHit hit; if (Physics.Raycast(transform.position + transform.up, -transform.up, out hit, 10f, m_RoadLayer)) { m_DestroyTimer = 0f; var road = hit.transform.GetComponentInParent <RoadChunk>(); if (m_CurrentAttachRoad != road) { // 更新路 m_CurrentAttachRoad?.removeCarFromRoad(); road?.addCarToRoad(); m_CurrentAttachRoad = road; // m_ConservativeAi.updateRoadState(m_AttachRoad.getRoadNum()); } } else { m_CurrentAttachRoad?.removeCarFromRoad(); m_CurrentAttachRoad = null; m_DestroyTimer += Time.deltaTime; if (m_DestroyTimer > 1f) { Destroy(gameObject); } } }
//Retorna la nueva dirección. private Vector2 setupNewRoadChunk(RoadChunk newGO, Vector2 position, Vector2 dir) { newGO.name = String.Format("part-{0}-{1}", (int)position.x, (int)position.y); newGO.transform.parent = track.transform; setChunk(position, newGO); newGO.transform.Rotate(new Vector3(0, getRotation(dir), 0)); return(getNewDirection(dir, newGO.turn)); }
private void setChunk(int i, int j, RoadChunk chunk) { chunks[i, j] = chunk; if (chunk == null) { return; } chunk.transform.localPosition = new Vector3((i - initialPosition.x) * scale, 0, (j - initialPosition.y) * scale); }
// TODO: Car Info public void generateCar(RoadChunk chunk) { var rand = Random.Range(0f, 1f); if (m_CarList.Count > 0) { var roadNum = chunk.getRoadNum(); var roadPicker = Random.Range(0, roadNum); var position = chunk.computeRoadCenterWorld(roadPicker); position.y = transform.position.y; var rotation = chunk.transform.rotation; var carPicker = Random.Range(0, m_CarList.Count); var carPrefab = m_CarList[carPicker]; var car = Instantiate(carPrefab, position, rotation, transform.root); var script = car.GetComponent <CarController>(); script.setEnginePower(0.7f); } }
/// <summary> /// Generate the level /// </summary> public void Generate() { scale = roadChunksStartBig[0].transform.localScale.x; chunks = new RoadChunk[matrixSize, matrixSize]; Vector2 currPosition = initialPosition; Vector2 dir = new Vector2(1f, 0f); chunks[(int)currPosition.x - 1, (int)currPosition.y] = roadChunksStartBig[0]; track = new GameObject(); track.name = "Track"; for (int i = 0; i < 3; i++) { GameObject plane = UnityEngine.Object.Instantiate(planePrefab) as GameObject; plane.name = String.Format("Plane {0}", i); plane.transform.parent = track.transform; plane.transform.position = new Vector3(-20.0f + ((i + 1) % 3) * 20, 0.0f, 0.0f); } RoadChunk newGO = UnityEngine.Object.Instantiate(roadChunksStartBig [1]) as RoadChunk; setupNewRoadChunk(newGO, currPosition, dir); dfs(currPosition + dir, dir, roadSize, true); /* for (int i=0; i<roadSize; i++) * { * int roadChunkIdx = getRandomChunkIndex(); * * RoadChunk newGO = UnityEngine.Object.Instantiate(roadChunks[roadChunkIdx]) as RoadChunk; * newGO.name = String.Format("part-{0}", i); * newGO.transform.parent = transform; * * setChunk(currPosition, newGO); * newGO.transform.Rotate(new Vector3(0, getRotation(dir), 0)); * dir = getNewDirection(dir, newGO.turn); * Debug.Log(dir); * currPosition += dir; * }*/ }
static void DrawGizmosSelected(RoadChunk script, GizmoType type) { if (script.getRoadNum() == 0) { return; } var original = script.transform.position; float maxWidth = (script.getRoadNum() / 2.0f) * script.getRoadWidth(); float drawLineLength = 5f; float heightOffset = 0.2f; var button = script.transform.TransformPoint(new Vector3(-maxWidth, heightOffset, -drawLineLength / 2.0f)); var top = script.transform.TransformPoint(new Vector3(-maxWidth, heightOffset, drawLineLength / 2.0f)); Gizmos.DrawLine(button, top); float maxOffset = maxWidth; for (int count = 0; count < script.getRoadNum(); ++count) { var _button = script.transform.TransformPoint(new Vector3(maxOffset, heightOffset, -drawLineLength / 2.0f)); var _top = script.transform.TransformPoint(new Vector3(maxOffset, heightOffset, drawLineLength / 2.0f)); Gizmos.DrawLine(_button, _top); maxOffset -= script.getRoadWidth(); } Vector3 forward = script.transform.TransformDirection(Vector3.forward); Gizmos.DrawRay(new Ray(script.transform.position, forward)); }
private bool completeTrack(Vector2 position, Vector2 dir) { Vector2 finalPosition = initialPosition - new Vector2(1.0f, 0.0f); setMatrix(chunks, finalPosition, null); Vector2[,] previusPositions = new Vector2[matrixSize, matrixSize]; Vector2[] direction = { new Vector2(0.0f, 1.0f), new Vector2(0.0f, -1.0f), new Vector2(1.0f, 0.0f), new Vector2(-1.0f, 0.0f) }; setMatrix(previusPositions, position, position - dir); Queue queue = new Queue(); queue.Enqueue(position); Vector2 currPosition = position; while (currPosition != finalPosition && queue.Count != 0) { currPosition = (Vector2)queue.Dequeue(); for (int i = 0; i < direction.Length; i++) { Vector2 nextPosition = currPosition + direction [i]; if (0 > (int)nextPosition.x || (int)nextPosition.x >= matrixSize) { continue; } if (0 > (int)nextPosition.y || (int)nextPosition.y >= matrixSize) { continue; } if (getMatrix(previusPositions, nextPosition) == Vector2.zero && getMatrix(chunks, nextPosition) == null) { setMatrix(previusPositions, nextPosition, currPosition); queue.Enqueue(nextPosition); } } } if (currPosition != finalPosition) { return(false); } Vector2 lastPosition = position - dir; dir = new Vector2(1.0f, 0.0f); //The first chunk is in this direction. Boolean fat = true; //The first chunk is starting fat. Boolean fatNeeded = getMatrix(chunks, lastPosition).endFat; while (currPosition != lastPosition) { Vector2 prevPosition = getMatrix(previusPositions, currPosition); Vector2 prevDir = currPosition - prevPosition; Direction turn = getTurnFromDirections(prevDir, dir); int index = getIndexFromDirection(turn); if (turn == Direction.STRAIGHT && fat != fatNeeded) { fat = !fat; index = 3; } RoadChunk[] roadChunks = fat ? roadChunksStartBig : roadChunksStartSmall; RoadChunk newGO = UnityEngine.Object.Instantiate(roadChunks [index]) as RoadChunk; fat = newGO.startFat; dir = prevDir; setupNewRoadChunk(newGO, currPosition, dir); currPosition = prevPosition; } return(fat == fatNeeded); }
private void setChunk(Vector2 p, RoadChunk chunk) { setChunk((int)p.x, (int)p.y, chunk); }