Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <functional>
- #include <vector>
- //#include <time.h>
- using namespace std;
- //Elk: The game of fast-paced action-packed boredom.
- //Version Alpha 0.21
- double ver = 0.21;
- /* --- VARIABLES --- */
- struct bounds { //board size
- const int x = 20;
- const int y = 40;
- };
- /* --- END VARIABLES --- */
- //vector code
- class Tile {
- public:
- int ID = 0;
- int col = 0;
- };
- typedef vector<vector<Tile>> i2d;
- //All this is for the playing field. It just constructs it.
- i2d retVec(int x, int y) {
- i2d vec;
- vec.resize(x);
- for(int a = 0; a < x; ++a){
- vec[a].resize(y);
- }
- return vec;
- }
- class Vec2D {
- public:
- i2d vec;
- Vec2D(int x, int y) {
- vec = retVec(x,y);
- };
- inline int& at(int x, int y) {
- return vec[x][y].ID;
- }
- inline int& atcol(int x, int y) {
- return vec[x][y].col;
- }
- };
- //end vector code
- //items code
- class ItemsC {
- //This will hold all the Unicode representations of every
- //items and entity in the game.
- public:
- const int32_t asize = 1000;
- vector<string> itemarray;
- vector<int> collision;
- ItemsC () {
- itemarray.resize(asize);
- itemarray.assign(asize-1, ".");
- };
- inline string& arr(int pos) {
- return itemarray[pos];
- }
- };
- //end items code
- //some important global stuff
- ItemsC items;
- bounds co;
- Vec2D board(co.x, co.y);
- //end important global stuff
- //print the board
- int show(i2d board) {
- int temp = 0;
- cout << endl;
- for (int a = 0; a < co.x; ++a) {
- for (int b = 0; b < co.y; ++b) {
- temp = board[a][b].ID;
- if (a == co.x-1 or b == co.y-1) {
- cout << "*"; //south/east boarder generation.
- }
- else if ((a == 0 or b == 0) or (a == co.x-1 or b == co.y-1)) {
- cout << "#"; //north/west boarder generation.
- }
- else if (temp <= items.asize) {
- cout << items.arr(temp); //print any Unicode representations in ItemsC.
- }
- else {
- cout << board[a][b].ID; //otherwise just print what's in the board vector.
- }
- //cout << " ";
- }
- cout << endl;
- }
- cout << endl;
- return 0;
- }
- //end printing the board
- //player stuff
- //ugh
- //x and y = Entity's X and Y co-ordinance.
- //pos = Direction to move to. Non-valid directions simply
- //return the collision value of the current tile.
- //ifCol = Whether to return collision or not. If this
- //is not set to "col", it will just return the ID of the
- //entity at the position this entity wants to go to.
- int getDir (int x, int y, string pos, string ifCol = "no") {
- if (pos == "up") {
- if (ifCol == "col") {
- return board.atcol(x-1,y);
- }
- else {
- return board.at(x-1,y);
- }
- }
- else if (pos == "down") {
- if (ifCol == "col") {
- return board.atcol(x+1,y);
- }
- else {
- return board.at(x+1,y);
- }
- }
- else if (pos == "left") {
- if (ifCol == "col") {
- return board.atcol(x,y-1);
- }
- else {
- return board.at(x,y-1);
- }
- }
- else if (pos == "right") {
- if (ifCol == "col") {
- return board.atcol(x,y+1);
- }
- else {
- return board.at(x,y+1);
- }
- }
- else {
- return board.atcol(x,y);
- }
- return 0;
- }
- //x and y = Entity's X and Y position.
- //xDir and yDir = Position to check before moving to it.
- //This function checks the collision value of the space
- //the entity using this function wants to move to.
- //Returns the collision value it gets.
- int colDet (int x, int y, int xDir, int yDir) {
- if (x == 1 and getDir(xDir,yDir,"down","col") > 0) {
- return board.atcol(xDir+1,yDir);
- }
- else if (x == -1 and getDir(xDir,yDir,"up","col") > 0) {
- return board.atcol(xDir-1,yDir);
- }
- else if (y == 1 and getDir(xDir,yDir,"right","col") > 0) {
- return board.atcol(xDir,yDir+1);
- }
- else if (y == -1 and getDir(xDir,yDir,"left","col") > 0) {
- return board.atcol(xDir,yDir-1);
- }
- else {
- }
- return 0;
- }
- class entity {
- public:
- int tempID = 0; //Movement stuff. Pay no mind.
- int tempCol = 0;
- int ID = 0;
- int Col = 0;
- int x = 0;
- int y = 0;
- int HP = 10;
- int Inv[20];
- //eID = Entity's ID. Give it a unique one!
- //exPos = X position to spawn at.
- //eyPos = Y position to spawn at.
- //eCol = Colision value to spawn with. Does not effect
- //the Escherichia coli status of the entity.
- entity (int eID, int exPos, int eyPos, int eCol = 1) {
- x = exPos;
- y = eyPos;
- ID = eID;
- Col = eCol;
- emov(0,0);
- };
- /*
- ~entity {
- };
- */
- //xPos = Position on the X co-ordinate to move in, by value of 1.
- //yPos = Same as above, for the Y co-ordinate.
- //This function actually moves an entity, but eamov is an abstraction
- //for this, so you shouldn't need it.
- int emov (int xPos = 0, int yPos = 0) {
- if (x <= 1 and xPos == -1) {
- return 1;
- }
- else if (y <= 1 and yPos == -1) {
- return 1;
- }
- else if (x >= co.x-2 and xPos == 1) {
- return 1;
- }
- else if (y >= co.y-2 and yPos == 1) {
- return 1;
- }
- else {
- if (colDet(xPos,yPos,x,y) == 1) {
- //cout << "Collision detection is working." << endl;
- return 1;
- }
- else {
- board.at(x,y) = tempID;
- board.atcol(x,y) = tempCol;
- tempID = board.at(x+xPos,y+yPos);
- tempCol = board.atcol(x+xPos,y+yPos);
- board.at(x+xPos,y+yPos) = ID;
- x += xPos;
- y += yPos;
- board.atcol(x,y) = Col;
- }
- }
- return 0;
- }
- //dir = Direction to move in.
- //Non-valid directions will not move the entity, but will update it.
- int eamov (string dir) {
- if (dir == "left" or dir == "a") {
- return emov(0, -1);
- }
- else if (dir == "right" or dir == "d" or dir == "g") {
- return emov(0, 1);
- }
- else if (dir == "up" or dir == "w" or dir == "r") {
- return emov(-1, 0);
- }
- else if (dir == "down" or dir == "s" or dir == "e") {
- return emov(1, 0);
- }
- else { //this is mainly for the player.
- return emov(0, 0);
- cout << "Invalid command." << endl;
- }
- return 0;
- }
- //lID = entity's ID to look for.
- //all = Look for any entity with an equal or higher lID.
- //agetCol = Check for collision value instead of an ID value.
- int anyoneThere (int lID = 1, int all = 0, string agetCol = "no") {
- string adarr[4] = {"up","down","left","right"};
- int retval = 0;
- int whchk = 0;
- while (whchk < 4) {
- if (all == 1) {
- if (getDir(x,y,adarr[whchk],agetCol) >= lID) {
- retval = whchk+1;
- }
- }
- else {
- if (getDir(x,y,adarr[whchk],agetCol) == lID) {
- retval = whchk+1;
- }
- }
- whchk += 1;
- }
- return retval;
- }
- //This function is autonomous thanks to all the functions
- //above. This simply checks for anything with collision
- //around it and collides against it.
- int movewith (void) {
- string mdarr[5] = {"none","down","up","right","left"};
- int atemp = anyoneThere(1,1,"col");
- eamov(mdarr[atemp]);
- return 0;
- }
- };
- class Player : public entity {
- public:
- Player (int eID, int exPos = 1, int eyPos = 1, int eCol = 1, string sitem = "?") : entity(eID, exPos, eyPos, eCol) {
- items.arr(eID) = sitem;
- };
- };
- class Death : public entity {
- public:
- string prev = "right";
- string dirarr[4] = {"right","up","left","down"};
- int dirnext = 0;
- int damn = 0;
- int UGH = 0;
- int derand = 0;
- Death (int eID, int exPos = 1, int eyPos = 1, int eCol = 1, string sitem = "?") : entity(eID, exPos, eyPos, eCol) {
- items.arr(eID) = sitem;
- };
- //Autonomous. Walks continuously in
- //one direction until it hits something,
- //the changes direction.
- int adhoc_ai() {
- if (UGH == 0) {
- damn = eamov(prev);
- if (damn == 1) {
- dirnext += 1;
- if (dirnext > 3) {
- dirnext = 0;
- }
- prev = dirarr[dirnext];
- }
- UGH = 1;
- }
- else if (UGH == 1) {
- UGH = 0;
- }
- return 0;
- }
- int talk (void) {
- if (anyoneThere(1) > 0) {
- cout << "Hello. I am Death, destroyer of worlds." << endl;
- return 1;
- }
- return 0;
- }
- };
- class Item : public entity {
- public:
- Item (int eID, int exPos = 1, int eyPos = 1, int eCol = 1, string sitem = "?") : entity(eID, exPos, eyPos, eCol) {
- items.arr(eID) = sitem;
- };
- int talk (void) {
- if (anyoneThere(1) > 0) {
- cout << "Hello there! I'm an item." << endl;
- return 1;
- }
- return 0;
- }
- };
- //pl = Entity to be affected by physical controls.
- //input = Input from the outside world to control the
- //entity being manipulated by this.
- //All this does is lets the player control something.
- int pcontrols(entity& pl, string input) {
- if (input == "end") {
- cout << "Thanks for listening." << endl;
- return -1;
- }
- else if (input == "skip") {
- return 1;
- }
- else if (input == "talk") {
- return 2;
- }
- else if (input == "pos") {
- cout << "X: " << pl.x << ", Y: " << pl.y << endl;
- cout << "The current collision tag is: " << board.atcol(pl.x,pl.y) << endl \
- << "The collision tag below is: " << board.atcol(pl.x+1,pl.y) << endl;
- }
- else {
- pl.eamov(input);
- }
- return 0;
- }
- //end player stuff
- int main(int argc, char **argv){
- cout << "Hi. Welcome to Elk v" << ver << " Alpha. After 9 days in development\n" \
- << "hopefully it will have been worth the wait. Please\n" \
- << "let me know what you think, after you've had a chance to play.\n" \
- << "Thanks, and have fun!\n";
- Player player(1,5,2,1,"@");
- Death death(4,3,6,1,"☺"); //2,6
- Item someItem(2,2,4,2,"&");
- Item moveItem(3,7,7,1,"%");
- Item tbl1(5,3,3,1); //tbl3 already tells the ID "5" to be a table leg!
- Item tbl2(6,3,4,1,"─");
- Item tbl3(5,3,5,1,"┬");
- //board.at(1,1) = 6;
- //board.at(2,3) = 7;
- //board.at(2,4) = 8;
- //board.at(2,5) = 7;
- //┬ ─
- string input;
- int retval = 0;
- for(;;) {
- show(board.vec);
- cout << "> ";
- cin >> input;
- moveItem.movewith();
- retval = pcontrols(player, input);
- if (retval == -1) {
- break;
- }
- //else if (retval == 1) {
- //death.adhoc_ai();
- //}
- else if (retval == 2) {
- if (someItem.talk() == 0 and death.talk() == 0) {
- cout << "No response." << endl;
- }
- }
- death.adhoc_ai();
- cin.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement