public void AreaCreationWorks() { Dictionary <Coordinate, GameBoardSquare> boardInfo = new Dictionary <Coordinate, GameBoardSquare>(); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { Coordinate coord = new Coordinate(i, j); GameBoardSquare square = new GameBoardSquare(coord); GameCell cell = new GameCell(); square.AddGameCell(cell); boardInfo.Add(coord, square); } } GameBoardSquare sq = boardInfo[new Coordinate(0, 0)]; sq.TopCell.blockedTop = false; sq.TopCell.blockedRight = false; GameBoardSquare sq2 = boardInfo[new Coordinate(1, 0)]; sq2.TopCell.blockedLeft = false; GameBoardSquare sq3 = boardInfo[new Coordinate(0, 1)]; sq3.TopCell.blockedBottom = false; sq3.TopCell.blockedRight = false; GameBoard board = new GameBoard(boardInfo); Assert.That(board.AreaForSquare(new Coordinate(0, 0)).Size() == 3); Assert.That(board.AreaForSquare(new Coordinate(1, 0)).Size() == 3); GameBoardSquare sq4 = boardInfo[new Coordinate(1, 1)]; sq4.TopCell.blockedLeft = false; sq4.TopCell.blockedRight = false; Assert.That(board.AreaForSquare(new Coordinate(0, 0)).Size() == 4); Assert.That(board.AreaForSquare(new Coordinate(1, 1)).Size() == 4); Assert.That(board.AreaForSquare(new Coordinate(2, 2)).Size() == 1); boardInfo[new Coordinate(2, 1)].TopCell.blockedLeft = false; boardInfo[new Coordinate(2, 1)].TopCell.blockedTop = false; boardInfo[new Coordinate(2, 2)].TopCell.blockedBottom = false; boardInfo[new Coordinate(2, 2)].TopCell.blockedLeft = false; boardInfo[new Coordinate(1, 2)].TopCell.blockedRight = false; Assert.That(board.AreaForSquare(new Coordinate(2, 2)).Size() == 7); }
public static GameBoardSquare GameBoardSquareFromNode(JSONNode square) { int row = square["row"]; int column = square["column"]; GameBoardSquare sq = new GameBoardSquare(); Coordinate coord = new Coordinate(row, column); sq.coordinate = coord; //TODO: only add GameCell if square is not "greyed out" (add something in the json for that) GameCell gameCell = GameCell.GameCellFromJSONNode(square); sq.AddGameCell(gameCell); return(sq); }
private void SetUpBoardSquares() { float size = GameCellBehavior.TILE_SIZE; foreach (Coordinate coord in this.gameBoard.BoardMap.Keys) { GameObject obj = Resources.Load("Prefabs/GridBoardSquare") as GameObject; GameObject instantiatedObj = GameObject.Instantiate(obj); instantiatedObj.transform.SetParent(transform); instantiatedObj.transform.localPosition = new Vector2(coord.row * size, coord.column * size); instantiatedObj.transform.localScale = Vector3.one; GridBoardSquareBehavior sqBehavior = instantiatedObj.GetComponent <GridBoardSquareBehavior>(); GameBoardSquare sq = this.gameBoard.BoardMap[coord]; sqBehavior.Initialize(sq); } }
public void Initialize(GameBoardSquare square) { this.boardSquare = square; GameCell topCell = square.TopCell; if (topCell != null) { GameObject obj = Resources.Load("Prefabs/GameCell") as GameObject; GameObject instantiatedObj = GameObject.Instantiate(obj); instantiatedObj.transform.SetParent(transform); instantiatedObj.transform.localPosition = Vector2.zero; instantiatedObj.transform.localScale = Vector2.one; GameCellBehavior cellBehavior = instantiatedObj.GetComponent <GameCellBehavior>(); cellBehavior.InitializeWithGameCell(topCell); this.AddGameCell(cellBehavior); } }
public void Start() { this.gameBoardDictionary = new Dictionary <Coordinate, EditorGridBoardSquareBehavior>(); Dictionary <Coordinate, GameBoardSquare> gameBoardDict = new Dictionary <Coordinate, GameBoardSquare>(); for (int i = 0; i < this.gridCount; ++i) { for (int j = 0; j < this.gridCount; ++j) { Coordinate coord = new Coordinate(i, j); GameBoardSquare square = new GameBoardSquare(coord); gameBoardDict.Add(coord, square); } } this.gameBoard = new GameBoard(gameBoardDict); this.SetUpBoardSquares(); }
private void SetUpBoardSquares() { float size = this.miniSize ? GameCellBehavior.MINI_TILE_SIZE : GameCellBehavior.TILE_SIZE; foreach (Coordinate coord in this.gameBoard.BoardMap.Keys) { GameObject obj = Resources.Load("Prefabs/EditorGridBoardSquare") as GameObject; GameObject instantiatedObj = GameObject.Instantiate(obj); instantiatedObj.transform.localScale = new Vector2(size / GameCellBehavior.TILE_SIZE, size / GameCellBehavior.TILE_SIZE); instantiatedObj.transform.SetParent(transform); instantiatedObj.transform.localPosition = new Vector2(coord.row * size, coord.column * size); EditorGridBoardSquareBehavior sqBehavior = instantiatedObj.GetComponent <EditorGridBoardSquareBehavior>(); this.gameBoardDictionary.Add(coord, sqBehavior); GameBoardSquare sq = this.gameBoard.BoardMap[coord]; sqBehavior.Initialize(sq); sqBehavior.Deactivate(); } }
public GameBoard(string levelName) { this.boardMap = new Dictionary <Coordinate, GameBoardSquare>(); this.tileMap = new Dictionary <Tile, Coordinate>(); if (levelName == "") { for (int i = 0; i < 5; ++i) { for (int j = 0; j < 7; ++j) { if (i == 3 || j == 3) { continue; } GameBoardSquare sq = new GameBoardSquare(); //GameCell cell = new GameCell(); //sq.AddGameCell(cell); Coordinate coord = new Coordinate(i, j); sq.coordinate = coord; boardMap.Add(coord, sq); } } } else { string path = "Assets/Resources/" + levelName + ".json"; //Read the text from directly from the test.txt file StreamReader reader = new StreamReader(path); string jsonText = reader.ReadToEnd(); reader.Close(); JSONNode node = JSON.Parse(jsonText); foreach (JSONNode square in node["squares"]) { GameBoardSquare sq = GameBoardSquare.GameBoardSquareFromNode(square); boardMap.Add(sq.coordinate, sq); } } }
public void MoveTileToSquare(Tile t, Coordinate coord) { if (this.tileMap.ContainsKey(t)) { int tileRow = this.tileMap[t].row; int tileCol = this.tileMap[t].column; foreach (Coordinate co in t.squareInfo.Keys) { Coordinate c = new Coordinate(co.row + tileRow, co.column + tileCol); GameBoardSquare square = this.boardMap[c]; square.RemoveGameCell(t.squareInfo[co]); } } foreach (Coordinate co in t.squareInfo.Keys) { Coordinate c = new Coordinate(coord.row + co.row, coord.column + co.column); GameBoardSquare square = this.boardMap[c]; square.AddGameCell(t.squareInfo[co]); } this.tileMap[t] = coord; }
public Area AreaForSquare(Coordinate coord) { Area area = new Area(); //keep track of explored cells - don't go in circles List <Coordinate> exploredCoordinates = new List <Coordinate>(); //keep track of cells we haven't fully explored List <Coordinate> coordinateStack = new List <Coordinate>(); Coordinate nextCoordinate = coord; int x = coord.row; int y = coord.column; bool foundNextCell = false; int i = 0; bool keepGoing = true; while (keepGoing) { foundNextCell = false; //first look left, then down, then right, then up GameBoardSquare nextBoardSquare = this.boardMap[nextCoordinate]; GameCell nextCell = nextBoardSquare.TopCell; x = nextCoordinate.row; y = nextCoordinate.column; //Debug.Log("now at (" + x + ", " + y + ")"); if (nextCell != null) { area.AddCell(nextCell); if (!exploredCoordinates.Contains(nextCoordinate)) { exploredCoordinates.Add(nextCoordinate); coordinateStack.Add(nextCoordinate); } if (!nextCell.blockedLeft) { Coordinate left = new Coordinate(x - 1, y); if (this.ContainsCoordinate(left)) { if (this.BoardMap[left].TopCell != null && this.BoardMap[left].TopCell.solid && !this.BoardMap[left].TopCell.blockedRight && !exploredCoordinates.Contains(left)) { x = x - 1; nextCoordinate = left; foundNextCell = true; } } } if (!nextCell.blockedBottom && !foundNextCell) { Coordinate bottom = new Coordinate(x, y - 1); if (this.ContainsCoordinate(bottom)) { if (this.BoardMap[bottom].TopCell != null && this.BoardMap[bottom].TopCell.solid && !this.BoardMap[bottom].TopCell.blockedTop && !exploredCoordinates.Contains(bottom)) { y = y - 1; nextCoordinate = bottom; foundNextCell = true; } } } if (!nextCell.blockedRight && !foundNextCell) { Coordinate right = new Coordinate(x + 1, y); if (this.ContainsCoordinate(right)) { if (this.BoardMap[right].TopCell != null && this.BoardMap[right].TopCell.solid && !this.BoardMap[right].TopCell.blockedLeft && !exploredCoordinates.Contains(right)) { x = x + 1; nextCoordinate = right; foundNextCell = true; } } } if (!nextCell.blockedTop && !foundNextCell) { Coordinate top = new Coordinate(x, y + 1); if (this.ContainsCoordinate(top)) { if (this.BoardMap[top].TopCell != null && this.BoardMap[top].TopCell.solid && !this.BoardMap[top].TopCell.blockedBottom && !exploredCoordinates.Contains(top)) { y = y + 1; nextCoordinate = top; foundNextCell = true; } } } if (!foundNextCell) { coordinateStack.RemoveAt(coordinateStack.Count - 1); if (coordinateStack.Count == 0) { keepGoing = false; } else { nextCoordinate = coordinateStack[coordinateStack.Count - 1]; } } ++i; if (i == 10) { keepGoing = false; } } else { //Debug.Log("ran into a null cell at (" + x + ", " + y + ")"); return(area); } } return(area); }