Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <ctime>
- #include <cstdlib>
- #include <cmath>
- #include <sstream>
- using namespace std;
- int random(int min, int max) {
- return (rand() % (max - min) + min);
- }
- //How far the player is able to see on each side
- int const EXT = 5;
- class Object {
- public:
- int xpos, ypos;
- string id;
- string type;
- Object () {
- xpos = -1;
- ypos = -1;
- id = "B";
- type = "Base";
- }
- void set_pos (int xloc, int yloc) {
- xpos = xloc, ypos = yloc;
- }
- bool operator== (Object& iobj) {
- if (xpos == iobj.xpos && ypos == iobj.ypos && id == iobj.id && type == iobj.type) {
- return true;
- } else {
- return false;
- }
- }
- };
- struct Item : public Object {
- Item () { id = "I"; type = "Item"; }
- };
- struct Wall : public Object {
- Wall () { id = "#"; type = "Wall"; }
- };
- struct Being : public Object {
- int hp;
- vector <Object> inventory;
- void move (int xoff, int yoff);
- void show_stats () {
- cout << "Hp: " << hp << ", Inventory: ";
- for (int disp = 0; disp < inventory.size (); ++disp) {
- cout << inventory [disp].id;
- if (disp != inventory.size ()) cout << ", ";
- }
- cout << endl;
- }
- };
- struct Gfx : public Object {
- bool show_on_top;
- int lifetime;
- Gfx (bool sot = false, int lifet = 1 ) {
- type = "Gfx";
- id = "!";
- show_on_top = sot;
- lifetime = lifet;
- }
- };
- struct Npc : public Being {
- int hp;
- Npc () { id = "()"; hp = 50; type = "Npc"; }
- };
- struct Player : public Being {
- Player () { id = "P"; hp = 100; type = "Player"; }
- void req_dir ();
- void pulse ();
- };
- struct database {
- Object NObject; //Null objects to return if an item isnt found
- Gfx NGfx;
- Npc NNpc;
- vector <Gfx> gfx;
- vector <Object> objects;
- vector <Npc> npcs;
- Player player;
- database () {
- NObject.id = " "; NObject.type = "Empty";
- NNpc.id = " "; NNpc.type = "Empty";
- }
- Object& search_coord_obj (int xloc, int yloc) {
- for (unsigned fobj = 0; fobj < objects.size (); ++fobj) {
- if (objects [fobj].xpos == xloc && objects [fobj].ypos == yloc) {
- return objects [fobj];
- }
- }
- return NObject;
- }
- Npc& search_coord_npc (int xloc, int yloc) {
- for (unsigned fnpc = 0; fnpc < npcs.size (); ++fnpc) {
- if (npcs [fnpc].xpos == xloc && npcs [fnpc].ypos == yloc) {
- return npcs [fnpc];
- }
- }
- return NNpc;
- }
- Gfx& get_gfx (int xloc, int yloc) {
- for (int find = 0; find < gfx.size (); ++find) {
- if (gfx [find].xpos == xloc && gfx [find].ypos == yloc) {
- return gfx [find];
- }
- }
- return NGfx;
- }
- bool has_gfx (int xloc, int yloc) {
- for (int find = 0; find < gfx.size (); ++find) {
- if (gfx [find].xpos == xloc && gfx [find].ypos == yloc) {
- return true;
- }
- }
- return false;
- }
- void set_player (int xloc, int yloc, string sid) {
- player.id = sid, player.xpos = xloc, player.ypos = yloc;
- }
- void add_item (int xloc, int yloc, string sid) {
- Item nitem;
- nitem.id = sid, nitem.xpos = xloc, nitem.ypos = yloc;
- objects.push_back (nitem);
- }
- void add_npc (int xloc, int yloc) {
- Npc nnpc;
- nnpc.xpos = xloc, nnpc.ypos = yloc;
- cout << "Nnpc hp "<< nnpc.hp << endl;
- npcs.push_back (nnpc);
- }
- // ((Wall start, wall end), y coord)
- void add_xwall (int sxcoord, int excoord, int ycoord) {
- Wall nwall;
- for (int fill = sxcoord; fill < excoord; ++fill) {
- nwall.xpos = fill, nwall.ypos = ycoord;
- objects.push_back (nwall);
- }
- }
- // (xcoord, (Wall start, Wall end))
- void add_ywall (int xcoord, int sycoord, int eycoord) {
- Wall nwall;
- for (int fill = sycoord; fill < eycoord; ++fill) {
- nwall.xpos = xcoord, nwall.ypos = fill;
- objects.push_back (nwall);
- }
- }
- void add_gfx (int xloc, int yloc, string iid, int life = 1) {
- Gfx ngfx;
- ngfx.xpos = xloc, ngfx.ypos = yloc;
- ngfx.id = iid;
- ngfx.lifetime = life;
- gfx.push_back (ngfx);
- }
- void make_outer_walls (double ratio) {
- int hext = EXT * ratio;
- add_xwall (0, hext, 0); //Top Wall
- add_ywall (0, 0, hext); //Left Wall
- add_xwall (0, hext, hext); //Bottom Wall
- add_ywall (hext, 0, hext); //Right Wall
- }
- string type_in_coord (int xloc, int yloc) {
- Object fobj = search_coord_obj (xloc, yloc);
- return fobj.type;
- }
- void delete_object (Object& cobject) {
- for (int find = 0; find < objects.size (); ++find) {
- if (objects [find] == cobject) objects.erase (objects.begin() + find);
- }
- }
- //Destroys any expired graphics, and decrements the
- // lives of remaining graphics
- void manage_gfx_lives () {
- for (int find = 0; find < gfx.size (); ++find) {
- if (gfx [find].lifetime < 1) {
- gfx.erase (gfx.begin () + find);
- } else {
- gfx [find].lifetime -= 1;
- }
- }
- }
- } db;
- void Being :: move (int xoff = 0, int yoff = 0) {
- Object& robject = db.search_coord (xpos + xoff, ypos + yoff);
- string dest_occ = robject.type;
- if (dest_occ == "Empty" || dest_occ == "Gfx") {
- xpos += xoff, ypos += yoff;
- cout << "Empty Space\n";
- } else if (dest_occ == "Item") {
- xpos += xoff, ypos += yoff;
- Object icopy = robject;
- db.delete_object (robject);
- inventory.push_back (icopy);
- cout << "Picked up an item: " << icopy.id << endl;
- } else if (dest_occ == "Wall") {
- cout << "You hit a wall\n";
- } else if (dest_occ == "Npc") {
- cout << "You ran into a Npc\n";
- }
- }
- void Player :: req_dir () {
- cout << "What direction?\n";
- string sdir; cin >> sdir;
- char dir = sdir [0];
- char act = '0';
- if (sdir.size () > 1) act = sdir [1];
- bool baddir = true;
- while (baddir) {
- if (dir == 'w' || dir == 'W') {
- move (0, -1); baddir = false;
- } else if (dir == 's' || dir == 'S') {
- move (0, 1); baddir = false;
- } else if (dir == 'a' || dir == 'A') {
- move (-1, 0); baddir = false;
- } else if (dir == 'd' || dir == 'D') {
- move (1, 0); baddir = false;
- }
- if (act == 'p' || act == 'P' || dir == 'p' || dir == 'P') {
- pulse (); baddir = false;
- }
- }
- }
- void Player :: pulse () {
- bool has_pulse = false;
- int used_iter = 0;
- for (int check = 0; check < inventory.size() && !has_pulse; ++check) {
- if (inventory [check].id == "1") {
- has_pulse = true;
- used_iter = check;
- }
- }
- if (has_pulse) {
- int range = 1;
- for (int yp = ypos - range; yp <= ypos + range; ++yp) {
- for (int xp = xpos - range; xp <= xpos + range; ++xp) {
- db.add_gfx (xp, yp, "><", 0);
- Object& fnpc = db.search_coord (xp, yp);
- if (fnpc.type == "Npc") {
- int hitdmg = random (5, 10);
- fnpc.hp -= hitdmg;
- cout << "Npc HP: " << fnpc.hp << endl;
- cout << "Hit for " << hitdmg << endl;
- if (fnpc.hp < 0) {
- db.delete_object (fnpc);
- db.add_gfx (xp, yp, "^^", 10);
- }
- }
- }
- }
- }
- }
- struct Display {
- void show_field (Object& cobject) {
- int xmin = (cobject.xpos - EXT),
- xmax = (cobject.xpos + EXT),
- ymin = (cobject.ypos - EXT),
- ymax = (cobject.ypos + EXT);
- for (int yc = ymin; yc < ymax; ++yc) {
- for (int xc = xmin; xc < xmax; ++xc) {
- Object fobj = db.search_coord (xc, yc);
- if (xc == db.player.xpos && yc == db.player.ypos){
- cout << setw (2) << db.player.id;
- } else if (db.has_gfx (xc, yc)) {
- Gfx fgfx = db.get_gfx (xc, yc);
- cout << setw (2) << fgfx.id;
- } else if (fobj.type != "Empty") {
- cout << setw (2) << fobj.id;
- } else cout << setw (2) << "";
- }
- cout << "\n";
- }
- }
- };
- int main() {
- srand (time (0));
- Display screen;
- //Player xpos, ypos, Marker
- db.add_gfx (1, 1, "$", 1);
- db.set_player (5, 5, "P");
- //Item xpos, ypos, Marker
- db.add_item (3, 3, "1");
- db.add_item (4, 4, "2");
- db.add_npc (6, 6);
- db.add_npc (7, 7);
- db.make_outer_walls (1.8);
- while (true) {
- screen.show_field (db.player);
- db.reset_ids ();
- db.player.show_inventory ();
- db.player.req_dir ();
- db.manage_gfx_lives ();
- }
- cout << "Done\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement