private void UpdateEntityMap(MapDataSo map) { GridSystem.UpdateMap(map, MapType.main); //PlantManager.Instance.LoadPlants(map, MapType.main); //MapItemManagerSystem.LoadPlantItems(map); }
public void LoadMap(MapDataSo map, bool deleteOld = true) { if (deleteOld) { for (int i = mapParent.transform.childCount; i > 0; --i) { DestroyImmediate(mapParent.transform.GetChild(0).gameObject); } } //add to grids array and make current }
public void LoadPlants(MapDataSo map, MapType mapType) { if (mapType == MapType.main) { PlantSoRefDict = map.plantRefList.GetDictionary(); } if (mapType == MapType.main) { if (mainMapContainer != null) { Destroy(mainMapContainer); } CreateParentFolders(); for (int i = 0; i < map.plantItems.Count; i++) { AddPlant(map.plantItems[i], mapType); } } else if (mapType == MapType.secondary) { if (secondaryMapContainer != null) { Destroy(secondaryMapContainer); } CreateParentFolders(); for (int i = 0; i < map.plantItems.Count; i++) { AddPlant(map.plantItems[i], mapType); } } else { Debug.LogError("Map type not recognized:: " + mapType); } }
public static void LoadBuildingItems(MapDataSo map, MapType mapType = MapType.current) { Debug.Log("Loading Map BUILDINGS to ECS: " + map.id); //-- Update References byte id; if (!BuildingReferences.IsCreated) { BuildingReferences = new NativeHashMap <byte, BuildingDataStruct>( map.buildingRefList.items.Length, Allocator.Persistent); for (int i = 0; i < map.buildingRefList.items.Length; i++) { id = map.buildingRefList.items[i].typeId; if (BuildingReferences.ContainsKey(id)) { Debug.LogError("Duplicate Building Reference ID: " + id); } BuildingReferences[id] = BuildingDataStruct.FromBuildingDataSo( map.buildingRefList.items[i]); } } if (mapType == MapType.current) { mapType = MapType.main; } //*4, whats the average size of biulding...... var size = (map.buildingItems.Count > 0) ? map.buildingItems.Count * 4 : 10; var items = new NativeHashMap <int, BuildingItem>( size, Allocator.Persistent); BuildingItem item; BuildingDataStruct data; int index; for (int i = 0; i < map.buildingItems.Count; i++) { item = map.buildingItems[i]; data = BuildingReferences[item.typeId]; for (int x = 0; x < data.Size.x; x++) { for (int y = 0; y < data.Size.y; y++) { index = map.GetGridIndex(new int2(item.pos.x + x, item.pos.y + y)); if (items.ContainsKey(index)) { Debug.LogError("Duplicate Building Reference, Index: " + index); items[index] = item; } } } } if (mapType == MapType.main) { if (MainMapBuildings.IsCreated) { MainMapBuildings.Dispose(); } MainMapBuildings = items; } else if (mapType == MapType.secondary) { if (SecondaryMapBuildings.IsCreated) { SecondaryMapBuildings.Dispose(); } SecondaryMapBuildings = items; } else { Debug.LogWarning("Map type not recongnized: " + mapType); } BuildingManager.Instance.LoadBuildings(map, mapType); }
public static void LoadPlantItems(MapDataSo map, MapType mapType) { Debug.Log("Loading Map ITEMS to ECS: " + map.id); //-- Update References if (!PlantItemReferences.IsCreated) { PlantItemReferences = new NativeHashMap <byte, PlantDataStruct>( map.plantRefList.list.Length, Allocator.Persistent); for (int i = 0; i < map.plantRefList.list.Length; i++) { PlantItemReferences[(byte)i] = PlantDataStruct.FromSo(map.plantRefList.list[i].data); } } if (mapType == MapType.current) { mapType = MapType.main; } var items = new NativeHashMap <int, PlantItem>( map.plantItems.Count, Allocator.Persistent); PlantItem p; PlantDataStruct pds; int index; for (int i = 0; i < map.plantItems.Count; i++) { p = map.plantItems[i]; pds = PlantItemReferences[p.TypeId]; index = map.GetGridIndex(p.Pos); items[index] = p; } if (mapType == MapType.main) { if (MainMapPlantItems.IsCreated) { MainMapPlantItems.Dispose(); } MainMapPlantItems = items; } else if (mapType == MapType.secondary) { if (SecondaryMapPlantIems.IsCreated) { SecondaryMapPlantIems.Dispose(); } SecondaryMapPlantIems = items; } else { Debug.LogWarning("Map type not recongnized: " + mapType); } //Update Mono Manager PlantManager.Instance.LoadPlants(map, mapType); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); manager = (MapDataSo)target; if (GUILayout.Button("Load Tilemap from Grid")) { var tilemap = GameObject.Find(manager.tilemapName).GetComponent <Tilemap>(); var tilemap2 = GameObject.Find(manager.tilemapSecondaryName).GetComponent <Tilemap>(); if (tilemap == null || tilemap2 == null) { Debug.LogWarning("Couldn't load tilemap object, please check name: " + manager.tilemapName); return; } float3 cellWorldPos; Vector3Int tilePos1; Cell cell; float3 offset = new float3(0.1f, 0.1f, 0f); for (int x = 0; x < manager.grid.gridSize.x; x++) { for (int y = 0; y < manager.grid.gridSize.y; y++) { //lower tile cell = manager.grid.GetCell(x, y); cellWorldPos = manager.GetCellWorldCoordinates(cell.pos, 0) + offset; tilePos1 = tilemap.layoutGrid.WorldToCell(cellWorldPos); ///Debug.Log($"Updating tile GridPos: {cell.pos}, TilePos: {tilePos1}, WorldPos: {cellWorldPos}"); if (manager.tileRefList.list[cell.tileRefIndex].isLowerGround) { tilemap.SetTile(tilePos1, manager.tileRefList.list[cell.tileRefIndex].tile); } else { tilemap2.SetTile(tilePos1, manager.tileRefList.list[cell.tileRefIndex].tile); //see if we have lower ground specified as well if (manager.tileRefList.list[cell.tileRefIndex].lowerGround != null) { tilemap.SetTile(tilePos1, manager.tileRefList.list[cell.tileRefIndex].lowerGround); } } } } } if (GUILayout.Button("Update Grid from Tilemap")) { Debug.Log("Update from Tilemap....."); Tilemap tilemap = GameObject.Find(manager.tilemapName).GetComponent <Tilemap>(); Tilemap tilemap2 = GameObject.Find(manager.tilemapSecondaryName).GetComponent <Tilemap>(); if (tilemap == null || tilemap2 == null) { Debug.LogWarning("Couldn't load tilemap object, please check name: " + manager.tilemapName); return; } Cell[] cells = new Cell[manager.grid.gridSize.x * manager.grid.gridSize.y]; float3 pos; TileBase tile; TileBase tile2; TileBase secondaryTile; Vector3Int tilePos; TileDataSo data; int index; int tileRefIndex; for (int x = 0; x < manager.grid.gridSize.x; x++) { for (int y = 0; y < manager.grid.gridSize.y; y++) { pos = manager.GetCellWorldCoordinates(new int2(x, y), 0); tilePos = tilemap.layoutGrid.WorldToCell(pos); tile2 = tilemap2.GetTile(tilePos); tileRefIndex = manager.tileRefList.GETRefIndex(tile2); if (tileRefIndex == -1) { //No upper ground tile, see if we have lower ground..... tile = tilemap.GetTile(tilePos); tileRefIndex = manager.tileRefList.GETRefIndex(tile); if (tileRefIndex == -1) { //Debug.Log($"Tile not found::: {tile}, tilePos:{tilePos}"); continue; } } //Debug.Log("TileRefIndex:: "+tileRefIndex); data = manager.tileRefList.list[tileRefIndex].data; index = manager.GetGridIndex(x, y); cells[index] = Cell.FromTileDataSo(data, new int2(x, y), (byte)tileRefIndex); } } manager.grid.cells = cells; } if (GUILayout.Button("Update Grid Plants from Tilemap")) { LoadPlantsFromTilemap(); } if (GUILayout.Button("Clear Tilemap Data")) { Tilemap tilemap = GameObject.Find(manager.tilemapName).GetComponent <Tilemap>(); if (tilemap == null) { Debug.LogWarning("Couldn't load tilemap object, please check name: " + manager.tilemapName); //return; } else { tilemap.ClearAllTiles(); } tilemap = GameObject.Find(manager.tilemapSecondaryName).GetComponent <Tilemap>(); if (tilemap == null) { Debug.LogWarning("Couldn't load secondary object, please check name: " + manager.tilemapName); //return; } else { tilemap.ClearAllTiles(); } tilemap = GameObject.Find(manager.plantTilemapName).GetComponent <Tilemap>(); if (tilemap == null) { Debug.LogWarning("Couldn't load secondary object, please check name: " + manager.tilemapName); //return; } else { tilemap.ClearAllTiles(); } } }
private void OnDrawGizmos() { if (!drawGizmos) { return; } if (mainMap == null) { return; //no map loaded } MapDataSo so = mainMap; Gizmos.color = gizmoColor; Vector3 pos = new Vector3(0.02f + so.originPosition.x, so.originPosition.y + 0.02f, -2); //outline UtilsGizmo.DrawThickLine( pos, pos + new Vector3(mainMap.grid.gridSize.x * so.cellSize.x, 0, 0), gizmoThickness); UtilsGizmo.DrawThickLine( pos, pos + new Vector3(0, mainMap.grid.gridSize.y * so.cellSize.y, 0), gizmoThickness); UtilsGizmo.DrawThickLine( pos + new Vector3(mainMap.grid.gridSize.x * so.cellSize.x, mainMap.grid.gridSize.y * so.cellSize.y, 0), pos + new Vector3(mainMap.grid.gridSize.y * so.cellSize.y, 0, 0), gizmoThickness); UtilsGizmo.DrawThickLine( pos + new Vector3(mainMap.grid.gridSize.x * so.cellSize.x, mainMap.grid.gridSize.y * so.cellSize.y, 0), pos + new Vector3(0, mainMap.grid.gridSize.x * so.cellSize.y, 0), gizmoThickness); Vector3 walkableOffset = new Vector3(so.cellSize.x / 2, so.cellSize.y / 3, -3f); Vector3 buildableOffset = new Vector3(so.cellSize.x / 4, so.cellSize.y / 3, -3f); Vector3 boxSize = new Vector3(so.cellSize.x / 5, so.cellSize.y / 5, 1); Cell c; for (int x = 0; x < so.grid.gridSize.x; x++) { Gizmos.color = gizmoBuildableColor; UtilsGizmo.DrawThickLine( pos + new Vector3(x * so.cellSize.x, 0, -2), pos + new Vector3(x * so.cellSize.x, mainMap.grid.gridSize.y * so.cellSize.y, -2)); UtilsGizmo.DrawThickLine( pos + new Vector3(0, x * so.cellSize.y, -2), pos + new Vector3(mainMap.grid.gridSize.x * so.cellSize.x, x * so.cellSize.y, -2)); /* * for (int y = 0; y < so.Grid.GridSize.y; y++) * { * Vector3 worldPos = so.GetCellWorldCoordinates(new int2(x, y), 0.02f); * * //UtilsGizmo.DrawText(worldPos,6, $"({x}:{y})"); * /* * * * c = GridSystem.getCell(new int2(x,y)); * //Debug.Log(data); * if (c.walkSpeed > 0) * { * Gizmos.color = gizmoWalkableColor; * UtilsGizmo.DrawBox(worldPos + walkableOffset, boxSize); * } * else * { * Gizmos.color = gizmoNotWalkable; * UtilsGizmo.DrawBox(worldPos + walkableOffset, boxSize); * } * if (c.canBuild) * { * Gizmos.color = gizmoBuildableColor; * UtilsGizmo.DrawBox(worldPos + buildableOffset, boxSize); * } * / * } */ } }