void LoadLevel() { //TODO: Display warning prompt Level loadedLevel = LevelLoader.Instance.LoadLevelFile(); if (loadedLevel != null) { List <MapElement> mapElements = new List <MapElement>(); HexagonGrid grid = MapSpawner.Instance.grid; foreach (Hex hex in grid.GetComponentsInChildren <Hex>()) { hex.gameObject.SetActive(false); HexBank.Instance.AddDisabledHex(hex.gameObject); } levelBeingEdited = loadedLevel; Debug.Log(levelBeingEdited); MapSpawner.Instance.SpawnHexs( levelBeingEdited, grid.transform.position + new Vector3(0, 30, 0), false ); } }
/// <summary> /// Move grid element to new destination specified with offset values. /// The left grid will become empty for the vacancy. /// </summary> public Tuple <int, int> Move(HexagonGrid grid, int x, int y, HexagonElement?hexagonElement = null) { HexagonElement element = hexagonElement == null ? new HexagonElement(MatchType.empty) : (HexagonElement)hexagonElement; bool valid = grid.IsValid(x, y); if (valid) { element = (HexagonElement)grid.GetElement(x, y); } if (element.matchType == MatchType.empty) { return(null); } foreach (Tuple <int, int> offset in offsets) { var dx = offset.Item1; var dy = offset.Item2; if (grid.GetMatchType(x + dx, y + dy) == MatchType.empty) { grid.SetElement(x + dx, y + dy, element); if (valid) { grid.SetElement(x, y, new HexagonElement(MatchType.empty)); } return(new Tuple <int, int>(x + dx, y + dy)); } } return(null); }
/// <summary> /// Check grid to decrease every bomb timer with every user action. /// If any bomb timer reaches to 0, then send this information to end the game. /// </summary> public BombEvent BombCheck(ref HexagonGrid grid) { BombEvent bombEvent = new BombEvent(false, new List <BombElement>()); for (int x = 0; x < grid.width; x++) { for (int y = 0; y < grid.height; y++) { if (!(bool)grid.GetElement(x, y)?.bombInfo.hasBomb) // If hexagon element does not have bomb then continue { continue; } HexagonElement hexagonElement = (HexagonElement)grid.GetElement(x, y); int bombTimer = hexagonElement.bombInfo.bombLeftMove; bombTimer--; if (bombTimer <= 0) // Bomb time reaches to zero end the game { bombEvent.isBombeExploded = true; Debug.Log("Bomb exploded at: (" + x + ", " + y + ")"); } grid.SetElement(x, y, new HexagonElement(hexagonElement.matchType, new BombInfo(hexagonElement.bombInfo.hasBomb, bombTimer))); bombEvent.bombElements.Add(new BombElement(new Vector2(x, y), new BombInfo(true, bombTimer))); // Send list of bomb info with their timer to renew UI and such } } return(bombEvent); }
public HexagonLogic(int width, int height, int colorNo, List <HexagonRule> rules, List <HexagonMove> moves, List <HexagonGenerator> generators = null, HexagonGenerator initializer = null) { this.grid = new HexagonGrid(width, height); this.rules = rules; this.colorNo = colorNo; this.rules.Sort(); this.moves = moves; this.moves.Sort(); this.generators = generators; this.initializer = initializer; this.selectionMoves = new SelectionHandler(width, height); this.scoring = new HexagonScoring(); // Create generators, at the top of the board. if (this.generators == null) { this.generators = new List <HexagonGenerator>(); for (int x = 0; x < width; x++) { this.generators.Add(new HexagonGenerator(new Tuple <int, int>(x, height), this.colorNo)); } } // Create initializers all over the board. if (this.initializer == null) { this.initializer = new HexagonGenerator(new Tuple <int, int>(width, height), this.colorNo); } // Create all board. this.Refresh(); }
void Awake() { startTime = Time.time; grid = GetComponent <HexagonGrid>(); grid.MakeGrid(); PlaceStart(RandInt(0, grid.size), RandInt(0, grid.size)); }
void SaveLevel() { List <MapElement> mapElements = new List <MapElement>(); HexagonGrid grid = MapSpawner.Instance.grid; foreach (Hex hex in grid.GetComponentsInChildren <Hex>()) { if (hex.gameObject == cursorHex) { continue; // Don't add the cursor hex to the save file } mapElements.Add(new MapElement( hex.typeOfHex, new Vector2Int(grid.WorldToCell(hex.transform.position).x, grid.WorldToCell(hex.transform.position).y), hex.hexAttribute) ); } Debug.Log(levelBeingEdited); if (levelBeingEdited == null) { levelBeingEdited = new Level(); } levelBeingEdited.hexs = mapElements.ToArray(); LevelLoader.Instance.SaveLevelFile(levelBeingEdited); // will make it so folders to where you can save it are limited for player input }
private void OnLevelWasLoaded(int level) { if (level == 1) { trees = new List <TreeBehaviour>(); addedTrees = new List <TreeBehaviour>(); turn = 0; playerCount = client.playerDic.Count; ui = GameObject.FindWithTag("UI").GetComponent <UIManager>(); map = GameObject.FindWithTag("Map").GetComponent <HexagonGrid>(); if (client.me.id == 0) { map.DrawMap(playerCount); } GameObject player = Instantiate(playerPrefab, new Vector3(0, 0, 0), Quaternion.identity); stats = player.GetComponent <PlayerStats>(); stats.id = client.me.id; player.GetComponent <playerController>().id = client.me.id; switch (client.me.id) { case 0: stats.GameStart(client.me.id, map.tP1); break; case 1: stats.GameStart(client.me.id, map.tP2); break; case 2: stats.GameStart(client.me.id, map.tP3); break; case 3: stats.GameStart(client.me.id, map.tP4); break; default: break; } if (turn == client.me.id) { stats.NextTurn(); ui.UpdateCurrentPlayer("You"); } else { NetworkPlayer p; client.playerDic.TryGetValue(turn, out p); ui.UpdateCurrentPlayer(p.name); } if (client.me.id == 0) { client.SendMapToClients(map.map, map.width, map.height); } } }
/// <summary> /// With match element, clone mock grids to simulate the situation if the match is valid or not. /// Search mock grids to find a match with specified rules. If there is match, add matches to the explode events, send turn numbe rof the mock grid and bomb events. /// </summary> /// <param name="matchElement">Threee coord values for the match.</param> /// <param name="simulate">If match is valid, make it a valid simulation by changing original grid with the mock one.</param> /// <returns>Explode event</returns> public ExplodeEvent Explode(MatchElement?matchElement, bool isTurnClockwise = false, bool simulate = true) { // Create mock grid to simulate request List <HexagonGrid> mockGrids = new List <HexagonGrid>(); for (int i = 0; i < 2; i++) { mockGrids.Add((HexagonGrid)this.grid.Clone()); } // Turn mock grids. First one with 120 degree, secone one with 240 degree. if (matchElement != null && matchElement?.coords != null) { int firstIndex = isTurnClockwise ? 0 : 2; int lastIndex = isTurnClockwise ? 2 : 0; for (int mockIndex = 0; mockIndex < 2; mockIndex++) { for (int turn = 0; turn < mockIndex + 1; turn++) { HexagonElement element = (HexagonElement)mockGrids[mockIndex].GetElement((int)matchElement?.coords[firstIndex].x, (int)matchElement?.coords[firstIndex].y); mockGrids[mockIndex].grid[(int)matchElement?.coords[firstIndex].x, (int)matchElement?.coords[firstIndex].y] = mockGrids[mockIndex].grid[(int)matchElement?.coords[1].x, (int)matchElement?.coords[1].y]; mockGrids[mockIndex].grid[(int)matchElement?.coords[1].x, (int)matchElement?.coords[1].y] = mockGrids[mockIndex].grid[(int)matchElement?.coords[lastIndex].x, (int)matchElement?.coords[lastIndex].y]; mockGrids[mockIndex].grid[(int)matchElement?.coords[lastIndex].x, (int)matchElement?.coords[lastIndex].y] = element; } } } // Check explode events on the mock grids to see if the action is valid or not. // Every rule has to be tried to be sure the match. List <ExplodeElement> explodeElements = new List <ExplodeElement>(); int selectedMockGridNo = 0; CheckGridWithrulesToFindMatch(mockGrids, explodeElements, ref selectedMockGridNo); // If action is valid and not for game over check if (explodeElements.Count > 0 && simulate) { this.grid = mockGrids[selectedMockGridNo]; } ExplodeEvent explodeEvent = new ExplodeEvent(explodeElements.Count > 0, selectedMockGridNo + 1, explodeElements); // If action is valid and not for game over check if (simulate) { scoring.SetScore(explodeEvent); } // If action is valid and done by user then check for bomb timers and explosion. if (matchElement != null && matchElement?.coords != null && matchElement?.coords.Count > 0 && simulate) { BombEvent bombEvent = scoring.BombCheck(ref this.grid); explodeEvent.bombEvent = bombEvent; } return(explodeEvent); }
public void Draw(SpriteBatch spriteBatch, HexagonGrid grid, Images images, FarSeerCamera2D camera) { foreach (Cell cell in grid) { Vector2 centerPixel = DeterminePositionToDraw(cell); Vector2 screenPosition = camera.ConvertWorldToScreen(centerPixel); if (_viewport.Contains(screenPosition)) { DrawCell(spriteBatch, cell, images, centerPixel); } } }
/// <summary> /// Set empty the grid to prepare fill or move event to the grid element. /// </summary> public List <Vector2> Clean(HexagonGrid grid, int x, int y) { var positions = new List <Vector2>(); foreach (Tuple <int, int> offset in offsets) { var dx = offset.Item1; var dy = offset.Item2; grid.SetMatchType(x + dx, y + dy, MatchType.empty); positions.Add(new Vector2(x + dx, y + dy)); } return(positions); }
/// <summary> /// Clone the the grid. /// </summary> public object Clone() { var g = new HexagonGrid(this.width, this.height); g.grid = new HexagonElement[this.width, this.height]; for (int x = 0; x < this.width; x++) { for (int y = 0; y < this.height; y++) { g.grid[x, y] = this.grid[x, y]; } } return(g); }
static void CleanupSpawnedHexes() { List <MapElement> mapElements = new List <MapElement>(); HexagonGrid grid = MapSpawner.Instance.grid; if (grid.GetComponentsInChildren <Hex>().Length > 0) { // TODO: Would you like to save prompt foreach (Hex hex in grid.GetComponentsInChildren <Hex>()) { hex.gameObject.SetActive(false); HexBank.Instance.AddDisabledHex(hex.gameObject); } } }
// Window has been selected void OnFocus() { if (grid == null) { grid = MapSpawner.Instance.grid; } UpdateHexCursorObject(); // Remove delegate listener if it has previously been assigned. SceneView.onSceneGUIDelegate -= this.OnSceneGUI; // Add (or re-add) the delegate. SceneView.onSceneGUIDelegate += this.OnSceneGUI; //LastTool = Tools.current; //Tools.current = Tool.None; }
/// <summary> /// Check coords according match rules to see if there is a match. /// First it check modular conditions, if it suits to that rule then check every offset value whether all the grids at offset coords are the same match type. /// If every grid have same match then turn true. It means there is a match according to this match rule. /// </summary> public bool Check(HexagonGrid grid, int x, int y) { if (!(x % 2 == this.modularCondition.Item1 && y % 2 == this.modularCondition.Item2)) { return(false); } foreach (Tuple <int, int> offset in offsets) { int dx = offset.Item1; int dy = offset.Item2; if (grid.GetMatchType(x + dx, y + dy) != this.type) { return(false); } } return(true); }
public void SaveLevel() { List <MapElement> mapElements = new List <MapElement>(); HexagonGrid grid = MapSpawner.Instance.grid; foreach (Hex hex in grid.GetComponentsInChildren <Hex>()) { mapElements.Add(new MapElement(hex.typeOfHex, new Vector2Int(grid.WorldToCell(hex.transform.position).x, grid.WorldToCell(hex.transform.position).y))); } Level level = levels[currentSessionData.levelIndex]; level.hexs = mapElements.ToArray(); Debug.Log(level.hexs.Length); LevelLoader.Instance.SaveLevelFile(level); // will make it so folders to where you can save it are limited for player input }
public override void OnInspectorGUI() { CheckCache(); EditorGUILayout.PropertyField(visualize); EditorGUILayout.PropertyField(showCoordinates); EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.PropertyField(gridBuilder); if (gridBuilder.objectReferenceValue != null) { CreateEditor(gridBuilder.objectReferenceValue).OnInspectorGUI(); if (GUILayout.Button("Build Grid")) { HexagonGrid hexagonGrid = (HexagonGrid)target; hexagonGrid.BuildGrid(); SceneView.RepaintAll(); } } else { EditorGUILayout.HelpBox("Please load a grid builder.", MessageType.Warning); HexagonGrid hexagonGrid = (HexagonGrid)target; hexagonGrid.ClearGrid(); SceneView.RepaintAll(); } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.PropertyField(matrix); EditorGUILayout.EndVertical(); serializedObject.ApplyModifiedProperties(); }
public WarlordsRevengeGameScreen() { _images = new Images(); _grid = MapReader.ReadFromFile("First.map"); }
public void Start() { m_WorldBuilderPrefab = GetComponentInChildren <HexagonGrid>(); }