Example #1
0
 public void AddCrate(Crate crate)
 {
     if (CrateList != null)
     {
         CrateList.Add(crate);
     }
 }
Example #2
0
File: Game.cs Project: kphl/Sokoban
        public void Move(Direction d)
        {
            Player   player = Grid.getPlayer();
            Position pos    = null;

            switch (d)
            {
            case Direction.DOWN:
                pos = new Position(0, 1);
                break;

            case Direction.UP:
                pos = new Position(0, -1);
                break;

            case Direction.LEFT:
                pos = new Position(-1, 0);
                break;

            case Direction.RIGHT:
                pos = new Position(1, 0);
                break;
            }

            Box      start = Grid.Box(player.Pos.X, player.Pos.Y);
            Position p     = pos + player.Pos;
            Box      end   = Grid.Box(p.X, p.Y);

            if (player.Move(d, end))
            {
                Movement m  = new Movement(start, end, player, d);
                bool     ok = true;

                if (end.Entity is Crate)
                {
                    Crate    c      = end.Entity as Crate;
                    Box      startC = Grid.Box(c.Pos.X, c.Pos.Y);
                    Position pC     = pos + c.Pos;
                    Box      endC   = Grid.Box(pC.X, pC.Y);

                    if (c.Move(d, endC))
                    {
                        Grid.Move(startC, endC);
                        Movement mC = new Movement(startC, endC, c, d);
                        m.CrateMovement = mC;
                    }
                    else
                    {
                        ok = false;
                    }
                }

                if (ok)
                {
                    moves.Add(m);
                    Grid.Move(start, end);
                }
            }
        }
Example #3
0
        public void polymorphMove()
        {
            Crate crate = new Crate(currentField);
            Truck truck = new Truck(currentField);
            Wall  wall  = new Wall();

            wall.Move(Direction.Right);
            crate.Move(Direction.Right);
            truck.Move(Direction.Right);
        }
Example #4
0
        public void polymorphPlace()
        {
            Crate crate = new Crate(currentField);
            Truck truck = new Truck(currentField);
            Wall  wall  = new Wall();

            FloorField floorField = new FloorField();
            WallField  wallField  = new WallField();
            EmptyField emptyField = new EmptyField();

            floorField.Place(crate);
            wallField.Place(crate);
            emptyField.Place(crate);
        }
Example #5
0
 public override bool MoveOnThis(Crate crate, Direction direction)
 {
     if (_Movable == null && _PlacedCounter >= 3)
     {
         crate._Field = null;
         crate        = null;
     }
     else if (_Movable == null)
     {
         _Movable     = crate;
         crate._Field = this;
         _PlacedCounter++;
     }
     else
     {
         return(false);
     }
     return(true);
 }
Example #6
0
        public void polymorphToChar()
        {
            Crate crate = new Crate(currentField);
            Truck truck = new Truck(currentField);
            Wall  wall  = new Wall();

            FloorField  floorField  = new FloorField();
            TargetField targetField = new TargetField();
            WallField   wallField   = new WallField();
            EmptyField  emptyField  = new EmptyField();

            crate.ToChar();
            truck.ToChar();
            wall.ToChar();

            floorField.ToChar();
            wallField.ToChar();
            emptyField.ToChar();
            targetField.ToChar();
        }
Example #7
0
        public void Reset()
        {
            // Rebuild the level from the tile data.
            tileSprites.Clear();
            Crates.Clear();

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    int tileId = TileData[y, x];
                    if (tileId == 0)
                    {
                        continue;                          // Nothing
                    }
                    var bgSprite = new Sprite {
                        Position = new Vector2f(x, y) * TileSize,
                        Texture  = tileId switch {
                            Tiles.Ground => Resources.groundTexture,
                            Tiles.Wall => Resources.bricksTexture,
                            Tiles.Target => Resources.targetTexture,
                            Tiles.Crate => Resources.groundTexture,
                            Tiles.Player => Resources.groundTexture,
                            _ => null
                        }
                    };
                    tileSprites.Add(bgSprite);

                    if (tileId == Tiles.Crate)
                    {
                        var crate = new Crate(this, new Vector2i(x, y));
                        Crates.Add(crate);
                    }
                    else if (tileId == Tiles.Player)
                    {
                        Player.Position = new Vector2i(x, y);
                    }
                }
            }
        }
