Advertisement
Carcigenicate

Crawler V2

Jan 21st, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.94 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <ctime>
  6. #include <cstdlib>
  7. #include <cmath>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12. int random(int min, int max) {
  13.     return (rand() % (max - min) + min);
  14. }
  15.  
  16. //How far the player is able to see on each side
  17. int const EXT = 5;
  18.  
  19. class Object {
  20.    
  21.     public:
  22.     int xpos, ypos;
  23.     string id;
  24.     string type;
  25.    
  26.     Object () {
  27.         xpos = -1;
  28.         ypos = -1;
  29.         id = "B";
  30.         type = "Base";
  31.     }
  32.    
  33.     void set_pos (int xloc, int yloc) {
  34.         xpos = xloc, ypos = yloc;
  35.     }
  36.    
  37.     bool operator== (Object& iobj) {
  38.         if (xpos == iobj.xpos && ypos == iobj.ypos && id == iobj.id && type == iobj.type) {
  39.             return true;
  40.         } else {
  41.             return false;
  42.         }
  43.     }
  44.    
  45. };
  46.  
  47. struct Item : public Object {
  48.     Item () { id = "I"; type = "Item"; }
  49. };
  50.  
  51. struct Wall : public Object {
  52.     Wall () { id = "#"; type = "Wall"; }
  53. };
  54.  
  55. struct Being : public Object {
  56.     int hp;
  57.     vector <Object> inventory;
  58.    
  59.     void move (int xoff, int yoff);
  60.    
  61.     void show_stats () {
  62.         cout << "Hp: " << hp << ", Inventory: ";
  63.         for (int disp = 0; disp < inventory.size (); ++disp) {
  64.             cout << inventory [disp].id;
  65.             if (disp != inventory.size ()) cout << ", ";
  66.         }
  67.         cout << endl;
  68.     }
  69.    
  70. };
  71.  
  72. struct Gfx : public Object {
  73.     bool show_on_top;
  74.     int lifetime;
  75.    
  76.     Gfx (bool sot = false, int lifet = 1 ) {
  77.         type = "Gfx";
  78.         id = "!";
  79.         show_on_top = sot;
  80.         lifetime = lifet;
  81.     }
  82.    
  83. };
  84.  
  85. struct Npc : public Being {
  86.     int hp;
  87.     Npc () { id = "()"; hp = 50; type = "Npc"; }
  88. };
  89.    
  90.  
  91. struct Player : public Being {
  92.     Player () { id = "P"; hp = 100; type = "Player"; }
  93.     void req_dir ();
  94.     void pulse ();
  95. };
  96.  
  97.  
  98. struct database {
  99.     Object NObject; //Null objects to return if an item isnt found
  100.     Gfx NGfx;
  101.     Npc NNpc;
  102.    
  103.     vector <Gfx> gfx;
  104.     vector <Object> objects;
  105.     vector <Npc> npcs;
  106.    
  107.     Player player;
  108.    
  109.     database () {
  110.         NObject.id = " "; NObject.type = "Empty";
  111.         NNpc.id = " "; NNpc.type = "Empty";
  112.         }
  113.    
  114.     Object& search_coord_obj (int xloc, int yloc) {
  115.         for (unsigned fobj = 0; fobj < objects.size (); ++fobj) {
  116.             if (objects [fobj].xpos == xloc && objects [fobj].ypos == yloc) {
  117.                 return objects [fobj];
  118.                 }
  119.         }
  120.         return NObject;
  121.     }
  122.    
  123.     Npc& search_coord_npc (int xloc, int yloc) {
  124.         for (unsigned fnpc = 0; fnpc < npcs.size (); ++fnpc) {
  125.             if (npcs [fnpc].xpos == xloc && npcs [fnpc].ypos == yloc) {
  126.                 return npcs [fnpc];
  127.                 }
  128.         }
  129.         return NNpc;
  130.     }
  131.    
  132.     Gfx& get_gfx (int xloc, int yloc) {
  133.         for (int find = 0; find < gfx.size (); ++find) {
  134.         if (gfx [find].xpos == xloc && gfx [find].ypos == yloc) {
  135.             return gfx [find];
  136.             }
  137.         }
  138.         return NGfx;
  139.     }
  140.    
  141.     bool has_gfx (int xloc, int yloc) {
  142.         for (int find = 0; find < gfx.size (); ++find) {
  143.         if (gfx [find].xpos == xloc && gfx [find].ypos == yloc) {
  144.             return true;
  145.             }
  146.         }
  147.         return false;
  148.     }
  149.    
  150.     void set_player (int xloc, int yloc, string sid) {
  151.         player.id = sid, player.xpos = xloc, player.ypos = yloc;
  152.     }
  153.    
  154.     void add_item (int xloc, int yloc, string sid) {
  155.         Item nitem;
  156.         nitem.id = sid, nitem.xpos = xloc, nitem.ypos = yloc;
  157.         objects.push_back (nitem);
  158.     }
  159.    
  160.     void add_npc (int xloc, int yloc) {
  161.         Npc nnpc;
  162.         nnpc.xpos = xloc, nnpc.ypos = yloc;
  163.         cout << "Nnpc hp "<< nnpc.hp << endl;
  164.         npcs.push_back (nnpc);
  165.     }
  166.    
  167.     // ((Wall start, wall end), y coord)
  168.     void add_xwall (int sxcoord, int excoord, int ycoord) {
  169.         Wall nwall;
  170.         for (int fill = sxcoord; fill < excoord; ++fill) {
  171.             nwall.xpos = fill, nwall.ypos = ycoord;
  172.             objects.push_back (nwall);
  173.         }
  174.     }
  175.    
  176.     // (xcoord, (Wall start, Wall end))
  177.     void add_ywall (int xcoord, int sycoord, int eycoord) {
  178.         Wall nwall;
  179.         for (int fill = sycoord; fill < eycoord; ++fill) {
  180.             nwall.xpos = xcoord, nwall.ypos = fill;
  181.             objects.push_back (nwall);
  182.         }
  183.     }
  184.    
  185.     void add_gfx (int xloc, int yloc, string iid, int life = 1) {
  186.         Gfx ngfx;
  187.         ngfx.xpos = xloc, ngfx.ypos = yloc;
  188.         ngfx.id = iid;
  189.         ngfx.lifetime = life;
  190.         gfx.push_back (ngfx);
  191.     }
  192.    
  193.     void make_outer_walls (double ratio) {
  194.     int hext = EXT * ratio;
  195.     add_xwall (0, hext, 0); //Top Wall
  196.     add_ywall (0, 0, hext); //Left Wall
  197.     add_xwall (0, hext, hext); //Bottom Wall
  198.     add_ywall (hext, 0, hext); //Right Wall
  199.     }
  200.    
  201.     string type_in_coord (int xloc, int yloc) {
  202.         Object fobj = search_coord_obj (xloc, yloc);
  203.         return fobj.type;
  204.     }
  205.    
  206.     void delete_object (Object& cobject) {
  207.         for (int find = 0; find < objects.size (); ++find) {
  208.             if (objects [find] == cobject) objects.erase (objects.begin() + find);
  209.         }
  210.     }
  211.    
  212.     //Destroys any expired graphics, and decrements the
  213.     //  lives of remaining graphics
  214.     void manage_gfx_lives () {
  215.         for (int find = 0; find < gfx.size (); ++find) {
  216.             if (gfx [find].lifetime < 1) {
  217.                 gfx.erase (gfx.begin () + find);
  218.             } else {
  219.                 gfx [find].lifetime -= 1;
  220.                 }
  221.         }
  222.     }
  223.  
  224. } db;
  225.  
  226. void Being :: move (int xoff = 0, int yoff = 0) {
  227.     Object& robject = db.search_coord (xpos + xoff, ypos + yoff);
  228.     string dest_occ = robject.type;
  229.    
  230.     if (dest_occ == "Empty" || dest_occ == "Gfx") {
  231.         xpos += xoff, ypos += yoff;
  232.         cout << "Empty Space\n";
  233.        
  234.     } else if (dest_occ == "Item") {
  235.         xpos += xoff, ypos += yoff;
  236.         Object icopy = robject;
  237.         db.delete_object (robject);
  238.         inventory.push_back (icopy);
  239.         cout << "Picked up an item: " << icopy.id << endl;
  240.        
  241.     } else if (dest_occ == "Wall") {
  242.         cout << "You hit a wall\n";
  243.        
  244.     } else if (dest_occ == "Npc") {
  245.         cout << "You ran into a Npc\n";
  246.     }
  247.     }
  248.    
  249. void Player :: req_dir () {
  250.         cout << "What direction?\n";
  251.         string sdir; cin >> sdir;
  252.         char dir = sdir [0];
  253.         char act = '0';
  254.         if (sdir.size () > 1) act = sdir [1];
  255.         bool baddir = true;
  256.         while (baddir) {
  257.             if (dir == 'w' || dir == 'W') {
  258.                 move (0, -1); baddir = false;
  259.             } else if (dir == 's' || dir == 'S') {
  260.                     move (0, 1); baddir = false;
  261.             } else if (dir == 'a' || dir == 'A') {
  262.                     move (-1, 0); baddir = false;
  263.             } else if (dir == 'd' || dir == 'D') {
  264.                     move (1, 0); baddir = false;
  265.             }
  266.             if (act == 'p' || act == 'P' || dir == 'p' || dir == 'P') {
  267.                 pulse (); baddir = false;
  268.             }
  269.         }
  270.     }
  271.    
  272. void Player :: pulse () {
  273.     bool has_pulse = false;
  274.     int used_iter = 0;
  275.     for (int check = 0; check < inventory.size() && !has_pulse; ++check) {
  276.         if (inventory [check].id == "1") {
  277.             has_pulse = true;
  278.             used_iter = check;
  279.             }
  280.     }
  281.    
  282.     if (has_pulse) {
  283.         int range = 1;
  284.         for (int yp = ypos - range; yp <= ypos + range; ++yp) {
  285.             for (int xp = xpos - range; xp <= xpos + range; ++xp) {
  286.                 db.add_gfx (xp, yp, "><", 0);
  287.                 Object& fnpc = db.search_coord (xp, yp);
  288.                 if (fnpc.type == "Npc") {
  289.                     int hitdmg = random (5, 10);
  290.                     fnpc.hp -= hitdmg;
  291.                     cout << "Npc HP: " << fnpc.hp << endl;
  292.                     cout << "Hit for " << hitdmg << endl;
  293.                     if (fnpc.hp < 0) {
  294.                         db.delete_object (fnpc);
  295.                         db.add_gfx (xp, yp, "^^", 10);
  296.                     }
  297.                 }
  298.             }
  299.         }
  300.     }
  301. }
  302.  
  303. struct Display {
  304.  
  305.     void show_field (Object& cobject) {
  306.         int xmin = (cobject.xpos - EXT),
  307.         xmax = (cobject.xpos + EXT),
  308.         ymin = (cobject.ypos - EXT),
  309.         ymax = (cobject.ypos + EXT);
  310.        
  311.         for (int yc = ymin; yc < ymax; ++yc) {
  312.             for (int xc = xmin; xc < xmax; ++xc) {
  313.                 Object fobj = db.search_coord (xc, yc);
  314.                 if (xc == db.player.xpos && yc == db.player.ypos){
  315.                     cout << setw (2) << db.player.id;
  316.                 } else if (db.has_gfx (xc, yc)) {
  317.                     Gfx fgfx = db.get_gfx (xc, yc);
  318.                     cout << setw (2) << fgfx.id;
  319.                     } else if (fobj.type != "Empty") {
  320.                         cout << setw (2) << fobj.id;
  321.                     } else cout << setw (2) << "";
  322.                 }
  323.             cout << "\n";
  324.             }
  325.         }
  326.  
  327. };
  328.  
  329. int main() {
  330.     srand (time (0));
  331.     Display screen;
  332.     //Player xpos, ypos, Marker
  333.     db.add_gfx (1, 1, "$", 1);
  334.     db.set_player (5, 5, "P");
  335.    
  336.     //Item xpos, ypos, Marker
  337.     db.add_item (3, 3, "1");
  338.     db.add_item (4, 4, "2");
  339.    
  340.     db.add_npc (6, 6);
  341.     db.add_npc (7, 7);
  342.    
  343.     db.make_outer_walls (1.8);
  344.  
  345.    
  346.     while (true) {
  347.         screen.show_field (db.player);
  348.        
  349.         db.reset_ids ();
  350.        
  351.         db.player.show_inventory ();
  352.        
  353.         db.player.req_dir ();
  354.        
  355.         db.manage_gfx_lives ();
  356.     }
  357.    
  358.    
  359.     cout << "Done\n";
  360.    
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement