Commit 550805fc by grlabu

Upload New File

parent f561c095
Showing with 269 additions and 0 deletions
#include "mainwindow.h"
#include "gamescreen.h"
#include "qdebug.h"
#include "ui_mainwindow.h"
#include "Header.h"
#include <QFile>
#include <QMessageBox>
#include <QRandomGenerator>
#include <iostream>
#include "gameengine.h"
#include "bossdefeated.h"
#include "gameover.h"
#include "bossfightend.h"
MainWindow::MainWindow(QWidget *parent, Player *player, int iDefeatedEnemies)
: QMainWindow(parent),
ui(new Ui::MainWindow),
defeatedEnemies(0),
specialAttack(0)
{
ui->setupUi(this);
// Funktsioon mängija ja vaenlase initsialiseerimiseks
if(player) {
defeatedEnemies = iDefeatedEnemies;
createPlayer(player);
} else {
createPlayer();
}
createEnemy();
ui->playerHPdisplay->setText(QString("Mängija HP: %1").arg(p->health));
ui->jookArvdisplay->setText(QString("Jooke alles: %1").arg(p->potionCount));
// Kuva vaenlase tervist
ui->VastaseHPdisplay->setText(QString("Vaenlase HP: %1").arg(enemy.health));
// Tervitusnachricht
std::cout << "Tere tulemast mängu!\n";
gameScreen = new gamescreen(this);
connect(gameScreen, &gamescreen::onGameFinished, this, [this] {
defeatedEnemies++;
levelUp();
createEnemy();
this->show();
gameScreen->hide();
});
connect(gameScreen, &gamescreen::onTrap, this, [this] {
p->health -= LOSE_HP;
ui->playerHPdisplay->setText(QString("Mängija HP: %1").arg(p->health));
});
connect(gameScreen, &gamescreen::onPotionGain, this, [this] {
p->potionCount++;
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createEnemy()
{
// Call the CreateEnemy function with the defeated enemies count
enemy = CreateEnemy(defeatedEnemies);
displayOutput(QString("Sa seisad silmitsi vaenlasega: %1!").arg(enemy.type));
// Display enemy health in UI or perform other actions
ui->VastaseHPdisplay->setText(QString("Vaenlase HP: %1").arg(enemy.health));
}
void MainWindow::on_nupp_rynda_clicked()
{
// Kuva vaenlase tüüp
if(defeatedEnemies == 5 && enemy.health <= BOSS_HEALTH && specialAttack == 0) {
bossFightLastPhase();
} else {
int playerAttack = rand() % p->attack + 1;
int enemyAttack = rand() % enemy.attack + 1;
if (playerAttack > enemyAttack)
{
int damageDealt = playerAttack;
enemy.health -= damageDealt;
ui->VastaseHPdisplay->setText(QString("Vaenlase HP: %1").arg(enemy.health)); // Uuenda vaenlase tervisenäidikut
if (enemy.health > 0)
{
displayOutput(QString("Mängija ründas. Vaenlase tervis: %1").arg(enemy.health));
}
else
{
displayOutput("Mängija võitis vaenlase!");
if(defeatedEnemies == 5) {
BossDefeated *bd = new BossDefeated();
bd->show();
return;
} else {
switchToGameScreen(); // Pärast vaenlase võitmist lülita mänguekraanile
}
}
}
else if (playerAttack < enemyAttack)
{
int damageDealt = enemyAttack;
p->health -= damageDealt;
ui->playerHPdisplay->setText(QString("Mängija HP: %1").arg(p->health)); // Uuenda mängija tervisenäidikut
if (p->health > 0)
{
displayOutput(QString("Vaenlane ründas. Mängija tervis: %1").arg(p->health));
}
else
{
ui->nupp_rynda->setDisabled(true);
ui->nupp_jook->setDisabled(true);
ui->nupp_pogene->setDisabled(true);
GameOver *gos = new GameOver();
gos->show();
this->close();
displayOutput("Vaenlane võitis mängija!");
// Rakenda loogika mängu lõpu või mängu ülemineku jaoks
}
}
else
{
displayOutput("Mõlemad rünnakud olid võrdsed. Kedagi ei tabatud.");
}
}
}
void MainWindow::on_nupp_pogene_clicked()
{
// Kuva vaenlase tüüp
displayOutput(QString("Sa põgenesid vaenlase %1 eest!").arg(enemy.type));
// int SaveGame(char *file, Player *p, int enemiesDefeated);
saveGame();
// Uuenda vaenlase tervisenäidikut pärast põgenemist (valikuline)
ui->VastaseHPdisplay->setText(QString("Vaenlase HP: %1").arg(enemy.health));
}
// Rakenda funktsioon
void MainWindow::on_nupp_jook_clicked()
{
if (p->potionCount > 0) // Kontrolli, kas mängijal on jooke alles
{
// Kasuta jooki ja uuenda mängija tervist
p->health += POTION;
p->potionCount--; // Vähenda jookide arvu
ui->jookArvdisplay->setText(QString("Jooke alles: %1").arg(p->potionCount)); // Uuenda jookide arvu näidikut
ui->playerHPdisplay->setText(QString("Mängija HP: %1").arg(p->health)); // Uuenda mängija tervisenäidikut
displayOutput("Kasutasid jooki ja taastasid oma tervise!"); // Kuva sõnum
}
else
{
displayOutput("Sul ei ole jooke alles!"); // Kuva sõnum, kui jooke ei ole alles
}
}
void MainWindow::switchToGameScreen()
{
gameScreen->show(); // Kuvab gamescreen'i
this->hide();
// this->close(); // Suleb praeguse akna
}
void MainWindow::displayOutput(const QString &output)
{
ui->Outputtxt->setText(output);
}
void MainWindow::saveGame()
{
//create file
if (!QFile::exists(MainWindow_NS::GAME_SAVES_FILENAME))
{
QFile newFile(MainWindow_NS::GAME_SAVES_FILENAME);
if (!newFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Failed to create file:" << newFile.errorString();
return;
}
newFile.close();
}
// clear file
QFile file(MainWindow_NS::GAME_SAVES_FILENAME);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
{
qDebug() << "Failed to open file for clearing:" << file.errorString();
return;
}
file.close();
if (!file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
{
qDebug() << "Failed to open file for writing:" << file.errorString();
return;
}
// Create a QTextStream object to write to the file
QTextStream out(&file);
// Write the data to the file
out << p->health << " " << p->attack << " " << p->potionCount << " " << defeatedEnemies << "\n";
file.close();
}
void MainWindow::createPlayer() {
// Generate player with health and potion count
p = new Player();
p->health = PLAYER_BASE_HEALTH;
p->attack = PLAYER_BASE_DAMAGE;
p->potionCount = POT_COUNT;
}
void MainWindow::createPlayer(Player *player) {
// Generate player with health and potion count
p = new Player();
p->health = player->health;
p->attack = player->attack;
p->potionCount = player->potionCount;
}
void MainWindow::levelUp()
{
p->health += LEVEL_UP_HP; // Add 20 HP to the player
ui->playerHPdisplay->setText(QString("Mängija HP: %1").arg(p->health));
p->attack += LEVEL_UP_ATTACK; // Add 5 attack power to the player
p->potionCount += 1; // Add 1 potion to the player
ui->jookArvdisplay->setText(QString("Jooke alles: %1").arg(p->potionCount));
}
int MainWindow::usePotion(Player* p, int currentHealth) {
currentHealth += POTION;
if (currentHealth > p->health) {
currentHealth = p->health;
}
return currentHealth;
}
void MainWindow::bossFightLastPhase()
{
BossFightEnd *bfe = new BossFightEnd();
bfe->show();
connect(bfe, &QDialog::finished, this, [this](int result) {
qDebug()<<"dialog result: " << result;
int correctChoice = QRandomGenerator::global()->bounded(2);
if(result == correctChoice) {
displayOutput(QString("Juht ohustab napilt sinu kätt oma mõõgaga ja sa pääsed puutumata.\n"
"Sinu valik päästis su elu.\n"));
} else {
displayOutput(QString("Juht tabab sind kõvasti oma mõõgaga rangluusse.\n"
"Karjud meeletust valust."));
p->health -= SPECIAL_DAMAGE;
ui->playerHPdisplay->setText(QString("Mängija HP: %1").arg(p->health));
displayOutput(QString("Tabas sind enda löögiga" + QString::number(SPECIAL_DAMAGE) + " ja vottid sinult nii palju elusid ära (alles on " + QString::number(p->health) + " HP)"));
}
});
specialAttack++;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment