public bool TryToPlaceFeatureRandomly(GP[,] feature, ref GP[,] map) { var randomH = UnityEngine.Random.Range(0, map.GetLength(0)-feature.GetLength(0)); var randomV = UnityEngine.Random.Range(0, map.GetLength(1)-feature.GetLength(1)); if(CanPlaceFeature(feature, ref map, randomH, randomV)) { PlaceFeature(feature, ref map, randomH, randomV); return true; } return false; }
public void PlaceFeature(GP[,] feature, ref GP[,] map, int hPos, int vPos) { for (var h = hPos; h < hPos + feature.GetLength(0); ++h) { for (var v = vPos; v < vPos + feature.GetLength(1); ++v) { map[h,v] = feature[h-hPos, v-vPos]; } } }
public void CreateGameBoard(GP[,] pieceArray) { var board = GameManager.Instance.Board; var width = pieceArray.GetLength(0); var height = pieceArray.GetLength(1); board.CreateBoard(width, height); for (var h = 0; h < width; ++h) { for (var v = 0; v < height; ++v) { // create the piece var piece = GameObject.Instantiate(PrefabFromGP(pieceArray[h,v])); // always add a floor piece even if piece isn't a floor piece. var isFloor = piece.IsFloorPiece; if (!isFloor) { var floorFirst = GameObject.Instantiate(FloorPiecePrefab); board.SetPiece(h, v, floorFirst); } // tell the game NonFloorPieces board.SetPiece(h, v, piece); } } }
public GP[,] GenerateMap() { GP[,] generatedMap = new GP[7*20, 7*20]; // mark every area as unset for(var i = 0; i < generatedMap.GetLength(0); ++i) for(var j = 0; j < generatedMap.GetLength(1); ++j) generatedMap[i,j] = GP.I; // place the white house randomly (special one time only chunk) var presidentPlaced = false; while(!presidentPlaced) { presidentPlaced = TryToPlaceFeatureRandomly(whiteHouse, ref generatedMap); } // Try to add some other features! The map is 140x140, keep in mind feature size and the fact that some will fail var features = new GP[][,] { survivorSmallHouse /* 7 */, bigHouse /* 14 */, emptyStreetWithDebrisOrTrees /* 7 */ }; var maxToPlace = new int[]{ 30, 20, 40 }; for (var i = 0; i < features.Length; ++i) { var addedCount = 0; for (var tries = 0; tries < maxToPlace[i]; ++tries) { addedCount += TryToPlaceFeatureRandomly(features[i], ref generatedMap) ? 1 : 0; } Debug.Log(string.Format("Feature {2}: added {0}/{1}", addedCount, maxToPlace[i], i)); } // Place some random starting zombies. var startingZombies = 2; var zombiesAdded = 0; while (zombiesAdded < startingZombies) { zombiesAdded += TryToPlaceFeatureRandomly(zombie, ref generatedMap) ? 1 : 0; } // Replace all remaining empty spaces with floors for (var i = 0; i < generatedMap.GetLength(0); ++i) for (var j = 0; j < generatedMap.GetLength(1); ++j) if (generatedMap[i, j] == GP.I) generatedMap[i, j] = GP.F; return generatedMap; }
public bool CanPlaceFeature(GP[,] feature, ref GP[,] map, int hPos, int vPos) { for (var h = hPos; h < hPos + feature.GetLength(0); ++h) { for (var v = vPos; v < vPos + feature.GetLength(1); ++v) { if(map[h,v] != GP.I) return false; } } return true; }