Willium_Bob_Cole

Profitulation V0.02

Oct 24th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. #include <iostream>
  2. #include "windows.h"
  3. #include "time.h"
  4.  
  5. #include "Tiles.h"
  6.  
  7.  
  8. using namespace std;
  9.  
  10. static CTiles TILES;
  11.  
  12.  
  13. //Game Declarations
  14. bool running = true;
  15.  
  16. const int FPS = 6;
  17.  
  18.  
  19. //Map Declarations
  20. const int mapWidth = 10;
  21. const int mapHeight = 10;
  22.  
  23. CTiles::TILE_OBJECT map[mapWidth][mapHeight];
  24.  
  25.  
  26. //Player Declarations
  27. CTiles::TILE_OBJECT currentTile;
  28. CTiles::TILE_OBJECT startingTile = TILES.SAND;
  29. int posX = 0;
  30. int posY = 0;
  31.  
  32.  
  33. //Functions
  34. void generateMap();
  35. void printMap();
  36. void gameLoop();
  37. void getInput();
  38. void playerMove(char dir);
  39. void updateMap(int nextX, int nextY);
  40. void plantSeed();
  41.  
  42.  
  43. int main()
  44. {
  45.     //map seed
  46.     srand(time(NULL));
  47.  
  48.     double seed = rand();
  49.  
  50.     generateMap();
  51.     printMap();
  52.  
  53.     gameLoop();
  54.  
  55.     system("CLS");
  56.     cout << "Game Over. Press Enter to Quit";
  57.  
  58.     cin.ignore();
  59.     cin.get();
  60.     return 0;
  61. }
  62.  
  63.  
  64. void generateMap()
  65. {
  66.     //referenced this http://breinygames.blogspot.co.uk/2012/06/generating-terrain-using-perlin-noise.html
  67.     //use this for tile graphics http://www.codeproject.com/Articles/106884/Implementing-Auto-tiling-Functionality-in-a-Tile-M
  68.  
  69.  
  70.     //flood map
  71.     for(int y = 0; y < mapHeight; y++)
  72.     {
  73.         for(int x = 0; x < mapWidth; x++)
  74.         {
  75.             map[x][y] = TILES.WATER;
  76.         }
  77.     }
  78.  
  79.     //generateNoise(seed);
  80.  
  81.     //grass island
  82.     for(int y = 1; y < mapHeight-1; y++)
  83.     {
  84.         for(int x = 1; x < mapWidth-1; x++)
  85.         {
  86.             map[x][y] = TILES.GRASS;
  87.         }
  88.     }
  89.  
  90.     //random sand
  91.     for(int i = 0; i < 30; i++)
  92.     {
  93.         int x = rand() % (mapWidth - 2) + 1;
  94.         int y = rand() % (mapHeight - 2) + 1;
  95.  
  96.         map[x][y] = TILES.SAND;
  97.     }
  98.  
  99.     do
  100.     {
  101.         posX = rand() % (mapWidth - 1) + 1;
  102.         posY = rand() % (mapHeight - 1) + 1;
  103.     }
  104.     while(map[posX][posY] != startingTile);
  105.  
  106.     currentTile = startingTile;
  107.  
  108.     map[posX][posY] = TILES.PLAYER;
  109. }
  110.  
  111.  
  112. void printMap()
  113. {
  114.     for(int y = 0; y < mapHeight; y++)
  115.     {
  116.         for(int x = 0; x < mapWidth; x++)
  117.         {
  118.             cout << map[x][y];
  119.         }
  120.  
  121.         cout << endl;
  122.     }
  123. }
  124.  
  125.  
  126. void gameLoop()
  127. {
  128.     while(running)
  129.     {
  130.         getInput();
  131.  
  132.         Sleep(1000/FPS);
  133.     }
  134. }
  135.  
  136.  
  137. void getInput()
  138. {
  139.     if(GetAsyncKeyState(VK_ESCAPE))
  140.     {
  141.         running = false;
  142.     }
  143.  
  144.     if(GetAsyncKeyState(VK_SPACE))
  145.     {
  146.         plantSeed();
  147.     }
  148.  
  149.     if(GetAsyncKeyState(VK_UP))
  150.     {
  151.         playerMove('N');
  152.     }
  153.  
  154.     if(GetAsyncKeyState(VK_RIGHT))
  155.     {
  156.         playerMove('E');
  157.     }
  158.  
  159.     if(GetAsyncKeyState(VK_DOWN))
  160.     {
  161.         playerMove('S');
  162.     }
  163.  
  164.     if(GetAsyncKeyState(VK_LEFT))
  165.     {
  166.         playerMove('W');
  167.     }
  168. }
  169.  
  170.  
  171. void playerMove(char dir)
  172. {
  173.     int nextX = posX;
  174.     int nextY = posY;
  175.  
  176.  
  177.     if(dir == 'N')
  178.     {
  179.         nextY -= 1;
  180.     }
  181.  
  182.     if(dir == 'E')
  183.     {
  184.         nextX += 1;
  185.     }
  186.  
  187.     if(dir == 'S')
  188.     {
  189.         nextY += 1;
  190.     }
  191.  
  192.     if(dir == 'W')
  193.     {
  194.         nextX -= 1;
  195.     }
  196.  
  197.     if(nextX >= 0 && nextX < mapWidth &&
  198.         nextY >= 0 && nextY < mapHeight)
  199.     {
  200.         if(map[nextX][nextY] == TILES.SAND
  201.         || map[nextX][nextY] == TILES.GRASS
  202.         || map[nextX][nextY] == TILES.FOREST
  203.         || map[nextX][nextY] == TILES.ROCK
  204.         || map[nextX][nextY] == TILES.SWAMP)
  205.         {
  206.             map[posX][posY] = currentTile;
  207.             currentTile = map[nextX][nextY];
  208.             map[nextX][nextY] = TILES.PLAYER;
  209.  
  210.             updateMap(nextX, nextY);
  211.  
  212.             posX = nextX;
  213.             posY = nextY;
  214.         }
  215.     }
  216. }
  217.  
  218.  
  219. void updateMap(int nextX, int nextY)
  220. {
  221.     COORD coord;
  222.  
  223.     coord.X = posX;
  224.     coord.Y = posY;
  225.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  226.  
  227.     cout << map[coord.X][coord.Y];
  228.  
  229.     coord.X = nextX;
  230.     coord.Y = nextY;
  231.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  232.  
  233.     cout << map[coord.X][coord.Y];
  234.  
  235.     //move cursor to end of map
  236.     coord.X = 0;
  237.     coord.Y = mapHeight;
  238.     SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
  239. }
  240.  
  241.  
  242. void plantSeed()
  243. {
  244.     if(currentTile == TILES.SAND
  245.     || currentTile == TILES.GRASS
  246.     || currentTile == TILES.FOREST
  247.     || currentTile == TILES.ROCK)
  248.     {
  249.         bool plantable = true;
  250.  
  251.         for(int y = -2; y <= 2; y++)
  252.         {
  253.             for(int x = -2; x <= 2; x++)
  254.             {
  255.                 if(map[posX + x][posY + y] == TILES.SEED /*|| map[x][y] == building*/)
  256.                 {
  257.                     plantable = false;
  258.                 }
  259.             }
  260.         }
  261.  
  262.  
  263.  
  264.         if(plantable == true)
  265.         {
  266.             currentTile = TILES.SEED;
  267.         }
  268.     }
  269. }
  270.  
  271.  
  272.  
  273.  
  274. /*double Noise(int x, int y)
  275. {
  276.     int n = x + y * 57;
  277.     n = (n<<13) ^ n;
  278.     return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 7) / 1073741824.0);    
  279. }
  280.  
  281.  
  282. double SmoothNoise_1(float x, float y)
  283. {
  284.     float corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16;
  285.     float sides   = ( Noise(x-1, y)  +Noise(x+1, y)  +Noise(x, y-1)  +Noise(x, y+1) ) /  8;
  286.     float center  =  Noise(x, y) / 4;
  287.     return corners + sides + center;
  288. }*/
  289.  
  290. /*
  291. double InterpolatedNoise_1(float x, float y)
  292. {
  293.  
  294.       integer_X    = int(x);
  295.       fractional_X = x - integer_X;
  296.  
  297.       integer_Y    = int(y);
  298.       fractional_Y = y - integer_Y;
  299.  
  300.       v1 = SmoothedNoise1(integer_X,     integer_Y);
  301.       v2 = SmoothedNoise1(integer_X + 1, integer_Y);
  302.       v3 = SmoothedNoise1(integer_X,     integer_Y + 1);
  303.       v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1);
  304.  
  305.       i1 = Interpolate(v1 , v2 , fractional_X);
  306.       i2 = Interpolate(v3 , v4 , fractional_X);
  307.  
  308.       return Interpolate(i1 , i2 , fractional_Y);
  309. }
  310.  
  311.  
  312. double PerlinNoise_2D(float x, float y)
  313. {
  314.  
  315.       int total = 0;
  316.       int p = persistence;
  317.       int n = Number_Of_Octaves - 1;
  318.  
  319.       for(int i = 0; i < n; i++)
  320.       {
  321.  
  322.           frequency = 2i;
  323.           amplitude = pi;
  324.  
  325.           total = total + InterpolatedNoisei(x * frequency, y * frequency) * amplitude;
  326.       }
  327.  
  328.       return total;
  329. }*/
Advertisement
Add Comment
Please, Sign In to add comment