SHARE
TWEET

Untitled

PhoenixMee Apr 11th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "MyGame.h"
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. CMyGame::CMyGame(void) {}
  7.  
  8. CMyGame::~CMyGame(void) {}
  9.  
  10. // --------  game initialisation --------
  11. void CMyGame::OnInitialize()
  12. {
  13.     // Loading graphics and sound assets
  14.     cout << "Loading assets" << endl;
  15.  
  16.     font.LoadDefault();
  17.  
  18.     // enable lighting
  19.     Light.Enable();
  20.  
  21.     // house model
  22.     house.LoadModel("House.obj","House.bmp");
  23.  
  24.     // player model
  25.     player.LoadModel("monkey.md2","brute.bmp");
  26.     player.SetScale( 1.0f);
  27.     player.Rotate(90);
  28.  
  29.     // van model
  30.     van.LoadModel("van.obj", "van.bmp");
  31.  
  32.     // enemy1 model
  33.     enemy1.LoadModel("Abarlith.md2", "Abarlith.bmp");
  34.     enemy1.SetScale(3.5f);
  35.     enemy2.LoadModel("Abarlith.md2", "Abarlith.bmp");
  36.     enemy2.SetScale(3.5f);
  37.     enemy3.LoadModel("Abarlith.md2", "Abarlith.bmp");
  38.     enemy3.SetScale(3.0f);
  39.     enemy4.LoadModel("Abarlith.md2", "Abarlith.bmp");
  40.     enemy4.SetScale(3.0f);
  41.     enemy5.LoadModel("Abarlith.md2", "Abarlith.bmp");
  42.     enemy5.SetScale(3.0f);
  43.     enemy6.LoadModel("Abarlith.md2", "Abarlith.bmp");
  44.     enemy6.SetScale(3.0f);
  45.  
  46.     // wall model
  47.     wall.LoadModel("jungle_wall.obj", "wall2.bmp");
  48.  
  49.     // canopy model
  50.     canopy.LoadModel("canopy.obj", "canopy.tga");
  51.  
  52.     // banana tree model
  53.     banana.LoadModel("banana.obj", "banana.tga");
  54.  
  55.     // grass model
  56.     grass.LoadModel("grass.obj", "grass.tga");
  57.  
  58.     // box model
  59.     box.LoadModel("box.obj", "pallette.bmp");
  60.  
  61.     // box2 model
  62.     box2.LoadModel("box2.obj", "box2.bmp");
  63.  
  64.     // barrel model
  65.     barrel.LoadModel("barrel.obj", "pallette.bmp");
  66.  
  67.     // fence model
  68.     fence.LoadModel("fence.obj", "fence.tga");
  69.  
  70.     // cage doors
  71.     cage1.LoadModel("cage.obj", "bar.tga");
  72.     cage1.SetScale(7.0f);
  73.     cage2.LoadModel("cage.obj", "bar.tga");
  74.     cage2.SetScale(7.0f);
  75.     cage3.LoadModel("cage.obj", "bar.tga");
  76.     cage3.SetScale(7.0f);
  77.     cage4.LoadModel("cage.obj", "bar.tga");
  78.     cage4.SetScale(7.0f);
  79.  
  80.     // shack model
  81.     shack.LoadModel("shack.obj", "shack.bmp");
  82.  
  83.     // watchtower model
  84.     watchtower.LoadModel("watchtower.obj", "watchtower.bmp");
  85.  
  86.     // track model
  87.     track.LoadModel("track.obj", "track.tga");
  88.  
  89.     // rain model
  90.     rain.LoadModel("rain.obj", "rain.tga");
  91.     rain.SetScale(1.0f);
  92.  
  93.     // cage switch models
  94.     cage_switch1.LoadModel("switch.obj", "switch.bmp");
  95.     cage_switch1.SetScale(7.0f);
  96.     cage_switch2.LoadModel("switch.obj", "switch.bmp");
  97.     cage_switch2.SetScale(7.0f);
  98.     cage_switch3.LoadModel("switch.obj", "switch.bmp");
  99.     cage_switch3.SetScale(7.0f);
  100.     cage_switch4.LoadModel("switch.obj", "switch.bmp");
  101.     cage_switch4.SetScale(7.0f);
  102.    
  103.     // floor texture
  104.     floor.LoadTexture("floor.bmp");
  105.     floor.SetTiling(true);
  106.  
  107.     // Load skydome geometry and texture
  108.     skydome.LoadModel("Skydome.obj", "Skydome2.bmp");
  109.     skydome.SetScale(300);
  110.     skydome.SetY(-800.0f);
  111.    
  112.     // start screen
  113.     startScreen.LoadImage("startScreen.bmp");
  114.     startScreen.SetPosition(Width/2.0f,Height/2.0f);
  115.     level=1;
  116.  
  117.     // lightning sprite
  118.     lightning.LoadImage("lightningloop.bmp", CColor::Black(), 6);
  119.     lightning.SetPosition(Width / 2.0f, Height / 2.0f);
  120. }
  121.  
  122.  
  123. void CMyGame::OnStartLevel(int level)
  124. {
  125.   score = 0;
  126.   floor.SetSize(9000,9000);
  127.   IsPlaying.SetStatus(100);
  128.  
  129.   rains.clear();
  130.   monkeys.clear();
  131.  
  132.   fstream input;
  133.   input.open("model_positions.txt");
  134.   do
  135.   {
  136.       input >> type >> x >> y >> z >> rot >> dir >> scle;
  137.       if (type == 1)
  138.       {
  139.           CModel* pHouse = house.clone();
  140.           pHouse->SetPosition((float)x, (float)y, (float)z);
  141.           pHouse->Rotate((float)rot);
  142.           pHouse->SetDirection((float)dir);
  143.           pHouse->SetScale((float)scle);
  144.           houses.push_back(pHouse);
  145.       }
  146.       else if (type == 2)
  147.       {
  148.           CModel* pWall = wall.clone();
  149.           pWall->SetPosition((float)x, (float)y, (float)z);
  150.           pWall->Rotate((float)rot);
  151.           pWall->SetDirection((float)dir);
  152.           pWall->SetScale((float)scle);
  153.           walls.push_back(pWall);
  154.       }
  155.       else if (type == 3)
  156.       {
  157.           CModel* pCanopy = canopy.clone();
  158.           pCanopy->SetPosition((float)x, (float)y, (float)z);
  159.           pCanopy->Rotate((float)rot);
  160.           pCanopy->SetDirection(float(rand() % 360));
  161.           pCanopy->SetScale((float)scle);
  162.           canopys.push_back(pCanopy);
  163.       }
  164.       else  if (type == 4)
  165.       {
  166.           CModel* pBanana = banana.clone();
  167.           pBanana->SetPosition((float)x, (float)y, (float)z);
  168.           pBanana->Rotate((float)rot);
  169.           pBanana->SetDirection((float)dir);
  170.           pBanana->SetScale((float)scle);
  171.           bananas.push_back(pBanana);
  172.       }
  173.       else if (type == 5)
  174.       {
  175.           CModel* pGrass = grass.clone();
  176.           pGrass->SetPosition((float)x, (float)y, (float)z);
  177.           pGrass->Rotate((float)rot);
  178.           pGrass->SetDirection(float(rand() % 360));
  179.           pGrass->SetScale((float)scle);
  180.           grasses.push_back(pGrass);
  181.       }
  182.       else  if (type == 6)
  183.       {
  184.           CModel* pBox = box.clone();
  185.           pBox->SetPosition((float)x, (float)y, (float)z);
  186.           pBox->Rotate((float)rot);
  187.           pBox->SetDirection((float)dir);
  188.           pBox->SetScale((float)scle);
  189.           boxes.push_back(pBox);
  190.       }
  191.       else  if (type == 7)
  192.       {
  193.           CModel* pBox2 = box2.clone();
  194.           pBox2->SetPosition((float)x, (float)y, (float)z);
  195.           pBox2->Rotate((float)rot);
  196.           pBox2->SetDirection((float)dir);
  197.           pBox2->SetScale((float)scle);
  198.           boxes2.push_back(pBox2);
  199.       }
  200.       else if (type == 8)
  201.       {
  202.           CModel* pBarrel = barrel.clone();
  203.           pBarrel->SetPosition((float)x, (float)y, (float)z);
  204.           pBarrel->Rotate((float)rot);
  205.           pBarrel->SetDirection((float)dir);
  206.           pBarrel->SetScale((float)scle);
  207.           barrels.push_back(pBarrel);
  208.       }
  209.       else if (type == 9)
  210.       {
  211.           CModel* pVan = van.clone();
  212.           pVan->SetPosition((float)x, (float)y, (float)z);
  213.           pVan->Rotate((float)rot);
  214.           pVan->SetDirection((float)dir);
  215.           pVan->SetScale((float)scle);
  216.           vans.push_back(pVan);
  217.       }
  218.       else if (type == 10)
  219.       {
  220.           CModel* pFence = fence.clone();
  221.           pFence->SetPosition((float)x, (float)y, (float)z);
  222.           pFence->Rotate((float)rot);
  223.           pFence->SetDirection((float)dir);
  224.           pFence->SetScale((float)scle);
  225.           fences.push_back(pFence);
  226.       }
  227.       else  if (type == 11)
  228.       {
  229.           CModel* pShack = shack.clone();
  230.           pShack->SetPosition((float)x, (float)y, (float)z);
  231.           pShack->Rotate((float)rot);
  232.           pShack->SetDirection((float)dir);
  233.           pShack->SetScale((float)scle);
  234.           shacks.push_back(pShack);
  235.       }
  236.       else if (type == 12)
  237.       {
  238.           CModel* pWatchtower = watchtower.clone();
  239.           pWatchtower->SetPosition((float)x, (float)y, (float)z);
  240.           pWatchtower->Rotate((float)rot);
  241.           pWatchtower->SetDirection((float)dir);
  242.           pWatchtower->SetScale((float)scle);
  243.           watchtowers.push_back(pWatchtower);
  244.       }
  245.       else if (type == 13)
  246.       {
  247.           CModel* pTrack = track.clone();
  248.           pTrack->SetPosition((float)x, (float)y, (float)z);
  249.           pTrack->Rotate((float)rot);
  250.           pTrack->SetDirection((float)dir);
  251.           pTrack->SetScale((float)scle);
  252.           tracks.push_back(pTrack);
  253.       }
  254.       else if (type == 14)
  255.       {
  256.           CModel* pMonkey = player.clone();
  257.           pMonkey->SetPosition((float)x, (float)y, (float)z);
  258.           pMonkey->Rotate((float)rot);
  259.           pMonkey->SetDirection((float)dir);
  260.           pMonkey->SetScale((float)scle);
  261.           monkeys.push_back(pMonkey);
  262.           pMonkey->PlayAnimation(1, 3, 6, true);
  263.       }
  264.   } while (!input.eof());
  265.  
  266.   input.close();
  267.  
  268.   // --- grass positions ---
  269.   for (int n = 1; n <= 200; n++)
  270.   {
  271.       CModel* pGrass = grass.clone();
  272.       pGrass->SetDirection(float(rand() % 360));
  273.       pGrass->Move(rand() % 1800);
  274.       grasses.push_back(pGrass);
  275.   }
  276.  
  277.   // --- cage positions ---
  278.     cage1.SetPosition(1300, 0, 0);
  279.     cage2.SetPosition(-600, 0, -1195);
  280.     cage3.SetPosition(350, 0, 650);
  281.     cage4.SetPosition(200, 0, 1150);
  282.  
  283.   // --- cage switch positions ---
  284.     cage_switch1.SetPosition(1200, 0, -200);
  285.     cage_switch1.Rotate(-90);
  286.     cage_switch2.SetPosition(-400, 0, -1095);
  287.     cage_switch3.SetPosition(200, 0, 400);
  288.     cage_switch3.Rotate(180);
  289.     cage_switch4.SetPosition(100, 0, 1000);
  290.     cage_switch4.Rotate(-90);
  291.  
  292.   // --- enemy positions ---
  293.     // watchtower enemies (non-active)
  294.   enemy1.SetPosition(350, 400, -1250);
  295.   enemy1.Rotate(-90);
  296.   enemy1.SetScale(2.3f);
  297.   enemy1.PlayAnimation(1, 39, 7, true);
  298.   enemy2.SetPosition(-1300, 400, -1250);
  299.   enemy2.Rotate(-90);
  300.   enemy2.SetScale(2.3f);
  301.   enemy2.PlayAnimation(1, 39, 7, true);
  302.     // active enemies
  303.   enemy3.SetPosition(900, 65, -400);
  304.   enemy4.SetPosition(-600, 65, -850);
  305.   enemy5.SetPosition(-1200, 65, 300);
  306.   enemy6.SetPosition(-375, 65, 1325);
  307.  
  308.   // --- enemy path positions ---
  309.     // path number reflects enemy number
  310.   path3.push_back(CVector(900, 0, -400));
  311.   path3.push_back(CVector(900, 0, 1000));
  312.   path4.push_back(CVector(-600, 0, -850));
  313.   path4.push_back(CVector(300, 0, -850));
  314.   path5.push_back(CVector(-1200, 0, 300));
  315.   path5.push_back(CVector(200, 0, 300));
  316.   path6.push_back(CVector(-375, 65, 1325));
  317.   path6.push_back(CVector(-375, 65, 1325));
  318.  
  319.   // --- player position ---
  320.   player.SetPosition(1200, 0, 1200);
  321.  
  322.   // --- setup healthbar ---
  323.   hbar.SetSize(100,15);
  324.   hbar.SetPosition(830,680);
  325.   hbar.SetHealth(100);
  326.  
  327.   // --- enemy dist bar ---
  328.   enemydist.SetColor(CColor::Cyan());
  329.   enemydist.SetSize(100, 15);
  330.   enemydist.SetPosition(830, 660);
  331.   enemydist.SetHealth(100);
  332.  
  333.   // --- background sounds ---
  334.   rainsound.Play("rain.wav", -1);
  335. }
  336.  
  337. void CMyGame::AddDistBar()
  338. {
  339.     if (gtd3 > 800 && gtd3 < 900 || gtd4 > 800 && gtd4 < 900 || gtd5 > 800 && gtd5 < 900 || gtd6 > 800 && gtd6 < 900) enemydist.SetHealth(83);
  340.     if (gtd3 > 700 && gtd3 < 800 || gtd4 > 700 && gtd4 < 800 || gtd5 > 700 && gtd5 < 800 || gtd6 > 700 && gtd6 < 800) enemydist.SetHealth(66);
  341.     if (gtd3 > 600 && gtd3 < 700 || gtd4 > 600 && gtd4 < 700 || gtd5 > 600 && gtd5 < 700 || gtd6 > 600 && gtd6 < 700) enemydist.SetHealth(49);
  342.     if (gtd3 > 500 && gtd3 < 600 || gtd4 > 500 && gtd4 < 600 || gtd5 > 500 && gtd5 < 600 || gtd6 > 500 && gtd6 < 600) enemydist.SetHealth(32);
  343.     if (gtd3 > 400 && gtd3 < 500 || gtd4 > 400 && gtd4 < 500 || gtd5 > 400 && gtd5 < 500 || gtd6 > 400 && gtd6 < 500) enemydist.SetHealth(15);
  344.     if (gtd3 > 300 && gtd3 < 400 || gtd4 > 300 && gtd4 < 400 || gtd5 > 300 && gtd5 < 400 || gtd6 > 300 && gtd6 < 400) enemydist.SetHealth(0);
  345. }
  346.  
  347. void CMyGame::AddRain()
  348. {
  349.     // --- simulating rain ---
  350.     if (rains.size() < 4200)
  351.     {
  352.         if (rand() % 5 == 1) // initial spawn rate
  353.         {
  354.             for (int n = 1; n <= 1200; n++)
  355.             {
  356.                 if (rand() % 20 == 1) // chance of rain to spawn at different heights
  357.                 {
  358.                     CModel* pRain = rain.clone();
  359.                     pRain->Rotate(float(rand() % 360));
  360.                     pRain->SetYVelocity(-600);
  361.                     rains.push_back(pRain);
  362.                 }
  363.             }
  364.         }
  365.     }
  366.     for (CModel* pRain : rains) if (pRain->GetY() < 20) pRain->SetPosition(rand() % 2800 - 1400, 900, rand() % 2800 - 1400); // do not delete if Y < 20, just move back up at random XZ!! (fixes memory leak)
  367.     rains.remove_if(deleted);
  368. }
  369.  
  370. void CMyGame::AddLightning()
  371. {
  372.     // --- simulating lightning ---
  373.     if (rand() % 300 == 1) lightning.SetStatus(60); // 0.3% chance per frame for lightning strike
  374.     if (lightning.GetStatus() > 58) // controlling the sprite life cycle and play sound because even if animation loop = false, still continues to loop(..?)
  375.     {
  376.         lightning.PlayAnimation(1, 6, 6, false);
  377.         lightning.SetStatus(lightning.GetStatus() - 1);
  378.     }
  379.     if (lightning.GetStatus() <= 0) lightning.Delete();
  380. }
  381.  
  382. void CMyGame::EnemyControl(CModelMd2* a, CModelMd2* b, vector<CVector>* c, float* d)
  383. {
  384.     a = &player;
  385.     float dx = a->GetX() - b->GetX();
  386.     float dz = a->GetZ() - b->GetZ();
  387.     float td = sqrt(dx * dx + dz * dz); // distance between player and enemy3 ('distance' function not working)
  388.  
  389.     if (b->IsAutoMoving()) b->PlayAnimation(40, 45, 5, true); // is moving, play run animation
  390.     else if (td < 300) b->PlayAnimation(123, 134, 5, true); // is player close, point animation
  391.     else b->PlayAnimation(1, 39, 7, true); // is not, play standing animation
  392.  
  393.     if (td > 300) // path finding
  394.     {
  395.         if (!b->IsAutoMoving() && b->GetStatus() < c->size())
  396.         {
  397.             if (c == &path3)
  398.             {
  399.                 CVector v = path3[b->GetStatus()];
  400.                 b->SetStatus(b->GetStatus() + 1);
  401.                 if (rand() % 90 == 1) b->MoveTo(v.x, v.z, 150); // random delay for playability and realism
  402.             }
  403.             else if (c == &path4)
  404.             {
  405.                 CVector v = path4[b->GetStatus()];
  406.                 b->SetStatus(b->GetStatus() + 1);
  407.                 if (rand() % 90 == 1) b->MoveTo(v.x, v.z, 150);
  408.             }
  409.             else if (c == &path5)
  410.             {
  411.                 CVector v = path5[b->GetStatus()];
  412.                 b->SetStatus(b->GetStatus() + 1);
  413.                 if (rand() % 90 == 1) b->MoveTo(v.x, v.z, 150);
  414.             }
  415.         }
  416.         if (!b->IsAutoMoving() && b->GetStatus() == c->size())
  417.         {
  418.             if (c == &path3)
  419.             {
  420.                 b->SetStatus(0);
  421.                 CVector v = path3[b->GetStatus()];
  422.                 if (rand() % 90 == 1) b->MoveTo(v.x, v.z, 150);
  423.             }
  424.             if (c == &path4)
  425.             {
  426.                 b->SetStatus(0);
  427.                 CVector v = path4[b->GetStatus()];
  428.                 if (rand() % 90 == 1) b->MoveTo(v.x, v.z, 150);
  429.             }
  430.             if (c == &path5)
  431.             {
  432.                 b->SetStatus(0);
  433.                 CVector v = path5[b->GetStatus()];
  434.                 if (rand() % 90 == 1) b->MoveTo(v.x, v.z, 150);
  435.             }
  436.         }
  437.     }
  438.     else if (td < 300) // alert when player is near and take health
  439.     {
  440.         IsPlaying.SetStatus(IsPlaying.GetStatus() - 1);
  441.         b->Stop();
  442.         b->SetRotationToPoint(a->GetX(), a->GetZ());
  443.         hbar.SetHealth(hbar.GetHealth() - 1);
  444.     }
  445.     if (td > 300 && td < 400) IsPlaying.SetStatus(100);
  446.     *d = td;
  447. }
  448.  
  449. void CMyGame::OnUpdate()
  450. {  
  451.     long t =  GetTime();
  452.  
  453.     // --- controls ---
  454.     PlayerControl();
  455.     EnemyControl(&player, &enemy3, &path3, &gtd3);
  456.     EnemyControl(&player, &enemy4, &path4, &gtd4);
  457.     EnemyControl(&player, &enemy5, &path5, &gtd5);
  458.     EnemyControl(&player, &enemy6, &path6, &gtd6);
  459.  
  460.     // --- rules ---
  461.     if (IsMenuMode() || IsGameOver()) return;
  462.     if (hbar.GetHealth() <= 0) GameOver();
  463.         // win condition
  464.     for (CModel* van : vans) if (player.HitTestFront(van) && score == 4)
  465.     {
  466.         win.Play("win.wav");
  467.         rainsound.Stop();
  468.         PauseGame();
  469.     }
  470.         // life recovery
  471.     if (gtd3 > 900 && gtd4 > 900 && gtd5 > 900 && gtd6 > 900) hbar.SetHealth(hbar.GetHealth() + 1);
  472.     else if (hbar.GetHealth() > 100) hbar.SetHealth(100);
  473.    
  474.     // timed alert sound
  475.     if (IsPlaying.GetStatus() > 97 && IsPlaying.GetStatus() < 100) alert.Play("found.wav");
  476.  
  477.     // gameover sound
  478.     if (IsGameOver())
  479.     {
  480.         rainsound.Stop();
  481.         gameover.Play("gameover.wav");
  482.     }
  483.  
  484.     // --- weather ---
  485.     AddRain();
  486.     AddLightning();
  487.  
  488.     // --- distance bar ---
  489.     AddDistBar();
  490.  
  491.     // --- deletes ---
  492.     if (cage1.GetY() < 300) cage1.Delete();
  493.     else if (cage2.GetY() < 300) cage2.Delete();
  494.     else if (cage3.GetY() < 300) cage3.Delete();
  495.     else if (cage4.GetY() < 300) cage4.Delete();
  496.     monkeys.remove_if(deleted);
  497.  
  498.     // --- updating models ---
  499.     player.Update(t);
  500.     enemy1.Update(t);
  501.     enemy2.Update(t);
  502.     enemy3.Update(t);
  503.     enemy4.Update(t);
  504.     enemy5.Update(t);
  505.     enemy6.Update(t);
  506.     cage1.Update(t);
  507.     cage2.Update(t);
  508.     cage3.Update(t);
  509.     cage4.Update(t);
  510.     monkeys.Update(t);
  511.     rains.Update(t);
  512.     lightning.Update(t);
  513. }
  514.  
  515. void CMyGame::PlayerControl()
  516. {
  517.     // --- player control ---
  518.     if (IsKeyDown(SDLK_w)) player.SetSpeed(300);
  519.     else player.SetSpeed(0);
  520.  
  521.     if (IsKeyDown(SDLK_a))
  522.     {
  523.         player.Rotate(5);
  524.         player.SetDirection(player.GetRotation());
  525.         rotation -= 5;
  526.     }
  527.     if (IsKeyDown(SDLK_d))
  528.     {
  529.         player.Rotate(-5);
  530.         player.SetDirection(player.GetRotation());
  531.         rotation += 5;
  532.     }
  533.    
  534.     // --- player animation ---
  535.     if (IsKeyDown(SDLK_w))  player.PlayAnimation(4,8,10,true);
  536.     else player.PlayAnimation(1,3,6,true);
  537.  
  538.     // --- hittesting ---
  539.     if (player.HitTestFront(&cage1) || player.HitTestFront(&cage2) || player.HitTestFront(&cage3) || player.HitTestFront(&cage4)
  540.         || player.HitTestFront(&cage_switch1) || player.HitTestFront(&cage_switch2) || player.HitTestFront(&cage_switch3) || player.HitTestFront(&cage_switch4)) player.SetSpeed(0);
  541.     for (CModel* pWall : walls) if (player.HitTestFront(pWall)) player.SetSpeed(0);
  542.     for (CModel* pBox : boxes) if (player.HitTestFront(pBox)) player.SetSpeed(0);
  543.     for (CModel* pBox2 : boxes2) if (player.HitTestFront(pBox2)) player.SetSpeed(0);
  544.     for (CModel* pBarrel : barrels) if (player.HitTestFront(pBarrel)) player.SetSpeed(0);
  545.     for (CModel* pVan : vans) if (player.HitTestFront(pVan)) player.SetSpeed(0);
  546.     for (CModel* pFence : fences) if (player.HitTestFront(pFence)) player.SetSpeed(0);
  547.     for (CModel* pHouse : houses) if (player.HitTestFront(pHouse)) player.SetSpeed(0);
  548.     for (CModel* pMonkey : monkeys) if (player.HitTestFront(pMonkey)) player.SetSpeed(0);
  549. }
  550.  
  551. //-----------------  Draw 2D overlay ----------------------
  552. void CMyGame::OnDraw(CGraphics* g)
  553. {
  554.     if (IsMenuMode())
  555.     {
  556.      startScreen.Draw(g);
  557.      return;
  558.     }
  559.  
  560.     // lightning and UI
  561.     if (lightning.GetStatus() > 0) lightning.Draw(g);
  562.     if (gtd3 < 900 || gtd4 < 900 || gtd5 < 900 || gtd6 < 900) enemydist.Draw(g);
  563.     hbar.Draw(g);
  564.  
  565.     // score
  566.     font.SetSize(32); font.SetColor( CColor::Red()); font.DrawNumber(10,Height-50, score), font.DrawText(28, Height - 50, "/4");
  567.    
  568.     if (IsGameOver())
  569.     {
  570.         font.SetSize(64); font.SetColor( CColor::Red()); font.DrawText(300,300, "GAME OVER");  
  571.     }
  572.     for (CModel* van : vans) if (player.HitTestFront(van) && score != 4)
  573.     {
  574.         font.SetSize(44); font.SetColor(CColor::Blue()); font.DrawText(Width/2 - 250, Height/2, "leave no monkey behind!");
  575.     }
  576.     for (CModel* van : vans) if (player.HitTestFront(van) && score == 4)
  577.     {
  578.         font.SetSize(44); font.SetColor(CColor::Blue()); font.DrawText(Width / 2 - 200, Height / 2, "we're outta here!");
  579.     }
  580.  
  581. }
  582.  
  583. // ----------------   Draw 3D world -------------------------
  584. void CMyGame::OnRender3D(CGraphics* g)
  585. {
  586.     CameraControl(g);
  587.    
  588.     floor.Draw(g);
  589.     enemy1.Draw(g);
  590.     enemy2.Draw(g);
  591.     enemy3.Draw(g);
  592.     enemy4.Draw(g);
  593.     enemy5.Draw(g);
  594.     enemy6.Draw(g);
  595.     player.Draw(g);
  596.     walls.Draw(g);
  597.     skydome.Draw(g);
  598.     canopys.Draw(g);
  599.     bananas.Draw(g);
  600.     grasses.Draw(g);
  601.     boxes.Draw(g);
  602.     barrels.Draw(g);
  603.     vans.Draw(g);
  604.     fences.Draw(g);
  605.     shacks.Draw(g);
  606.     monkeys.Draw(g);
  607.     boxes2.Draw(g);
  608.     houses.Draw(g);
  609.     watchtowers.Draw(g);
  610.     tracks.Draw(g);
  611.     rains.Draw(g);
  612.     cage_switch1.Draw(g);
  613.     cage_switch2.Draw(g);
  614.     cage_switch3.Draw(g);
  615.     cage_switch4.Draw(g);
  616.     cage1.Draw(g);
  617.     cage2.Draw(g);
  618.     cage3.Draw(g);
  619.     cage4.Draw(g);
  620.  
  621.     ShowCoordinateSystem();
  622. }
  623.  
  624. void CMyGame::CameraControl(CGraphics* g)
  625. {
  626.     // game world tilt
  627.  
  628.     // ------ Global Transformation Functions -----
  629.     glTranslatef(0, -300, 800);  // move game world down
  630.  
  631.     glRotatef(tilt, 1, 0, 0);   // tilt game world around x axis
  632.  
  633.     glScalef(scale, scale, scale);          // scaling the game world
  634.     glRotatef(rotation, 0, 1, 0);               // rotating the game world
  635.  
  636.                                                 // ---- 3rd person camera setup -----
  637.                                                 // translate game world to player position
  638.     glTranslatef(-player.GetX(), 0, -player.GetZ());
  639.  
  640.     UpdateView();
  641.     Light.Apply();
  642. }
  643.  
  644. // called at the start of a new game - display menu here
  645. void CMyGame::OnDisplayMenu()
  646. {
  647. }
  648.  
  649. // called when Game Mode entered
  650. void CMyGame::OnStartGame()
  651. {
  652.     OnStartLevel(1);
  653. }
  654.  
  655.  
  656. // called when Game is Over
  657. void CMyGame::OnGameOver()
  658. {
  659. }
  660.  
  661. // one time termination code
  662. void CMyGame::OnTerminate()
  663. {
  664. }
  665.  
  666. // -------    Keyboard Event Handling ------------
  667.  
  668. void CMyGame::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode)
  669. {
  670.     if (sym == SDLK_F4 && (mod & (KMOD_LALT | KMOD_RALT)))
  671.         StopGame();
  672.     if (sym == SDLK_SPACE)
  673.     {
  674.         if (IsPaused()) ResumeGame();
  675.         else PauseGame();
  676.     }
  677.     if (sym == SDLK_F2)
  678.     {
  679.         NewGame();
  680.         rainsound.Clear();
  681.     }
  682.     if (sym == SDLK_e)
  683.     {
  684.         if (player.HitTest(&cage_switch1) && cage1.GetStatus() == 0) { cageopen.Play("cageopen.wav"); cage1.Open(); }
  685.         if (player.HitTest(&cage_switch2) && cage2.GetStatus() == 0) { cageopen.Play("cageopen.wav"); cage2.Open(); }
  686.         if (player.HitTest(&cage_switch3) && cage3.GetStatus() == 0) { cageopen.Play("cageopen.wav"); cage3.Open(); }
  687.         if (player.HitTest(&cage_switch4) && cage4.GetStatus() == 0) { cageopen.Play("cageopen.wav"); cage4.Open(); }
  688.         for (CModel* pMonkey : monkeys)
  689.             if (player.HitTest(pMonkey))
  690.             {
  691.                 score++;
  692.                 collect.Play("collect.wav");
  693.                 pMonkey->Delete();
  694.             }
  695.     }
  696.  
  697.     // --- camera control testing ---
  698.     if (sym == SDLK_LEFT)
  699.         tilt += 5;
  700.     if (sym == SDLK_RIGHT)
  701.         tilt -= 5;
  702.     if (sym == SDLK_UP)
  703.         scale += 1.0f;
  704.     if (sym == SDLK_DOWN)
  705.         scale -= 1.0f;
  706. }
  707.  
  708. void CMyGame::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode)
  709. {
  710. }
  711.  
  712. // -----  Mouse Events Handlers -------------
  713.  
  714. void CMyGame::OnMouseMove(Uint16 x,Uint16 y,Sint16 relx,Sint16 rely,bool bLeft,bool bRight,bool bMiddle)
  715. {
  716. }
  717.  
  718. void CMyGame::OnLButtonDown(Uint16 x,Uint16 y)
  719. {    
  720.     if (IsMenuMode()) StartGame();
  721. }
  722.  
  723. void CMyGame::OnLButtonUp(Uint16 x,Uint16 y)
  724. {
  725. }
  726.  
  727. void CMyGame::OnRButtonDown(Uint16 x,Uint16 y)
  728. {
  729. }
  730.  
  731. void CMyGame::OnRButtonUp(Uint16 x,Uint16 y)
  732. {
  733. }
  734.  
  735. void CMyGame::OnMButtonDown(Uint16 x,Uint16 y)
  736. {
  737. }
  738.  
  739. void CMyGame::OnMButtonUp(Uint16 x,Uint16 y)
  740. {
  741. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top