Beispiel #1
0
        private void Start()
        {
            for (int x = 0; x < levelBehavior.Data.Width; ++x)
            {
                for (int y = 0; y < levelBehavior.Data.Height; ++y)
                {
                    Cell cell = levelBehavior.Data.GetForegroundCellAt(x, y);
                    if (cell is RootBranchCell)
                    {
                        RootBranchCell rootBranchCell = cell as RootBranchCell;
                        rootBranchTipCells.Add(rootBranchCell);
                    }
                }
            }

            rootBranchTipCells.Sort((a, b) => a.Index.CompareTo(b.Index));

            CenterCameraToSelectedRootBranch();
            Camera.main.orthographicSize = defaultOrthoSize;

            prevMousePosition = Input.mousePosition;
        }
        public LevelData(int width = 256, int height = 64)
        {
            Width  = width;
            Height = height;

            backgroundCells = new Cell[width, height];
            foregroundCells = new Cell[width, height];

            // For now, initialize the whole level data in the constructor.
            // Might want to make this asynchronous in the future.
            // Will also need to move this somewhere for random generation
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    backgroundCells[x, y]          = new SoilCell(x, y);
                    backgroundCells[x, y].IsHidden = true;

                    float tileTypeRoll = Random.Range(0.0f, 1.0f);
                    if (tileTypeRoll <= 0.2f)
                    {
                        ObstacleCell obstacleCell = new ObstacleCell(x, y);
                        foregroundCells[x, y] = obstacleCell;
                    }
                    else if (tileTypeRoll <= 0.5f)
                    {
                        NutrientCell nutrientCell = new NutrientCell(x, y);
                        nutrientCell.NutrientValue    = 2;
                        nutrientCell.BranchPointValue = 2;
                        foregroundCells[x, y]         = nutrientCell;
                    }
                    else
                    {
                        EmptyCell emptyCell = new EmptyCell(x, y);
                        emptyCell.IsHidden    = false;
                        foregroundCells[x, y] = emptyCell;
                    }
                }
            }

            Cell seedCell = new EmptyCell(width / 2 - 1, height - 1);

            seedCell.IsHidden   = false;
            seedCell.IsDiggable = false;
            foregroundCells[seedCell.X, seedCell.Y] = seedCell;
            SetBackgroundCellHidden(seedCell.X, seedCell.Y, false);

            RootBranchCell leftRootBranch = new RootBranchCell(seedCell.X - 1, seedCell.Y, 0);

            leftRootBranch.SetParent(seedCell);
            leftRootBranch.IsHidden = false;
            foregroundCells[leftRootBranch.X, leftRootBranch.Y] = leftRootBranch;
            SetBackgroundCellHidden(leftRootBranch.X, leftRootBranch.Y, false);

            SetCellsHidden(leftRootBranch.X - 1, leftRootBranch.Y, false);
            SetCellsHidden(leftRootBranch.X, leftRootBranch.Y - 1, false);
            SetCellsHidden(leftRootBranch.X, leftRootBranch.Y + 1, false);

            RootBranchCell rightRootBranch = new RootBranchCell(seedCell.X + 1, seedCell.Y, 2);

            rightRootBranch.SetParent(seedCell);
            rightRootBranch.IsHidden = false;
            foregroundCells[rightRootBranch.X, rightRootBranch.Y] = rightRootBranch;
            SetBackgroundCellHidden(rightRootBranch.X, rightRootBranch.Y, false);

            SetCellsHidden(rightRootBranch.X + 1, rightRootBranch.Y, false);
            SetCellsHidden(rightRootBranch.X, rightRootBranch.Y - 1, false);
            SetCellsHidden(rightRootBranch.X, rightRootBranch.Y + 1, false);

            RootBranchCell bottomRootBranch = new RootBranchCell(seedCell.X, seedCell.Y - 1, 1);

            bottomRootBranch.SetParent(seedCell);
            bottomRootBranch.IsHidden = false;
            foregroundCells[bottomRootBranch.X, bottomRootBranch.Y] = bottomRootBranch;
            SetBackgroundCellHidden(bottomRootBranch.X, bottomRootBranch.Y, false);

            SetCellsHidden(bottomRootBranch.X - 1, bottomRootBranch.Y, false);
            SetCellsHidden(bottomRootBranch.X + 1, bottomRootBranch.Y, false);
            SetCellsHidden(bottomRootBranch.X, bottomRootBranch.Y - 1, false);
        }
