11import { randomBetween } from './utils' ;
2+ const Y_FROM_FLOOR = [ 128 , 112 , 96 , 80 , 68 ] ;
3+ const GROUND = 0 ;
4+ const COLUMN_ROOT = 1 ;
5+ const COLUMN_MID = 2 ;
6+ const COLUMN_TOP = 3 ;
7+ const CLOUD_RIGHT = 8 ;
8+ const CLOUD_LEFT = 7 ;
9+ const PLATFORM = 9 ;
10+ const HEIGHTS = [ - 100 , 13 , 29 , 45 , 61 ] ;
211
312export default class Map {
413 constructor ( tiles , mapstring ) {
@@ -13,41 +22,36 @@ export default class Map {
1322 _randomCloud ( ) {
1423 return { x : randomBetween ( - 6 , 160 ) , y : randomBetween ( 0 , 120 ) , speed : randomBetween ( 0.05 , 0.2 ) } ;
1524 }
16- update ( time ) {
25+ _updateCloud ( cloud , time ) {
26+ if ( cloud . x > - 40 ) {
27+ cloud . x -= time . delta * cloud . speed ;
28+ } else {
29+ cloud . x = 160 ;
30+ cloud . y = randomBetween ( 0 , 120 ) ;
31+ cloud . speed = randomBetween ( 0.05 , 0.2 ) ;
32+ }
33+ }
34+ update ( time , delta , ticks ) {
1735 this . x -= time . delta * 0.1 ;
18-
19- this . clouds . forEach ( ( cloud ) => {
20- if ( cloud . x > - 40 ) {
21- cloud . x -= time . delta * cloud . speed ;
22- } else {
23- cloud . x = 160 ;
24- cloud . y = this . _randomCloud ( ) . y ;
25- cloud . speed = this . _randomCloud ( ) . speed ;
26- }
27- } ) ;
36+ for ( let i = 0 ; i < this . clouds . length ; i ++ ) {
37+ this . _updateCloud ( this . clouds [ i ] , time ) ;
38+ }
2839 }
29- whatsHere ( offset ) {
30- let heights = [ - 100 , 13 , 29 , 45 , 61 ] ;
40+ heightHere ( offset ) {
3141 let index = Math . floor ( ( - this . x + offset ) / this . tiles . mapTiles . tileWidth ) ;
3242 let tile = this . stage [ index ] ;
33- return { height : heights [ tile ] } ;
43+ return HEIGHTS [ tile ] ;
3444 }
3545 render ( graphics , ctx ) {
36- const y = [ 128 , 112 , 96 , 80 , 68 ] ;
37- const GROUND = 0 ;
38- const COLUMN_ROOT = 1 ;
39- const COLUMN_MID = 2 ;
40- const COLUMN_TOP = 3 ;
41- const PLATFORM = 9 ;
42- const CLOUD_LEFT = 7 ;
43- const CLOUD_RIGHT = 8 ;
4446 let leftmostTile = Math . floor ( ( - this . x ) / this . tiles . mapTiles . tileWidth ) ;
4547 let rightmostTile = leftmostTile + 10 ;
4648
47- this . clouds . forEach ( ( cloud ) => {
49+ let cloud ;
50+ for ( let i = 0 ; i < this . clouds . length ; i ++ ) {
51+ cloud = this . clouds [ i ] ;
4852 graphics . drawTile ( this . tiles . mapTiles , CLOUD_LEFT , 0 , cloud . x , cloud . y , ctx ) ;
4953 graphics . drawTile ( this . tiles . mapTiles , CLOUD_RIGHT , 0 , cloud . x + 16 , cloud . y , ctx ) ;
50- } ) ;
54+ } ;
5155
5256 this . stage . forEach ( ( t , index ) => {
5357 if ( index < leftmostTile || index > rightmostTile ) {
@@ -61,7 +65,7 @@ export default class Map {
6165 case '2' :
6266 case '3' :
6367 case '4' :
64- graphics . drawTile ( this . tiles . mapTiles , PLATFORM , 0 , x , y [ t - 1 ] , ctx ) ;
68+ graphics . drawTile ( this . tiles . mapTiles , PLATFORM , 0 , x , Y_FROM_FLOOR [ t - 1 ] , ctx ) ;
6569 break ;
6670 }
6771 } ) ;
0 commit comments