Example #8
0
        private void move()
        {
            checkDirections();
            rotate();
            if (checkGameStatus())
            {
                if (modelLevel.levelData.Tilemap[nextY, nextX] == null)
                {
                    if (modelLevel.levelData.Tiles[nextY][nextX] is Floor || modelLevel.levelData.Tiles[nextY][nextX] is Dest)
                    {
                        updateMoves();

                        modelLevel.levelData.Tilemap[nextY, nextX] = modelLevel.levelData.Tilemap[pY, pX];
                        modelLevel.levelData.Tilemap[pY, pX]       = null;

                        modelLevel.levelData.player.X = nextX;
                        modelLevel.levelData.player.Y = nextY;

                        gameGrid.reDrawPlayer();
                        gameState.computeBroke(modelLevel.levelData);
                    }
                }
                else
                {
                    if (modelLevel.levelData.Tilemap[secondY, secondX] == null && !(modelLevel.levelData.Tiles[secondY][secondX] is Wall))
                    {
                        updateMoves();

                        if (modelLevel.levelData.Tiles[secondY][secondX] is Dest)
                        {
                            Deliver delivery = new Deliver();
                            modelLevel.levelData.Tilemap[secondY, secondX] = delivery;
                            modelLevel.levelData.Tilemap[nextY, nextX]     = modelLevel.levelData.Tilemap[pY, pX];
                            modelLevel.levelData.Tilemap[pY, pX]           = null;
                            gameGrid.reDrawFloor(nextX, nextY);
                        }
                        else if (modelLevel.levelData.Tilemap[nextY, nextX] is Deliver)
                        {
                            Crate crate = new Crate();
                            modelLevel.levelData.Tilemap[secondY, secondX] = crate;
                            modelLevel.levelData.Tilemap[nextY, nextX]     = modelLevel.levelData.Tilemap[pY, pX];
                            modelLevel.levelData.Tilemap[pY, pX]           = null;
                            gameGrid.reDrawFloor(nextX, nextY);
                        }
                        else
                        {
                            modelLevel.levelData.Tilemap[secondY, secondX] = modelLevel.levelData.Tilemap[nextY, nextX];
                            modelLevel.levelData.Tilemap[nextY, nextX]     = modelLevel.levelData.Tilemap[pY, pX];
                            modelLevel.levelData.Tilemap[pY, pX]           = null;
                        }

                        modelLevel.levelData.player.X = nextX;
                        modelLevel.levelData.player.Y = nextY;

                        modelLevel.levelData.Tilemap[secondY, secondX].X = secondX;
                        modelLevel.levelData.Tilemap[secondY, secondX].Y = secondY;

                        gameGrid.reDrawComp(secondX, secondY);

                        if (modelLevel.levelData.Tiles[secondY][secondX] is Dest)
                        {
                            modelLevel.levelData.remaingTargets--;
                        }
                        if (modelLevel.levelData.Tiles[nextY][nextX] is Dest)
                        {
                            modelLevel.levelData.remaingTargets++;
                        }
                        targetStatus();

                        if (modelLevel.levelData.remaingTargets <= 0)
                        {
                            GameCompleteStatus(true, "all targets you win");
                        }
                        else
                        {
                            gameState.computeBroke(modelLevel.levelData);
                        }
                    }
                }
            }
        }
Example #9
0
        private Maze InitMaze(int idMaze, string[] lines, int length, Sokoban sokoban)
        {
            Maze maze = new Maze();

            maze.MazeNumber = idMaze;
            Tile[,] tiles   = new Tile[lines.Length, length];

            int x = 0;
            int y = 0;

            foreach (String line in lines)
            {
                y = 0;
                foreach (char character in line.ToCharArray())
                {
                    Tile tile = null;
                    switch (character)
                    {
                    case 'x':
                        tile = new Destination();
                        maze.DestinationsAmount++;
                        break;

                    case '.':
                        tile = new Normal();
                        break;

                    case 'o':
                        tile = new Normal();

                        Crate crate = new Crate(tile);
                        tile.Entity = crate;

                        maze.Crates.Add(crate);
                        break;

                    case '@':
                        tile = new Normal();

                        Player player = new Player(tile);
                        tile.Entity = player;

                        maze.Player = player;
                        break;

                    case '#':
                        tile = new Wall();
                        break;

                    case '0':
                        tile = new Destination();

                        crate = new Crate(tile);
                        crate.OnDestination = true;
                        tile.Entity         = crate;

                        maze.Crates.Add(crate);
                        break;

                    case '$':
                        tile = new Normal();

                        Worker worker = new Worker(tile);
                        tile.Entity = worker;

                        maze.Worker = worker;
                        break;

                    case ' ':
                        tile = new Empty();
                        break;

                    case '~':
                        tile = new PitFall(sokoban);
                        break;
                    }

                    tiles[x, y] = tile;
                    y++;
                }

                x++;
            }

            for (int i = 0; i < tiles.GetLength(0); i++)
            {
                for (int j = 0; j < tiles.GetLength(1); j++)
                {
                    Tile tile = tiles[i, j];

                    if (tile == null)
                    {
                        continue;
                    }

                    tile.North = getTile(tiles, i - 1, j);
                    tile.East  = getTile(tiles, i, j + 1);
                    tile.South = getTile(tiles, i + 1, j);
                    tile.West  = getTile(tiles, i, j - 1);

                    if (i == 0 && j == 0)
                    {
                        maze.First = tile;
                    }
                }
            }

            return(maze);
        }
