Skip to content

Commit

Permalink
More bug fix + new default texture + some basic map + disable play bu…
Browse files Browse the repository at this point in the history
…tton for now
  • Loading branch information
DenisD3D committed Mar 16, 2024
1 parent 91812fa commit 6c6f5eb
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 19 deletions.
Binary file modified images/ground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
Binary file removed images/wall.bmp
Binary file not shown.
Binary file added images/wall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added maps/default_1.skm
Binary file not shown.
Binary file added maps/default_2.skm
Binary file not shown.
Binary file added maps/default_3.skm
Binary file not shown.
Binary file removed maps/map1.skm
Binary file not shown.
Binary file removed maps/map2.skm
Binary file not shown.
Binary file removed maps/map3.skm
Binary file not shown.
6 changes: 3 additions & 3 deletions resources.qrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<RCC>
<qresource prefix="/">
<file>images/body.png</file>
<file>images/head.png</file>
<file>images/sneak_body.png</file>
<file>images/sneak_head.png</file>
<file>images/ground.png</file>
<file>images/wall.bmp</file>
<file>images/wall.png</file>
<file>images/apple.png</file>
</qresource>
</RCC>
8 changes: 4 additions & 4 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Map::Map() {

Map::Map(const QString &absolute_file_path, const bool create_map): file(absolute_file_path) {
default_tile = "ground";
apple_texture = QPixmap(":/images/apple.png");
snake_head_texture.load(":/images/head.png");
snake_body_texture.load(":/images/body.png");
apple_texture.load(":/images/apple.png");
snake_head_texture.load(":/images/sneak_head.png");
snake_body_texture.load(":/images/sneak_body.png");

// Load default tiles types
types["ground"] = {GROUND, QPixmap(":/images/ground.png"), true};
types["wall"] = {WALL, QPixmap(":/images/wall.bmp"), true};
types["wall"] = {WALL, QPixmap(":/images/wall.png"), true};

if (create_map) {
tiles = new TileType *[width * height];
Expand Down
30 changes: 24 additions & 6 deletions src/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,37 @@ class Map {
void setAuthor(const QString &a) { author = a; }
void setInitDirection(const Direction d) { init_direction = d; }

void setSnakeHeadTexture(const QPixmap &texture) {
void setSnakeHeadTexture(const QPixmap *texture) {
if (texture == nullptr) {
has_custom_snake_head_texture = false;
snake_head_texture.load(":/images/sneak_head.png");
return;
}

has_custom_snake_head_texture = true;
snake_head_texture = texture;
snake_head_texture = *texture;
}

void setSnakeBodyTexture(const QPixmap &texture) {
void setSnakeBodyTexture(const QPixmap *texture) {
if (texture == nullptr) {
has_custom_snake_body_texture = false;
snake_body_texture.load(":/images/sneak_body.png");
return;
}

has_custom_snake_body_texture = true;
snake_body_texture = texture;
snake_body_texture = *texture;
}

void setAppleTexture(const QPixmap &texture) {
void setAppleTexture(const QPixmap *texture) {
if (texture == nullptr) {
has_custom_apple_texture = false;
apple_texture.load(":/images/apple.png");
return;
}

has_custom_apple_texture = true;
apple_texture = texture;
apple_texture = *texture;
}

void setDefaultTile(const QString &type) { default_tile = type; }
Expand Down
39 changes: 35 additions & 4 deletions src/screens/editorscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ EditorScreen::EditorScreen(const QString &file_info, const bool create_map, QWid
customTextureTable->verticalHeader()->hide();
customTextureTable->setSelectionMode(QAbstractItemView::NoSelection);
customTextureTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
customTextureTable->setContextMenuPolicy(Qt::CustomContextMenu);
customTextureTable->setFocusPolicy(Qt::NoFocus);
customTextureTable->setMinimumWidth(150);
customTextureTable->setMaximumHeight(150);
Expand All @@ -237,7 +238,7 @@ EditorScreen::EditorScreen(const QString &file_info, const bool create_map, QWid
appleTexture->setIcon(QIcon(map.getAppleTexture()));
customTextureTable->setItem(2, 0, appleTexture);

connect(customTextureTable, &CustomTableWidget::doubleClicked,
connect(customTextureTable, &QTableWidget::doubleClicked,
[this, customTextureTable](const QModelIndex &index) {
const QString texturePath = QFileDialog::getOpenFileName(this, "Open Image", "",
"Image Files (*.png *.jpg *.bmp)");
Expand All @@ -250,13 +251,13 @@ EditorScreen::EditorScreen(const QString &file_info, const bool create_map, QWid

switch (index.row()) {
case 0:
map.setSnakeHeadTexture(newTexture);
map.setSnakeHeadTexture(&newTexture);
break;
case 1:
map.setSnakeBodyTexture(newTexture);
map.setSnakeBodyTexture(&newTexture);
break;
case 2:
map.setAppleTexture(newTexture);
map.setAppleTexture(&newTexture);
break;
default:
break;
Expand All @@ -266,6 +267,36 @@ EditorScreen::EditorScreen(const QString &file_info, const bool create_map, QWid
item->setIcon(QIcon(newTexture));
gameArea->update();
});
connect(customTextureTable, &QTableWidget::customContextMenuRequested,
[this, customTextureTable](const QPoint &pos) {
QMenu contextMenu;

const auto action = new QAction("Reset", this);
connect(action, &QAction::triggered, [this, pos, customTextureTable] {
auto *item = customTextureTable->itemAt(pos);
switch (customTextureTable->row(item)) {
case 0:
map.setSnakeHeadTexture(nullptr);
item->setIcon(QIcon(map.getSnakeHeadTexture()));
break;
case 1:
map.setSnakeBodyTexture(nullptr);
item->setIcon(QIcon(map.getSnakeBodyTexture()));
break;
case 2:
map.setAppleTexture(nullptr);
item->setIcon(QIcon(map.getAppleTexture()));
break;
default:
break;
}

gameArea->update();
});
contextMenu.addAction(action);

contextMenu.exec(customTextureTable->mapToGlobal(pos));
});

auto *dafaultTypeLabel = new QLabel("Default Tile Type");
for (auto it = types.begin(); it != types.end(); ++it) {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/gamescreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GameScreen::GameScreen(QWidget *parent, const QString &file_info): QWidget(paren
gameArea = new GameArea(jeu.getMap(), jeu.getSnake(), jeu.getApplePos(), false, this);

auto *layout = new QVBoxLayout;
auto *mapNameLabel = new QLabel(jeu.getMap().getAuthor() + " by " + jeu.getMap().getAuthor(), this);
auto *mapNameLabel = new QLabel(jeu.getMap().getName() + " by " + jeu.getMap().getAuthor(), this);
auto *scoreLabel = new QLabel("Score: 0", this);

layout->addWidget(mapNameLabel);
Expand Down
1 change: 1 addition & 0 deletions src/screens/mainmenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MainMenu final : public QWidget {
const auto layout = new QVBoxLayout(this);

const auto startButton = new QPushButton("Start Game", this);
startButton->setDisabled(true);
const auto exitButton = new QPushButton("Exit", this);

layout->addWidget(startButton);
Expand Down
2 changes: 1 addition & 1 deletion src/screens/snakewindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ SnakeWindow::SnakeWindow(QWidget *pParent, Qt::WindowFlags flags)
connect(openMapPlayAction, &QAction::triggered, this, [this] {
const auto fileName = QFileDialog::getOpenFileName(this, tr("Open Map"), QString(), tr("Map Files (*.skm)"));
if (!fileName.isEmpty()) {
std::cout << "Opening map: " << fileName.toStdString() << std::endl;
qDebug() << "Opening map: " << fileName;
gameScreen = new GameScreen(this, fileName);
stackedWidget->addWidget(gameScreen);
stackedWidget->setCurrentWidget(gameScreen);
Expand Down

0 comments on commit 6c6f5eb

Please sign in to comment.