public void Pawel() { foreach (var direction in Vector2IntUtilities.Neighbours8(Vector2Int.zero)) { Console.WriteLine(direction + ", " + ZRotationForLookAt2D(direction.x, direction.y)); } }
public void DwaDalej() { var position = new Vector2Int(3, 10); foreach (var vector2Int in Vector2IntUtilities.Neighbours8(Vector2Int.zero) .Select(v => new Vector2Int(v.x * 2, v.y * 2)) .Select(v => position + v)) { Console.WriteLine(vector2Int); } }
private int GetEmptyPositionsAround(ActorData actorData) { int emptyPositions = 8; var positionsAround = Vector2IntUtilities.Neighbours8(actorData.LogicalPosition); foreach (Vector2Int position in positionsAround) { if (!_gridInfoProvider.IsWalkable(position) || _entityDetector.DetectActors(position).Any()) { --emptyPositions; } } return(emptyPositions); }
private Vector2Int GetPositionToPlaceActor(Vector2Int position) { List <Vector2Int> candidates = Vector2IntUtilities.Neighbours8(position); var candidatesFurther = Vector2IntUtilities.Neighbours8(Vector2Int.zero) .Select(v => new Vector2Int(v.x * 2, v.y * 2)) .Select(v => position + v); candidates.AddRange(candidatesFurther); foreach (Vector2Int neighbour in candidates) { if (_gridInfoProvider.IsWalkable(neighbour) && !_entityDetector.DetectActors(neighbour).Any()) { return(neighbour); } } return(position); }
//and here's the one generating the whole map public bool CreateDungeon(Vector3Int gridBoundsMin, int inx, int iny, int inobj) { _offset = gridBoundsMin; this._objects = inobj < 1 ? 10 : inobj; // adjust the size of the map, if it's smaller or bigger than the limits if (inx < 3) { this._xsize = 3; } else if (inx > xmax) { this._xsize = xmax; } else { this._xsize = inx; } if (iny < 3) { this._ysize = 3; } else if (iny > ymax) { this._ysize = ymax; } else { this._ysize = iny; } Console.WriteLine(MsgXSize + this._xsize); Console.WriteLine(MsgYSize + this._ysize); Console.WriteLine(MsgMaxObjects + this._objects); // redefine the map var, so it's adjusted to our new map size this._dungeonMap = new GenTile[this._xsize * this._ysize]; // start with making the "standard stuff" on the map this.Initialize(); /******************************************************************************* * And now the code of the random-map-generation-algorithm begins! *******************************************************************************/ // start with making a room in the middle, which we can start building upon this.MakeRoom(this._xsize / 2, this._ysize / 2, _roomSizeMaxX, _roomSizeMaxY, RandomDirection()); // getrand saken f????r att slumpa fram riktning p?? rummet // keep count of the number of "objects" we've made int currentFeatures = 1; // +1 for the first room we just made // then we sart the main loop for (int countingTries = 0; countingTries < 1000; countingTries++) { // check if we've reached our quota if (currentFeatures == this._objects) { break; } // start with a random wall int newx = 0; int xmod = 0; int newy = 0; int ymod = 0; Direction?validTile = null; // 1000 chances to find a suitable object (room or corridor).. for (int testing = 0; testing < 1000; testing++) { newx = this.GetRand(1, this._xsize - 1); newy = this.GetRand(1, this._ysize - 1); if (GetCellTypeNoOffset(newx, newy) == GenTile.DirtWall || GetCellTypeNoOffset(newx, newy) == GenTile.Corridor) { var surroundings = this.GetSurroundings(new Vector2Int(newx, newy)); // check if we can reach the place var canReach = surroundings.FirstOrDefault(s => s.Item2.Item2 == GenTile.Corridor || s.Item2.Item2 == GenTile.DirtFloor); if (canReach == null) { continue; } validTile = canReach.Item2.Item1; switch (canReach.Item2.Item1) { case Direction.North: xmod = 0; ymod = -1; break; case Direction.East: xmod = 1; ymod = 0; break; case Direction.South: xmod = 0; ymod = 1; break; case Direction.West: xmod = -1; ymod = 0; break; default: throw new InvalidOperationException(); } // check that we haven't got another door nearby, so we won't get alot of openings besides // each other if (GetCellTypeNoOffset(newx, newy + 1) == GenTile.Door) // north { validTile = null; } else if (GetCellTypeNoOffset(newx - 1, newy) == GenTile.Door) // east { validTile = null; } else if (GetCellTypeNoOffset(newx, newy - 1) == GenTile.Door) // south { validTile = null; } else if (GetCellTypeNoOffset(newx + 1, newy) == GenTile.Door) // west { validTile = null; } // if we can, jump out of the loop and continue with the rest if (validTile.HasValue) { break; } } } if (validTile.HasValue) { // choose what to build now at our newly found place, and at what direction int feature = this.GetRand(0, 100); if (feature <= ChanceRoom) { // a new room if (this.MakeRoom(newx + xmod, newy + ymod, 8, 6, validTile.Value)) { currentFeatures++; // add to our quota // then we mark the wall opening with a door this.SetCell(newx, newy, GenTile.Door); // clean up infront of the door so we can reach it this.SetCell(newx + xmod, newy + ymod, GenTile.DirtFloor); } } else if (feature >= ChanceRoom) { // new corridor if (this.MakeCorridor(newx + xmod, newy + ymod, 6, validTile.Value)) { // same thing here, add to the quota and a door currentFeatures++; this.SetCell(newx, newy, GenTile.Door); } } } } /******************************************************************************* * All done with the building, let's finish this one off *******************************************************************************/ AddSprinkles(); // all done with the map generation, tell the user about it and finish Console.WriteLine(MsgNumObjects + currentFeatures); for (int y = 0; y < this._ysize; y++) { for (int x = 0; x < this._xsize; x++) { if (GetCellTypeNoOffset(x, y) == GenTile.Corridor) { List <Vector2Int> neighbours = Vector2IntUtilities.Neighbours8(new Vector2Int(x, y)); foreach (Vector2Int neighbour in neighbours) { GenTile neighbourCell = GetCellTypeNoOffset(neighbour.x, neighbour.y); if (neighbourCell == GenTile.Unused) { SetCell(neighbour.x, neighbour.y, GenTile.StoneWall); } } } } } return(true); }
private void PlaceTilesBasingOnDungeon(BoundsInt gridBounds, Dungeon generator) { bool stairsGenerated = false; foreach (Vector3Int position in gridBounds.allPositionsWithin) { Vector2Int position2D = position.ToVector2Int(); GenTile genTile = generator.GetCellType(position2D.x, position2D.y); switch (genTile) { case GenTile.DirtFloor: { _gameContext.DirtTilemap.SetTile(position, Dirt); if (_rng.Check(0.03f)) { _gameContext.EnvironmentTilemap.SetTile(position, _rng.Choice(FloorEnvironmetals)); } if (_rng.Check(0.06f)) { if (Vector2IntUtilities.Neighbours8(position2D).All(n => generator.GetCellType(n.x, n.y) == GenTile.DirtFloor)) { _gameContext.ObjectsTilemap.SetTile(position, _rng.Choice(WallEnvironmetals)); } } break; } case GenTile.Corridor: { _gameContext.DirtTilemap.SetTile(position, Dirt); break; } case GenTile.StoneWall: case GenTile.DirtWall: { _gameContext.WallsTilemap.SetTile(position, Wall); if (_rng.Check(0.04f)) { _gameContext.EnvironmentTilemap.SetTile(position, _rng.Choice(WallAttachmentEnvironmetals)); } break; } case GenTile.Upstairs: { _gameContext.DirtTilemap.SetTile(position, Dirt); _gameContext.EnvironmentTilemap.SetTile(position, StairsUp); _gameContext.WallsTilemap.SetTile(position, null); stairsGenerated = true; break; } case GenTile.Downstairs: { _gameContext.DirtTilemap.SetTile(position, Dirt); break; } case GenTile.Door: { _gameContext.DirtTilemap.SetTile(position, Dirt); GenTile tileToRight = generator.GetCellType(position.x + 1, position.y); bool isHorizontalDoor = tileToRight == GenTile.Corridor || tileToRight == GenTile.DirtFloor; bool doorsAreOpen = _rng.Check(0.3f); if (doorsAreOpen) { _gameContext.EnvironmentTilemap.SetTile(position, isHorizontalDoor ? DoorsHorizontalOpen : DoorsVerticalOpen); } else { _gameContext.WallsTilemap.SetTile(position, isHorizontalDoor ? DoorsHorizontalClosed : DoorsVerticalClosed); } break; } default: { break; } } } if (!stairsGenerated) { BoundsInt randomRoom = _rng.Choice(generator.Rooms); Vector3Int center = BoundsIntUtilities.Center(randomRoom).ToVector3Int(); _gameContext.DirtTilemap.SetTile(center, Dirt); _gameContext.EnvironmentTilemap.SetTile(center, StairsUp); _gameContext.WallsTilemap.SetTile(center, null); Debug.Log("Missing stairs in dungeon! Generating in random room on position: " + center); } }
private void GenerateActorsInDungeon(Dungeon currentDungeon, DungeonConfig dungeonConfig, bool isFirstDungeon) { BoundsInt playerRoom = new BoundsInt(); if (isFirstDungeon) { BoundsInt furthestRoomToStairs = FurthestRoomToStairsResolver.GetFurthestRoomToStairs(currentDungeon); playerRoom = furthestRoomToStairs; BoundsInt roomToSpawnPlayerIn = playerRoom; Vector2Int playerPosition = BoundsIntUtilities.Center(roomToSpawnPlayerIn); Vector2Int breadPosition = Vector2Int.zero; var neighboursRebourse = Vector2IntUtilities.Neighbours8(playerPosition).ToList(); neighboursRebourse.Reverse(); foreach (var neighbour in neighboursRebourse) { if (_gridInfoProvider.IsWalkable(neighbour) && neighbour != playerPosition) { _entitySpawner.SpawnItem(BreadItem, neighbour); breadPosition = neighbour; break; } } foreach (var neighbour in neighboursRebourse) { if (neighbour != breadPosition && _gridInfoProvider.IsWalkable(neighbour)) { _entitySpawner.SpawnItem(KeyItem, neighbour); break; } } BoundsInt aroundPlayerRoom = new BoundsInt(playerRoom.position - new Vector3Int(1, 1, 0), playerRoom.size + new Vector3Int(2, 2, 0)); foreach (Vector3Int positionInPlayerRoom in aroundPlayerRoom.allPositionsWithin) { if (_gameContext.WallsTilemap.GetTile(positionInPlayerRoom) == DoorsVerticalClosed || _gameContext.EnvironmentTilemap.GetTile(positionInPlayerRoom) == DoorsVerticalOpen) { _gameContext.WallsTilemap.SetTile(positionInPlayerRoom, HeavyDoorsVerticalClosed); _gameContext.EnvironmentTilemap.SetTile(positionInPlayerRoom, null); } if (_gameContext.WallsTilemap.GetTile(positionInPlayerRoom) == DoorsHorizontalClosed || _gameContext.EnvironmentTilemap.GetTile(positionInPlayerRoom) == DoorsHorizontalOpen) { _gameContext.WallsTilemap.SetTile(positionInPlayerRoom, HeavyDoorsHorizontalClosed); _gameContext.EnvironmentTilemap.SetTile(positionInPlayerRoom, null); } } var playerActorBehaviour = _entitySpawner.SpawnActor(ActorType.Player, playerPosition); playerActorBehaviour.ActorData.ControlledByPlayer = true; _gameContext.PlayerActor = playerActorBehaviour; _gameConfig.FollowPlayerCamera.Follow = playerActorBehaviour.transform; _uiConfig.Arrows.transform.parent = playerActorBehaviour.transform; _uiConfig.Arrows.transform.localPosition = new Vector3(0, -0.1f, 0); } foreach (BoundsInt room in currentDungeon.Rooms.Skip(isFirstDungeon ? 1 :0)) { if (room == playerRoom) { continue; } float populationValue = _rng.NextFloat(); int populationInRoom = Mathf.RoundToInt(dungeonConfig.ChanceToRoomPopulation.Evaluate(populationValue)); for (int i = 0; i < populationInRoom; i++) { ActorDefinition[] actorTypesAvailable = dungeonConfig.EnemiesToSpawn; ActorType actorTypeChosen = _rng.Choice(actorTypesAvailable).ActorType; Vector2Int centralPosition = BoundsIntUtilities.Center(room); _entitySpawner.SpawnActor(actorTypeChosen, centralPosition); } } _entitySpawner.SpawnActor(dungeonConfig.BossToSpawn.ActorType, currentDungeon.StairsLocation, true); }