private GolfCell ReachableCellWithBallAndDirection(Ball ball, Direction dir) { bool cellIsOkay = true; GolfCell cell = null; try { Ball b = null; for (int i = 1; i < ball.numberOfStrokes; i++) { int xi = ball.positionOnGridX + (dir == Direction.LEFT ? -1 : (dir == Direction.RIGHT ? 1 : 0)) * i; int yi = ball.positionOnGridY + (dir == Direction.UP ? 1 : (dir == Direction.DOWN ? -1 : 0)) * i; cell = this.GetCell(xi, yi); b = GetBall(xi, yi); cellIsOkay &= (cell.BallCanPassThrough() && b == null); } int xmax = ball.positionOnGridX + (dir == Direction.LEFT ? -1 : (dir == Direction.RIGHT ? 1 : 0)) * ball.numberOfStrokes; int ymax = ball.positionOnGridY + (dir == Direction.UP ? 1 : (dir == Direction.DOWN ? -1 : 0)) * ball.numberOfStrokes; cell = this.GetCell(xmax, ymax); b = GetBall(xmax, ymax); cellIsOkay &= (cell.BallCanStopOn() && b == null); } catch (System.Exception ex) { cellIsOkay = false; } if (cellIsOkay) { return(cell); } return(null); }
public void Init(GolfCell d) { data = d; selectable = false; selected = false; hovered = false; SetColor(); UpdateAppearance(); }
public HerugolfGameState(int width, int height) { gridDimensions = new Vector2(width, height); gridArray = new GolfCell[width, height]; balls = new List <Ball>(); for (int xi = 0; xi < width; xi++) { for (int yi = 0; yi < height; yi++) { gridArray[xi, yi] = new GolfCell(xi, yi, CellType.GREEN); } } }
public void BuildGrid(HerugolfGameState gridState) { gridWidth = Mathf.RoundToInt(gridState.gridDimensions.x); gridHeight = Mathf.RoundToInt(gridState.gridDimensions.y); displayedGrid = new GameObject[gridWidth, gridHeight]; for (int xi = 0; xi < gridState.gridDimensions.x; xi++) { for (int yi = 0; yi < gridState.gridDimensions.y; yi++) { GolfCell cellData = gridState.GetCell(xi, yi); displayedGrid[xi, yi] = BuildSquare(xi, yi, cellData); } } foreach (Ball ball in gridState.balls) { AddBall(ball); } }
public List <GolfCell> CellsReachableWithBall(Ball ball) { List <GolfCell> result = new List <GolfCell>(); int x = ball.positionOnGridX; int y = ball.positionOnGridY; // down GolfCell cell = ReachableCellWithBallAndDirection(ball, Direction.DOWN); if (cell != null) { result.Add(cell); } // up cell = ReachableCellWithBallAndDirection(ball, Direction.UP); if (cell != null) { result.Add(cell); } // left cell = ReachableCellWithBallAndDirection(ball, Direction.LEFT); if (cell != null) { result.Add(cell); } // right cell = ReachableCellWithBallAndDirection(ball, Direction.RIGHT); if (cell != null) { result.Add(cell); } return(result); }
private GameObject BuildSquare(int coordX, int coordY, GolfCell dataCell) { GameObject cellPrefab = null; switch (dataCell.type) { case CellType.GREEN: cellPrefab = greenSquarePrefab; break; case CellType.BUNKER: cellPrefab = bunkerSquarePrefab; break; case CellType.HOLE_EMPTY: cellPrefab = holeSquarePrefab; break; } Vector3 cellPosition = new Vector3(coordX + 0.5f - gridWidth / 2.0f, 0, coordY + 0.5f - gridHeight / 2.0f) * gridCellSize; GameObject squareGo = Instantiate(cellPrefab, cellPosition, Quaternion.identity, gridParent); squareGo.GetComponent <GridCellBehaviour>().Init(dataCell); return(squareGo); }