Advertisement
zCool

MainLoop.cpp

May 20th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. #include <string>
  2. #include "Player.h"
  3. #include "Npc.h"
  4. #include "Stick.h"
  5. #include "Rock.h"
  6. #include "Wall.h"
  7. #include "Grass.h"
  8. #include <iostream>
  9. #include "time.h"
  10.  
  11. using namespace std;
  12.  
  13. void drawMap(int map[], int width, int height, Entity entities[], int numEntities)
  14. {
  15.     for(int y=0; y<height; y++)
  16.     {
  17.         for(int x=0; x<width; x++)
  18.         {
  19.             cout << entities[map[x+width*y]].getSprite();
  20.         }
  21.         cout << endl;
  22.     }
  23. }
  24.  
  25. void drawScore(int playerScore, int npcScore)
  26. {
  27.     cout << "Player Score: " << playerScore << " " << "Npc Score: " << npcScore << endl;
  28. }
  29.  
  30. Character::directions getValidatedInput()
  31. {
  32.     cout << "Please enter a direction:(east, west, north, south) ";
  33.     string temp = "";
  34.     getline(cin, temp);
  35.  
  36.     while(temp.compare("east") != 0 && temp.compare("west") != 0 &&
  37.         temp.compare("north") != 0 && temp.compare("south") != 0)
  38.     {
  39.         cout << "Please enter a direction:(east, west, north, south) ";
  40.         getline(cin, temp);
  41.        
  42.     }
  43.  
  44.     if(temp.compare("east") == 0) return Character::EAST;
  45.     else if(temp.compare("west") == 0) return Character::WEST;
  46.     else if(temp.compare("north") == 0) return Character::NORTH;
  47.     else if(temp.compare("south") == 0) return Character::SOUTH;
  48. }
  49.  
  50. bool checkForCollision(Entity a, Entity b)
  51. {
  52.     bool collision = (a.getX() == b.getX()) && (a.getY() == b.getX());
  53.     return collision;
  54. }
  55.  
  56. void update()
  57. {
  58.  
  59. }
  60.  
  61. void waitForEnter()
  62. {
  63.     while(1)
  64.     {
  65.         if('\n' == getchar())
  66.         {
  67.             break;
  68.         }
  69.     }
  70. }
  71.  
  72. int main()
  73. {
  74.     //define the sprites
  75.     const string wallSprite = "#";
  76.     const string grassSprite = ".";
  77.     const string stickSprite = "-";
  78.     const string rockSprite = "o";
  79.     const string playerSprite = "@";
  80.     const string npcSprite = "&";
  81.  
  82.     //define the entities
  83.     Wall wall(0, 0, wallSprite);
  84.     Grass grass(0, 0, grassSprite);
  85.     Stick stick(0, 0, stickSprite);
  86.     Rock rock(0, 0, rockSprite);
  87.     Player player(7, 3, playerSprite, 0);
  88.     Npc npc(3, 2, npcSprite, 0);
  89.  
  90.     //define user input direction enum and npc direction enum
  91.     Character::directions playerDirection;
  92.     Character::directions npcDirection;
  93.    
  94.     Entity entities[6] = {wall, grass, player, rock, stick, npc};
  95.    
  96.  
  97.     //define the map
  98.     //0 - Wall 1-Grass 2-Player 3-Rock 4-Stick 5-Npc
  99.     int map[17*5] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100.                       0, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 0,
  101.                       0, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 0,
  102.                       0, 1, 1, 1, 1, 3, 1, 2, 1, 3, 1, 1, 4, 1, 1, 1, 0,
  103.                       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  104.  
  105.     //seed random generator for generating new stick coordinates
  106.     srand(time(NULL));
  107.  
  108.     while(player.getScore() < 10)
  109.     {
  110.         //Draw the map
  111.         drawMap(map, 17, 5, entities, 6);
  112.         //Draw the score
  113.         drawScore(player.getScore(), npc.getScore());
  114.  
  115.         //Get User Input and NPC AI
  116.         playerDirection = getValidatedInput();
  117.         npcDirection = npc.decideWhatToDo();
  118.    
  119.         //update based on user input
  120.         player.move(playerDirection);
  121.         //npc.move(npcDirection);
  122.  
  123.         //check for collisions
  124.         //wall
  125.         if(map[player.getX()+17*player.getY()] == 0)
  126.         {
  127.             player.undoMove(playerDirection);
  128.         }
  129.         //grass
  130.         if(map[player.getX()+17*player.getY()] == 1)
  131.         {
  132.             //nothing not a collision??
  133.             //update map to reflect player movement *super ugly!!
  134.             map[player.getX() + 17*player.getY()] = 2;
  135.             player.undoMove(playerDirection);
  136.             map[player.getX() + 17*player.getY()] = 1;
  137.             player.move(playerDirection);
  138.         }
  139.         //rock
  140.         if(map[player.getX()+17*player.getY()] == 3)
  141.         {
  142.             player.undoMove(playerDirection);
  143.         }
  144.         //stick
  145.         if(map[player.getX()+17*player.getY()] == 4)
  146.         {
  147.             stick.generateCoordinates(15, 3);
  148.             while(map[stick.getX() + 17*stick.getY()] == 3)
  149.             {
  150.                 stick.generateCoordinates(15, 3);
  151.             }
  152.             map[stick.getX() + 17*stick.getY()] = 4;
  153.             player.increaseScore();
  154.             map[player.getX() + 17*player.getY()] = 2;
  155.             player.undoMove(playerDirection);
  156.             map[player.getX() + 17*player.getY()] = 1;
  157.             player.move(playerDirection);
  158.         }
  159.         //npc
  160.         if(map[player.getX()+17*player.getY()] == 5)
  161.         {
  162.             player.undoMove(playerDirection);
  163.         }
  164.  
  165.         //update based on npc "AI"
  166.         npc.move(npcDirection);
  167.         //npc.move(npcDirection);
  168.  
  169.         //check for collisions
  170.         //wall
  171.         if(map[npc.getX()+17*npc.getY()] == 0)
  172.         {
  173.             npc.undoMove(npcDirection);
  174.         }
  175.         //grass
  176.         if(map[npc.getX()+17*npc.getY()] == 1)
  177.         {
  178.             //nothing not a collision??
  179.             //update map to reflect player movement *super ugly!!
  180.             map[npc.getX() + 17*npc.getY()] = 5;
  181.             npc.undoMove(npcDirection);
  182.             map[npc.getX() + 17*npc.getY()] = 1;
  183.             npc.move(npcDirection);
  184.         }
  185.         //rock
  186.         if(map[npc.getX()+17*npc.getY()] == 3)
  187.         {
  188.             npc.undoMove(npcDirection);
  189.         }
  190.         //stick
  191.         if(map[player.getX()+17*player.getY()] == 4)
  192.         {
  193.             stick.generateCoordinates(15, 3);
  194.             while(map[npc.getX() + 17*npc.getY()] == 3)
  195.             {
  196.                 stick.generateCoordinates(15, 3);
  197.             }
  198.             map[stick.getX() + 17*stick.getY()] = 4;
  199.             npc.increaseScore();
  200.             map[npc.getX() + 17*npc.getY()] = 5;
  201.             npc.undoMove(npcDirection);
  202.             map[npc.getX() + 17*npc.getY()] = 1;
  203.             npc.move(npcDirection);
  204.         }
  205.         //player
  206.         if(map[npc.getX()+17*npc.getY()] == 2)
  207.         {
  208.             npc.undoMove(npcDirection);
  209.         }
  210.        
  211.  
  212.     }
  213.     //Enter to exit
  214.     cout << "Press Enter To Exit...";
  215.     waitForEnter();
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement