Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "windows.h"
- #include "time.h"
- #include "Tiles.h"
- using namespace std;
- static CTiles TILES;
- //Game Declarations
- bool running = true;
- const int FPS = 6;
- //Map Declarations
- const int mapWidth = 10;
- const int mapHeight = 10;
- CTiles::TILE_OBJECT map[mapWidth][mapHeight];
- //Player Declarations
- CTiles::TILE_OBJECT currentTile;
- CTiles::TILE_OBJECT startingTile = TILES.SAND;
- int posX = 0;
- int posY = 0;
- //Functions
- void generateMap();
- void printMap();
- void gameLoop();
- void getInput();
- void playerMove(char dir);
- void updateMap(int nextX, int nextY);
- void plantSeed();
- int main()
- {
- //map seed
- srand(time(NULL));
- double seed = rand();
- generateMap();
- printMap();
- gameLoop();
- system("CLS");
- cout << "Game Over. Press Enter to Quit";
- cin.ignore();
- cin.get();
- return 0;
- }
- void generateMap()
- {
- //referenced this http://breinygames.blogspot.co.uk/2012/06/generating-terrain-using-perlin-noise.html
- //use this for tile graphics http://www.codeproject.com/Articles/106884/Implementing-Auto-tiling-Functionality-in-a-Tile-M
- //flood map
- for(int y = 0; y < mapHeight; y++)
- {
- for(int x = 0; x < mapWidth; x++)
- {
- map[x][y] = TILES.WATER;
- }
- }
- //generateNoise(seed);
- //grass island
- for(int y = 1; y < mapHeight-1; y++)
- {
- for(int x = 1; x < mapWidth-1; x++)
- {
- map[x][y] = TILES.GRASS;
- }
- }
- //random sand
- for(int i = 0; i < 30; i++)
- {
- int x = rand() % (mapWidth - 2) + 1;
- int y = rand() % (mapHeight - 2) + 1;
- map[x][y] = TILES.SAND;
- }
- do
- {
- posX = rand() % (mapWidth - 1) + 1;
- posY = rand() % (mapHeight - 1) + 1;
- }
- while(map[posX][posY] != startingTile);
- currentTile = startingTile;
- map[posX][posY] = TILES.PLAYER;
- }
- void printMap()
- {
- for(int y = 0; y < mapHeight; y++)
- {
- for(int x = 0; x < mapWidth; x++)
- {
- cout << map[x][y];
- }
- cout << endl;
- }
- }
- void gameLoop()
- {
- while(running)
- {
- getInput();
- Sleep(1000/FPS);
- }
- }
- void getInput()
- {
- if(GetAsyncKeyState(VK_ESCAPE))
- {
- running = false;
- }
- if(GetAsyncKeyState(VK_SPACE))
- {
- plantSeed();
- }
- if(GetAsyncKeyState(VK_UP))
- {
- playerMove('N');
- }
- if(GetAsyncKeyState(VK_RIGHT))
- {
- playerMove('E');
- }
- if(GetAsyncKeyState(VK_DOWN))
- {
- playerMove('S');
- }
- if(GetAsyncKeyState(VK_LEFT))
- {
- playerMove('W');
- }
- }
- void playerMove(char dir)
- {
- int nextX = posX;
- int nextY = posY;
- if(dir == 'N')
- {
- nextY -= 1;
- }
- if(dir == 'E')
- {
- nextX += 1;
- }
- if(dir == 'S')
- {
- nextY += 1;
- }
- if(dir == 'W')
- {
- nextX -= 1;
- }
- if(nextX >= 0 && nextX < mapWidth &&
- nextY >= 0 && nextY < mapHeight)
- {
- if(map[nextX][nextY] == TILES.SAND
- || map[nextX][nextY] == TILES.GRASS
- || map[nextX][nextY] == TILES.FOREST
- || map[nextX][nextY] == TILES.ROCK
- || map[nextX][nextY] == TILES.SWAMP)
- {
- map[posX][posY] = currentTile;
- currentTile = map[nextX][nextY];
- map[nextX][nextY] = TILES.PLAYER;
- updateMap(nextX, nextY);
- posX = nextX;
- posY = nextY;
- }
- }
- }
- void updateMap(int nextX, int nextY)
- {
- COORD coord;
- coord.X = posX;
- coord.Y = posY;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- cout << map[coord.X][coord.Y];
- coord.X = nextX;
- coord.Y = nextY;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- cout << map[coord.X][coord.Y];
- //move cursor to end of map
- coord.X = 0;
- coord.Y = mapHeight;
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ),coord);
- }
- void plantSeed()
- {
- if(currentTile == TILES.SAND
- || currentTile == TILES.GRASS
- || currentTile == TILES.FOREST
- || currentTile == TILES.ROCK)
- {
- bool plantable = true;
- for(int y = -2; y <= 2; y++)
- {
- for(int x = -2; x <= 2; x++)
- {
- if(map[posX + x][posY + y] == TILES.SEED /*|| map[x][y] == building*/)
- {
- plantable = false;
- }
- }
- }
- if(plantable == true)
- {
- currentTile = TILES.SEED;
- }
- }
- }
- /*double Noise(int x, int y)
- {
- int n = x + y * 57;
- n = (n<<13) ^ n;
- return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 7) / 1073741824.0);
- }
- double SmoothNoise_1(float x, float y)
- {
- float corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16;
- float sides = ( Noise(x-1, y) +Noise(x+1, y) +Noise(x, y-1) +Noise(x, y+1) ) / 8;
- float center = Noise(x, y) / 4;
- return corners + sides + center;
- }*/
- /*
- double InterpolatedNoise_1(float x, float y)
- {
- integer_X = int(x);
- fractional_X = x - integer_X;
- integer_Y = int(y);
- fractional_Y = y - integer_Y;
- v1 = SmoothedNoise1(integer_X, integer_Y);
- v2 = SmoothedNoise1(integer_X + 1, integer_Y);
- v3 = SmoothedNoise1(integer_X, integer_Y + 1);
- v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1);
- i1 = Interpolate(v1 , v2 , fractional_X);
- i2 = Interpolate(v3 , v4 , fractional_X);
- return Interpolate(i1 , i2 , fractional_Y);
- }
- double PerlinNoise_2D(float x, float y)
- {
- int total = 0;
- int p = persistence;
- int n = Number_Of_Octaves - 1;
- for(int i = 0; i < n; i++)
- {
- frequency = 2i;
- amplitude = pi;
- total = total + InterpolatedNoisei(x * frequency, y * frequency) * amplitude;
- }
- return total;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment