void SetTile(int tileLayer, MapCoor coor, char c) { if (coor.x > mapWidth) { coor.x -= mapWidth; } else if (coor.x < 0) { coor.x += mapWidth; } if (coor.y > mapHeight) { coor.y -= mapHeight; } else if (coor.y < 0) { coor.y += mapHeight; } switch (tileLayer) { case (0): groundGrid[(coor.x) % mapWidth, (coor.y) % mapHeight] = c; break; case (1): walkLevelGrid[(coor.x) % mapWidth, (coor.y) % mapHeight] = c; break; case (2): dontCheckGrid[(coor.x) % mapWidth, (coor.y) % mapHeight] = c; break; } }
public char GetTile(MapCoor coor, bool isGround) { if (coor.x < 0) { coor.x = mapWidth + coor.x; } else if (coor.x >= mapWidth) { coor.x = coor.x - mapWidth; } if (coor.y < 0) { coor.y = mapHeight + coor.y; } else if (coor.y >= mapHeight) { coor.y = coor.y - mapHeight; } if (isGround) { return(groundGrid[coor.x, coor.y]); } else { return(walkLevelGrid[coor.x, coor.y]); } }
// void CreateMapFeature(FeatureTypes featureType, MapCoor center) { string[] mapFeature; switch (featureType) { case (FeatureTypes.FOREST_CIRCLE): mapFeature = MapFeatures.forestCircle; break; default: mapFeature = MapFeatures.forestCircle; break; } MapCoor featureCenter = new MapCoor(mapFeature[0].Length / 2, mapFeature.Length / 2); for (int row = 0; row < mapFeature.Length; row++) { char[] rowOfChars = mapFeature[row].ToCharArray(); for (int col = 0; col < rowOfChars.Length; col += 3) { if (rowOfChars[col] == ' ') { SetTile(1, new MapCoor(center.x + col / 3, center.y + row), '\0'); } else { SetTile(1, new MapCoor(center.x + col / 3, center.y + row), rowOfChars[col]); } } } }
// 取得隨機地圖拾取位置 public MapCoor NextPickup() { if (DataMap.pthis.DataRoad.Count <= GameDefine.iPickupBorder) { return(new MapCoor()); } MapCoor Road = DataMap.pthis.DataRoad[Random.Range(GameDefine.iPickupBorder, DataMap.pthis.DataRoad.Count - GameDefine.iPickupBorder)]; for (int iCount = 0; iCount < GameDefine.iPickupSearch; ++iCount) { MapCoor Result = new MapCoor(Road.X + Random.Range(-GameDefine.iPackupRange, GameDefine.iPackupRange), Road.Y + Random.Range(0, GameDefine.iPackupRange)); bool bCheck = true; foreach (MapCoor Itor in DataMap.pthis.DataRoad) { bCheck &= (Result.X == Itor.X && Result.Y == Itor.Y) == false; } foreach (Pickup Itor in DataPickup.pthis.Data) { bCheck &= (Result.X == Itor.Pos.X && Result.Y == Itor.Pos.Y) == false; } if (bCheck) { return(Result); } } //for return(new MapCoor()); }
// A land circle is mostly comprised of groundType, but has sand and coast around the outside if hasCoast is true. // The greater patchy is, the more empty spots the circle will have. void CreateCircle(MapCoor center, int radius, char groundType, bool hasCoast = true, int patchy = 0) { //groundGrid[(int)center.x, (int)center.y] = groundType; float area = 3.14f * radius * radius; float theta = 0; float deltaTheta = 3.14f / (radius * 5); for (; theta < 6.28f; theta += deltaTheta) { for (int r = 0; r < radius; r++) { int x = (int)Mathf.Round(center.x + Mathf.Cos(theta) * r); int y = (int)Mathf.Round(center.y + Mathf.Sin(theta) * r); // Make sure the new tile is within the map. If not, it will wrap around to the other side. if (x < 0) { x = mapWidth + x; } else if (x >= mapWidth) { x = x - mapWidth; } if (y < 0) { y = mapHeight + y; } else if (y >= mapHeight) { y = y - mapHeight; } // Set the ground tile char currTile = GetGroundTileAtCoor(new MapCoor(x, y)); bool isSparse = 0 != UnityEngine.Random.Range(0, patchy + 1); if (!isSparse) { if (CharIsGround(groundType)) { groundGrid[x, y] = groundType; } else { // The circle is on the interactable level, such as trees or something if (groundGrid[x, y] != 's' && groundGrid[x, y] != 'd' && CharIsGround(groundGrid[x, y])) { // interactable things can only be placedd on ground that isn't coast walkLevelGrid[x, y] = groundType; } } } } } }
Vector3 ClampToPixel(MapCoor nextCoor) { Vector3 newPos = new Vector3( currCoor.x * 16, currCoor.y * -16 + 4, transform.position.z); if (clampToPixel) { return(new Vector3(Mathf.Round(newPos.x), Mathf.Round(newPos.y), newPos.z)); } else { return(newPos); } }
// void CreateMapFeature(FeatureCenterPair fcp) { FeatureTypes featureType = fcp.featureType; MapCoor center = fcp.center; string[] mapFeature = MapFeatures.GetFeature(featureType); int layer = 1; if (fcp.featureType == FeatureTypes.CASTLE) { layer = 2; } if (fcp.mirrored) { string[] copy = mapFeature; for (int i = 0; i < copy.Length; i++) { mapFeature[i] = new string(mapFeature[i].ToCharArray().Reverse().ToArray()); for (int j = 0; j < copy[0].Length; j += 3) { // mapFeature[i].ToCharArray()[j] = copy[i].ToCharArray()[copy[0].Length - j - 1]; } } } for (int row = 0; row < mapFeature.Length; row++) { char[] rowOfChars = mapFeature[row].ToCharArray(); for (int col = 0; col < rowOfChars.Length; col += 3) { if (rowOfChars[col] == ' ') { SetTile(layer, new MapCoor(center.x + col / 3, center.y + row), '\0'); } else { SetTile(layer, new MapCoor(center.x + col / 3, center.y + row), rowOfChars[col]); } } } }
// ------------------------------------------------------------------ // 產生肉肉. public void CreateMeet(MapCoor pMapCoor) { Pickup pData = new Pickup(); pData.Pos = pMapCoor; pData.iType = (int)ENUM_Pickup.Meet; pData.iCount = 1; pData.iLooks = 0; pData.bPickup = false; DataPickup.pthis.Data.Add(pData); GameObject pObjPickup = UITool.pthis.CreatePickup(PlayerCreater.pthis.gameObject, (ENUM_Pickup)pData.iType, 0, 0); pObjPickup.transform.position = MapCreater.pthis.GetMapObj(pMapCoor.ToVector2()).transform.position; Data.Add(pObjPickup); DataPickup.pthis.Save(); }
public MapCoor Add(ENUM_Dir emDir, int iInterval) { MapCoor Result = new MapCoor(X, Y); switch (emDir) { case ENUM_Dir.Up: Result.Y += iInterval; break; case ENUM_Dir.Left: Result.X -= iInterval; break; case ENUM_Dir.Right: Result.X += iInterval; break; case ENUM_Dir.Down: Result.Y -= iInterval; break; default: break; } //switch return(Result); }
// ------------------------------------------------------------------ public void CreateByStandOutRoad(int iMonster, int iRoad_Min, int iRoad_Max) { // 取得路編號. int iRoad = CameraCtrl.pthis.iNextRoad + Random.Range(iRoad_Min, iRoad_Max); if (iRoad > DataMap.pthis.DataRoad.Count - 1) { return; } // 取得路資料. MapCoor Road = DataMap.pthis.DataRoad[iRoad]; for (int iCount = 0; iCount < GameDefine.iPickupSearch; ++iCount) { MapCoor Result = new MapCoor(Road.X + Random.Range(-5, 6), Road.Y + Random.Range(0, 3)); bool bCheck = true; // 確認是否為道路. foreach (MapCoor Itor in DataMap.pthis.DataRoad) { bCheck &= (Result.X == Itor.X && Result.Y == Itor.Y) == false; } bCheck &= (MapCreater.pthis.GetMapObj(Result.ToVector2()) != null); if (bCheck) { if (iMonster == 0) { PickupCreater.pthis.CreateMeet(Result); } else { GameObject pObjMon = CreateOneEnemy(iMonster, -1, Vector2.zero); pObjMon.transform.position = MapCreater.pthis.GetMapObj(Result.ToVector2()).transform.position; } return; } } }
bool TestAdjacent(MapCoor current, Direction direction) { MapCoor possiblePathPoint = GetAdjacent(current, startPathDir); if (mazeArray[Mathf.Abs(possiblePathPoint.x % mazeWidth), Mathf.Abs(possiblePathPoint.y % mazeHeight)] == '\0') { testAdjacent = possiblePathPoint; return(true); } else { // the point already has a path on it if (--errorCheck > 0) { errorCheck = 8; errors++; return(false); } return(TestAdjacent(current, direction)); } }
public void SetCoor(MapCoor curr) { if (curr.x < 0) { curr.x += MapGenerator.mapWidth; } else { curr.x = curr.x % MapGenerator.mapWidth; } if (curr.y < 0) { curr.y += MapGenerator.mapWidth; } else { curr.y = curr.y % MapGenerator.mapWidth; } currCoor = curr; }
MapCoor GetAdjacent(MapCoor current, Direction direction) { switch (direction) { case (Direction.LEFT): current.x--; break; case (Direction.RIGHT): current.x++; break; case (Direction.DOWN): current.y--; break; case (Direction.UP): current.y++; break; } return(current); }
char GetGroundTileAtCoor(MapCoor coor) { int x = coor.x; int y = coor.y; if (x < 0) { x = mapWidth + x; } else if (x >= mapWidth) { x = x - mapWidth; } if (y < 0) { y = mapHeight + y; } else if (y >= mapHeight) { y = y - mapHeight; } return(groundGrid[x, y]); }
// 更新地圖 public void Refresh(int iRoad) { MapCoor RoadPos = DataMap.pthis.DataRoad.Count > iRoad ? DataMap.pthis.DataRoad[iRoad] : new MapCoor(); // 建立新物件 for (int iX = -GameDefine.iMapBorder; iX <= GameDefine.iMapBorder; ++iX) { for (int iY = -GameDefine.iMapBorder; iY <= GameDefine.iMapBorder; ++iY) { MapCoor Pos = new MapCoor(RoadPos.X + iX, RoadPos.Y + iY); if (Pos.X < 0 || Pos.Y < 0) { continue; } Vector2 PosVec = Pos.ToVector2(); if (Data.ContainsKey(PosVec)) { continue; } if (DataMap.pthis.DataObjt.ContainsKey(PosVec) == false) { continue; } MapObjt pObjt = DataMap.pthis.DataObjt[PosVec]; float fPosX = PosVec.x + (pObjt.Width * GameDefine.iBlockSize) / 2; float fPosY = PosVec.y + (pObjt.Height * GameDefine.iBlockSize) / 2; Data.Add(PosVec, UITool.pthis.CreateMap(gameObject, pObjt.Type, DataPlayer.pthis.iStyle, fPosX, fPosY)); } //for } //for }
// 建立地圖物件 private void CreateObjt() { // 建立道路物件 for (int iPos = 0; iPos < DataMap.pthis.DataRoad.Count; ++iPos) { MapCoor Pos = DataMap.pthis.DataRoad[iPos]; // 判斷是否該建立起點物件 if (iPos == 0) { MapObjt Temp = new MapObjt(); Temp.Pos = new MapCoor(Pos.X, Pos.Y - 1); Temp.Type = (int)ENUM_Map.MapStart; Temp.Width = GameDefine.ObjtStart.X; Temp.Height = GameDefine.ObjtStart.Y; DataMap.pthis.DataObjt.Add(Temp.Pos.ToVector2(), Temp); } //if // 判斷是否該建立終點物件 if (iPos == DataMap.pthis.DataRoad.Count - 1) { MapObjt Temp = new MapObjt(); Temp.Pos = new MapCoor(Pos.X - 1, Pos.Y + 1); Temp.Type = (int)ENUM_Map.MapEnd; Temp.Width = GameDefine.ObjtEnd.X; Temp.Height = GameDefine.ObjtEnd.Y; DataMap.pthis.DataObjt.Add(Temp.Pos.ToVector2(), Temp); } //if // 建立道路物件 { MapObjt Temp = new MapObjt(); Temp.Pos = new MapCoor(Pos.X, Pos.Y); Temp.Type = (int)ENUM_Map.MapRoad; Temp.Width = GameDefine.ObjtBase.X; Temp.Height = GameDefine.ObjtBase.Y; DataMap.pthis.DataObjt.Add(Temp.Pos.ToVector2(), Temp); } } //for // 建立一般物件 foreach (MapCoor Itor in DataMap.pthis.DataRoad) { for (int iCount = 0; iCount < GameDefine.iObjDensity; ++iCount) { MapCoor Pos = new MapCoor(Itor.X + Random.Range(-GameDefine.iMapBorder, GameDefine.iMapBorder + 1), Itor.Y + Random.Range(-GameDefine.iMapBorder, GameDefine.iMapBorder + 1)); if (Itor.X == Pos.X && Itor.Y == Pos.Y) { continue; } int iIndex = Random.Range(0, GameDefine.ObjtScale.Count); MapObjt Temp = new MapObjt(); Temp.Pos = Pos; Temp.Type = iIndex + (int)ENUM_Map.MapObjt_0; Temp.Width = GameDefine.ObjtScale[iIndex].X; Temp.Height = GameDefine.ObjtScale[iIndex].Y; if (CheckCover(Temp)) { DataMap.pthis.DataObjt.Add(Temp.Pos.ToVector2(), Temp); } } //for } //for // 填滿地圖 foreach (MapCoor Itor in DataMap.pthis.DataRoad) { for (int iX = -GameDefine.iMapBorder; iX <= GameDefine.iMapBorder; ++iX) { for (int iY = -GameDefine.iMapBorder; iY <= GameDefine.iMapBorder; ++iY) { MapCoor Pos = new MapCoor(Itor.X + iX, Itor.Y + iY); if (Pos.X < 0 || Pos.Y < 0) { continue; } if (DataMap.pthis.DataObjt.ContainsKey(Pos.ToVector2())) { continue; } MapObjt Temp = new MapObjt(); Temp.Pos = Pos; Temp.Type = (int)ENUM_Map.MapBase; Temp.Width = GameDefine.ObjtBase.X; Temp.Height = GameDefine.ObjtBase.Y; if (CheckCover(Temp)) { DataMap.pthis.DataObjt.Add(Temp.Pos.ToVector2(), Temp); } } //for } //for } //for }
public FeatureCenterPair(MapCoor center, FeatureTypes featureType, bool mirrored = false) { this.center = center; this.featureType = featureType; this.mirrored = mirrored; }
void TestGenerateMap(int width, int height, int numContinents, float continentSize) { groundGrid = new char[width, height]; walkLevelGrid = new char[width, height]; dontCheckGrid = new char[width, height]; mapHeight = height; mapWidth = width; // A map must always have a starting zone, but the other continents are mostly random. // ContinentType[] cTypes = new ContinentType[numContinents]; int rand = UnityEngine.Random.Range(0, numContinents); cTypes[rand] = ContinentType.STARTING_AREA; // Make sure the continents are varied; // if there is already a mountain, for example, a different type will be picked unless all types have been picked /* * for (int i = 1; i < numContinents; i++) * { * int randContinent = UnityEngine.Random.Range(0, numContinents); * while (cTypes[randContinent] != ContinentType.None) * { * randContinent = UnityEngine.Random.Range(0, numContinents); * } * * if (!isMountain) * { * cTypes[randContinent] = ContinentType.MOUNTAINOUS; * isMountain = true; * } * else * { * cTypes[randContinent] = (ContinentType)Random.Range(1, 3); * } * } */ MapCoor mapCenter = new MapCoor(mapWidth / 2, mapHeight / 2); CreateMapFeature(FeatureTypes.FOREST_CIRCLE, new MapCoor(mapWidth / 2, mapHeight / 2)); /* * for (int continent = 0; continent < numContinents - 0; continent++) * { * MapCoor continentStartPos = new MapCoor((int)(mapCenter.x + fromMapCenterRadius * Mathf.Cos(theta)), (int)(mapCenter.y + fromMapCenterRadius * Mathf.Sin(theta))); * //Debug.Log(string.Format("Type: {3}, Radius = {0}, theta = {1}, Center = {2}", fromMapCenterRadius, theta, continentStartPos, cTypes[continent])); * * // Continents are created in a circle around the center of the map * /**EXAMPLE: * WIDTH numContinents = 3 * _____________________ * H | | * E | C3 C2 | * I | | * G | C1 | * H | C4 | * T | C5 | * |_____________________| * * * * */ /* * int circleRadius = (int)(ratio * continentSize * 2.5f); * * * // A continent is formed by several circles of varying size built off of each other. * CreateContinent(continentStartPos.ToVector(), circleRadius, 8, cTypes[continent]); * * // Calculations for next circle * theta += 6.28f / (float)(numContinents); * fromMapCenterRadius = defaultRadius - Mathf.Abs(Mathf.Sin(theta) * defaultRadius * (1 - 1 / ratio)); * }*/ //BuildCoast(); StartCoroutine(InstantiateTiles()); }
public static bool AdvanceStepCount(Vector2 currPos) { MapCoor currCoor = new MapCoor((int)currPos.x / 16, (int)currPos.y / 16); if (encountersEnabled) { char groundType = 'g'; ContinentType currArea = ContinentType.GRASSLAND; if (MazeGenerator.inMaze) { groundType = MazeGenerator.groundType; switch (groundType) { case ('g'): encounterStep -= 1; currArea = ContinentType.FOREST; break; case ('\0'): encounterStep -= 1; currArea = ContinentType.OCEAN; break; default: encounterStep--; currArea = ContinentType.DESERT; break; } } else { currCoor.x %= MapGenerator.mapWidth; currCoor.y %= MapGenerator.mapHeight; currCoor.y = Mathf.Abs(currCoor.y) + 1; groundType = MapGenerator.mg.GetTile(currCoor, true); switch (groundType) { case ('g'): encounterStep -= 1; break; case ('s'): encounterStep -= 1; currArea = ContinentType.DESERT; break; case ('r'): encounterStep -= 2; currArea = ContinentType.GRASSLAND; break; case ('\0'): encounterStep -= 1; currArea = ContinentType.OCEAN; break; case ('o'): encounterStep -= 1; currArea = ContinentType.VOLCANO; break; default: encounterStep--; break; } } GameManager.currAreaName = currArea; if (encounterStep < 0) { encounterStep = UnityEngine.Random.Range(8, 24); BattleManager.bManager.StartBattle(); return(true); } return(false); } return(false); }
void CreateContinent(Vector2 center, int largestRadius, int complexity, ContinentType continentType, bool hasCoast = true) { Vector2 newCenter = center; Vector2 tempCenter = center; float curveAngle = 0; // The continents generally twist inwards // if the center is high on the map if (center.y > groundGrid.GetLength(1) / 2) { curveAngle = 270 + UnityEngine.Random.Range(-3.14f / 2, 3.14f / 2); } else { //if the center is low on the map curveAngle = 90 + UnityEngine.Random.Range(-3.14f / 2, 3.14f / 2); } curveAngle = UnityEngine.Random.Range(0, 360); char groundType = 'g'; switch (continentType) { case (ContinentType.GRASSLAND): if (GameManager.gm.leader == null) { GameManager.gm.GetComponent <HeroPartyManager>() .AddKnight(center); knightStartPoint = new MapCoor((int)center.x, (int)center.y); walkLevelGrid[knightStartPoint.x, knightStartPoint.y] = '-'; dontCheckGrid[knightStartPoint.x, knightStartPoint.y] = '-'; //leaderCoorX = (int) center.x / 16; //leaderCoorY = (int)(Mathf.Abs(center.y / 16)); // @@@@ Place test tiles @@@@ dontCheckGrid[(int)center.x - 6, (int)center.y] = '&'; dontCheckGrid[(int)center.x - 5, (int)center.y] = '&'; //featuresToPlaceOnMap.Add(new FeatureCenterPair(new MapCoor((int)center.x + 4, (int)center.y - 4), FeatureTypes.CASTLE)); } else { } break; case (ContinentType.MOUNTAIN): dontCheckGrid[(int)center.x, (int)center.y + 2] = '$'; groundType = 'r'; // TODO break; case (ContinentType.DESERT): dontCheckGrid[(int)center.x, (int)center.y + 2] = '#'; groundType = 's'; // TODO break; case (ContinentType.GLACIER): dontCheckGrid[(int)center.x, (int)center.y + 2] = '@'; groundType = 'h'; // TODO break; case (ContinentType.VOLCANO): dontCheckGrid[(int)center.x, (int)center.y + 2] = '%'; groundType = 'o'; // TODO break; } List <Vector2> circleCenters = new List <Vector2>(); for (int circleNum = 0; circleNum < complexity; circleNum++) { CreateCircle(new MapCoor((int)tempCenter.x, (int)tempCenter.y), largestRadius, groundType, hasCoast); circleCenters.Add(tempCenter); int radius = largestRadius; int randGeoFeature = UnityEngine.Random.Range(0, 14); // Create geographical features particular to the continentType; // this is where the majority of the environmental features // will be implemented switch (continentType) { case (ContinentType.GRASSLAND): if (circleNum == 3 && darkForestCounter-- == 0) { featuresToPlaceOnMap.Add(new FeatureCenterPair( new MapCoor(tempCenter), FeatureTypes.DARK_FOREST)); } if (randGeoFeature < 6) { // create a forest CreateForest(tempCenter, largestRadius, 10); } else if (randGeoFeature < 12) { // create a forest circle (TODO) if (--forestCircleCounter < 0) { if (radius > 5) { featuresToPlaceOnMap.Add(new FeatureCenterPair( new MapCoor(tempCenter), FeatureTypes.FOREST_CIRCLE)); forestCircleCounter = forestCircleFrequency; } else { forestCircleCounter = 0; } } } else { // Place a chest walkLevelGrid[(Mathf.Abs((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'c'; } break; case (ContinentType.GLACIER): if (randGeoFeature < 5) { // Place a penguin salesman walkLevelGrid[(Mathf.Abs((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = '1'; } else if (randGeoFeature < 10) { // Place an igloo walkLevelGrid[(Mathf.Abs((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'e'; } else if (randGeoFeature > 11) { // Place a chest walkLevelGrid[(Mathf.Abs((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'c'; } break; case (ContinentType.MOUNTAIN): if (randGeoFeature < 5) { // Place a mouuntain walkLevelGrid[Mathf.Abs(((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'm'; } else if (randGeoFeature > 11) { // Place a chest walkLevelGrid[(Mathf.Abs((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'c'; } break; case (ContinentType.DESERT): if (randGeoFeature < 4) { // Place a pyramid walkLevelGrid[((int)tempCenter.x) % mapWidth, ((int)tempCenter.y) % mapHeight] = '^'; } else if (randGeoFeature < 11) { // Place a cactus walkLevelGrid[((int)tempCenter.x) % mapWidth, ((int)tempCenter.y) % mapHeight] = '*'; } else if (randGeoFeature > 11) { // Place a chest walkLevelGrid[(Mathf.Abs((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'c'; } break; case (ContinentType.VOLCANO): if (!castlePlaced && circleNum > 3) { castlePlaced = true; featuresToPlaceOnMap.Add(new FeatureCenterPair(new MapCoor((int)center.x + 4, (int)center.y - 4), FeatureTypes.CASTLE)); } else if (randGeoFeature < 5) { // Place a mouuntain walkLevelGrid[Mathf.Abs(((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'm'; } else if (randGeoFeature > 11) { // Place a chest walkLevelGrid[(Mathf.Abs((int)tempCenter.x) % mapWidth), Mathf.Abs(((int)tempCenter.y) % mapHeight)] = 'c'; } break; } // Features that are only added with the last land circle if (circleNum == complexity - 1) { switch (continentType) { case (ContinentType.GRASSLAND): if (numAirshipSalesmenPlaced++ < numAirshipSalesmen) { MapCoor placeToPutAirship = new MapCoor((int)tempCenter.x, (int)tempCenter.y); int leftOrRight = UnityEngine.Random.Range(-1, 2); if (leftOrRight == 0) { leftOrRight++; } while (GetTile(placeToPutAirship, true) != '\0') { placeToPutAirship.x += leftOrRight; } placeToPutAirship.x -= leftOrRight; if (leftOrRight < 0) { featuresToPlaceOnMap.Add(new FeatureCenterPair(placeToPutAirship, FeatureTypes.AIRSHIP_SALESMAN)); } else { featuresToPlaceOnMap.Add(new FeatureCenterPair(placeToPutAirship, FeatureTypes.AIRSHIP_SALESMAN, true)); } } break; default: break; } } // Decide where the next circle is // tempCenter is the next circle's center. curveAngle += UnityEngine.Random.Range(-3.14f / 6, 3.14f / 6); int whichCircleToBuildOffOf = UnityEngine.Random.Range(0, 3); if (whichCircleToBuildOffOf != 0) { // Create a circle on the perimeter of a random circle that has already been built // I haven't tested how well this actually works. Vector2 randCenter = circleCenters[UnityEngine.Random.Range(0, circleCenters.Count)]; int tempCurvAngle = UnityEngine.Random.Range(0, 360); tempCenter = new Vector2(randCenter.x + radius * Mathf.Cos(tempCurvAngle) * 1.2f, randCenter.y + radius * Mathf.Sin(tempCurvAngle) * 1.2f); } else { // Create a circle in the direction the ccontinent is generally going towards newCenter = new Vector2(newCenter.x + radius * Mathf.Cos(curveAngle), newCenter.y + radius * Mathf.Sin(curveAngle)); tempCenter = newCenter; } // The next circle is smaller. largestRadius = Mathf.RoundToInt(radius * .8f); } }
void GenerateMap(int width, int height, int numContinents, float continentSize) { //Debug.Log(string.Format("Width {0}, height {1}, numContinents {2}, continentSize {3}", width, height, numContinents, continentSize)); groundGrid = new char[width, height]; walkLevelGrid = new char[width, height]; dontCheckGrid = new char[width, height]; instantiatedTiles = new List <GameObject> [width, height]; mapHeight = height; mapWidth = width; // A map must always have a starting zone, but the other continents are mostly random. // ContinentType[] cTypes = new ContinentType[numContinents]; cTypes[0] = ContinentType.GRASSLAND; // Make sure the continents are varied; cTypes[1] = ContinentType.DESERT; cTypes[2] = ContinentType.GLACIER; cTypes[3] = ContinentType.MOUNTAIN; cTypes[4] = ContinentType.VOLCANO; MapCoor mapCenter = new MapCoor(mapWidth / 2, mapHeight / 2); float fromMapCenterRadius = mapWidth / 3; float defaultRadius = fromMapCenterRadius; float theta = 0; float ratio = (float)mapWidth / mapHeight; for (int continent = 0; continent < numContinents - 0; continent++) { MapCoor continentStartPos = new MapCoor((int)(mapCenter.x + fromMapCenterRadius * Mathf.Cos(theta)), (int)(mapCenter.y + fromMapCenterRadius * Mathf.Sin(theta))); //Debug.Log(string.Format("Type: {3}, Radius = {0}, theta = {1}, Center = {2}", fromMapCenterRadius, theta, continentStartPos, cTypes[continent])); // Continents are created in a circle around the center of the map /**EXAMPLE: * WIDTH numContinents = 5 * _____________________ * H | | * E | C3 C2 | * I | | * G | C1 | * H | C4 | * T | C5 | * |_____________________| * * */ int circleRadius = (int)(ratio * continentSize * 2.5f); // A continent is formed by several circles of varying size built off of each other. CreateContinent(continentStartPos.ToVector(), circleRadius, 12, cTypes[continent]); // Calculations for next circle theta += 6.28f / (float)(numContinents); fromMapCenterRadius = defaultRadius - Mathf.Abs(Mathf.Sin(theta) * defaultRadius * (1 - 1 / ratio)); } BuildCoast(); PlaceFeatures(); StartCoroutine(InstantiateTiles()); }
public CenterRadiusPair(MapCoor center, int radius) { this.center = center; this.radius = radius; }
public bool Cover(MapCoor Data, int W, int H) { return(Cover(Pos.X, Pos.Y, Width, Height, Data.X, Data.Y, W, H)); }
public bool Cover(MapCoor Data) { return(Cover(Data, 1, 1)); }
public FeatureCenterPair(MapCoor center, FeatureTypes featureType) { this.center = center; this.featureType = featureType; }
// 建立地圖道路 private void CreateRoad() { int iRoadSize = GameDefine.iRoadSizeBase + (int)(DataPlayer.pthis.iStage * GameDefine.fUpgradeRoad); // 取得地圖道路長度 ENUM_Dir emGrowDir = ENUM_Dir.Null; int iGrowLength = 0; CDice <ENUM_Dir> DirDiceUp = new CDice <ENUM_Dir>(); CDice <ENUM_Dir> DirDiceLeft = new CDice <ENUM_Dir>(); CDice <ENUM_Dir> DirDiceRight = new CDice <ENUM_Dir>(); DirDiceUp.Set(ENUM_Dir.Up, 2); DirDiceUp.Set(ENUM_Dir.Left, 3); DirDiceUp.Set(ENUM_Dir.Right, 3); DirDiceLeft.Set(ENUM_Dir.Up, 3); DirDiceLeft.Set(ENUM_Dir.Left, 2); DirDiceRight.Set(ENUM_Dir.Up, 3); DirDiceRight.Set(ENUM_Dir.Right, 2); while (iRoadSize > 0) { // 決定成長方向與長度 if (emGrowDir == ENUM_Dir.Null) { emGrowDir = ENUM_Dir.Up; iGrowLength = GameDefine.iPathStart; } else { if (emGrowDir == ENUM_Dir.Up) { emGrowDir = DirDiceUp.Roll(); } else if (emGrowDir == ENUM_Dir.Left) { emGrowDir = DirDiceLeft.Roll(); } else if (emGrowDir == ENUM_Dir.Right) { emGrowDir = DirDiceRight.Roll(); } else { emGrowDir = ENUM_Dir.Up; } iGrowLength = Random.Range(GameDefine.iPathMin, GameDefine.iPathMax); } //if // 新增道路 while (iGrowLength > 0 && iRoadSize > 0) { MapCoor Pos = null; if (DataMap.pthis.DataRoad.Count <= 0) { Pos = new MapCoor(Random.Range(GameDefine.iMapRoadXMin, GameDefine.iMapRoadXMax), GameDefine.iMapBorder); } else { Pos = DataMap.pthis.DataRoad[DataMap.pthis.DataRoad.Count - 1].Add(emGrowDir, 1); } if (Pos.X >= GameDefine.iMapRoadXMin && Pos.X <= GameDefine.iMapRoadXMax && Pos.Y >= GameDefine.iMapBorder) { DataMap.pthis.DataRoad.Add(Pos); --iGrowLength; // 減少成長次數 --iRoadSize; // 減少還需要產生的地圖道路長度 } else { iGrowLength = 0; } } //while } //while }
public CoorPathLengthPair(MapCoor coor, int pathLength) { this.coor = coor; this.pathLength = pathLength; }