Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include "Player.h"
- #include "Npc.h"
- #include "Stick.h"
- #include "Rock.h"
- #include "Wall.h"
- #include "Grass.h"
- #include <iostream>
- #include "time.h"
- using namespace std;
- void drawMap(int map[], int width, int height, Entity entities[], int numEntities)
- {
- for(int y=0; y<height; y++)
- {
- for(int x=0; x<width; x++)
- {
- cout << entities[map[x+width*y]].getSprite();
- }
- cout << endl;
- }
- }
- void drawScore(int playerScore, int npcScore)
- {
- cout << "Player Score: " << playerScore << " " << "Npc Score: " << npcScore << endl;
- }
- Character::directions getValidatedInput()
- {
- cout << "Please enter a direction:(east, west, north, south) ";
- string temp = "";
- getline(cin, temp);
- while(temp.compare("east") != 0 && temp.compare("west") != 0 &&
- temp.compare("north") != 0 && temp.compare("south") != 0)
- {
- cout << "Please enter a direction:(east, west, north, south) ";
- getline(cin, temp);
- }
- if(temp.compare("east") == 0) return Character::EAST;
- else if(temp.compare("west") == 0) return Character::WEST;
- else if(temp.compare("north") == 0) return Character::NORTH;
- else if(temp.compare("south") == 0) return Character::SOUTH;
- }
- bool checkForCollision(Entity a, Entity b)
- {
- bool collision = (a.getX() == b.getX()) && (a.getY() == b.getX());
- return collision;
- }
- void update()
- {
- }
- void waitForEnter()
- {
- while(1)
- {
- if('\n' == getchar())
- {
- break;
- }
- }
- }
- int main()
- {
- //define the sprites
- const string wallSprite = "#";
- const string grassSprite = ".";
- const string stickSprite = "-";
- const string rockSprite = "o";
- const string playerSprite = "@";
- const string npcSprite = "&";
- //define the entities
- Wall wall(0, 0, wallSprite);
- Grass grass(0, 0, grassSprite);
- Stick stick(0, 0, stickSprite);
- Rock rock(0, 0, rockSprite);
- Player player(7, 3, playerSprite, 0);
- Npc npc(3, 2, npcSprite, 0);
- //define user input direction enum and npc direction enum
- Character::directions playerDirection;
- Character::directions npcDirection;
- Entity entities[6] = {wall, grass, player, rock, stick, npc};
- //define the map
- //0 - Wall 1-Grass 2-Player 3-Rock 4-Stick 5-Npc
- int map[17*5] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 0,
- 0, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 0,
- 0, 1, 1, 1, 1, 3, 1, 2, 1, 3, 1, 1, 4, 1, 1, 1, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- //seed random generator for generating new stick coordinates
- srand(time(NULL));
- while(player.getScore() < 10)
- {
- //Draw the map
- drawMap(map, 17, 5, entities, 6);
- //Draw the score
- drawScore(player.getScore(), npc.getScore());
- //Get User Input and NPC AI
- playerDirection = getValidatedInput();
- npcDirection = npc.decideWhatToDo();
- //update based on user input
- player.move(playerDirection);
- //npc.move(npcDirection);
- //check for collisions
- //wall
- if(map[player.getX()+17*player.getY()] == 0)
- {
- player.undoMove(playerDirection);
- }
- //grass
- if(map[player.getX()+17*player.getY()] == 1)
- {
- //nothing not a collision??
- //update map to reflect player movement *super ugly!!
- map[player.getX() + 17*player.getY()] = 2;
- player.undoMove(playerDirection);
- map[player.getX() + 17*player.getY()] = 1;
- player.move(playerDirection);
- }
- //rock
- if(map[player.getX()+17*player.getY()] == 3)
- {
- player.undoMove(playerDirection);
- }
- //stick
- if(map[player.getX()+17*player.getY()] == 4)
- {
- stick.generateCoordinates(15, 3);
- while(map[stick.getX() + 17*stick.getY()] == 3)
- {
- stick.generateCoordinates(15, 3);
- }
- map[stick.getX() + 17*stick.getY()] = 4;
- player.increaseScore();
- map[player.getX() + 17*player.getY()] = 2;
- player.undoMove(playerDirection);
- map[player.getX() + 17*player.getY()] = 1;
- player.move(playerDirection);
- }
- //npc
- if(map[player.getX()+17*player.getY()] == 5)
- {
- player.undoMove(playerDirection);
- }
- //update based on npc "AI"
- npc.move(npcDirection);
- //npc.move(npcDirection);
- //check for collisions
- //wall
- if(map[npc.getX()+17*npc.getY()] == 0)
- {
- npc.undoMove(npcDirection);
- }
- //grass
- if(map[npc.getX()+17*npc.getY()] == 1)
- {
- //nothing not a collision??
- //update map to reflect player movement *super ugly!!
- map[npc.getX() + 17*npc.getY()] = 5;
- npc.undoMove(npcDirection);
- map[npc.getX() + 17*npc.getY()] = 1;
- npc.move(npcDirection);
- }
- //rock
- if(map[npc.getX()+17*npc.getY()] == 3)
- {
- npc.undoMove(npcDirection);
- }
- //stick
- if(map[player.getX()+17*player.getY()] == 4)
- {
- stick.generateCoordinates(15, 3);
- while(map[npc.getX() + 17*npc.getY()] == 3)
- {
- stick.generateCoordinates(15, 3);
- }
- map[stick.getX() + 17*stick.getY()] = 4;
- npc.increaseScore();
- map[npc.getX() + 17*npc.getY()] = 5;
- npc.undoMove(npcDirection);
- map[npc.getX() + 17*npc.getY()] = 1;
- npc.move(npcDirection);
- }
- //player
- if(map[npc.getX()+17*npc.getY()] == 2)
- {
- npc.undoMove(npcDirection);
- }
- }
- //Enter to exit
- cout << "Press Enter To Exit...";
- waitForEnter();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement