Advertisement
ThomasDM

Super Mario

Nov 4th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.83 KB | None | 0 0
  1. //-----------------------------------------------------
  2. // Name, first name:De Mulder Thomas
  3. // Group: 1DAE2
  4. //-----------------------------------------------------
  5.  
  6. //---------------------------
  7. // Includes
  8. //---------------------------
  9. #include "Mario.h"
  10.  
  11. //---------------------------
  12. // Defines
  13. //---------------------------
  14. #define GAME_ENGINE (GameEngine::GetSingleton())
  15. #include <iostream>
  16. #include <fstream>
  17. #include <string>
  18. using namespace std;
  19.  
  20. //---------------------------
  21. // Constructor & Destructor
  22. //---------------------------
  23. Mario::Mario(HitRegion *hitLevel, DOUBLE2 gravity,MATRIX3X2 scale,MATRIX3X2 position):
  24.     m_HitLevelPtr(hitLevel),m_Gravity(gravity),
  25.     m_MatBigger(scale),m_MatSetOrigin(position),
  26.     m_TickCount(0),m_MarioPos(0,0),
  27.     m_MarioScale(1,1),m_MarioVelocity(0,0),
  28.     m_CameraPos(0,0),m_BmpMarioPtr(nullptr),
  29.     m_DrawHitregion(false),m_HitLevelPlatformPtr(nullptr),
  30.     m_BmpBarPtr(nullptr),m_MarioAcceleration(0),
  31.     m_MaxSpeed(100),m_Drag(100),
  32.     m_IsKPressed(false),m_AudioJumpPtr(nullptr),
  33.     m_AudioDiePtr(nullptr),m_AudioSmallerPtr(nullptr),
  34.     m_BmpBigBarPtr(nullptr),m_BigBar(4824,376),
  35.     m_BigBarSpeed(-80),m_BigStance(false),
  36.     m_AudioMidGatePtr(nullptr),m_AudioEndPtr(nullptr),
  37.     m_AudioBackgroundPtr(nullptr)
  38. {
  39.     m_Movement=STANDING;
  40.     m_Stance=SMALL;
  41.     m_BmpMarioPtr= new Bitmap("resources/MarioSprite.png");
  42.     m_MarioRect.left=0;
  43.     m_MarioRect.right=m_MarioRect.left+20;
  44.     m_MarioRect.top=0;
  45.     m_MarioRect.bottom=m_MarioRect.top+31;
  46.     m_HitLevelPlatformPtr = new HitRegion();
  47.     m_HitLevelPlatformPtr->CreateFromSVG("resources/WorldPlatforms.svg");
  48.     m_BmpBarPtr= new Bitmap("Resources/Bar.png");
  49.     for (int i = 0; i < BLOCKS; i++)
  50.     {
  51.         m_HitBlockArr[i]=new HitRegion();
  52.         m_HitBlockArr[i]->CreateFromRect(0,0,16,16);
  53.     }
  54.     for (int i = 0; i < TURN; i++)
  55.     {
  56.         m_TurnBlockHitArr[i]=false;
  57.     }
  58.     m_AudioJumpPtr = new Audio("resources/smw_jump.wav");
  59.     m_AudioDiePtr = new Audio("resources/smw_death.wav");
  60.     m_AudioSmallerPtr = new Audio("resources/smw_powerdown.wav");
  61.     m_BmpBigBarPtr= new Bitmap("resources/BigBar.png");
  62.     m_AudioMidGatePtr= new Audio("resources/smw_midway_gate.wav");
  63.     m_AudioEndPtr= new Audio("resources/smw_course_clear.wav");
  64.     m_AudioBackgroundPtr= new Audio("resources/SMW_BackgroundMusic.mp3");
  65.  
  66. }
  67.  
  68. Mario::~Mario()
  69. {
  70.     delete m_BmpMarioPtr;
  71.     delete m_HitLevelPlatformPtr;
  72.     delete m_BmpBarPtr;
  73.     for (int i = 0; i < BLOCKS; i++)
  74.     {
  75.         delete m_HitBlockArr[i];
  76.     }
  77.     delete m_AudioJumpPtr;
  78.     delete m_AudioDiePtr;
  79.     delete m_AudioSmallerPtr;
  80.     delete m_BmpBigBarPtr;
  81.     delete m_AudioMidGatePtr;
  82.     delete m_AudioEndPtr;
  83.     delete m_AudioBackgroundPtr;
  84. }
  85.  
  86. //---------------------------
  87. // Methods - Member functions
  88. //---------------------------
  89. void Mario::Keypressed(TCHAR ckey)
  90.  
  91. {
  92.     switch (ckey)
  93.     {
  94.     case 'H':
  95.         m_DrawHitregion=!m_DrawHitregion;
  96.         break;
  97.     case 'G':
  98.         m_MarioPos.x+=1000;
  99.         m_MarioPos.y-=100;
  100.         break;
  101.     case 'F':
  102.         m_MarioPos.x-=1000;
  103.         m_MarioPos.y-=100;
  104.         break;
  105.     case 'B':
  106.         GetBigger();
  107.         break;
  108.     case 'N':
  109.         GetSmaller();
  110.         break;
  111.     case 'K':
  112.         m_IsKPressed=true;
  113.         break;
  114.     }
  115.  
  116. }
  117. void Mario::Paint()
  118. {
  119.  
  120.     // Choosing the pictures
  121.     switch (m_Stance)
  122.     {
  123.     case BIG:
  124.         switch (m_Movement)
  125.         {
  126.         case Mario::STANDING:
  127.             m_MarioRect.left=0;
  128.             m_MarioRect.right=m_MarioRect.left+19;
  129.             m_MarioRect.top=0;
  130.             m_MarioRect.bottom=m_MarioRect.top+30;
  131.             break;
  132.         case Mario::WALKING:
  133.             m_MarioRect.left=20*(m_TickCount/5);
  134.             m_MarioRect.right=m_MarioRect.left+19;
  135.             m_MarioRect.top=0;
  136.             m_MarioRect.bottom=m_MarioRect.top+30;
  137.             break;
  138.         case Mario::JUMPING:
  139.             if(m_MarioVelocity.y>0)
  140.             {
  141.                 m_MarioRect.left=140;
  142.                 m_MarioRect.right=m_MarioRect.left+19;
  143.                 m_MarioRect.top=0;
  144.                 m_MarioRect.bottom=m_MarioRect.top+30;
  145.             }
  146.             if(m_MarioVelocity.y<0)
  147.             {
  148.                 m_MarioRect.left=120;
  149.                 m_MarioRect.right=m_MarioRect.left+19;
  150.                 m_MarioRect.top=0;
  151.                 m_MarioRect.bottom=m_MarioRect.top+30;
  152.             }
  153.             break;
  154.         case Mario::CROUCHING:
  155.             m_MarioRect.left=100;
  156.             m_MarioRect.right=m_MarioRect.left+19;
  157.             m_MarioRect.top=0;
  158.             m_MarioRect.bottom=m_MarioRect.top+30;
  159.             break;
  160.         case Mario::LOOKINGUP:
  161.             m_MarioRect.left=80;
  162.             m_MarioRect.right=m_MarioRect.left+19;
  163.             m_MarioRect.top=0;
  164.             m_MarioRect.bottom=m_MarioRect.top+30;
  165.             break;
  166.         case Mario::RUNNING:
  167.             m_MarioRect.left=20*(m_TickCount/5);
  168.             m_MarioRect.right=m_MarioRect.left+19;
  169.             m_MarioRect.top=0;
  170.             m_MarioRect.bottom=m_MarioRect.top+30;
  171.             break;
  172.         case Mario::CHANGE:
  173.             m_MarioRect.left=60;
  174.             m_MarioRect.right=m_MarioRect.left+19;
  175.             m_MarioRect.top=0;
  176.             m_MarioRect.bottom=m_MarioRect.top+30;
  177.             break;
  178.         }
  179.         break;
  180.  
  181.     case SMALL:
  182.         switch (m_Movement)
  183.         {
  184.         case Mario::STANDING:
  185.             m_MarioRect.left=0;
  186.             m_MarioRect.right=m_MarioRect.left+19;
  187.             m_MarioRect.top=94;
  188.             m_MarioRect.bottom=m_MarioRect.top+30;
  189.             break;
  190.         case Mario::WALKING:
  191.             m_MarioRect.left=20*(m_TickCount/5);
  192.             m_MarioRect.right=m_MarioRect.left+19;
  193.             m_MarioRect.top=94;
  194.             m_MarioRect.bottom=m_MarioRect.top+30;
  195.             break;
  196.         case Mario::JUMPING:
  197.             if(m_MarioVelocity.y>0)
  198.             {
  199.                 m_MarioRect.left=140;
  200.                 m_MarioRect.right=m_MarioRect.left+19;
  201.                 m_MarioRect.top=94;
  202.                 m_MarioRect.bottom=m_MarioRect.top+30;
  203.             }
  204.             if(m_MarioVelocity.y<0)
  205.             {
  206.                 m_MarioRect.left=120;
  207.                 m_MarioRect.right=m_MarioRect.left+19;
  208.                 m_MarioRect.top=94;
  209.                 m_MarioRect.bottom=m_MarioRect.top+30;
  210.             }
  211.             break;
  212.         case Mario::CROUCHING:
  213.             m_MarioRect.left=100;
  214.             m_MarioRect.right=m_MarioRect.left+19;
  215.             m_MarioRect.top=94;
  216.             m_MarioRect.bottom=m_MarioRect.top+30;
  217.             break;
  218.         case Mario::LOOKINGUP:
  219.             m_MarioRect.left=80;
  220.             m_MarioRect.right=m_MarioRect.left+19;
  221.             m_MarioRect.top=94;
  222.             m_MarioRect.bottom=m_MarioRect.top+30;
  223.             break;
  224.         case Mario::RUNNING:
  225.             m_MarioRect.left=20*(m_TickCount/3);
  226.             m_MarioRect.right=m_MarioRect.left+19;
  227.             m_MarioRect.top=94;
  228.             m_MarioRect.bottom=m_MarioRect.top+30;
  229.             break;
  230.         case Mario::CHANGE:
  231.             m_MarioRect.left=60;
  232.             m_MarioRect.right=m_MarioRect.left+19;
  233.             m_MarioRect.top=94;
  234.             m_MarioRect.bottom=m_MarioRect.top+30;
  235.             break;
  236.         }
  237.         break;
  238.     case DEAD:
  239.         {
  240.             m_MarioRect.left=180;
  241.             m_MarioRect.right=m_MarioRect.left+19;
  242.             m_MarioRect.top=94;
  243.             m_MarioRect.bottom=m_MarioRect.top+30;
  244.         }
  245.         break;
  246.     case END:
  247.         {
  248.             if(m_BigStance)
  249.             {
  250.                 if(m_MarioPos.x<4930)
  251.                 {
  252.                     m_MarioRect.left=20*(m_TickCount/5);
  253.                     m_MarioRect.right=m_MarioRect.left+19;
  254.                     m_MarioRect.top=0;
  255.                     m_MarioRect.bottom=m_MarioRect.top+30;
  256.                 }
  257.                 else
  258.                 {
  259.                     m_MarioRect.left=160;
  260.                     m_MarioRect.right=m_MarioRect.left+19;
  261.                     m_MarioRect.top=0;
  262.                     m_MarioRect.bottom=m_MarioRect.top+30;
  263.                 }
  264.             }
  265.             else
  266.             {
  267.                 if(m_MarioPos.x<4930)
  268.                 {
  269.                     m_MarioRect.left=20*(m_TickCount/5);
  270.                     m_MarioRect.right=m_MarioRect.left+19;
  271.                     m_MarioRect.top=94;
  272.                     m_MarioRect.bottom=m_MarioRect.top+30;
  273.                 }
  274.                 else
  275.                 {
  276.                     m_MarioRect.left=160;
  277.                     m_MarioRect.right=m_MarioRect.left+19;
  278.                     m_MarioRect.top=94;
  279.                     m_MarioRect.bottom=m_MarioRect.top+30;
  280.                 }
  281.             }
  282.         }
  283.         break;
  284.     }
  285.     // Locals
  286.     MATRIX3X2 matTransform, matTranslate,matScale,matCenter;
  287.     MATRIX3X2 matView,matCamera;
  288.     matCamera.SetAsTranslate(m_CameraPos);
  289.     matView=matCamera.Inverse();
  290.  
  291.     //Bar
  292.     if(m_BmpBarPtr!=nullptr)
  293.     {
  294.         matTranslate.SetAsTranslate(2573,370);
  295.         matTransform=matTranslate*matView*m_MatSetOrigin*m_MatBigger;
  296.         GAME_ENGINE->SetTransformMatrix(matTransform);
  297.         GAME_ENGINE->DrawBitmap(m_BmpBarPtr);
  298.     }
  299.     //BigBar
  300.     if(m_BmpBigBarPtr!=nullptr)
  301.     {
  302.         matTranslate.SetAsTranslate(m_BigBar);
  303.         matTransform=matTranslate*matView*m_MatSetOrigin*m_MatBigger;
  304.         GAME_ENGINE->SetTransformMatrix(matTransform);
  305.         GAME_ENGINE->DrawBitmap(m_BmpBigBarPtr);
  306.     }
  307.  
  308.     // Making him move
  309.     matTranslate.SetAsTranslate(m_MarioPos);
  310.     matScale.SetAsScale(m_MarioScale.x,m_MarioScale.y);
  311.     matCenter.SetAsTranslate(-8,0);
  312.     matTransform=matCenter*matScale*matCenter.Inverse()*matTranslate*matView*m_MatSetOrigin*m_MatBigger;
  313.     GAME_ENGINE->SetTransformMatrix(matTransform);
  314.     GAME_ENGINE->DrawBitmap(m_BmpMarioPtr,0,0,m_MarioRect);
  315.  
  316.     //HitRegions
  317.     if(m_DrawHitregion)
  318.     {
  319.         matView=matCamera.Inverse()*m_MatSetOrigin*m_MatBigger;
  320.         GAME_ENGINE->SetTransformMatrix(matView);
  321.         GAME_ENGINE->SetColor(COLOR(255,0,0,125));
  322.         GAME_ENGINE->FillHitRegion(m_HitLevelPlatformPtr);
  323.     }
  324.  
  325.     //Console Check
  326.     /*GAME_ENGINE->ConsoleClear();
  327.     cout <<"Vertical velocity : "<<m_MarioVelocity.y << endl;
  328.     cout <<"Horizontal velocity : "<<m_MarioVelocity.x << endl;
  329.     cout <<endl;
  330.     cout <<"Vertical position : "<<m_MarioPos.y<< endl;*/
  331.     //cout <<"Horizontal position : "<<m_MarioPos.x<< endl;
  332. }
  333. void Mario::Tick(double deltaTime)
  334. {
  335.     //Audio
  336.     m_AudioJumpPtr->Tick();
  337.     m_AudioDiePtr->Tick();
  338.     m_AudioSmallerPtr->Tick();
  339.     m_AudioMidGatePtr->Tick();
  340.     m_AudioEndPtr->Tick();
  341.     m_AudioBackgroundPtr->Tick();
  342.  
  343.     if(m_Stance!=DEAD&&m_Stance!=END)
  344.     {
  345.        
  346.         m_AudioBackgroundPtr->Play();
  347.         //TickCount
  348.         if (GAME_ENGINE->IsKeyDown('S'))
  349.         {
  350.             m_TickCount=0;
  351.             m_Movement=CROUCHING;
  352.         }
  353.         else if(m_MarioVelocity.y!=0)
  354.         {
  355.             m_TickCount=0;
  356.             m_Movement=JUMPING;
  357.         }
  358.         else if(m_MarioVelocity.x>5&&GAME_ENGINE->IsKeyDown('Q'))
  359.         {
  360.             m_Movement=CHANGE;
  361.         }
  362.         else if(m_MarioVelocity.x<-5&&GAME_ENGINE->IsKeyDown('D'))
  363.         {
  364.             m_Movement=CHANGE;
  365.         }
  366.         else if(m_MarioVelocity.x>120||m_MarioVelocity.x<-120)
  367.         {
  368.             m_TickCount=(m_TickCount+1)%9;
  369.             m_Movement=RUNNING;
  370.         }
  371.         else if (GAME_ENGINE->IsKeyDown('D')||m_MarioVelocity.x>5)
  372.         {
  373.             m_TickCount=(m_TickCount+1)%10;
  374.             m_Movement=WALKING;
  375.         }
  376.         else if (GAME_ENGINE->IsKeyDown('Q')||m_MarioVelocity.x<-5)
  377.         {
  378.             m_TickCount=(m_TickCount+1)%10;
  379.             m_Movement=WALKING;
  380.         }
  381.         else if (GAME_ENGINE->IsKeyDown('Z'))
  382.         {
  383.             m_TickCount=0;
  384.             m_Movement=LOOKINGUP;
  385.         }
  386.         else
  387.         {
  388.             m_Movement=STANDING;
  389.             m_TickCount=0;
  390.         }
  391.  
  392.         //Raycast working
  393.         MoveMario(deltaTime);
  394.  
  395.         //Bar
  396.         Bar();
  397.  
  398.         //BigBar
  399.         BigBar();
  400.         m_BigBar.y+=m_BigBarSpeed*deltaTime;
  401.  
  402.         if(m_BigBar.y>376&&m_BigBarSpeed>0)
  403.             m_BigBarSpeed*=-1;
  404.  
  405.         if(m_BigBar.y<250&&m_BigBarSpeed<0)
  406.             m_BigBarSpeed*=-1;
  407.  
  408.         //End
  409.         if(m_MarioPos.x>4830)
  410.         {
  411.             if(m_Stance==BIG)
  412.                 m_BigStance=true;
  413.             m_Stance=END;
  414.             m_AudioBackgroundPtr->Stop();
  415.         }
  416.     }
  417.     else if(m_Stance==END)
  418.     {
  419.         if(m_MarioPos.x<4930)
  420.         {
  421.             m_TickCount=(m_TickCount+1)%10;
  422.             m_MarioPos.x+=30*deltaTime;
  423.         }
  424.  
  425.         if(m_MarioPos.y<354)
  426.         {
  427.             m_MarioVelocity+=m_Gravity*deltaTime;
  428.             m_MarioPos.y+=m_MarioVelocity.y*deltaTime;
  429.         }
  430.         if(m_MarioPos.y>354)
  431.             m_MarioPos.y=354;
  432.     }
  433.     else
  434.     {
  435.         m_AudioBackgroundPtr->Stop();
  436.         m_MarioVelocity.x=0;
  437.         m_MarioVelocity+=m_Gravity/4*deltaTime;
  438.         m_MarioPos+=m_MarioVelocity/4*deltaTime;
  439.  
  440.     }
  441.  
  442. }
  443. void Mario::MoveMario(double deltaTime)
  444. {
  445.     //Horizontal Movement
  446.     if(m_Movement!=CROUCHING||m_MarioVelocity.y!=0)
  447.     {
  448.         if(GAME_ENGINE->IsKeyDown('L'))
  449.         {
  450.             m_MaxSpeed=150;
  451.         }
  452.         else m_MaxSpeed=100;
  453.         if(GAME_ENGINE->IsKeyDown('Q'))
  454.         {
  455.             m_MarioAcceleration=-300;
  456.             m_MarioScale.x=-1;
  457.         }
  458.         else if (GAME_ENGINE->IsKeyDown('D'))
  459.         {
  460.             m_MarioAcceleration=300;
  461.             m_MarioScale.x=1;
  462.         }
  463.         else
  464.         {
  465.             m_MarioAcceleration=0;
  466.             if(m_MarioVelocity.x>-5&&m_MarioVelocity.x<5)
  467.             {
  468.                 m_MarioVelocity.x=0;
  469.             }
  470.         }
  471.     }
  472.     else m_MarioAcceleration=0;
  473.  
  474.     if(m_MarioVelocity.x<m_MaxSpeed&&m_MarioVelocity.x>-m_MaxSpeed)
  475.         m_MarioVelocity.x+=m_MarioAcceleration*deltaTime;
  476.     if(m_MarioVelocity.x>0)
  477.         m_MarioVelocity.x+=-m_Drag*deltaTime;
  478.     if(m_MarioVelocity.x<0)
  479.         m_MarioVelocity.x+=m_Drag*deltaTime;
  480.  
  481.     //Gravity
  482.     m_MarioVelocity+=m_Gravity*deltaTime;
  483.     m_MarioPos+=m_MarioVelocity*deltaTime;
  484.  
  485.     if(m_CameraPos.x>m_MarioPos.x-50&&m_CameraPos.x>50)
  486.         m_CameraPos.x=m_MarioPos.x-50;
  487.     if(m_CameraPos.x<m_MarioPos.x-150)
  488.         m_CameraPos.x=m_MarioPos.x-150;
  489.  
  490.     ////Prevent from leaving
  491.     //if(m_MarioPos.y > 389 && m_MarioVelocity.y>0)
  492.     //{
  493.     //  m_MarioPos.y=389;
  494.     //  m_MarioVelocity.y=0;
  495.     //}
  496.  
  497.     //Vertical collision
  498.  
  499.     VerticalCollision(m_HitLevelPtr);
  500.     VerticalNTopCollision(m_HitLevelPlatformPtr);
  501.     for (int i = 0; i < 19; i++)
  502.     {
  503.         if(i>8)
  504.         {
  505.             if(!(m_TurnBlockHitArr[i-9])) VerticalCollision(m_HitBlockArr[i]);
  506.         }
  507.         else VerticalCollision(m_HitBlockArr[i]);
  508.  
  509.  
  510.     }
  511.  
  512.     //Hoirzontal collision
  513.     HorizontalCollision(m_HitLevelPtr);
  514.     for (int i = 0; i < 19; i++)
  515.     {
  516.  
  517.         if(i>8)
  518.         {
  519.             if(!(m_TurnBlockHitArr[i-9])) HorizontalCollision(m_HitBlockArr[i]);
  520.         }
  521.         else HorizontalCollision(m_HitBlockArr[i]);
  522.  
  523.     }
  524.  
  525. }
  526. DOUBLE2 Mario::GetMarioPos()
  527. {
  528.     return m_MarioPos;
  529. }
  530. void Mario::VerticalCollision(HitRegion *level)
  531. {
  532.     /*RECT2 verticalRect=mover->CollisionTest(level);
  533.     if(verticalRect.bottom-verticalRect.top>0)
  534.     {
  535.     if(abs(mover->GetBounds().bottom -verticalRect.bottom)<1)
  536.     {
  537.     m_MarioPos.y-=verticalRect.bottom-verticalRect.top;
  538.     m_MarioVelocity.y=0;
  539.     if(m_IsKPressed)
  540.     {
  541.     m_MarioVelocity.y=-400;
  542.     m_IsKPressed=false;
  543.     }
  544.     }
  545.  
  546.     if(abs(mover->GetBounds().top -verticalRect.top)<1)
  547.     {
  548.     m_MarioPos.y+=verticalRect.bottom-verticalRect.top;
  549.     m_MarioVelocity.y=0;
  550.     }
  551.     }*/
  552.     HIT hitArr[1];
  553.     DOUBLE2 startPoint = m_MarioPos;
  554.     DOUBLE2 vector = DOUBLE2(0,13);
  555.  
  556.     startPoint.x = m_MarioPos.x+4;
  557.     startPoint.y = m_MarioPos.y+17;
  558.     int numbHits = 0;
  559.     numbHits = level->Raycast(startPoint,vector,hitArr,1);
  560.     if(numbHits > 0)
  561.     {
  562.         m_MarioVelocity.y = 0;
  563.         double depth = vector.Length() * (1-hitArr[0].lambda);
  564.         m_MarioPos.y -= depth;
  565.         if(m_IsKPressed)
  566.         {
  567.             m_MarioVelocity.y=-400;
  568.             m_IsKPressed=false;
  569.             m_AudioJumpPtr->Play();
  570.         }
  571.  
  572.     }
  573.     else{
  574.         startPoint.x = m_MarioPos.x+12;
  575.         numbHits = 0;
  576.         numbHits = level->Raycast(startPoint,vector,hitArr,1);
  577.         if(numbHits > 0)
  578.         {
  579.             m_MarioVelocity.y = 0;
  580.             double depth = vector.Length() * (1-hitArr[0].lambda);
  581.             m_MarioPos.y -= depth;
  582.             if(m_IsKPressed)
  583.             {
  584.                 m_MarioVelocity.y=-400;
  585.                 m_IsKPressed=false;
  586.                 m_AudioJumpPtr->Play();
  587.             }
  588.  
  589.         }
  590.         else
  591.         {
  592.             vector = DOUBLE2(0,-14);
  593.  
  594.             startPoint.x = m_MarioPos.x+4;
  595.             startPoint.y = m_MarioPos.y+17;
  596.             numbHits = 0;
  597.             numbHits = level->Raycast(startPoint,vector,hitArr,1);
  598.             if(numbHits > 0)
  599.             {
  600.                 m_MarioVelocity.y = 0;
  601.                 double depth = vector.Length() * (1-hitArr[0].lambda);
  602.                 m_MarioPos.y += depth;
  603.  
  604.             }
  605.             else
  606.             {
  607.                 startPoint.x = m_MarioPos.x+12;
  608.                 numbHits = 0;
  609.                 numbHits = level->Raycast(startPoint,vector,hitArr,1);
  610.                 if(numbHits > 0)
  611.                 {
  612.                     m_MarioVelocity.y = 0;
  613.                     double depth = vector.Length() * (1-hitArr[0].lambda);
  614.                     m_MarioPos.y += depth;
  615.  
  616.                 }
  617.             }
  618.         }
  619.     }
  620. }
  621. void Mario::VerticalNTopCollision(HitRegion *level)
  622. {
  623.     /*RECT2 verticalPlatformRect=mover->CollisionTest(level);
  624.     if(verticalPlatformRect.bottom-verticalPlatformRect.top>0&&m_MarioVelocity.y>0)
  625.     {
  626.     if(abs(mover->GetBounds().bottom -verticalPlatformRect.bottom)<10)
  627.     {
  628.     m_MarioPos.y-=verticalPlatformRect.bottom-verticalPlatformRect.top;
  629.     m_MarioVelocity.y=0;
  630.     if(m_IsKPressed)
  631.     {
  632.     m_MarioVelocity.y=-400;
  633.     m_IsKPressed=false;
  634.     }
  635.     }
  636.     }*/
  637.  
  638.     HIT hitArr[1];
  639.     DOUBLE2 startPoint = m_MarioPos;
  640.     DOUBLE2 vector = DOUBLE2(0,25);
  641.  
  642.     startPoint.x = m_MarioPos.x+4;
  643.     startPoint.y = m_MarioPos.y+4;
  644.     int numbHits = 0;
  645.     numbHits = level->Raycast(startPoint,vector,hitArr,1);
  646.     if(numbHits > 0&&m_MarioVelocity.y>=0)
  647.     {
  648.         m_MarioVelocity.y = 0;
  649.         double depth = vector.Length() * (1-hitArr[0].lambda);
  650.         m_MarioPos.y -= depth;
  651.         if(m_IsKPressed)
  652.         {
  653.             m_MarioVelocity.y=-400;
  654.             m_IsKPressed=false;
  655.             m_AudioJumpPtr->Play();
  656.         }
  657.  
  658.     }
  659.     else
  660.     {
  661.         startPoint.x = m_MarioPos.x+12;
  662.         numbHits = 0;
  663.         numbHits = level->Raycast(startPoint,vector,hitArr,1);
  664.         if(numbHits > 0&&m_MarioVelocity.y>=0)
  665.         {
  666.             m_MarioVelocity.y = 0;
  667.             double depth = vector.Length() * (1-hitArr[0].lambda);
  668.             m_MarioPos.y -= depth;
  669.             if(m_IsKPressed)
  670.             {
  671.                 m_MarioVelocity.y=-400;
  672.                 m_IsKPressed=false;
  673.                 m_AudioJumpPtr->Play();
  674.             }
  675.  
  676.         }
  677.     }
  678.  
  679. }
  680. void Mario::HorizontalCollision(HitRegion *level)
  681. {
  682.     /*RECT2 horizontalRect=mover->CollisionTest(level);
  683.     if(horizontalRect.right-horizontalRect.left>0)
  684.     {
  685.     if(abs(mover->GetBounds().right -horizontalRect.right)<1)
  686.     {
  687.     m_MarioPos.x-=horizontalRect.right-horizontalRect.left;
  688.     m_MarioVelocity.x=0;
  689.     }
  690.     if(abs(mover->GetBounds().left -horizontalRect.left)<1)
  691.     {
  692.     m_MarioPos.x+=horizontalRect.right-horizontalRect.left;
  693.     m_MarioVelocity.x=0;
  694.     }
  695.     }*/
  696.     HIT hitArr[1];
  697.     DOUBLE2 startPoint = m_MarioPos;
  698.     DOUBLE2 vector = DOUBLE2(6,0);
  699.  
  700.  
  701.     startPoint.y = m_MarioPos.y+7;
  702.     startPoint.x = m_MarioPos.x+10;
  703.     int numbHits = 0;
  704.     numbHits = level->Raycast(startPoint,vector,hitArr,1);
  705.     if(numbHits > 0)
  706.     {
  707.  
  708.         double depth = vector.Length() * (1-hitArr[0].lambda);
  709.         m_MarioPos.x -= depth;
  710.         m_MarioVelocity.x=0;
  711.  
  712.     }
  713.     else
  714.     {
  715.         startPoint.y = m_MarioPos.y+17;
  716.         numbHits = 0;
  717.         numbHits = level->Raycast(startPoint,vector,hitArr,1);
  718.         if(numbHits > 0)
  719.         {
  720.  
  721.             double depth = vector.Length() * (1-hitArr[0].lambda);
  722.             m_MarioPos.x -= depth;
  723.             m_MarioVelocity.x=0;
  724.  
  725.         }
  726.         else{
  727.             startPoint.y = m_MarioPos.y+26;
  728.             numbHits = 0;
  729.             numbHits = level->Raycast(startPoint,vector,hitArr,1);
  730.             if(numbHits > 0)
  731.             {
  732.  
  733.                 double depth = vector.Length() * (1-hitArr[0].lambda);
  734.                 m_MarioPos.x -= depth;
  735.                 m_MarioVelocity.x = 0;
  736.             }
  737.             else{
  738.                 vector = DOUBLE2(-10,0);
  739.                 startPoint.y = m_MarioPos.y+7;
  740.                 numbHits = 0;
  741.                 numbHits = level->Raycast(startPoint,vector,hitArr,1);
  742.                 if(numbHits > 0)
  743.                 {
  744.  
  745.                     double depth = vector.Length() * (1-hitArr[0].lambda);
  746.                     m_MarioPos.x += depth;
  747.                     m_MarioVelocity.x=0;
  748.  
  749.                 }
  750.                 else{
  751.                     startPoint.y = m_MarioPos.y+17;
  752.                     numbHits = 0;
  753.                     numbHits = level->Raycast(startPoint,vector,hitArr,1);
  754.                     if(numbHits > 0)
  755.                     {
  756.  
  757.                         double depth = vector.Length() * (1-hitArr[0].lambda);
  758.                         m_MarioPos.x += depth;
  759.                         m_MarioVelocity.x=0;
  760.  
  761.                     }
  762.                     else{
  763.                         startPoint.y = m_MarioPos.y+26;
  764.                         numbHits = 0;
  765.                         numbHits = level->Raycast(startPoint,vector,hitArr,1);
  766.                         if(numbHits > 0)
  767.                         {
  768.                             m_MarioVelocity.x = 0;
  769.                             double depth = vector.Length() * (1-hitArr[0].lambda);
  770.                             m_MarioPos.x += depth;
  771.  
  772.                         }
  773.                     }
  774.                 }
  775.             }
  776.         }
  777.     }
  778. }
  779. void Mario::GetBigger()
  780. {
  781.     m_Stance=BIG;
  782. }
  783. void Mario::GetSmaller()
  784. {
  785.     switch (m_Stance)
  786.     {
  787.     case Mario::SMALL:
  788.         m_Stance=Mario::DEAD;
  789.         m_MarioVelocity.y=-200;
  790.         m_AudioDiePtr->Play();
  791.         break;
  792.     case Mario::BIG:
  793.         m_Stance=Mario::SMALL;
  794.         m_AudioSmallerPtr->Play();
  795.         break;
  796.     }
  797. }
  798. void Mario::Bar()
  799. {
  800.     if(m_MarioPos.x>2570&&m_MarioPos.x<2575&&m_MarioPos.y>350)
  801.     {
  802.         m_AudioMidGatePtr->Play();
  803.         delete m_BmpBarPtr;
  804.         m_BmpBarPtr=nullptr;
  805.         GetBigger();
  806.     }
  807. }
  808. void Mario::SetHitregion(DOUBLE2 blockPosArr[])
  809. {
  810.     for (int i = 0; i < 19; i++)
  811.     {
  812.         m_HitBlockArr[i]->SetPos(blockPosArr[i]);
  813.     }
  814.  
  815. }
  816. void Mario::ReadFile(string fileName,char sep)
  817. {
  818.     ifstream file(fileName);
  819.  
  820.     if (!file) cout << "Could not open the file 'test.txt'." << endl;
  821.     else    
  822.     {
  823.         string sLine;
  824.         getline(file, sLine);
  825.         ProcessLine(sLine,sep);
  826.         file.close();
  827.     }
  828.  
  829. }
  830. void Mario::ProcessLine(string line,char sep)
  831. {
  832.     string text = line;        
  833.     string text1 = line.substr(0,line.find(sep));  
  834.     string text2 = line.substr(line.find(sep)+1);      
  835.  
  836.     int x=0,y=0;
  837.  
  838.  
  839.     stringstream bufferText1;          
  840.     bufferText1 << text1;              
  841.     bufferText1 >> x;                  
  842.  
  843.     stringstream bufferText2;          
  844.     bufferText2 << text2;              
  845.     bufferText2 >> y;                  
  846.  
  847.     m_MarioPos.x=x;
  848.     m_MarioPos.y=y;
  849. }
  850. void Mario::SetBoolTurn(bool blockArr[])
  851. {
  852.     for (int i = 0; i < TURN; i++)
  853.     {
  854.         m_TurnBlockHitArr[i]=blockArr[i];
  855.     }
  856. }
  857. bool Mario::SetDead()
  858. {
  859.     if(m_Stance==DEAD&&m_MarioPos.y>420)
  860.     {
  861.         return true;
  862.     }
  863.     else return false;
  864. }
  865. bool Mario::IsDead()
  866. {
  867.     if(m_Stance==DEAD)
  868.     {
  869.         return true;
  870.     }
  871.     else return false;
  872. }
  873. void Mario::BigBar()
  874. {
  875.     if(m_MarioPos.x>4830)
  876.     {
  877.         m_AudioEndPtr->Play();
  878.         delete m_BmpBigBarPtr;
  879.         m_BmpBigBarPtr=nullptr;
  880.     }
  881. }
  882. void Mario::SetJump()
  883. {
  884.     m_MarioVelocity.y=-300;
  885. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement