private void Movement() { if (Input.GetMouseButtonDown(0)) { oldPos = transform.position; Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 tilePos = tilemap.WorldToCell(mousePos); Vector3Int intPos = new Vector3Int((int)tilePos.x, (int)tilePos.y, 0); if (overlayMap.GetTile(intPos) != null) { Debug.Log(tilemap.GetTile(intPos)); if (tilemap.GetTile(intPos) != tileRemover.wallTile && tilemap.GetTile(intPos) != tileRemover.destructibleTile) { x = intPos.x; y = intPos.y; transform.position = tilemap.CellToWorld(new Vector3Int(x, y, 0)); currPos = transform.position; Overlay(intPos); ghost.enabled = true; //Instantiate(ghostImage, transform.position, transform.rotation); } } } }
private void _OnClick() { Vector3 world = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3Int cellPos = tileMap.WorldToCell(world); Vector2Int pos = TileMapUtil.CovertTileMapPosToDataPos(new Vector2Int(cellPos.x, cellPos.y)); if (TileMapUtil.IsTileOfType <SwitchableTile>(tileMap, cellPos) && !clickCheckboard[pos.x, pos.y]) { int num = nowCheckboard[pos.x, pos.y]; Vector3 worldPos = tileMap.CellToWorld(cellPos) + new Vector3(0.5f, 0.5f, 0); clickCheckboard[pos.x, pos.y] = true; GameObject icon; if (TileMapUtil.IsHead(num)) { GameObject head = Instantiate(Head); head.transform.position = worldPos; icon = head; } else if (TileMapUtil.IsBody(num)) { GameObject body = Instantiate(Body); body.transform.position = worldPos; icon = body; } else { GameObject missing = Instantiate(Missing); missing.transform.position = worldPos; icon = missing; } TileMapUtil.SortBattleIcon(icon); allyGrid.OnRecevie(pos); } }
void TilemapList() { countOfTiles = 0; Debug.Log("<b>Liste all Tiles in a Tilemap: </b>"); liste.Clear(); for (int n = tileMap.cellBounds.xMin; n < tileMap.cellBounds.xMax; n++) { for (int p = tileMap.cellBounds.yMin; p < tileMap.cellBounds.yMax; p++) { Vector3Int localPlace = (new Vector3Int(n, p, (int)tileMap.transform.position.y)); Vector3 place = tileMap.CellToWorld(localPlace); TileBase slt = tileMap.GetTile(localPlace); if (slt) { Debug.Log($"Tiles:{slt} localPlace:{localPlace} place:{place}"); if (!liste.Contains(slt)) { liste.Add(slt); countOfTiles++; } } } } Debug.Log($"<b>il y a {countOfTiles} tiles.</b>"); }
/// <summary> /// Returns a random position on the ground floor of the grid /// </summary> /// <param name="targetTilemap"></param> /// <param name="grid"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="startingHeight"></param> /// <param name="xMin"></param> /// <param name="xMax"></param> /// <param name="shouldBeFilled"></param> /// <param name="maxIterations"></param> /// <returns></returns> public static Vector2 GetRandomPositionOnGround(Tilemap targetTilemap, Grid grid, int width, int height, int startingHeight, int xMin, int xMax, bool shouldBeFilled = true, int maxIterations = 1000) { int iterationsCount = 0; Vector3Int randomCoordinate = Vector3Int.zero; while (iterationsCount < maxIterations) { randomCoordinate.x = UnityEngine.Random.Range(xMin, xMax); randomCoordinate.y = startingHeight; randomCoordinate += MMTilemapGridRenderer.ComputeOffset(width - 1, height - 1); int counter = height; while (counter > 0) { bool hasTile = targetTilemap.HasTile(randomCoordinate); if (hasTile == shouldBeFilled) { randomCoordinate.y++; return(targetTilemap.CellToWorld(randomCoordinate) + (grid.cellSize / 2)); } randomCoordinate.y--; counter--; } iterationsCount++; } return(Vector2.zero); }
private void SelectWalkable(Tilemap tilemap) { Vector3Int gridMousePosition = GridMousePosition(); Vector3 cellPosition = grid.CellToWorld(gridMousePosition) + new Vector3(0, .5f, 0); selectionOutline.transform.position = cellPosition; if (Input.GetMouseButtonDown(0)) { List <Vector3Int> availableDestinationTiles = player.AvailableDestinationTiles(tilemap); if (availableDestinationTiles.Contains(gridMousePosition)) { Debug.Log("AvailableDestinationTiles contains GridMousePosition!"); BoundsInt cellBounds = tilemap.cellBounds; Vector2Int navGridDestination = GameManager.instance.TileIndexOnNavGrid(cellBounds, gridMousePosition); player.Move(navGridDestination, tilemap.CellToWorld(gridMousePosition) + new Vector3(0, .25f, 0)); //cellPosition? } else { Debug.Log("AvailableDestinationTiles does NOT contain GridMousePosition!"); } } }
/// <summary> /// Sets up the tiles on a tilemap with the Pseudo 3D Cube Collider /// </summary> /// <param name="tilemap"></param> /// <param name="sortingOrder"></param> public void SetUp3DTiles(Tilemap tilemap, int sortingOrder) { tileWorldLocations = new List <Vector3>(); var floorNumberString = tilemap.name.Substring(5); //assumes name is like "Floor5" var floorNumber = -999; int.TryParse(floorNumberString, out floorNumber); foreach (var pos in tilemap.cellBounds.allPositionsWithin) { Vector3Int localPlace = new Vector3Int(pos.x, pos.y, pos.z); Vector3 place = tilemap.CellToWorld(localPlace); if (tilemap.HasTile(localPlace)) { tileWorldLocations.Add(place); //Debug.Log(place); var psuedo3DTileCollider = Instantiate(prefab, this.transform, true); psuedo3DTileCollider.transform.position = new Vector3(place.x, place.y, 0f); psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().sortingOrder = sortingOrder; psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().tilePosition = new Vector2Int(Convert.ToInt32(place.x), Convert.ToInt32(place.y) - (sortingOrder - 1)); psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().playerBuffer = playerBuffer; psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().colliderBuffer = colliderBuffer; psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().zNegPlayerBuffer = zNegPlayerBuffer; psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().CreatePseudo3DCubeCollider(); psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().tilemap = tilemap; psuedo3DTileCollider.GetComponent <Pseudo3DCubeCollider>().floorNumber = floorNumber; //name the collider by position psuedo3DTileCollider.name = $"({psuedo3DTileCollider.GetComponent<Pseudo3DCubeCollider>().tilePosition.x}," + $"{psuedo3DTileCollider.GetComponent<Pseudo3DCubeCollider>().tilePosition.y},{floorNumber})"; } } }
void locatTheEnemy() { //locate the enemy in a random position in the cave TilemapCaveGeneratorQ_6 a = tilemap.GetComponent <TilemapCaveGeneratorQ_6>(); int[,] data = a.caveGenerator.GetMap(); int x = 0, y = 0; while (data[x, y] == 1) { x = Random.Range(0, a.gridSize); y = Random.Range(0, a.gridSize); } transform.position = tilemap.CellToWorld(new Vector3Int(x, y, 0)); //if its done locating the enemy false the flag for the next level if (enemyNumber == 1) { a.E1flag = false; } else { a.E2flag = false; } }
public virtual void SetTile(Vector3Int location, GridTile tile) { if (tile == null) { return; } if (tilemap == null) { tilemap = GetComponent <Tilemap>(); } tile.transform.position = tilemap.CellToWorld(location) + new Vector3(0, .25f, 0); tile.transform.SetParent(tilemap.transform); tile.spriteRenderer.sortingOrder = layer; tile.location = location; tiles[location] = tile; foreach (Grid newGrid in subGrids) { if (tile.GetType() == newGrid.type) { newGrid.SetTile(location, tile); } } // UpdateNeighbors(location); }
//private void TileMapSpawner(Tilemap tilemap, int[,] map, Object objectToSpawn, int chance,int min, int max) //{ -------------THE OLD SPAWNER--------- // // BoundsInt bounds = tilemap.cellBounds; // // TileBase[] allTiles = tilemap.GetTilesBlock(bounds); // int spawnCounter = 0; // Vector3Int coordinate = new Vector3Int(0, 0, 0); // for (int x = 0; x < bounds.size.x; x++) // { // for (int y = 0; y < bounds.size.y; y++) // { // TileBase tile = allTiles[x + y * bounds.size.x]; // if (tile != null && map[x, y] == 1 && x > min && x < max && y > min && y < max) // { // coordinate.x = x; // coordinate.y = y; // Debug.Log(tilemap.CellToWorld(coordinate)); // if (Random.Range(1, 1001) < chance && spawnCounter <= spawnCap) // { // spawnCounter++; // var instance = Instantiate(objectToSpawn, tilemap.CellToWorld(new Vector3Int(x, y, 0) + offset), Quaternion.identity); // } // } // } // } //} public void TileMapSpawner(Object objectToSpawn, int spawnCap, float chance, int min, int max) { int spawnCounter = 0; var cellMin = groundTileMap.origin.x + min; var cellMax = groundTileMap.origin.x + max; for (int y = groundTileMap.origin.y; y < groundTileMap.size.y; y++) { for (int x = groundTileMap.origin.x; x < groundTileMap.size.x; x++) { var cellCoord = new Vector3Int(x, y, 0); TileBase tile = groundTileMap.GetTile <RuleTile>(cellCoord); if (tile != null && x > cellMin && x < cellMax && y > cellMin && y < cellMax) { Vector3 worldCoords = groundTileMap.CellToWorld(cellCoord); if (Random.value <= chance && spawnCounter <= spawnCap) { spawnCounter++; var instance = Instantiate(objectToSpawn, worldCoords, Quaternion.identity); } } } } }
private void OnCreateBoard(IBoard board) { var maxPosY = float.MinValue; var maxPosX = float.MinValue; var minPosY = float.MaxValue; var minPosX = float.MaxValue; foreach (var pos in board.Positions) { var hex = pos.Point; var cell = BoardManipulationOddR.GetCellCoordinate(hex); var worldCellPos = tileMap.CellToWorld(cell); if (worldCellPos.x > maxPosX) { maxPosX = worldCellPos.x; } if (worldCellPos.y > maxPosY) { maxPosY = worldCellPos.y; } if (worldCellPos.x < minPosX) { minPosX = worldCellPos.x; } if (worldCellPos.y < minPosY) { minPosY = worldCellPos.y; } } Centralize(minPosX, minPosY, maxPosX, maxPosY); }
private void FixedUpdate() { if (path == null) { return; } if (path.Count >= 0) { if (Vector3.Distance(objectToMove.GetComponent <Rigidbody2D>().position, nextPos) > 0.01f) { objectToMove.GetComponent <Rigidbody2D>().position = Vector3.MoveTowards(objectToMove.GetComponent <Rigidbody2D>().position, nextPos, Time.deltaTime * 2f); } else { if (path.Count == 0) { Destroy(objectToMove.GetComponent <PathMovement>()); } else { nextPos = tm.CellToWorld(path.Pop()); } } } }
//Generate a list of tiles; in this list there are all buildable tiles generated automatically void GenerateBuidableTiles() { //create a list where all tiles are stored availablePlaces = new List <Vector3>(); for (int n = tileMap.cellBounds.xMin; n < tileMap.cellBounds.xMax; n++) { for (int p = tileMap.cellBounds.yMin; p < tileMap.cellBounds.yMax; p++) { Vector3Int localPlace = (new Vector3Int(n, p, (int)tileMap.transform.position.y)); Vector3 place = tileMap.CellToWorld(localPlace); //makes all of the tiles move a bit up and to the right; this centers every buildable tile object to the grid place = place + new Vector3(0.5f, 0.5f); if (tileMap.HasTile(localPlace)) { //Tile at "place" availablePlaces.Add(place); Instantiate(buildableTile, place, Quaternion.identity); } else { //No tile at "place" } } } }
public Cell CreateCell(long cellId, Vector3Int cellPos) { if (ground == null) { ground = GameObject.Find("Level").transform.GetChild("Ground").GetComponent <Tilemap>(); } var real_pos = ground.CellToWorld(cellPos) + new Vector3(1, 1); var cell = Instantiate(Resources.Load <Transform>($"Prefabs/Unit/{CellManager.GetPrefab(cellId)}")); cell.position = real_pos; cell.SetParent(transform); var cell_comp = cell.gameObject.AddComponent <Cell>(); cell_comp.CellId = cellId; var cell_fire_ctrl = cell.gameObject.AddComponent <CellFireController>(); cell_fire_ctrl.WeaponId = CellManager.GetRookieWeaponId(cellId); Cells[cellPos] = cell_comp; LevelManager.AminoAcidAmount -= CellManager.GetCost(BuildManager.BuildingCellId); BuildManager.DeselectCell(); return(cell_comp); }
void Start() { t = GetComponent <Tilemap>(); BoundsInt bounds = t.cellBounds; var s = t.layoutGrid.cellSize / 2; var availablePlaces = new List <Vector3>(); for (int n = t.cellBounds.xMin; n < t.cellBounds.xMax; n++) { for (int p = t.cellBounds.yMin; p < t.cellBounds.yMax; p++) { Vector3Int localPlace = (new Vector3Int(n, p, (int)t.transform.position.y)); Vector3 place = t.CellToWorld(localPlace); var tile = t.GetTile(localPlace); if (tile) { availablePlaces.Add(place); var c = new GameObject().AddComponent <BoxCollider2D>(); c.isTrigger = true; c.transform.parent = t.transform; c.transform.localPosition = t.CellToLocal(localPlace) + s; c.gameObject.layer = LayerMask.NameToLayer("SpeedBoost"); } } } }
public void Spawn() { int index = 0; int rnd = 0; availablePlaces = new List <Vector3>(); for (int n = tileMap.cellBounds.xMin; n < tileMap.cellBounds.xMax; n++) { for (int p = tileMap.cellBounds.yMin; p < tileMap.cellBounds.yMax; p++) { Vector3Int localPlace = (new Vector3Int(n, p, (int)tileMap.transform.position.y)); Vector3 place = tileMap.CellToWorld(localPlace); if (tileMap.HasTile(localPlace)) { availablePlaces.Add(place); } } } for (int i = 0; i < _items.Count; i++) { for (index = 0; index < _itemsNumber[i]; index++) { if (!(Random.Range(0, 100) < probability)) { continue; } rnd = (int)Random.Range(0f, availablePlaces.Count); GameObject go = (GameObject)Instantiate(_items[i]); go.transform.position = availablePlaces[rnd]; availablePlaces.RemoveAt(rnd); } } }
/** * Affiche l'effet visuel de curse et lance l'effet */ public void LaunchCurseRoomEffect() { if (null == currentRoom) { return; } Vector3 currentRoomWorldPos = roomTilemap.CellToWorld(new Vector3Int(currentRoom.x, currentRoom.y, 0)) + new Vector3(0, 0.75f, 0); //On lance les particules de Curse sur la room Instantiate(roomFxList[0], currentRoomWorldPos, Quaternion.identity, roomEffectsParent); if (null != cursedCard) { GameManager.Instance.AddCardToDiscardPile(cursedCard); } StartCoroutine(TimedRoomEffect()); }
void Start() { tileMap = transform.GetComponentInParent <Tilemap>(); availablePlaces = new List <Vector3>(); for (int n = tileMap.cellBounds.xMin; n < tileMap.cellBounds.xMax; n++) { for (int p = tileMap.cellBounds.yMin; p < tileMap.cellBounds.yMax; p++) { Vector3Int localPlace = (new Vector3Int(n, p, (int)tileMap.transform.position.y)); Vector3 place = tileMap.CellToWorld(localPlace); if (tileMap.HasTile(localPlace)) { //Tile at "place" availablePlaces.Add(place); } else { //No tile at "place" } } } Debug.Log(tileMap.GetTile(new Vector3Int(-9, -7, 0))); foreach (var position in tileMap.cellBounds.allPositionsWithin) { //Debug.Log(position.x + ", " + position.y); } }
void DestroyBlock(Collider2D collision) { Vector3 hitPosition = Vector3.zero; Vector3 blockPosition = Vector3.zero; List <Vector3> tilesHit = new List <Vector3>(); //Tile tileHit; bool tileDestroyed = false; hitPosition.x = collision.transform.position.x; hitPosition.y = collision.transform.position.y - 0.2f; var cellPos = tilemap.WorldToCell(hitPosition); if (tilemap.GetTile(cellPos) != null) { blockPosition = tilemap.CellToWorld(cellPos) + tilemap.tileAnchor; // Get The tile sprite for replacement Sprite replacementSprite = tilemap.GetSprite(cellPos); // Delete tile tilemap.SetTile(cellPos, null); tileDestroyed = true; tilesHit.Add(blockPosition); } if (tileDestroyed) { foreach (var location in tilesHit) { Instantiate(lavaBlockPrefab, location, Quaternion.identity); } } }
// coroutine pour le deplacement aletoire IEnumerator Deplacement() { // verifie si il lui reste des mouvements, si oui il choisi une position aleatoir while (_mouvementCourant > 0) { List <OrientationHero> orientationValide = OrientationValide(); // determine les orientations possible OrientationHero orientation = OrientationAleatoire(orientationValide); // choisis un orientation Vector3Int targetPos = ObtenirTargetPosition(_grid.WorldToCell(transform.position), orientation); Vector3 destinationPos = CentrerPositionGrille(_grid.CellToWorld(targetPos)); float vitesse = _vitesse * Time.deltaTime; float _distanceRestante = DistanceRestanteCalcul(destinationPos); // bouge le personnage while (_distanceRestante > float.Epsilon) { transform.position = Vector3.MoveTowards(transform.position, destinationPos, vitesse); _distanceRestante = DistanceRestanteCalcul(destinationPos); yield return(null); } _mouvementCourant = _mouvementCourant - 1; } _mouvementCourant = _nbMouvement; TurnManager.instance.CompleterTour(TourPerso.Ennemi); //termine le tour ennemi }
void Start() { tileMap = transform.GetComponentInParent <Tilemap>(); availablePlaces = new List <Vector3>(); activeEnemies = new List <GameObject>(); for (int n = tileMap.cellBounds.xMin; n < tileMap.cellBounds.xMax; n++) { for (int p = tileMap.cellBounds.yMin; p < tileMap.cellBounds.yMax; p++) { Vector3Int localPlace = (new Vector3Int(n, p, (int)tileMap.transform.position.y)); Vector3 place = tileMap.CellToWorld(localPlace); if (tileMap.HasTile(localPlace)) { //Node node = new Node(); Debug.Log("Tile at: " + place); availablePlaces.Add(place); } else { //Debug.Log("No tile at: " + place); } } } }
private void GetTilemapTiles(Tilemap tileMap, Dictionary <Vector3, WorldTile> tileDict) { tileMap.CompressBounds(); Tilemap groundTile = instance.groundTilemap; foreach (Vector3Int pos in groundTile.cellBounds.allPositionsWithin) { var localPlace = new Vector3Int(pos.x, pos.y, pos.z); /*if (!tileMap.HasTile(localPlace){ * continue; * }*/ var tile = new WorldTile() { LocalPlace = localPlace, WorldLocation = Vector3Int.RoundToInt(groundTile.CellToWorld(localPlace)), TileBase = tileMap.GetTile(localPlace), TilemapMember = tileMap, Name = localPlace.x + "," + localPlace.y, WorldObject = tileMap == instance.objectTilemap ? GetObjectInCell(Vector3Int.RoundToInt(groundTile.CellToWorld(localPlace))) : null, DefaultWorldObjectData = (tileMap == instance.objectTilemap && GetObjectInCell(Vector3Int.RoundToInt(groundTile.CellToWorld(localPlace))) != null) ? GetObjectInCell(Vector3Int.RoundToInt(groundTile.CellToWorld(localPlace))).Data : null, WaterLevel = 0 }; if (tileDict.TryGetValue(tile.WorldLocation, out WorldTile tryTile)) { tryTile = tile; } else { tileDict.Add(tile.WorldLocation, tile); } } }
private void RelocateTiles(Tilemap source, Tilemap target) { var bounds = source.cellBounds; var positions = bounds.allPositionsWithin; var toErase = new List <Vector3Int>(); var toMerge = (new List <Vector3Int>(), new List <TileBase>()); foreach (var position in positions) { var tile = source.GetTile(position); if (tile != null) { var worldPosition = source.CellToWorld(position); var targetPosition = target.WorldToCell(worldPosition); toMerge.Item1.Add(targetPosition); toMerge.Item2.Add(tile); toErase.Add(position); } } source.SetTiles(toErase.ToArray(), Enumerable.Repeat <TileBase>(null, toErase.Count).ToArray()); target.SetTiles(toMerge.Item1.ToArray(), toMerge.Item2.ToArray()); }
void Start() { TilemapGO = gameObject; //Instantiate if ice, particlesystem on it if (isIce) { Vector3 tilePosition; Vector3Int coordinate = new Vector3Int(0, 0, 0); for (int i = 0; i < Tilemap.size.x; i++) //loop over every tile coordinate { for (int j = -100; j < Tilemap.size.y; j++) //kind of hacky to start y at -100. But basically adjust to whatever you need. { coordinate.x = i; coordinate.y = j; TileBase tempTile = Tilemap.GetTile(coordinate); //see if we have a tile there if (tempTile) //if we do, spawn effect there { tilePosition = Tilemap.CellToWorld(coordinate); ParticleSystem newEffect = Instantiate(iceParticle, tilePosition, Quaternion.identity); newEffect.transform.parent = TilemapGO.transform; } } } } }
// Start is called before the first frame update void Start() { animator = GetComponent <Animator>(); rb = GetComponent <Rigidbody2D>(); BoundsInt bounds = walls.cellBounds; TileBase[] allTiles = walls.GetTilesBlock(bounds); for (int x = 0; x < bounds.size.x; x++) { for (int y = 0; y < bounds.size.y; y++) { TileBase tile = allTiles[x + y * bounds.size.x]; if (tile == null) { continue; } Vector3 pos = walls.CellToWorld(new Vector3Int(x, y, 0)); Vector3 force = transform.position - pos; int charge = GetCharge(tile.name); force = force * charge * magnetStrength; rb.AddForce(force.normalized); } } InvokeRepeating("PlayAnim", animationPeriodicity, animationPeriodicity); }
public bool tilemapIsPlaceable(Tilemap tilemap, List <Vector3> tilemapPositions) { //check all tiles in the current selected object for (int r = tilemap.cellBounds.xMin; r < tilemap.cellBounds.xMax; r++) { for (int c = tilemap.cellBounds.yMin; c < tilemap.cellBounds.yMax; c++) { //local position in tilemap Vector3Int localPos = new Vector3Int(r, c, (int)tilemap.transform.position.z); //check there is a tile in the currect position if (tilemap.GetTile(localPos) != null) { //local position translated to world coordinates Vector3 worldPos = tilemap.CellToWorld(localPos); //if position already occupied then can't be placed if (occupiedSpaces.Contains(worldPos) || !isPointInPolygon(worldPos)) { return(false); } else { //add the world position to a list tilemapPositions.Add(worldPos); } } } } return(true); }
void Update() { if (GameManager.instance.currentGameState == GameManager.GameStates.PausedState) { return; } //SelectedTile listi için, 3 tane varolan image'ı dolrurur ve world pozisyonlarını bu listenin içindeki tileların world pozisyonlarıyla doldurur. if (selectedTiles.tileList.Count == 3) { for (int i = 0; i < selectedTileImages.Count; i++) { selectedTileImages[i].SetActive(true); TileClass selectedTile = selectedTiles.tileList[i]; Vector3 worldPos = tilemap.CellToWorld(new Vector3Int(selectedTile.x, selectedTile.y, 1)); selectedTileImages[i].transform.position = worldPos; } } else { for (int i = 0; i < selectedTileImages.Count; i++) { selectedTileImages[i].SetActive(false); } } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.W)) { changePlacingTile(tiletypes[1]); activePlacement = true; } if (Input.GetKeyDown(KeyCode.E)) { changePlacingTile(tiletypes[0]); activePlacement = true; } if (Input.GetMouseButton(1)) { activePlacement = false; mouseIcon.sprite = null; } if (Input.GetMouseButtonDown(0) && activePlacement) { Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10)); var tile = tiles.GetTile(tiles.WorldToCell(pos)); if (tile == airTile && tiles.GetTile(tiles.WorldToCell(pos) + new Vector3Int(0, -1, 0)) != airTile) { tiles.SetTile(tiles.WorldToCell(pos), placingBuilding.tile); var newRoom = new tileType(); newRoom.position = tiles.CellToWorld(tiles.WorldToCell(pos)); newRoom.cost = placingBuilding.cost; newRoom.time = placingBuilding.time; newRoom.occupied = false; RoomManager.instance.addRoom(newRoom); } } }
// Start is called before the first frame update void Start() { mainCam = GetComponentInParent <WaveManager>().mainCam; tileMap = GetComponentInParent <WaveManager>().tileMap; availablePlaces = new List <Vector3>(); for (int n = tileMap.cellBounds.xMin; n < tileMap.cellBounds.xMax; n++) { for (int p = tileMap.cellBounds.yMin; p < tileMap.cellBounds.yMax; p++) { Vector3Int localPlace = (new Vector3Int(n, p, (int)tileMap.transform.position.y)); Vector3 place = tileMap.CellToWorld(localPlace); place.z = -2; if (tileMap.HasTile(localPlace)) { //Tile at "place" availablePlaces.Add(place); } else { //No tile at "place" } } } }
private void SwitchToMining() { anim.SetBool("isRunning", false); anim.SetBool("isMining", true); Vector3Int cellPos = tilemap.WorldToCell(transform.position); Vector3 worldCellPos = tilemap.CellToWorld(cellPos); float animationOffset = -0.05f; if (worldCellPos.x < transform.position.x) { isoPos.Translate(isoPos.WorldToIso(new Vector3(animationOffset, 0f))); if (!FacingRight) { transform.Rotate(Vector3.up, 180); FacingRight = true; } } if (worldCellPos.x > transform.position.x) { isoPos.Translate(isoPos.WorldToIso(new Vector3(-animationOffset, 0f))); if (FacingRight) { transform.Rotate(Vector3.up, 180); FacingRight = false; } } }
void SpawnPedestrians(BoundsInt outerBoundsInt) { TileBase[] tiles = pedestrians.GetTilesBlock(outerBoundsInt); int marginWidth = maxSpawnMargin - minSpawnMargin; int farMarginX = outerBoundsInt.size.x - marginWidth; int farMarginY = outerBoundsInt.size.y - marginWidth; int carCount = Physics2D.OverlapAreaAll( (Vector3)outerBoundsInt.min, (Vector3)outerBoundsInt.max, layerMask).Length; foreach (int y in Enumerable.Range(0, outerBoundsInt.size.y).OrderBy(y => Random.value)) { foreach (int x in Enumerable.Range(0, outerBoundsInt.size.x).OrderBy(x => Random.value)) { // Only use tiles that are within the margin but not visible to the camera. if (carCount < targetCount && (x < marginWidth || y < marginWidth || x >= farMarginX || y >= farMarginY)) { TileBase tile = tiles[y * outerBoundsInt.size.x + x]; Vector3 point = pedestrians.CellToWorld( new Vector3Int(outerBoundsInt.xMin + x, outerBoundsInt.yMin + y, 0)) + halfCell; int dir = tile == null ? -1 : dirs.IndexOf(tile.name); if (dir > -1 && Physics2D.OverlapCircle(point, clearRadius, layerMask) == null) { PedestrianScript pedestrian = pedestrianPrefabs[Random.Range(0, pedestrianPrefabs.Length)]; Instantiate(pedestrian, point + new Vector3(0, 0, pedestrian.transform.position.z), Quaternion.Euler(0, 0, 0 * dir), transform); carCount += 1; } } } } }