HTML5 swipe controlled Sokoban game made with Phaser featuring unlimited undos – solving and advancing through levels

Read all posts about "" game
In this new step of the Sokoban series I am showing you how to check if the player solved a level, and how to advance through levels. The concept is very simple: a level is solved when there aren’t crates outside target spots, which means when the level array does not have an item with CRATE value in it. Have a look at the game:
Swipe to move the player, double click/tap to undo last move. If you have a mobile device, you can play the game at this link. The first level is ridiculously easy to solve, just to show you how to check for a level to be solved and advance through levels. This is the source code, with new or modified lines highlighted. I did not highlight each level changed to level[this.currentLevel] not to create a visual mess, but you should keep in mind level was transformed from a two-dimensional array into a three-dimensional array.
var game;
var gameOptions = {
    tileSize: 40,
    gameWidth: 320,
    gameHeight: 320,
    gameSpeed: 100
}
var level = [
    // level 0
    [[1,1,1,1,1,1,1,1],
    [1,0,0,1,1,1,1,1],
    [1,0,0,1,1,1,1,1],
    [1,0,0,0,0,0,0,1],
    [1,1,4,2,1,0,0,1],
    [1,0,0,3,1,0,0,1],
    [1,0,0,0,1,1,1,1],
    [1,1,1,1,1,1,1,1]],
    // level 1
    [[1,1,1,1,1,1,1,1],
    [1,0,0,1,1,1,1,1],
    [1,0,0,1,1,1,1,1],
    [1,0,0,0,0,0,0,1],
    [1,1,4,2,1,3,0,1],
    [1,0,0,0,1,0,0,1],
    [1,0,0,0,1,1,1,1],
    [1,1,1,1,1,1,1,1]]
];
var EMPTY = 0;
var WALL = 1;
var SPOT = 2;
var CRATE = 3;
var PLAYER = 4;
window.onload = function() {
    game = new Phaser.Game(gameOptions.gameWidth, gameOptions.gameHeight);
    game.state.add("PlayGame", playGame);
    game.state.start("PlayGame", true, false, 0);
}
var playGame = function(game){}
playGame.prototype = {
    init: function(currentLevel){
            this.currentLevel = currentLevel;
    },
    preload: function(){
        game.load.spritesheet("tiles", "tiles.png", 40, 40);
        game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        game.scale.pageAlignHorizontally = true;
        game.scale.pageAlignVertically = true;
        game.stage.disableVisibilityChange = true;
    },
    create: function(){
        this.undoArray = [];
        this.crates = [];
        this.drawLevel();
        game.input.onTap.add(this.handleTap, this);
        game.input.onDown.add(this.beginSwipe, this);
    },
    drawLevel: function(){
        this.staticAssetsGroup = game.add.group();
        this.movingAssetsGroup = game.add.group();
        this.crates.length = 0;
        for(var i = 0; i < level[this.currentLevel].length; i++){
            this.crates[i] = [];
            for(var j = 0; j < level[this.currentLevel][i].length; j++){
                this.crates[i][j] = null;
                switch(level[this.currentLevel][i][j]){
                    case PLAYER:
                    case PLAYER + SPOT:
                        this.player = game.add.sprite(gameOptions.tileSize * j, gameOptions.tileSize * i, "tiles");
                        this.player.frame = level[this.currentLevel][i][j];
                        this.player.posX = j;
                        this.player.posY = i;
                        this.movingAssetsGroup.add(this.player);
                        var tile = game.add.sprite(gameOptions.tileSize * j, gameOptions.tileSize * i, "tiles");
                        tile.frame = level[this.currentLevel][i][j] - PLAYER;
                        this.staticAssetsGroup.add(tile);
                        break;
                    case CRATE:
                    case CRATE + SPOT:
                        this.crates[i][j] = game.add.sprite(gameOptions.tileSize * j, gameOptions.tileSize * i, "tiles");
                        this.crates[i][j].frame = level[this.currentLevel][i][j];
                        this.movingAssetsGroup.add(this.crates[i][j]);
                        var tile = game.add.sprite(gameOptions.tileSize * j, gameOptions.tileSize * i, "tiles");
                        tile.frame = level[this.currentLevel][i][j] - CRATE;
                        this.staticAssetsGroup.add(tile);
                        break;
                    default:
                        var tile = game.add.sprite(gameOptions.tileSize * j, gameOptions.tileSize * i, "tiles");
                        tile.frame = level[this.currentLevel][i][j];
                        this.staticAssetsGroup.add(tile);
                }
            }
        }
    },
    handleTap: function(pointer, doubleTap){
        if(doubleTap){
            if(this.undoArray.length&gt;0){
				var undoLevel = this.undoArray.pop();
                this.staticAssetsGroup.destroy();
                this.movingAssetsGroup.destroy();
     			level[this.currentLevel] = [];
     			level[this.currentLevel] = this.copyArray(undoLevel);
     			this.drawLevel();
			}
        }
    },
    beginSwipe: function(e) {
        game.input.onDown.remove(this.beginSwipe, this);
        game.input.onUp.add(this.endSwipe, this);
    },
    endSwipe: function(e) {
        game.input.onUp.remove(this.endSwipe, this);
        var swipeTime = e.timeUp - e.timeDown;
        var swipeDistance = Phaser.Point.subtract(e.position, e.positionDown);
        var swipeMagnitude = swipeDistance.getMagnitude();
        var swipeNormal = Phaser.Point.normalize(swipeDistance);
        if(swipeMagnitude &gt; 20 &amp;&amp; swipeTime < 1000 &amp;&amp; (Math.abs(swipeNormal.y) &gt; 0.8 || Math.abs(swipeNormal.x) &gt; 0.8)) {
            if(swipeNormal.x &gt; 0.8) {
                this.checkMove(1, 0);
            }
            if(swipeNormal.x < -0.8) {
                this.checkMove(-1, 0);
            }
            if(swipeNormal.y &gt; 0.8) {
                this.checkMove(0, 1);
            }
            if(swipeNormal.y < -0.8) {
                this.checkMove(0, -1);
            }
        } else {
            game.input.onDown.add(this.beginSwipe, this);
        }
    },
    checkMove: function(deltaX, deltaY){
        if(this.isWalkable(this.player.posX + deltaX, this.player.posY + deltaY)){
            this.undoArray.push(this.copyArray(level[this.currentLevel]));
            this.movePlayer(deltaX, deltaY);
            return;
        }
        if(this.isCrate(this.player.posX + deltaX, this.player.posY + deltaY)){
            if(this.isWalkable(this.player.posX + 2 * deltaX, this.player.posY + 2 * deltaY)){
                this.undoArray.push(this.copyArray(level[this.currentLevel]));
                this.moveCrate(deltaX, deltaY);
                this.movePlayer(deltaX, deltaY);
                return;
            }
        }
        game.input.onDown.add(this.beginSwipe, this);
    },
    isWalkable: function(posX, posY){
       return level[this.currentLevel][posY][posX] == EMPTY || level[this.currentLevel][posY][posX] == SPOT;
    },
    isCrate: function(posX, posY){
        return level[this.currentLevel][posY][posX] == CRATE || level[this.currentLevel][posY][posX] == CRATE + SPOT;
    },
    movePlayer: function(deltaX, deltaY){
        var playerTween =game.add.tween(this.player);
		playerTween.to({
			x: this.player.x + deltaX * gameOptions.tileSize,
			y: this.player.y + deltaY * gameOptions.tileSize
		}, gameOptions.gameSpeed, Phaser.Easing.Linear.None, true);
		playerTween.onComplete.add(function(){
            game.input.onDown.add(this.beginSwipe, this);
            this.player.frame = level[this.currentLevel][this.player.posY][this.player.posX];
            if(this.isLevelSolved()){
                this.currentLevel++;
                this.game.state.start("PlayGame", true, false, this.currentLevel);
            }
        }, this);
        level[this.currentLevel][this.player.posY][this.player.posX] -= PLAYER;
        this.player.posX += deltaX;
        this.player.posY += deltaY;
        level[this.currentLevel][this.player.posY][this.player.posX] += PLAYER;
	},
    moveCrate: function(deltaX, deltaY){
	    var crateTween = game.add.tween(this.crates[this.player.posY + deltaY][this.player.posX + deltaX]);
		crateTween.to({
		    x: this.crates[this.player.posY + deltaY][this.player.posX + deltaX].x + deltaX * gameOptions.tileSize,
			y: this.crates[this.player.posY + deltaY][this.player.posX + deltaX].y + deltaY * gameOptions.tileSize,
		}, gameOptions.gameSpeed, Phaser.Easing.Linear.None, true);
        crateTween.onComplete.add(function(){
            this.crates[this.player.posY + deltaY][this.player.posX + deltaX].frame = level[this.currentLevel][this.player.posY + deltaY][this.player.posX + deltaX];
        }, this);
	    this.crates[this.player.posY + 2 * deltaY][this.player.posX + 2 * deltaX] = this.crates[this.player.posY + deltaY][this.player.posX + deltaX];
        this.crates[this.player.posY + deltaY][this.player.posX + deltaX] = null;
        level[this.currentLevel][this.player.posY + deltaY][this.player.posX + deltaX] -= CRATE;
        level[this.currentLevel][this.player.posY + 2 * deltaY][this.player.posX + 2 * deltaX] += CRATE;
	},
    isLevelSolved: function(){
        for(var i = 0; i < level[this.currentLevel].length; i++){
            for(var j = 0; j < level[this.currentLevel][i].length; j++){
                if(level[this.currentLevel][i][j] == CRATE){
                    return false;
                }
            }
        }
        return true;
    },
    copyArray: function(a){
        var newArray = a.slice(0);
    	for(var i = newArray.length; i &gt; 0; i--){
			if(newArray[i] instanceof Array){
				newArray[i] = this.copyArray(newArray[i]);
			}
		}
		return newArray;
	}
}

Next time I will add more levels and a level selection screen, meanwhile download the source code.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers