Files
aliendefender/mainwindow.cpp
2010-02-07 19:22:29 +03:00

128 lines
2.7 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);
//sndr = new UdpSender("127.0.0.1",10101);
//sndr->connectSend("hellow");
int cellsize = 16;
timer = new QTimer(this);
//adp = new adpainter(this->width(),this->height(),cellsize);
//adp->clear();
//adp->drawgrid();
connect(timer, SIGNAL(timeout()), this, SLOT(ADrender()));
GameMain = new games(this->size(),QPoint(0,this->height()/2),QPoint(this->width()-cellsize,this->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::ADrender()
{
//adp->drawAlien(aliens->alienAt(0));
maliens->drawAliens();
this->repaint();
}
void MainWindow::paintEvent(QPaintEvent*)
{
QPainter painter;
painter.begin(this);
painter.drawPixmap(0,0,*GameMain->buff);
painter.end();
}
void MainWindow::mouseMoveEvent (QMouseEvent *event)
{
if (mousebt == 1) ClearCell(event->pos());
if (mousebt == 2) SetCell(event->pos());
}
void MainWindow::mousePressEvent (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, (this->width()/GameMain->cellsize)*GameMain->cellsize,
(this->height()/GameMain->cellsize)*GameMain->cellsize);
if (wdrect.contains(pos)) {
cp.setX(pos.x()/GameMain->cellsize);
cp.setY(pos.y()/GameMain->cellsize);
mtowers->drawcell(cp);
if (GameMain->Cells[cp.x()][cp.y()] == 1) 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, (this->width()/GameMain->cellsize)*GameMain->cellsize,
(this->height()/GameMain->cellsize)*GameMain->cellsize);
if (wdrect.contains(pos)) {
cp.setX(pos.x()/GameMain->cellsize);
cp.setY(pos.y()/GameMain->cellsize);
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;
//delete adp;
}