/// <summary> /// Registers brush from its button. /// </summary> /// <param name="brushAlias"> Internal brush type. </param> /// <param name="brushType"> Object to place with this brush. </param> public void RegisterBrush(MapObject brushAlias, GameObject brushType) { if (this.brushes == null) { this.brushes = new Dictionary <MapObject, GameObject>(); } this.brushes.Add(brushAlias, brushType); }
private int VeinPlacement(Level level, int x, int y, MapObject veinType, int maxCount, float expandingChance) { int placed = 0; level.objectMap[y, x] = veinType; placed++; for (int k = 1; k < 8; k += 2) { if (level.objectMap[y + Utility.Dy[k], x + Utility.Dx[k]] == MapObject.Floor && Random.value < expandingChance && placed < maxCount) { placed += this.VeinPlacement(level, x + Utility.Dx[k], y + Utility.Dy[k], veinType, maxCount - placed, expandingChance); } } return(placed); }
public void SelectBrush(MapObject brushType) { if (this.selectedBrush >= MapObject.PortalRed && this.selectedBrush <= MapObject.PortalYellow && brushType != this.selectedBrush && this.portalMap[this.selectedBrush].Item1 != null && this.portalMap[this.selectedBrush].Item2 == null) { var portalPos = new Vector2Int(Mathf.RoundToInt(this.portalMap[this.selectedBrush].Item1.transform.position.x), Mathf.RoundToInt(this.portalMap[this.selectedBrush].Item1.transform.position.y)); Destroy(this.map[portalPos.y][portalPos.x].obj); this.map[portalPos.y][portalPos.x].obj = Instantiate(this.brushes[MapObject.Floor], new Vector3(portalPos.x, portalPos.y, 0), Quaternion.identity); this.map[portalPos.y][portalPos.x].obj.transform.SetParent(this.grid.transform); this.map[portalPos.y][portalPos.x].alias = MapObject.Floor; this.portalMap[this.selectedBrush].Item1 = null; } this.selectedBrush = brushType; GameLogger.LogMessage($"Brush selected is {brushType}", "EditorManager"); }
private void ProcessButtons(GameObject[] buttonTypes, GameObject panel) { int xCoord = 25; int yCoord = 125; foreach (var item in buttonTypes) { MapObject mapObject = MapObject.Wall; if (item.name == "Portal" || item.name == "CaveFloor" || Enum.TryParse(item.name, out mapObject)) { if (item.name == "CaveFloor") { mapObject = MapObject.Floor; } // Portal buttons processing. if (item.name == "Portal") { for (var alias = MapObject.PortalRed; alias <= MapObject.PortalYellow; alias++) { this.AddBrush(alias, item, panel, xCoord, yCoord); xCoord += 50; if (xCoord > Screen.width - 25) { xCoord = 25; yCoord -= 50; } } } else { this.AddBrush(mapObject, item, panel, xCoord, yCoord); xCoord += 50; if (xCoord > Screen.width - 25) { xCoord = 25; yCoord -= 50; } } } } }
/// <summary> /// Instantiates object of type <paramref name="alias"/> on level and returns it. /// </summary> /// <param name="alias"> Internal object type. </param> /// <param name="pos"> Position of instantiating. </param> /// <param name="isInit"> Is it initialization of map. </param> /// <returns> Instantiated object. </returns> private GameObject InstantiateOnMap(MapObject alias, Vector2 pos, bool isInit = false) { if (!isInit) { MonoBehaviour.Destroy(this.map[(int)pos.y][(int)pos.x].obj); } GameObject obj = Instantiate(this.dummy, pos, Quaternion.identity); obj.transform.SetParent(this.grid.transform); obj.GetComponent <EditorDummy>().attachedObject = this.brushes[alias]; if (alias >= MapObject.PortalRed && alias <= MapObject.PortalYellow) { obj.GetComponent <EditorDummy>().attachedObject.GetComponent <Portal>().SetupPortal(null, alias); if (this.portalMap[alias].Item1 == null && this.portalMap[alias].Item2 == null) { this.portalMap[alias].Item1 = obj; } else if (this.portalMap[alias].Item1 != null && this.portalMap[alias].Item2 == null) { this.portalMap[alias].Item2 = obj; } else { GameLogger.LogError($"Too many portals of color {alias}", "EditorManager"); return(null); } } obj.GetComponent <EditorDummy>().UpdateSprite(); if (isInit) { this.map[(int)pos.y].Add(new GridObject(alias, obj)); } else { this.map[(int)pos.y][(int)pos.x].obj = obj; this.map[(int)pos.y][(int)pos.x].alias = alias; } return(obj); }
public void SetupPortal(GameObject outObject, MapObject color) { this.portalOut = outObject; this.GetComponent <SpriteRenderer>().color = Colors[color]; if (this.portalOut != null) { var portalComp = this.portalOut.GetComponent <Portal>(); if (portalComp != null) { portalComp.GetComponent <SpriteRenderer>().color = Colors[color]; portalComp.portalOut = this.gameObject; } } if (this.portalOut != null) { GameLogger.LogMessage($"Portal of color {color} on {this.transform.position} and {outObject.transform.position} are connected", "Portal"); } }
/// <summary> /// Generate button for panel. /// </summary> /// <param name="alias"> Internal brush type. </param> /// <param name="brushType"> Object to instantiate with this brush. </param> /// <param name="panel"> Parent's panel. </param> /// <param name="x"> X coordinate of button. </param> /// <param name="y"> Y coordinate of button.</param> private void AddBrush(MapObject alias, GameObject brushType, GameObject panel, int x, int y) { var newButton = Instantiate(this.brushButton) as GameObject; newButton.transform.SetParent(panel.transform, false); newButton.transform.position = new Vector3(x, y, 0); newButton.name = alias.ToString("G"); var toolBtn = newButton.GetComponent <ToolButton>(); toolBtn.brushAlias = alias; toolBtn.editorManager = this; newButton.GetComponent <Image>().sprite = brushType.GetComponent <SpriteRenderer>().sprite; newButton.GetComponent <Image>().color = brushType.GetComponent <SpriteRenderer>().color; if (alias >= MapObject.PortalRed && alias <= MapObject.PortalYellow) { newButton.GetComponent <Image>().color = Portal.Colors[alias]; } this.brushes.Add(alias, brushType); GameLogger.LogMessage($"Brush {alias} added to {panel.name}", "EditorManager"); }
public GridObject(MapObject ch, GameObject obj) { this.alias = ch; this.obj = obj; }
private void MakeAutomatonSteps(MapObject[,] map, int deathLimit, int birthLimit, int stepsCount) { // Buffer init var buffer = new MapObject[height, width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { buffer[i, j] = MapObject.Floor; } } for (int t = 0; t < stepsCount; t++) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (i <= 1 || j <= 1 || i >= height - 2 || j >= width - 2) { buffer[i, j] = map[i, j]; continue; } int neighbours = 0; for (int k = 0; k < 8; k++) { if (map[i + Utility.Dy[k], j + Utility.Dx[k]] == MapObject.Wall) { neighbours++; } } // If someone will make it simplier would it work as intended? // Second condion with 't' is to prevent huge open spaces if (map[i, j] == MapObject.Wall) { if (neighbours >= deathLimit /*|| (neighbours == 0 && t < 3)*/) { buffer[i, j] = MapObject.Wall; } else { buffer[i, j] = MapObject.Floor; } } else { if (neighbours > birthLimit /*|| (neighbours == 0 && t < 3)*/) { buffer[i, j] = MapObject.Wall; } else { buffer[i, j] = MapObject.Floor; } } } } Utility.Clone(buffer, map, width, height); } }
/// <summary> /// Set up a <paramref name="level"/>. /// </summary> /// <param name="level"> Level to set up. </param> private void LevelSetup(Level level) { this.levelGrid = new GameObject("LevelGrid").transform; this.portalMap = new Dictionary <MapObject, GameObject>(); for (int i = 0; i < level.height - 1; i++) { for (int j = 0; j < level.width - 1; j++) { var settingFloor = Utility.SelectRandomItem(level.floorAssets[MapObject.Floor]); var instanceFloor = Instantiate(settingFloor, new Vector3(j, i, 0f), Quaternion.identity) as GameObject; instanceFloor.transform.SetParent(this.levelGrid); if (level.wallMap[i, j] != 0) { MapObject material = MapObject.Wall; if (Level.Walls.Contains(level.objectMap[i, j]) && level.objectMap[i, j] != MapObject.Wall) { material = level.objectMap[i, j]; } else if (Level.Walls.Contains(level.objectMap[i + 1, j]) && level.objectMap[i + 1, j] != MapObject.Wall) { material = level.objectMap[i + 1, j]; } else if (Level.Walls.Contains(level.objectMap[i + 1, j + 1]) && level.objectMap[i + 1, j + 1] != MapObject.Wall) { material = level.objectMap[i + 1, j + 1]; } else if (Level.Walls.Contains(level.objectMap[i + 1, j + 1]) && level.objectMap[i + 1, j + 1] != MapObject.Wall) { material = level.objectMap[i, j + 1]; } GameObject instanceWall = level.wallAssets[material].GetComponent <Wall>().InstantiateWall(j, i, level); level.wallObjects[i, j] = instanceWall; instanceWall.transform.SetParent(this.levelGrid); } if (!Level.Walls.Contains(level.objectMap[i, j]) && level.objectMap[i, j] != MapObject.Floor) { if (level.objectMap[i, j] >= MapObject.PortalRed && level.objectMap[i, j] <= MapObject.PortalYellow) { var instance = Instantiate(this.portal, new Vector3(j, i, 0f), Quaternion.identity) as GameObject; instance.transform.SetParent(this.levelGrid); if (this.portalMap.ContainsKey(level.objectMap[i, j])) { GameObject portalOut = this.portalMap[level.objectMap[i, j]]; instance.GetComponent <Portal>().SetupPortal(portalOut, level.objectMap[i, j]); } else { this.portalMap.Add(level.objectMap[i, j], instance); } } else { GameObject settingObject = level.objectsAssets[level.objectMap[i, j]][Random.Range(0, level.objectsAssets[level.objectMap[i, j]].Count)]; var instanceObject = Instantiate(settingObject, new Vector3(j, i, 0f), Quaternion.identity) as GameObject; instanceObject.name = settingObject.name; instanceObject.transform.SetParent(this.levelGrid); } } } } GameLogger.LogMessage("Player pos: " + level.playerStart, "LevelManager"); Object.Instantiate(this.player, new Vector3(level.playerStart.x, level.playerStart.y, 0f), Quaternion.identity); GameLogger.LogMessage("Exit pos: " + level.exitPos, "LevelManager"); Object.Instantiate(this.exit, new Vector3(level.exitPos.x, level.exitPos.y, 0f), Quaternion.identity); GameLogger.LogMessage("Level object map:", "LevelManager"); Utility.PrintMatrix(level.objectMap, level.width, level.height); }