示例#1
0
        public bool IsCellInsideGameField(GameFieldCell cell)
        {
            var fieldSize = Settings.GameFieldSize;

            return(cell.X >= 0 && cell.X < fieldSize &&
                   cell.Y >= 0 && cell.Y < fieldSize &&
                   cell.Z >= 0 && cell.Z < fieldSize);
        }
示例#2
0
        public override void Move()
        {
            if (_die)
            {
                return;
            }

            var newStartCell = _decisionMove(_cells[0]);

            _cells.Insert(0, newStartCell);
            _gameManager.AssignGameEntityToGameFieldCell(this, newStartCell);

            _prevLastCell = _cells[_cells.Count - 1];
            _cells.RemoveAt(_cells.Count - 1);
            _gameManager.UnassignGameEntityToGameFieldCell(this, _prevLastCell);
        }
示例#3
0
        public override void Spawn(IGameManager gameManager)
        {
            base.Spawn(gameManager);
            var snakeLength = _gameManager.Settings.SnakeStartLength;

            while (true)
            {
                var startCell = _gameManager.GetRandomEmptyCell();
                var fit       = true;

                for (var dX = 1; dX < snakeLength; dX++)
                {
                    if (startCell.X + dX >= _gameManager.Settings.GameFieldSize ||
                        _gameManager.GetGameFieldCellEntities(startCell.X + dX, startCell.Y, startCell.Z).Count != 0)
                    {
                        fit = false;
                        break;
                    }
                }

                if (fit)
                {
                    for (var dX = 0; dX < snakeLength; dX++)
                    {
                        var cell = new GameFieldCell
                        {
                            X = startCell.X + dX,
                            Y = startCell.Y,
                            Z = startCell.Z
                        };

                        _gameManager.AssignGameEntityToGameFieldCell(this, cell);

                        var snakePart = Instantiate(_gameManager.Settings.SnakePartPrefab,
                                                    _gameManager.GameCellToWorldCoords(cell),
                                                    Quaternion.identity,
                                                    transform);
                        _snakeParts.Add(snakePart.transform);
                        _cells.Add(cell);
                    }

                    break;
                }
            }
        }
示例#4
0
 public HashSet <IGameEntity> GetGameFieldCellEntities(GameFieldCell cell) => _gameField[cell.X, cell.Y, cell.Z];
示例#5
0
 public void UnassignGameEntityToGameFieldCell(IGameEntity gameEntity, GameFieldCell cell)
 {
     _gameField[cell.X, cell.Y, cell.Z].Remove(gameEntity);
 }
示例#6
0
 public void AssignGameEntityToGameFieldCell(IGameEntity gameEntity, GameFieldCell cell)
 {
     _gameField[cell.X, cell.Y, cell.Z].Add(gameEntity);
 }
示例#7
0
 public Vector3 GameCellToWorldCoords(GameFieldCell cell)
 => GameCellToWorldCoords(cell.X, cell.Y, cell.Z);