Files
aliendefender/mainwindow.cpp
2010-02-09 10:14:09 +03:00

175 lines
3.9 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
qDebug() << "Load START!";
ui->setupUi(this);
wd = new USWidget(ui->frGame);
//ui->frGame->addWidget(wd);
wd->resize(ui->frGame->size());
connect(wd,SIGNAL(paintEvent(QPaintEvent*)),this,SLOT(paint(QPaintEvent*)));
connect(wd,SIGNAL(mouseMoveEvent(QMouseEvent*)),
this,SLOT(mouse_move(QMouseEvent*)));
connect(wd,SIGNAL(mousePressEvent(QMouseEvent*)),
this,SLOT(mouse_press(QMouseEvent*)));
//sndr = new UdpSender("127.0.0.1",10101);
//sndr->connectSend("hellow");
int cellsize = 16;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(ADrender()));
qDebug() << wd->size();
GameMain = new games(wd->size(),QPoint(0,wd->height()/2),
QPoint(wd->width()-cellsize,wd->height()/2),
cellsize);
maliens = new Aliens(GameMain);
maliens->AddAlien();
mmap = new map(GameMain);
mmap->clear();
mmap->drawgrid();
mtowers = new towers(GameMain);
//this->setGeometry(100,100,this->width(),this->height());
timer->start(50);
mousebt = 0;
qDebug() << "Load DONE!";
}
void MainWindow::resizeEvent(QResizeEvent *)
{
int cellsize = 16;
wd->resize(ui->frGame->size());
delete mtowers;
delete mmap;
delete maliens;
delete GameMain;
GameMain = new games(wd->size(),QPoint(0,wd->height()/2),
QPoint(wd->width()-cellsize,wd->height()/2),
cellsize);
maliens = new Aliens(GameMain);
maliens->AddAlien();
mmap = new map(GameMain);
mmap->clear();
mmap->drawgrid();
mtowers = new towers(GameMain);
}
void MainWindow::ADrender()
{
QString str;
maliens->drawAliens();
str.setNum(maliens->size());
ui->lbCount->setText(QString::fromUtf8("Количество: ")+str+QString::fromUtf8(" шт"));
wd->repaint();
}
void MainWindow::paint(QPaintEvent* )
{
QPainter painter;
painter.begin(wd);
painter.drawPixmap(0,0,*GameMain->buff);
painter.end();
}
void MainWindow::mouse_move(QMouseEvent *event)
{
if (mousebt == 1) ClearCell(event->pos());
if (mousebt == 2) SetCell(event->pos());
}
void MainWindow::mouse_press(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
ClearCell(event->pos());
mousebt = 1;
return;
}
if(event->button() == Qt::LeftButton) {
SetCell(event->pos());
mousebt = 2;
return;
}
mousebt = 0;
}
void MainWindow::SetCell(QPoint pos)
{
bool trace = false;
QPoint cp;
QRect wdrect(0, 0, (wd->width()/GameMain->cellsize)*GameMain->cellsize,
(wd->height()/GameMain->cellsize)*GameMain->cellsize);
if (wdrect.contains(pos)) {
cp.setX(pos.x()/GameMain->cellsize);
cp.setY(pos.y()/GameMain->cellsize);
//qDebug() << cp << GameMain->Cells[cp.x()][cp.y()];
if (GameMain->Cells[cp.x()][cp.y()] == 10) return;
mtowers->drawcell(cp);
if (GameMain->Cells[cp.x()][cp.y()] == 5) trace = true;
GameMain->Cells[cp.x()][cp.y()] = - 1;
if (trace)
if (!maliens->retrace()) ClearCell(pos);
}
}
void MainWindow::ClearCell(QPoint pos)
{
QPoint cp;
QRect wdrect(0, 0, (wd->width()/GameMain->cellsize)*GameMain->cellsize,
(wd->height()/GameMain->cellsize)*GameMain->cellsize);
if (wdrect.contains(pos)) {
cp.setX(pos.x()/GameMain->cellsize);
cp.setY(pos.y()/GameMain->cellsize);
if (GameMain->Cells[cp.x()][cp.y()] == 10) return;
GameMain->Cells[cp.x()][cp.y()] = 0;
mtowers->clearcell(cp);
maliens->retrace();
}
}
void MainWindow::keyPressEvent(QKeyEvent * e)
{
//qDebug() << e->key();
switch(e->key()) {
case 65:
maliens->AddAlien();
break;
case 68:
if (maliens->size() > 0) maliens->DelAlien(maliens->size()-1);
break;
}
}
MainWindow::~MainWindow()
{
delete ui;
delete mtowers;
delete mmap;
delete maliens;
delete GameMain;
}
void MainWindow::on_pushButton_clicked()
{
this->close();
}
void MainWindow::on_pushButton_3_clicked()
{
if (maliens->size() > 0) maliens->DelAlien(maliens->size()-1);
}
void MainWindow::on_pushButton_2_clicked()
{
maliens->AddAlien();
}