import TerrainGenerator from './TerrainGenerator' ; import { Point , TileData } from '../core/Components' ; import RectGrid from '../core/Grid' ; class TerrainRandomGenerator extends TerrainGenerator { constructor ( width , height , tileData ? , ignore ? ) { super ( width , height , tileData , ignore ) ; } param topLeft param bottomRight generateTerrain ( topLeft ? , bottomRight ? ) { const grid = new RectGrid ( this . width , this . height ) ; if ( topLeft === undefined ) { topLeft = { x : 1 , y : 1 } ; } if ( bottomRight === undefined ) { bottomRight = { x : grid . getWidth ( ) - 2 , y : grid . getHeight ( ) - 2 } ; } for ( let x = topLeft . x - 1 ; x <= bottomRight . x + 1 ; x ++ ) { for ( let y = topLeft . y - 1 ; y <= bottomRight . y + 1 ; y ++ ) { if ( getRand ( 0 , 3 ) === 0 ) { this . draw ( grid , { point : { x : x , y : y } , data : this . getTerrain ( ) } ) ; } } } return grid ; } } param min param max function getRand ( min , max ) { return Math . floor ( Math . random ( ) * ( max + 1 - min ) + min ) ; } export default TerrainRandomGenerator ;