import { CONST } from '../const/const' ; import { Tile } from '../objects/tile' ; export class GameScene extends Phaser . Scene { private canMove : boolean ; private tileGrid : Tile [ ] [ ] ; private firstSelectedTile : Tile ; private secondSelectedTile : Tile ; constructor ( ) { super ( { key : 'GameScene' } ) ; } init ( ) { this . canMove = true ; this . cameras . main . setBackgroundColor ( 0x78aade ) ; this . tileGrid = [ ] ; for ( let y = 0 ; y < CONST . gridHeight ; y ++ ) { this . tileGrid [ y ] = [ ] ; for ( let x = 0 ; x < CONST . gridWidth ; x ++ ) { this . tileGrid [ y ] [ x ] = this . addTile ( x , y ) ; } } this . firstSelectedTile = undefined ; this . secondSelectedTile = undefined ; this . input . on ( 'gameobjectdown' , this . tileDown , this ) ; this . checkMatches ( ) ; } param x param y private addTile ( x , y ) { let randomTileType = CONST . candyTypes [ Phaser . Math . RND . between ( 0 , CONST . candyTypes . length - 1 ) ] ; return new Tile ( { scene : this , x : x * CONST . tileWidth , y : y * CONST . tileHeight , texture : randomTileType } ) ; } param pointer param gameobject param event private tileDown ( pointer , gameobject , event ) { if ( this . canMove ) { if ( ! this . firstSelectedTile ) { this . firstSelectedTile = gameobject ; } else { this . secondSelectedTile = gameobject ; let dx = Math . abs ( this . firstSelectedTile . x - this . secondSelectedTile . x ) / CONST . tileWidth ; let dy = Math . abs ( this . firstSelectedTile . y - this . secondSelectedTile . y ) / CONST . tileHeight ; if ( ( dx === 1 && dy === 0 ) || ( dx === 0 && dy === 1 ) ) { this . canMove = false ; this . swapTiles ( ) ; } } } } /**
     * This function will take care of the swapping of the two selected tiles.
     * It will only work, if two tiles have been selected.
     */ private swapTiles ( ) { if ( this . firstSelectedTile && this . secondSelectedTile ) { let firstTilePosition = { x : this . firstSelectedTile . x , y : this . firstSelectedTile . y } ; let secondTilePosition = { x : this . secondSelectedTile . x , y : this . secondSelectedTile . y } ; this . tileGrid [ firstTilePosition . y / CONST . tileHeight ] [ firstTilePosition . x / CONST . tileWidth ] = this . secondSelectedTile ; this . tileGrid [ secondTilePosition . y / CONST . tileHeight ] [ secondTilePosition . x / CONST . tileWidth ] = this . firstSelectedTile ; this . add . tween ( { targets : this . firstSelectedTile , x : this . secondSelectedTile . x , y : this . secondSelectedTile . y , ease : 'Linear' , duration : 400 , repeat : 0 , yoyo : false } ) ; this . add . tween ( { targets : this . secondSelectedTile , x : this . firstSelectedTile . x , y : this . firstSelectedTile . y , ease : 'Linear' , duration : 400 , repeat : 0 , yoyo : false , onComplete : ( ) => { this . checkMatches ( ) ; } } ) ; this . firstSelectedTile = this . tileGrid [ firstTilePosition . y / CONST . tileHeight ] [ firstTilePosition . x / CONST . tileWidth ] ; this . secondSelectedTile = this . tileGrid [ secondTilePosition . y / CONST . tileHeight ] [ secondTilePosition . x / CONST . tileWidth ] ; } } private checkMatches ( ) { let matches = this . getMatches ( this . tileGrid ) ; if ( matches . length > 0 ) { this . removeTileGroup ( matches ) ; this . resetTile ( ) ; this . fillTile ( ) ; this . tileUp ( ) ; this . checkMatches ( ) ; } else { this . swapTiles ( ) ; this . tileUp ( ) ; this . canMove = true ; } } private resetTile ( ) { for ( let y = this . tileGrid . length - 1 ; y > 0 ; y -- ) { for ( let x = this . tileGrid [ y ] . length - 1 ; x > 0 ; x -- ) { if ( this . tileGrid [ y ] [ x ] === undefined && this . tileGrid [ y - 1 ] [ x ] !== undefined ) { let tempTile = this . tileGrid [ y - 1 ] [ x ] ; this . tileGrid [ y ] [ x ] = tempTile ; this . tileGrid [ y - 1 ] [ x ] = undefined ; this . add . tween ( { targets : tempTile , y : CONST . tileHeight * y , ease : 'Linear' , duration : 200 , repeat : 0 , yoyo : false } ) ; x = this . tileGrid [ y ] . length ; } } } } private fillTile ( ) { for ( var y = 0 ; y < this . tileGrid . length ; y ++ ) { for ( var x = 0 ; x < this . tileGrid [ y ] . length ; x ++ ) { if ( this . tileGrid [ y ] [ x ] === undefined ) { let tile = this . addTile ( x , y ) ; this . tileGrid [ y ] [ x ] = tile ; } } } } private tileUp ( ) { this . firstSelectedTile = undefined ; this . secondSelectedTile = undefined ; } private removeTileGroup ( matches ) { for ( var i = 0 ; i < matches . length ; i ++ ) { var tempArr = matches [ i ] ; for ( var j = 0 ; j < tempArr . length ; j ++ ) { let tile = tempArr [ j ] ; let tilePos = this . getTilePos ( this . tileGrid , tile ) ; if ( tilePos . x !== - 1 && tilePos . y !== - 1 ) { tile . destroy ( ) ; this . tileGrid [ tilePos . y ] [ tilePos . x ] = undefined ; } } } } private getTilePos ( tileGrid , tile ) { let pos = { x : - 1 , y : - 1 } ; for ( let y = 0 ; y < tileGrid . length ; y ++ ) { for ( let x = 0 ; x < tileGrid [ y ] . length ; x ++ ) { if ( tile === tileGrid [ y ] [ x ] ) { pos . x = x ; pos . y = y ; break ; } } } return pos ; } private getMatches ( tileGrid ) { let matches = [ ] ; let groups = [ ] ; for ( let y = 0 ; y < tileGrid . length ; y ++ ) { let tempArray = tileGrid [ y ] ; groups = [ ] ; for ( let x = 0 ; x < tempArray . length ; x ++ ) { if ( x < tempArray . length - 2 ) { if ( tileGrid [ y ] [ x ] && tileGrid [ y ] [ x + 1 ] && tileGrid [ y ] [ x + 2 ] ) { if ( tileGrid [ y ] [ x ] . texture . key === tileGrid [ y ] [ x + 1 ] . texture . key && tileGrid [ y ] [ x + 1 ] . texture . key === tileGrid [ y ] [ x + 2 ] . texture . key ) { if ( groups . length > 0 ) { if ( groups . indexOf ( tileGrid [ y ] [ x ] ) == - 1 ) { matches . push ( groups ) ; groups = [ ] ; } } if ( groups . indexOf ( tileGrid [ y ] [ x ] ) == - 1 ) { groups . push ( tileGrid [ y ] [ x ] ) ; } if ( groups . indexOf ( tileGrid [ y ] [ x + 1 ] ) == - 1 ) { groups . push ( tileGrid [ y ] [ x + 1 ] ) ; } if ( groups . indexOf ( tileGrid [ y ] [ x + 2 ] ) == - 1 ) { groups . push ( tileGrid [ y ] [ x + 2 ] ) ; } } } } } if ( groups . length > 0 ) { matches . push ( groups ) ; } } for ( let j = 0 ; j < tileGrid . length ; j ++ ) { var tempArr = tileGrid [ j ] ; groups = [ ] ; for ( let i = 0 ; i < tempArr . length ; i ++ ) { if ( i < tempArr . length - 2 ) if ( tileGrid [ i ] [ j ] && tileGrid [ i + 1 ] [ j ] && tileGrid [ i + 2 ] [ j ] ) { if ( tileGrid [ i ] [ j ] . texture . key === tileGrid [ i + 1 ] [ j ] . texture . key && tileGrid [ i + 1 ] [ j ] . texture . key === tileGrid [ i + 2 ] [ j ] . texture . key ) { if ( groups . length > 0 ) { if ( groups . indexOf ( tileGrid [ i ] [ j ] ) == - 1 ) { matches . push ( groups ) ; groups = [ ] ; } } if ( groups . indexOf ( tileGrid [ i ] [ j ] ) == - 1 ) { groups . push ( tileGrid [ i ] [ j ] ) ; } if ( groups . indexOf ( tileGrid [ i + 1 ] [ j ] ) == - 1 ) { groups . push ( tileGrid [ i + 1 ] [ j ] ) ; } if ( groups . indexOf ( tileGrid [ i + 2 ] [ j ] ) == - 1 ) { groups . push ( tileGrid [ i + 2 ] [ j ] ) ; } } } } if ( groups . length > 0 ) matches . push ( groups ) ; } return matches ; } }