void Start() { RebuildCache(); startingPoint = DDA.playerPos(); Game.Player.Reload(new Vector3(0, 0, 0)); RebuildAll(); }
public void Start() { var playerPos = DDA.playerPos(); CreateMap(playerPos); Game.Player.Reload(playerPos); Game.Camera.MoveTo(playerPos); }
public void RemoveOldChunks() { foreach (Transform child in gameObject.transform) { var chunk = child.gameObject.GetComponent <TestChunk>(); Vector3Int playerPos = DDA.playerPos(); Vector2Int truncStartingPoint = new Vector2Int(playerPos.x / chunkSize * chunkSize + chunkSize, playerPos.z / chunkSize * chunkSize + chunkSize); Vector2Int chunkStart = new Vector2Int(truncStartingPoint.x - chunkSize / 2 - chunkRadius * chunkSize, truncStartingPoint.y - chunkSize / 2 - 1 - chunkRadius * chunkSize); Vector2Int chunkEnd = new Vector2Int(truncStartingPoint.x + chunkSize / 2 + chunkRadius * chunkSize, truncStartingPoint.y + chunkSize / 2 - 1 + chunkRadius * chunkSize); if (chunk.start.x < chunkStart.x || chunk.start.y < chunkStart.y || chunk.end.x > chunkEnd.x || chunk.end.y > chunkEnd.y) { Destroy(child.gameObject); } } }
public void Update() { int i = 0; if (!needReload) { return; } needReload = false; Vector3Int playerPos = DDA.playerPos(); Vector2Int truncPlayerPos = new Vector2Int(playerPos.x / chunkSize * chunkSize, playerPos.z / chunkSize * chunkSize); Vector2Int truncStartingPoint = new Vector2Int(truncPlayerPos.x + chunkSize, truncPlayerPos.y + chunkSize); Vector2Int dTruncPos = new Vector2Int((playerPos.x - startingPoint.x) / chunkSize * chunkSize - chunkSize, (playerPos.z - startingPoint.z) / chunkSize * chunkSize - chunkSize); for (int x = -chunkRadius; x <= chunkRadius; x++) { for (int y = -chunkRadius; y <= chunkRadius; y++) { var chunkStart = new Vector2Int(x * chunkSize, y * chunkSize); Vector2Int start = new Vector2Int(truncStartingPoint.x - chunkSize / 2 - 1 + chunkStart.x, truncStartingPoint.y - chunkSize / 2 - 1 + chunkStart.y); Vector2Int end = new Vector2Int(truncStartingPoint.x + chunkSize / 2 - 1 + chunkStart.x, truncStartingPoint.y + chunkSize / 2 - 1 + chunkStart.y); string chunkName = "chunk_" + start.x.ToString("D2") + "_" + start.y.ToString("D2"); if (GameObject.Find("Map/" + chunkName) == null) { var obj = new GameObject(); var chunk = obj.AddComponent <TestChunk>(); chunk.start = start; chunk.end = end; obj.transform.parent = gameObject.transform; obj.name = chunkName; chunk.transform.localPosition = new Vector3( (chunkStart.x - startingPoint.x % chunkSize + dTruncPos.x + chunkSize / 2 + chunkSize - 1) * tileSize, 0, (chunkStart.y - startingPoint.z % chunkSize + dTruncPos.y + chunkSize / 2 + chunkSize - 1) * tileSize); chunk.Rebuild(i * 10); i++; } } } }
// TODO: almost the same as UpdateEntities, need to refactor public void UpdateFurniture() { GameObject furnObj = GameObject.Find("furniture"); if (furnObj != null) { ClearGameObject(furnObj); } else { furnObj = new GameObject("furniture"); } Vector3Int playerPos = DDA.playerPos(); int size = 60; Vector2Int from = new Vector2Int(playerPos.x - size, playerPos.z - size); Vector2Int to = new Vector2Int(playerPos.x + size, playerPos.z + size); Tile[] tiles = DDA.GetTilesBetween(from, to).tiles; foreach (var tile in tiles) { if (tile.furn == 0) { continue; } string stringId; furnIds.TryGetValue(tile.furn, out stringId); string name = stringId == null ? "f_unknown" : stringId; GameObject obj = new GameObject(name); obj.transform.parent = furnObj.transform; var pos = new Vector3(tile.loc.x, 0, tile.loc.z); obj.transform.Translate((pos - startingPoint) * tileSize); // probably should prepare GameObjects and do their clones instead var mr = obj.AddComponent <MeshRenderer>(); var mf = obj.AddComponent <MeshFilter>(); mf.sharedMesh = voxModelsCache[name].ToMesh(); mr.sharedMaterial = terrainMaterial; mf.sharedMesh.RecalculateNormals(); } }
public void Rebuild() { Vector3Int playerPos = DDA.playerPos(); Game.Player.Reload((playerPos - startingPoint) * tileSize); //if (TestGame.Started == false) return; // only for editor mode: // ReattachMainDispatch(); RemoveOldChunks(); needReload = true; // reload on next frame because Destroy method finishes at the end of frame if (voxModelsCache.Count == 0) { RebuildCache(); } UpdateEntities(); UpdateFurniture(); }
public void UpdateEntities() { GameObject entitiesObj = GameObject.Find("entities"); if (entitiesObj != null) { ClearGameObject(entitiesObj); } else { entitiesObj = new GameObject("entities"); } Vector3Int playerPos = DDA.playerPos(); int size = 60; Vector2Int from = new Vector2Int(playerPos.x - size, playerPos.z - size); Vector2Int to = new Vector2Int(playerPos.x + size, playerPos.z + size); Entity[] entities = DDA.GetEntities(from, to); Debug.Log("found " + entities.Length + " entities: "); foreach (var entity in entities) { Debug.Log(entity.name); string stringId; monIds.TryGetValue(entity.type, out stringId); string name = stringId == null ? "mon_unknown" : stringId; GameObject obj = new GameObject(name); obj.transform.parent = entitiesObj.transform; var pos = new Vector3(entity.loc.x, 0, entity.loc.y); obj.transform.Translate((pos - startingPoint) * tileSize); // probably should prepare GameObjects and do their clones instead var mr = obj.AddComponent <MeshRenderer>(); var mf = obj.AddComponent <MeshFilter>(); mf.sharedMesh = voxModelsCache[name].ToMesh(); mr.sharedMaterial = terrainMaterial; mf.sharedMesh.RecalculateNormals(); } }