public static void CreateWallMount() { ObjectTile tile = TileBuilder.CreateTile <ObjectTile>(LayerType.Objects); tile.Rotatable = true; tile.Offset = true; TileBuilder.CreateAsset(tile, "WallMountTile"); }
private static void Json2Map() { GameObject map = GameObject.FindGameObjectWithTag("Map"); MetaTileMap metaTileMap = map.GetComponentInChildren <MetaTileMap>(); metaTileMap.ClearAllTiles(); TilemapConverter converter = new TilemapConverter(); TileMapBuilder builder = new TileMapBuilder(metaTileMap, true); Dictionary <string, TilemapLayer> layers = DeserializeJson(); List <Tuple <Vector3Int, ObjectTile> > objects = new List <Tuple <Vector3Int, ObjectTile> >(); foreach (KeyValuePair <string, TilemapLayer> layer in layers) { List <Vector3Int> positions = layer.Value.TilePositions.ConvertAll(coord => new Vector3Int(coord.X, coord.Y, 0)); for (int i = 0; i < positions.Count; i++) { Vector3Int position = positions[i]; GenericTile tile = converter.DataToTile(layer.Value.Tiles[i]); if (tile is ObjectTile) { if (!objects.Exists(t => t.Item1.Equals(position) && t.Item2 == tile)) { objects.Add(new Tuple <Vector3Int, ObjectTile>(position, (ObjectTile)tile)); } } else { builder.PlaceTile(position, tile); } } } foreach (Tuple <Vector3Int, ObjectTile> tuple in objects) { Vector3Int position = tuple.Item1; ObjectTile obj = tuple.Item2; Matrix4x4 matrix = obj.Rotatable ? FindObjectPosition(metaTileMap, ref position, obj) : Matrix4x4.identity; builder.PlaceTile(position, obj, matrix); } // mark as dirty, otherwise the scene can't be saved. EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene()); Logger.Log("Import kinda finished"); }
public override void SetTile(Vector3Int position, GenericTile tile, Matrix4x4 transformMatrix, Color color) { ObjectTile objectTile = tile as ObjectTile; if (objectTile) { //hack to expand bounds when placing items in editor base.InternalSetTile(position, tile); base.InternalSetTile(position, null); objectTile.SpawnObject(position, tilemap, transformMatrix); } }
// Start is called before the first frame update void Awake() { PlaceObjects place = FindObjectOfType <PlaceObjects>(); tiles = new ObjectTile[tileCountX, tileCountY]; for (int i = 0; i < tileCountX; i++) { for (int j = 0; j < tileCountY; j++) { tiles[i, j] = new ObjectTile(); } } }
public override void SetTile(Vector3Int position, GenericTile tile, Matrix4x4 transformMatrix) { ObjectTile objectTile = tile as ObjectTile; if (objectTile) { if (!objectTile.IsItem) { tilemap.SetTile(position, null); } objectTile.SpawnObject(position, tilemap, transformMatrix); } else { base.SetTile(position, tile, transformMatrix); } }
public override void PaintPreview(GridLayout gridLayout, GameObject brushTarget, Vector3Int position) { if (brushTarget == null) { return; } MetaTileMap metaTilemap = brushTarget.GetComponent <MetaTileMap>(); if (!metaTilemap) { return; } TileBase tile = brush.cells[0].tile; if (tile != _currentPreviewTile) { if (tile is LayerTile) { ObjectTile objectTile = tile as ObjectTile; if (objectTile && objectTile.Offset) { brush.cells[0].matrix = Matrix4x4.TRS(Vector3.up, Quaternion.identity, Vector3.one); } previewTiles = new[] { (LayerTile)tile }; } else if (tile is MetaTile) { previewTiles = ((MetaTile)tile).GetTiles().ToArray(); } _currentPreviewTile = tile; } if (previewTiles != null) { for (int i = 0; i < previewTiles.Length; i++) { SetPreviewTile(metaTilemap, position, previewTiles[i]); } } _currentPreviewTilemap = metaTilemap; }
//Creates object at Tile[x,y] if there is no other object public void CreateObject(GameObject newObj, int x, int y) { if (!InBounds(x, y)) { Debug.Log("Tried to create an object outside of bounds and failed"); return; } ObjectTile tile = GetComponent <TileLayout>().GetTile(x, y); GameObject oldObj = tile.getObjectOnTile(); if (oldObj == null) { Vector2 position = new Vector2(x, y); GameObject obj = Instantiate(newObj, position, Quaternion.identity); tile.setObjectOnTile(obj); } }
//Destroys object at Tile[x,y] if there is an object there public void DestroyObject(int x, int y) { Tutorial.Instance.TriggerDialogue(5); if (!InBounds(x, y)) { Debug.Log("Tried to destroy an object outside of bounds and failed"); return; } ObjectTile tile = GetComponent <TileLayout>().GetTile(x, y); GameObject objectOnTile = tile.getObjectOnTile(); if (objectOnTile != null) { Destroy(objectOnTile); } GetComponent <TileLayout>().GetTile(x, y).ResetTileInfo(); }
public void generate(string mapName, string tileType) { string tileCsv = "/" + mapName + "/" + tileType + ".csv"; string[][] tiles2DArray = ReadCsv(tileCsv); int arrayCounter = 0; if (tileType.Equals("objects")) { objectTiles = new ObjectTile[tiles2DArray.Length]; for (int i = 0; i < tiles2DArray.Length; i++) // rows of the csv { objectTiles[arrayCounter] = new ObjectTile( int.Parse((string)tiles2DArray[i][0]), int.Parse((string)tiles2DArray[i][1]), "Maps/tiles/" + (string)tiles2DArray[i][2].Replace(".png", "") ); arrayCounter++; } } else { floorTiles = new FloorTile[tiles2DArray.Length]; for (int i = 0; i < tiles2DArray.Length; i++) // rows of the csv { floorTiles[arrayCounter] = new FloorTile( int.Parse((string)tiles2DArray[i][0]), int.Parse((string)tiles2DArray[i][1]), "Maps/tiles/" + (string)tiles2DArray[i][2].Replace(".png", ""), (tiles2DArray[i][3].Equals("true")), (tiles2DArray[i][4].Equals("true")) ); arrayCounter++; } } }
public static void GenerateTiles(string subject) { //Moving old tiles string basePath = Application.dataPath + "/Resources/Prefabs/"; string basePath2 = basePath + subject + "/"; // Debug.Log (basePath2); int counter = 0; int created = 0; string[] scan = Directory.GetFiles(basePath2, "*.prefab", SearchOption.AllDirectories); foreach (string file in scan) { counter++; int t = scan.Length; EditorUtility.DisplayProgressBar(counter + "/" + scan.Length + " Generating Tiles for " + subject, "Tile: " + counter, counter / (float)scan.Length); // Debug.Log ("Longpath data: " + file); //Get the filename without extention and path string name = Path.GetFileNameWithoutExtension(file); // Debug.Log ("Creating tile for prefab: " + name); //Generating the path needed to hook onto for selecting the game object string smallPath = file.Substring(file.IndexOf("Assets") + 0); // Debug.Log ("smallpath data: " + smallPath); //Generating the path needed to chose the right tile output sub-folder string subPath = smallPath.Substring(smallPath.IndexOf(subject) + 7); // Debug.Log ("subPath data: " + subPath); string barePath = subPath.Substring(0, subPath.LastIndexOf(Path.DirectorySeparatorChar)); // Debug.Log ("barePath data: " + barePath); //Check if tile already exists if (File.Exists(Application.dataPath + "/Tilemaps/Tiles/" + subject + "/" + barePath + "/" + name + ".asset")) { // Debug.Log ("A tile for " + name + " already exists... Skipping..."); } else { //setup building the tile// ObjectTile tile = TileBuilder.CreateTile <ObjectTile>(LayerType.Objects); //Cast the gameobject GameObject cast = AssetDatabase.LoadAssetAtPath(smallPath, typeof(GameObject)) as GameObject; if (barePath == "/WallMounts") { tile.Rotatable = true; tile.Offset = true; } else { tile.Rotatable = false; tile.Offset = false; } tile.Object = cast; //Create the tile TileBuilder.CreateAsset(tile, name, "Assets/Tilemaps/Tiles/" + subject + "/" + barePath); PreviewSpriteBuilder.Create(cast); created++; } } EditorUtility.ClearProgressBar(); Debug.Log(created + " / " + counter + " Tiles created for prefabs"); }
void BuildMap(int[][] Map) { boardHolder = new GameObject("Board").transform; for (int i = 0; i < Map.Length; i++) { for (int j = 0; j < Map[i].Length; j++) { int type = CurrentMap[i][j]; int x = i; int y = j; int health = 0; GameObject toInstantiate = GroundTile; bool ifObject = false; switch (type) { case 0: toInstantiate = GroundTile; break; case 1: toInstantiate = MountainTile; ifObject = true; health = 2; break; case 2: toInstantiate = WaterTile; break; case 3: toInstantiate = ForestTile[0]; break; case 5: toInstantiate = CityTile[0]; ifObject = true; health = 1; break; case 6: toInstantiate = CityTile[1]; ifObject = true; health = 2; break; case 7: toInstantiate = CityTile[2]; ifObject = true; health = 3; break; } if (!ifObject) { GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject; instance.transform.SetParent(boardHolder); CurrentMapTiled[i][j] = new Tile(x, y, instance, 0, CurrentMap[x][y]); } else { GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject; instance.transform.SetParent(boardHolder); CurrentMapTiled[i][j] = new ObjectTile(x, y, instance, 0, CurrentMap[x][y], health); } } } }
void Update() { if (_currentPlayer.human) { switch (_state) { case 0: //Välj en bricka att hoppa från if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = _camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { ObjectTile obj = hit.transform.GetComponent <ObjectTile>(); if (_gl.currentState.TileRows[obj.yIndex][obj.xIndex].isOccupied) { _startTile = _gl.currentState.TileRows[obj.yIndex][obj.xIndex]; if (_board.GetTileCoords(_startTile.xPos, _startTile.yPos).GetComponent <ObjectTile>().marbleObject.owner != _currentPlayer) //Kollar om rätt person äger kulan { _startTile = null; break; } } else { break; } _state++; } } break; case 1: //Välj en bricka att hoppa till _board.ShowValidMoves(_startTile, true); if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = _camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { ObjectTile obj = hit.transform.GetComponent <ObjectTile>(); _endTile = _gl.currentState.TileRows[obj.yIndex][obj.xIndex]; if (_endTile != _startTile) { _board.ShowValidMoves(_startTile, false); //Tar bort färgen från hoppbara kulor _state++; } } } else if (Input.GetMouseButtonDown(1)) //Avsluta pågående drag { _board.ShowValidMoves(_startTile, false); //Tar bort färgen från hoppbara kulor _state = 0; } break; case 2: //Gör själva flytten int kidOfMove = _gl.MoveAMarble(_gl.currentState, _startTile, _endTile); _board.PlaceTheMarbles(_gl.currentState); if (_gl.WinCheck(_gl.currentState, _currentPlayer)) { _state = -2; } else if (kidOfMove == 0) { _state = -1; } else if (kidOfMove == 1) { _gl.currentState.VisitTile(_startTile); _state++; } else { _state = 1; } break; case 3: _board.ShowJumpMoves(_startTile, false); _startTile = _endTile; if (_gl.GetJumpMoves(_startTile, _gl.currentState).Count != 0) { _board.ShowJumpMoves(_startTile, true); List <Tile> moves = _gl.GetJumpMoves(_startTile, _gl.currentState); if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = _camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { ObjectTile obj = hit.transform.GetComponent <ObjectTile>(); _endTile = _gl.currentState.TileRows[obj.yIndex][obj.xIndex]; if (_endTile != _startTile && ExistsInList(_endTile, moves)) { _board.ShowJumpMoves(_startTile, false); //Tar bort färgen från hoppbara kulor _state++; } else { _endTile = _startTile; } } } if (Input.GetMouseButtonDown(1)) { _state = -1; _board.ShowJumpMoves(_startTile, false); } } else { _state = -1; } break; case 4: kidOfMove = _gl.MoveAMarble(_gl.currentState, _startTile, _endTile); _board.PlaceTheMarbles(_gl.currentState); _gl.currentState.VisitTile(_startTile); _state = 3; if (_gl.WinCheck(_gl.currentState, _currentPlayer)) { _state = -2; } break; case -1: PassTheTurn(); _state = 0; _gl.currentState.ClearVisitList(); break; case -2: winState(); break; case -3: break; } } else if (!_gameWon) { AITurn(); } }