@@ -143,9 +143,8 @@ class Mario {
143143 } ;
144144
145145 update ( ) {
146-
147146 const TICK = this . game . clockTick ;
148-
147+
149148 // I used this page to approximate my constants
150149 // https://web.archive.org/web/20130807122227/http://i276.photobucket.com/albums/kk21/jdaster64/smb_playerphysics.png
151150 // I converted these values from hex and into units of pixels and seconds.
@@ -420,9 +419,15 @@ class Mario {
420419 if ( this . velocity . x >= MAX_WALK && ! this . game . B ) this . velocity . x = MAX_WALK ;
421420 if ( this . velocity . x <= - MAX_WALK && ! this . game . B ) this . velocity . x = - MAX_WALK ;
422421
423-
424- // update position
422+ // Collision detection
423+ // update X position
425424 this . x += this . velocity . x * TICK * PARAMS . SCALE ;
425+ this . updateLastBB ( ) ;
426+ this . updateBB ( ) ;
427+ // Handle horizontal collisions
428+ this . handleHorizontalCollisions ( ) ;
429+
430+ // Update Y position
426431 this . y += this . velocity . y * TICK * PARAMS . SCALE ;
427432
428433 // Apply left boundary constraint
@@ -433,155 +438,12 @@ class Mario {
433438
434439 this . updateLastBB ( ) ;
435440 this . updateBB ( ) ;
441+ // Handle vertical collisions
442+ this . handleVerticalCollisions ( ) ;
436443
437444 // if mario fell of the map he's dead
438445 if ( this . y > PARAMS . BLOCKWIDTH * 16 ) this . die ( ) ;
439446
440- // collision
441- var that = this ;
442- this . game . entities . forEach ( function ( entity ) {
443- if ( entity . BB && that . BB . collide ( entity . BB ) ) {
444- if ( that . velocity . y > 0 ) { // falling
445- if ( ( entity instanceof Ground || entity instanceof Brick || entity instanceof Block || entity instanceof Tube || entity instanceof SideTube ) // landing
446- && ( that . lastBB . bottom ) <= entity . BB . top ) { // was above last tick
447- if ( that . size === 0 || that . size === 3 ) { // small
448- that . y = entity . BB . top - PARAMS . BLOCKWIDTH ;
449- } else { // big
450- that . y = entity . BB . top - 2 * PARAMS . BLOCKWIDTH ;
451- }
452- that . velocity . y = 0 ;
453-
454- if ( that . state === 4 ) that . state = 0 ; // set state to idle
455- //that.updateBB();
456-
457- if ( entity instanceof Tube && entity . destination && that . game . down ) {
458- that . game . camera . loadLevel ( bonusLevelOne , 2.5 * PARAMS . BLOCKWIDTH , 0 * PARAMS . BLOCKWIDTH , false , false ) ;
459- }
460- }
461- else if ( entity instanceof Lift && that . lastBB . bottom <= entity . BB . top + PARAMS . SCALE * 3 ) {
462- if ( that . size === 0 || that . size === 3 ) { // small
463- that . y = entity . BB . top - PARAMS . BLOCKWIDTH ;
464- } else { // big
465- that . y = entity . BB . top - 2 * PARAMS . BLOCKWIDTH ;
466- }
467- that . velocity . y = 0 ;
468-
469- if ( that . state === 4 ) that . state = 0 ; // set state to idle
470- //that.updateBB();
471- }
472- else if ( ( entity instanceof Goomba || entity instanceof Koopa || entity instanceof KoopaParatroopaGreen || entity instanceof KoopaParatroopaRed || entity instanceof KoopaShell ) // squish Goomba
473- && ( that . lastBB . bottom ) <= entity . BB . top // was above last tick
474- && ! entity . dead ) { // can't squish an already squished Goomba
475- entity . dead = true ;
476- that . velocity . y = - 240 ; // bounce
477- ASSET_MANAGER . playAsset ( "./audio/stomp.mp3" ) ;
478- }
479- }
480- else if ( that . velocity . y < 0 ) { // jumping
481- if ( ( entity instanceof Brick ) // hit ceiling
482- && ( that . lastBB . top ) >= entity . BB . bottom ) { // was below last tick
483-
484- if ( that . BB . collide ( entity . leftBB ) && that . BB . collide ( entity . rightBB ) ) { // collide with the center point of the brick
485- entity . bounce = true ;
486- that . velocity . y = 0 ;
487-
488- if ( entity . type == 1 && that . size != 0 && that . size != 3 ) { // if it's a regular brick, and mario is big
489- entity . explode ( ) ;
490- }
491- }
492- else if ( that . BB . collide ( entity . leftBB ) ) {
493- that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
494- }
495- else {
496- that . x = entity . BB . right ;
497- }
498-
499- }
500- else if ( entity instanceof Lift && that . lastBB . bottom <= entity . BB . top + PARAMS . SCALE * 3 ) {
501- if ( that . size === 0 || that . size === 3 ) { // small
502- that . y = entity . BB . top - PARAMS . BLOCKWIDTH ;
503- } else { // big
504- that . y = entity . BB . top - 2 * PARAMS . BLOCKWIDTH ;
505- }
506- that . velocity . y = 0 ;
507- //that.updateBB();
508- }
509- }
510- if ( ( entity instanceof Brick && entity . type ) // hit a visible brick
511- && that . BB . collide ( entity . BB ) ) {
512- let overlap = that . BB . overlap ( entity . BB ) ;
513- if ( overlap . y > 2 ) { // hit the side
514- if ( that . BB . collide ( entity . leftBB ) && that . lastBB . right <= entity . BB . left ) {
515- that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
516- if ( that . velocity . x > 0 ) that . velocity . x = 0 ;
517- } else if ( that . BB . collide ( entity . rightBB ) && that . lastBB . left >= entity . BB . right ) {
518- that . x = entity . BB . right ;
519- if ( that . velocity . x < 0 ) that . velocity . x = 0 ;
520- }
521- }
522- //that.updateBB();
523- }
524- else if ( ( entity instanceof Tube || entity instanceof SideTube || entity instanceof Ground || entity instanceof Block ) ) {
525- if ( that . lastBB . right <= entity . BB . left ) { // Collided with the left
526- that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
527- if ( that . velocity . x > 0 ) that . velocity . x = 0 ;
528- if ( entity instanceof SideTube && that . game . right ) {
529- that . game . camera . loadLevel ( levelOne , 162.5 * PARAMS . BLOCKWIDTH , 11 * PARAMS . BLOCKWIDTH )
530- }
531- } else if ( that . lastBB . left >= entity . BB . right ) { // Collided with the right
532- that . x = entity . BB . right ;
533- if ( that . velocity . x < 0 ) that . velocity . x = 0 ;
534- }
535- //that.updateBB();
536- }
537- else if ( entity instanceof Mushroom && ! entity . emerging ) {
538- entity . removeFromWorld = true ;
539- ASSET_MANAGER . playAsset ( "./audio/power-up.mp3" ) ;
540- if ( entity . type === 'Growth' ) {
541- that . y -= PARAMS . BLOCKWIDTH ;
542- that . size = 1 ;
543- that . game . addEntity ( new Score ( that . game , that . x , that . y , 1000 ) ) ;
544- } else {
545- that . game . camera . lives ++ ;
546- }
547- }
548- else if ( entity instanceof Flower && ! entity . emerging ) {
549- entity . removeFromWorld = true ;
550- ASSET_MANAGER . playAsset ( "./audio/power-up.mp3" ) ;
551- if ( that . size == 1 ) {
552- that . size = 2 ;
553- } else if ( that . size == 0 ) {
554- that . size = 1 ;
555- }
556- }
557- else if ( entity instanceof Coin ) {
558- entity . removeFromWorld = true ;
559- that . game . camera . score += 200 ;
560- that . game . camera . addCoin ( ) ;
561- }
562- else if ( entity instanceof FireBar_Fire ) {
563- that . die ( ) ;
564- }
565- else if ( entity instanceof Flag ) {
566- that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
567- that . velocity . x = 0 ;
568- entity . win = true ;
569- that . state = 6 ;
570- that . win = true ;
571- if ( ! that . flagTouchedY ) {
572- that . flagTouchedY = that . y ;
573- }
574- }
575- }
576-
577- // counting the number of fireballs currently in play
578- if ( entity instanceof Fireball ) {
579- that . fireballsThrown ++ ;
580- }
581-
582- that . updateBB ( ) ;
583- } ) ;
584-
585447 // mario throws fireballs
586448 if ( this . size == 2 ) {
587449 if ( this . fireballsThrown >= 2 ) {
@@ -610,18 +472,169 @@ class Mario {
610472 else if ( Math . abs ( this . velocity . x ) > MAX_WALK ) this . state = 2 ;
611473 else if ( Math . abs ( this . velocity . x ) >= MIN_WALK ) this . state = 1 ;
612474 else this . state = 0 ;
613- } else {
614-
615475 }
616476
617-
618-
619477 // update direction
620478 if ( this . velocity . x < 0 ) this . facing = 1 ;
621479 if ( this . velocity . x > 0 ) this . facing = 0 ;
622-
623480 }
624481 } ;
482+
483+ handleHorizontalCollisions ( ) {
484+ const that = this ;
485+ this . game . entities . forEach ( function ( entity ) {
486+ if ( entity . BB && that . BB . collide ( entity . BB ) ) {
487+ if ( ( entity instanceof Brick && entity . type ) // hit a visible brick
488+ && that . BB . collide ( entity . BB ) ) {
489+ let overlap = that . BB . overlap ( entity . BB ) ;
490+ if ( that . BB . collide ( entity . leftBB ) && that . lastBB . right <= entity . BB . left ) {
491+ that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
492+ if ( that . velocity . x > 0 ) that . velocity . x = 0 ;
493+ } else if ( that . BB . collide ( entity . rightBB ) && that . lastBB . left >= entity . BB . right ) {
494+ that . x = entity . BB . right ;
495+ if ( that . velocity . x < 0 ) that . velocity . x = 0 ;
496+ }
497+ that . updateBB ( ) ;
498+ }
499+ else if ( ( entity instanceof Tube || entity instanceof SideTube || entity instanceof Ground || entity instanceof Block ) ) {
500+ if ( that . lastBB . right <= entity . BB . left ) { // Collided with the left
501+ that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
502+ if ( that . velocity . x > 0 ) that . velocity . x = 0 ;
503+ if ( entity instanceof SideTube && that . game . right ) {
504+ that . game . camera . loadLevel ( levelOne , 162.5 * PARAMS . BLOCKWIDTH , 11 * PARAMS . BLOCKWIDTH )
505+ }
506+ } else if ( that . lastBB . left >= entity . BB . right ) { // Collided with the right
507+ that . x = entity . BB . right ;
508+ if ( that . velocity . x < 0 ) that . velocity . x = 0 ;
509+ }
510+ that . updateBB ( ) ;
511+ }
512+ else if ( entity instanceof Mushroom && ! entity . emerging ) {
513+ entity . removeFromWorld = true ;
514+ ASSET_MANAGER . playAsset ( "./audio/power-up.mp3" ) ;
515+ if ( entity . type === 'Growth' ) {
516+ that . y -= PARAMS . BLOCKWIDTH ;
517+ that . size = 1 ;
518+ that . game . addEntity ( new Score ( that . game , that . x , that . y , 1000 ) ) ;
519+ } else {
520+ that . game . camera . lives ++ ;
521+ }
522+ }
523+ else if ( entity instanceof Flower && ! entity . emerging ) {
524+ entity . removeFromWorld = true ;
525+ ASSET_MANAGER . playAsset ( "./audio/power-up.mp3" ) ;
526+ if ( that . size == 1 ) {
527+ that . size = 2 ;
528+ } else if ( that . size == 0 ) {
529+ that . size = 1 ;
530+ }
531+ }
532+ else if ( entity instanceof Coin ) {
533+ entity . removeFromWorld = true ;
534+ that . game . camera . score += 200 ;
535+ that . game . camera . addCoin ( ) ;
536+ }
537+ else if ( entity instanceof FireBar_Fire ) {
538+ that . die ( ) ;
539+ }
540+ else if ( entity instanceof Flag ) {
541+ that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
542+ that . velocity . x = 0 ;
543+ entity . win = true ;
544+ that . state = 6 ;
545+ that . win = true ;
546+ if ( ! that . flagTouchedY ) {
547+ that . flagTouchedY = that . y ;
548+ }
549+ }
550+ }
551+
552+ // counting the number of fireballs currently in play
553+ if ( entity instanceof Fireball ) {
554+ that . fireballsThrown ++ ;
555+ }
556+
557+ that . updateBB ( ) ;
558+ } ) ;
559+ }
560+
561+ handleVerticalCollisions ( ) {
562+ const that = this ;
563+ this . game . entities . forEach ( function ( entity ) {
564+ if ( entity . BB && that . BB . collide ( entity . BB ) ) {
565+ if ( that . velocity . y > 0 ) { // falling
566+ if ( ( entity instanceof Ground || entity instanceof Brick || entity instanceof Block || entity instanceof Tube || entity instanceof SideTube ) // landing
567+ && ( that . lastBB . bottom ) <= entity . BB . top ) { // was above last tick
568+ if ( that . size === 0 || that . size === 3 ) { // small
569+ that . y = entity . BB . top - PARAMS . BLOCKWIDTH ;
570+ } else { // big
571+ that . y = entity . BB . top - 2 * PARAMS . BLOCKWIDTH ;
572+ }
573+ that . velocity . y = 0 ;
574+
575+ if ( that . state === 4 ) that . state = 0 ; // set state to idle
576+ that . updateBB ( ) ;
577+
578+ if ( entity instanceof Tube && entity . destination && that . game . down ) {
579+ that . game . camera . loadLevel ( bonusLevelOne , 2.5 * PARAMS . BLOCKWIDTH , 0 * PARAMS . BLOCKWIDTH , false , false ) ;
580+ }
581+ }
582+ else if ( entity instanceof Lift && that . lastBB . bottom <= entity . BB . top + PARAMS . SCALE * 3 ) {
583+ if ( that . size === 0 || that . size === 3 ) { // small
584+ that . y = entity . BB . top - PARAMS . BLOCKWIDTH ;
585+ } else { // big
586+ that . y = entity . BB . top - 2 * PARAMS . BLOCKWIDTH ;
587+ }
588+ that . velocity . y = 0 ;
589+
590+ if ( that . state === 4 ) that . state = 0 ; // set state to idle
591+ }
592+ else if ( ( entity instanceof Goomba || entity instanceof Koopa || entity instanceof KoopaParatroopaGreen || entity instanceof KoopaParatroopaRed || entity instanceof KoopaShell ) // squish Goomba
593+ && ( that . lastBB . bottom ) <= entity . BB . top // was above last tick
594+ && ! entity . dead ) { // can't squish an already squished Goomba
595+ entity . dead = true ;
596+ that . velocity . y = - 240 ; // bounce
597+ ASSET_MANAGER . playAsset ( "./audio/stomp.mp3" ) ;
598+ }
599+ }
600+ else if ( that . velocity . y < 0 ) { // jumping
601+ if ( ( entity instanceof Brick ) // hit ceiling
602+ && ( that . lastBB . top ) >= entity . BB . bottom ) { // was below last tick
603+
604+ // Check for center collision with brick
605+ if ( that . BB . collide ( entity . leftBB ) && that . BB . collide ( entity . rightBB ) ) {
606+ entity . bounce = true ;
607+ that . velocity . y = 0 ;
608+
609+ if ( entity . type == 1 && that . size != 0 && that . size != 3 ) { // if it's a regular brick, and mario is big
610+ entity . explode ( ) ;
611+ }
612+ }
613+ // Check for side collisions
614+ else if ( that . BB . collide ( entity . leftBB ) ) {
615+ that . x = entity . BB . left - PARAMS . BLOCKWIDTH ;
616+ if ( that . velocity . x > 0 ) that . velocity . x = 0 ;
617+ }
618+ else if ( that . BB . collide ( entity . rightBB ) ) {
619+ that . x = entity . BB . right ;
620+ if ( that . velocity . x < 0 ) that . velocity . x = 0 ;
621+ }
622+
623+ }
624+ else if ( entity instanceof Lift && that . lastBB . bottom <= entity . BB . top + PARAMS . SCALE * 3 ) {
625+ if ( that . size === 0 || that . size === 3 ) { // small
626+ that . y = entity . BB . top - PARAMS . BLOCKWIDTH ;
627+ } else { // big
628+ that . y = entity . BB . top - 2 * PARAMS . BLOCKWIDTH ;
629+ }
630+ that . velocity . y = 0 ;
631+ that . updateBB ( ) ;
632+ }
633+ }
634+ }
635+ that . updateBB ( ) ;
636+ } ) ;
637+ }
625638
626639 drawMinimap ( ctx , mmX , mmY ) {
627640 ctx . fillStyle = "Red" ;
0 commit comments