mirror of
https://github.com/BreizhHardware/Poulpes-de-l-Espace-La-derniere-ligne-de-Defense.git
synced 2026-01-18 16:27:20 +01:00
Add randomly generated map
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
//
|
||||
|
||||
#include "Game.h"
|
||||
#include <QGraphicsView>
|
||||
#include <iostream>
|
||||
|
||||
Game::Game(){
|
||||
@@ -12,7 +13,7 @@ Game::Game(){
|
||||
|
||||
void Game::start() {
|
||||
// Create the map
|
||||
gameMap.generateMap(1280, 720);
|
||||
gameMap.generateMap(25, 14);
|
||||
std::cout << "Map generated" << std::endl;
|
||||
|
||||
// Set the user gold
|
||||
@@ -32,4 +33,7 @@ void Game::show() {
|
||||
// Show the map
|
||||
gameMap.show();
|
||||
std::cout << "Map shown" << std::endl;
|
||||
|
||||
auto* view = new QGraphicsView(&gameMap);
|
||||
view->show();
|
||||
}
|
||||
72
src/Map.cpp
72
src/Map.cpp
@@ -3,15 +3,20 @@
|
||||
//
|
||||
|
||||
#include "Map.h"
|
||||
#include <QRandomGenerator>
|
||||
|
||||
Map::Map(QObject *parent) : QGraphicsScene(parent) {
|
||||
}
|
||||
|
||||
void Map::generateMap(int width, int height) {
|
||||
void Map::generateMap(const int width, const int height) {
|
||||
tiles = QVector<QVector<Tile*>>(height, QVector<Tile*>(width));
|
||||
int x = 0, y = height - 1;
|
||||
// Length of the path
|
||||
int length = 0;
|
||||
// Randomize path length within a range
|
||||
int minLength = 20;
|
||||
int maxLength = 50;
|
||||
int pathLength = QRandomGenerator::global()->bounded(minLength, maxLength + 1);
|
||||
|
||||
// Create a new Start tile
|
||||
Tile* startTile = new Tile(Tile::Start);
|
||||
@@ -20,54 +25,49 @@ void Map::generateMap(int width, int height) {
|
||||
addItem(startTile);
|
||||
length++;
|
||||
|
||||
while (length < 100){
|
||||
while (length < pathLength){
|
||||
// Randomly choose direction (horizontal or vertical)
|
||||
int direction = QRandomGenerator::global()->bounded(2);
|
||||
if (direction == 0) {
|
||||
// Move horizontally
|
||||
if (x < width - 1) {
|
||||
x++;
|
||||
}
|
||||
} else {
|
||||
// Move vertically
|
||||
if (y > 0) {
|
||||
y--;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new Road tile
|
||||
Tile* tile = new Tile(Tile::Road);
|
||||
tile->setRect(x * 50, y * 50, 50, 50);
|
||||
tiles[y][x] = tile;
|
||||
addItem(tile);
|
||||
length++;
|
||||
}
|
||||
|
||||
// Move horizontally to the right edge
|
||||
while (x < width -1 && length < 100){
|
||||
x++;
|
||||
Tile* tile = new Tile(Tile::Road);
|
||||
tile->setRect(x * 50, y * 50, 50, 50);
|
||||
tiles[y][x] = tile;
|
||||
addItem(tile);
|
||||
length++;
|
||||
}
|
||||
// Create a new End tile
|
||||
Tile* endTile = new Tile(Tile::End);
|
||||
endTile->setRect(x * 50, y * 50, 50, 50);
|
||||
tiles[y][x] = endTile;
|
||||
addItem(endTile);
|
||||
|
||||
// Move vertically to the top edge
|
||||
while (y > 0 && length < 100){
|
||||
y--;
|
||||
Tile* tile = new Tile(Tile::Road);
|
||||
tile->setRect(x * 50, y * 50, 50, 50);
|
||||
tiles[y][x] = tile;
|
||||
addItem(tile);
|
||||
length++;
|
||||
}
|
||||
|
||||
// Create a new End tile
|
||||
Tile* endTile = new Tile(Tile::End);
|
||||
endTile->setRect(x * 50, y * 50, 50, 50);
|
||||
tiles[y][x] = endTile;
|
||||
addItem(endTile);
|
||||
|
||||
// Fill the rest of the map with Other tiles
|
||||
for (int i = 0; i < height; i++){
|
||||
for (int j = 0; j < width; j++){
|
||||
if (tiles[i][j] == nullptr){
|
||||
Tile* tile = new Tile(Tile::Other);
|
||||
tile->setRect(j * 50, i * 50, 50, 50);
|
||||
tiles[i][j] = tile;
|
||||
addItem(tile);
|
||||
}
|
||||
// Fill the rest of the map with Other tiles
|
||||
for (int i = 0; i < height; i++){
|
||||
for (int j = 0; j < width; j++){
|
||||
if (tiles[i][j] == nullptr){
|
||||
Tile* tile = new Tile(Tile::Other);
|
||||
tile->setRect(j * 50, i * 50, 50, 50);
|
||||
tiles[i][j] = tile;
|
||||
addItem(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Map::show() {
|
||||
// Display the map in the main window
|
||||
setSceneRect(0, 0, 1280, 720);
|
||||
|
||||
Reference in New Issue
Block a user