Beispiel #3
0
        private void Update()
        {
            if (gameState.IsGameOver)
            {
                return;
            }

            int dx = 0, dy = 0;

            if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
            {
                dy = 1;
            }
            else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
            {
                dy = -1;
            }
            else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
            {
                dx = -1;
            }
            else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
            {
                dx = 1;
            }

            if ((dx != 0) || (dy != 0))
            {
                RootBranchCell currentRootBranchCell = rootBranchTipCells[selectedRootBranchIndex];
                Cell           targetCell            = levelBehavior.Data.GetForegroundCellAt(currentRootBranchCell.X + dx, currentRootBranchCell.Y + dy);
                if ((targetCell != null) &&
                    targetCell.IsDiggable)
                {
                    if (targetCell.Type == Cell.CellType.Nutrient)
                    {
                        NutrientCell nutrientCell = targetCell as NutrientCell;

                        gameState.NutrientPoints += nutrientCell.NutrientValue;
                        gameState.BranchPoints   += nutrientCell.BranchPointValue;
                    }

                    gameState.BranchPoints = Mathf.Max(gameState.BranchPoints - targetCell.BranchCost, 0);

                    RootBranchCell newRootBranch = new RootBranchCell(targetCell.X, targetCell.Y, selectedRootBranchIndex);
                    newRootBranch.SetParent(rootBranchTipCells[selectedRootBranchIndex]);
                    newRootBranch.IsHidden = false;
                    rootBranchTipCells[selectedRootBranchIndex].AddChild(newRootBranch);
                    rootBranchTipCells[selectedRootBranchIndex] = newRootBranch;

                    levelBehavior.Data.SetForegroundCellAt(newRootBranch.X, newRootBranch.Y, newRootBranch);
                    levelBehavior.Data.SetBackgroundCellHidden(newRootBranch.X, newRootBranch.Y, false);

                    levelBehavior.Data.SetCellsHidden(newRootBranch.X - 1, newRootBranch.Y, false);
                    levelBehavior.Data.SetCellsHidden(newRootBranch.X + 1, newRootBranch.Y, false);
                    levelBehavior.Data.SetCellsHidden(newRootBranch.X, newRootBranch.Y - 1, false);
                    levelBehavior.Data.SetCellsHidden(newRootBranch.X, newRootBranch.Y + 1, false);

                    CenterCameraToSelectedRootBranch();

                    if (!HasValidMoves())
                    {
                        gameState.GameOver();
                    }
                }
            }

            if (Input.GetKeyUp(KeyCode.Q))
            {
                selectedRootBranchIndex = ((selectedRootBranchIndex - 1) + rootBranchTipCells.Count) % rootBranchTipCells.Count;
                CenterCameraToSelectedRootBranch();
            }
            else if (Input.GetKeyUp(KeyCode.E))
            {
                selectedRootBranchIndex = (selectedRootBranchIndex + 1) % rootBranchTipCells.Count;
                CenterCameraToSelectedRootBranch();
            }

            if (Input.GetKeyUp(KeyCode.Space))
            {
                CenterCameraToSelectedRootBranch();
            }
        }
Beispiel #4
0
 public void RemoveChild(RootBranchCell rootBranchCell)
 {
     children.Remove(rootBranchCell);
 }
Beispiel #5
0
 public void AddChild(RootBranchCell rootBranchCell)
 {
     children.Add(rootBranchCell);
 }