private RoomChunk GetNewChunk(Vector2 spawnPosition) { GameObject go = Instantiate(chunkPrefabs[Random.Range(0, chunkPrefabs.Length)], new Vector3(spawnPosition.x * chunkSize, 0, spawnPosition.y * chunkSize), Quaternion.identity); RoomChunk chunk = go.GetComponent <RoomChunk>(); chunk.level = this; chunk.gridCoords = spawnPosition; return(chunk); }
public void RegisterMove(RoomChunk chunk, Vector2 newCoords) { if (chunks.ContainsKey(newCoords)) { chunks[newCoords] = chunk; } else { chunks.Add(newCoords, chunk); } }
private RoomChunk ReplaceComptoir(Vector2 spawnPosition) { GameObject go = Instantiate(comptoirPrefab, new Vector3(spawnPosition.x * chunkSize, 0, spawnPosition.y * chunkSize), Quaternion.identity); RoomChunk chunk = go.GetComponent <RoomChunk>(); chunk.level = this; chunk.gridCoords = spawnPosition; chunks[comptoirCoordinates].comptoir = false; chunk.comptoir = true; return(chunk); }
private void GenerateGrid(int gridDimensions, float chunkSize) { _gridLevel.gridDimensions = gridDimensions; _gridLevel.chunkSize = chunkSize; _gridLevel.chunkPrefabs = chunkPrefabs; _gridLevel.comptoirPrefab = comptoirPrefab; int comptoirI = Random.Range(0, gridDimensions); int comptoirJ = Random.Range(0, gridDimensions); int letterID = 0; for (int j = gridDimensions - 1; j >= 0; j--) { for (int i = 0; i < gridDimensions; i++) { Vector3 position = new Vector3(chunkSize * i, 0, chunkSize * j); GameObject go = null; if (i == comptoirI && j == comptoirJ) { go = Instantiate(comptoirPrefab, position, Quaternion.identity); go.GetComponent <RoomChunk>().comptoir = true; _gridLevel.comptoirCoordinates = new Vector2(i, j); } else { go = Instantiate(chunkPrefabs[Random.Range(0, chunkPrefabs.Length)], position, Quaternion.identity); } RoomChunk chunk = go.GetComponent <RoomChunk>(); Vector2 gridCoords = new Vector2(i, j); chunk.level = _gridLevel; chunk.gridCoords = gridCoords; chunk.SetLetterID(letterID++); _gridLevel.chunks.Add(gridCoords, chunk); } } _gridLevel.OnLevelGenerated(); }
public void InitializeRequestItems() { List <Item> items = new List <Item>(); if (_gridLevel != null) { foreach (Vector2 coord in _gridLevel.chunks.Keys) { RoomChunk chunk = _gridLevel.chunks[coord]; if (chunk == null) { continue; } if (chunk.HasItems()) { } else { // randomize handles. Transform[] handles = chunk.GetItemHandles(); for (int i = 0; i < handles.Length; i++) { Transform temp = handles[i]; int randomIndex = Random.Range(i, handles.Length); handles[i] = handles[randomIndex]; handles[randomIndex] = temp; } // spawn and set items. int chunkItemCount = Mathf.Min(handles.Length, _itemPerChunk); Item[] newChunkItems = new Item[chunkItemCount]; for (int i = 0; i < chunkItemCount; i++) { Transform t = handles[i]; GameObject itemGameObject = GameObject.Instantiate(_itemPrefabs[(prefabId++) % _itemPrefabs.Length]); itemGameObject.transform.position = t.position; itemGameObject.transform.rotation = t.rotation; itemGameObject.transform.parent = t.parent; Item newItem = itemGameObject.GetComponent <Item>(); int letterId = chunk.letterID; string chunkLabel = "?"; if (letterId >= 0 && letterId < lettres.Length) { chunkLabel = lettres[letterId]; } newItem.SetChunkLabel(chunkLabel); newItem.SetRequestManager(this); newChunkItems[i] = newItem; _transformToItemLookup[t] = newItem; } chunk.SetItems(newChunkItems); } if (chunk as ComptoirChunk != null) { (chunk as ComptoirChunk).RegisterListener(this); } Item[] chunkItems = chunk.GetItems(); if (chunkItems != null) { items.AddRange(chunkItems); } } } for (int i = 0; i < items.Count; i++) { Item temp = items[i]; int randomIndex = Random.Range(i, items.Count); items[i] = items[randomIndex]; items[randomIndex] = temp; } _requestedItems = items.ToArray(); }
private void AddChunkAtRandom() { bool horizontal = true; bool positive = false; bool replaceComptoir = false; int letterID = 0; Vector2 spawnPosition = GetRandomSpawnPosition(ref horizontal); if (horizontal) { positive = spawnPosition.x > 0 ? true : false; if (positive) { if (comptoirCoordinates == new Vector2(0, spawnPosition.y)) { replaceComptoir = true; } letterID = chunks[new Vector2(0, spawnPosition.y)].letterID; } else { if (comptoirCoordinates == new Vector2(gridDimensions - 1, spawnPosition.y)) { replaceComptoir = true; } letterID = chunks[new Vector2(gridDimensions - 1, spawnPosition.y)].letterID; } } else { positive = spawnPosition.y > 0 ? true : false; if (positive) { if (comptoirCoordinates == new Vector2(spawnPosition.x, 0)) { replaceComptoir = true; } letterID = chunks[new Vector2(spawnPosition.x, 0)].letterID; } else { if (comptoirCoordinates == new Vector2(spawnPosition.x, gridDimensions - 1)) { replaceComptoir = true; } letterID = chunks[new Vector2(spawnPosition.x, gridDimensions - 1)].letterID; } } RoomChunk chunk = default; if (replaceComptoir) { chunk = ReplaceComptoir(spawnPosition); } else { chunk = GetNewChunk(spawnPosition); } chunk.SetLetterID(letterID); MoveRow(chunk, horizontal, positive); }
private void MoveRow(RoomChunk newChunk, bool horizontal, bool positive) { if (horizontal) { for (int i = 0; i < gridDimensions; i++) { RoomChunk chunk = chunks[new Vector2(i, newChunk.gridCoords.y)]; Vector2 nextCoords = positive ? chunk.gridCoords + Vector2.left : chunk.gridCoords + Vector2.right; chunk.MoveToNewPosition(nextCoords, chunkSize); if (positive) { if (i == 0) { walls[nextCoords].Open(); } } else { if (i == gridDimensions - 1) { walls[nextCoords].Open(); } } } Vector2 newCoords = positive ? newChunk.gridCoords + Vector2.left : newChunk.gridCoords + Vector2.right; walls[newChunk.gridCoords].Open(); newChunk.MoveToNewPosition(newCoords, chunkSize); } else { for (int i = 0; i < gridDimensions; i++) { RoomChunk chunk = chunks[new Vector2(newChunk.gridCoords.x, i)]; Vector2 nextCoords = positive ? chunk.gridCoords + Vector2.down : chunk.gridCoords + Vector2.up; chunk.MoveToNewPosition(nextCoords, chunkSize); if (positive) { if (i == 0) { walls[nextCoords].Open(); } } else { if (i == gridDimensions - 1) { walls[nextCoords].Open(); } } } Vector2 newCoords = positive ? newChunk.gridCoords + Vector2.down : newChunk.gridCoords + Vector2.up; walls[newChunk.gridCoords].Open(); newChunk.MoveToNewPosition(newCoords, chunkSize); } }