Example #10
0
        public Maze CreateMaze(int mazeNumber)
        {
            try
            {
                this._maze     = new Maze();
                this._fileName = "..\\..\\Doolhofs\\doolhof" + mazeNumber + ".txt";

                _reader = new StreamReader(_fileName);

                List <Tile> currentTileList  = new List <Tile>();
                List <Tile> previousTileList = null;


                this._input  = new FileStream(this._fileName, FileMode.Open, FileAccess.Read);
                this._reader = new StreamReader(this._input);
                string lineString = this._reader.ReadLine();

                while (lineString != null)
                {
                    currentTileList = new List <Tile>();

                    Tile currentTile  = null;
                    Tile previousTile = null;

                    for (int x = 0; x < lineString.Length; x++)
                    {
                        currentTile = null;

                        switch (lineString[x])
                        {
                        case 'x':
                            currentTile = new Destination();
                            break;

                        case '~':
                            currentTile = new Trap();
                            break;

                        case 'Z':
                            currentTile = new Floor();

                            Employee employeeASleep = new Employee(currentTile, false);
                            currentTile.ObjectOnTile = employeeASleep;
                            _maze.Employee           = employeeASleep;
                            break;

                        case 'o':
                            currentTile = new Floor();

                            Crate crateNormal = new Crate(currentTile);
                            _maze.AddCrate(crateNormal);
                            currentTile.ObjectOnTile = crateNormal;

                            break;

                        case '0':
                            currentTile = new Destination();

                            Crate crateOnDestination = new Crate(currentTile);
                            _maze.AddCrate(crateOnDestination);
                            currentTile.ObjectOnTile = crateOnDestination;
                            break;

                        case '$':
                            currentTile = new Floor();

                            Employee employeeAwake = new Employee(currentTile, true);
                            currentTile.ObjectOnTile = employeeAwake;
                            _maze.Employee           = employeeAwake;
                            break;

                        case '.':
                            currentTile = new Floor();
                            break;

                        case '@':
                            currentTile = new Floor();

                            Truck truck = new Truck(currentTile);
                            currentTile.ObjectOnTile = truck;
                            _maze.Truck = truck;
                            break;

                        case ' ':
                            currentTile = new EmptySpot();
                            break;

                        case '#':
                            currentTile = new Wall();
                            break;

                        default:
                            break;
                        }
                        if (previousTile != null)
                        {
                            currentTile.LeftTile   = previousTile;
                            previousTile.RightTile = currentTile;
                        }
                        previousTile = currentTile;
                        currentTileList.Add(currentTile);
                    }

                    if (previousTileList != null)
                    {
                        for (int i = 0; i < currentTileList.Count; i++)
                        {
                            currentTileList[i].UpTile    = previousTileList[i];
                            previousTileList[i].DownTile = currentTileList[i];
                        }
                    }
                    else
                    {
                        this._maze.OriginalTile = currentTileList[0];
                    }

                    previousTileList = currentTileList;

                    lineString = this._reader.ReadLine();
                }

                this._reader.Close();
                this._input.Close();

                return(_maze);
            }
            catch
            {
                throw;
            }
        }
Example #11
0
        public void ConnectHorizontal()
        {
            for (int outer = 0; outer < _lines.Count; outer++)
            {
                char[] currentLine = _lines[outer];
                for (int inner = 0; inner < currentLine.Length; inner++)
                {
                    Tile temp;
                    switch (currentLine[inner])
                    {
                    case '#':
                        temp = new Wall(false);
                        break;

                    case '.':
                        temp = new Floor(false);
                        break;

                    case 'x':
                        temp = new Floor(true);
                        break;

                    case 'o':
                        Floor crateFloor = new Floor(false);
                        Crate crate      = new Crate(crateFloor);
                        crateFloor.Crate = crate;
                        _maze.Crates.Add(crate);
                        temp = crateFloor;
                        break;

                    case '@':
                        Floor    forkliftFloor = new Floor(false);
                        Forklift forklift      = new Forklift(forkliftFloor);
                        forkliftFloor.Forklift = forklift;
                        _maze.Forklift         = forklift;
                        temp = forkliftFloor;
                        break;

                    case '~':
                        temp = new BrokenFloor();
                        break;

                    case '$':
                        Floor    employeeFloor = new Floor(false);
                        Employee employee      = new Employee(employeeFloor);
                        employeeFloor.Employee = employee;
                        _maze.Employee         = employee;
                        temp = employeeFloor;
                        break;

                    default:
                        temp = new Wall(true);
                        break;
                    }

                    if (inner == 0)
                    {
                        _heads[outer] = temp;
                    }
                    else
                    {
                        _heads[outer].AddEast(temp);
                    }
                }
            }
        }