bool Bishop::movePossible(Cell* cell) { // vertical direction if (abs(this->cell->x() - cell->x()) == abs(this->cell->y() - cell->y())) { int vdir; if (cell->y() > this->cell->y()) vdir = 1; else vdir = -1; // horizontal direction int hdir; if (cell->x() > this->cell->x()) hdir = 1; else hdir = -1; // distance int dist = abs(this->cell->x() - cell->x()); for (int i = 1; i < dist; ++i) { if (!cellEmpty(this->cell->x() + hdir * i, this->cell->y() + vdir * i)) { return false; } } return true; } return false; }
void GameDisplay::mousePressEvent(QMouseEvent *click) { switch(plantType)//switch statement used to place a plant where you click { case 1://pea shooter { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&cellEmpty(click->x(),click->y()))//tracks where you click on the grid, then assigns the plant to be placed on a fixed coordinate for that cell { p = new Plants(1,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[0].restart(); emit disableButton(1); break; } } } grid[click->y()/100][click->x()/90]=false; break; } case 2: { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&cellEmpty(click->x(),click->y()))//tracks where you click on the grid, then assigns the plant to be placed on a fixed coordinate for that cell { p = new Plants(2,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[1].restart(); emit disableButton(2); break; } } } grid[click->y()/100][click->x()/90]=false; break; } case 3: { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&cellEmpty(click->x(),click->y()))//tracks where you click on the grid, then assigns the plant to be placed on a fixed coordinate for that cell { p = new Plants(3,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[2].restart(); emit disableButton(3); break; } } } grid[click->y()/100][click->x()/90]=false; break; } case 4: { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&cellEmpty(click->x(),click->y()))//tracks where you click on the grid, then assigns the plant to be placed on a fixed coordinate for that cell { p = new Plants(4,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[3].restart(); emit disableButton(4); break; } } } grid[click->y()/100][click->x()/90]=false; break; } case 5: { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&cellEmpty(click->x(),click->y()))//tracks where you click on the grid, then assigns the plant to be placed on a fixed coordinate for that cell { p = new Plants(5,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[4].restart(); emit disableButton(5); break; } } } grid[click->y()/100][click->x()/90]=false; break; } case 6: { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&cellEmpty(click->x(),click->y()))//tracks where you click on the grid, then assigns the plant to be placed on a fixed coordinate for that cell { p = new Plants(6,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[5].restart(); emit disableButton(6); break; } } } grid[click->y()/100][click->x()/90]=false; break; } case 7: { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&cellEmpty(click->x(),click->y()))//tracks where you click on the grid, then assigns the plant to be placed on a fixed coordinate for that cell { p = new Plants(7,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[6].restart(); emit disableButton(7); break; } } } grid[click->y()/100][click->x()/90]=false; break; } case 8: { for(int x=0;x<900;x+=90) { for(int y=0;y<500;y+=100) { for(int i=0;i<plantVector.size();i++) { if(click->x()>x&&click->x()<(x+90)&&click->y()>y&&click->y()<(y+100)&&plantVector[i]->getType()==1&&plantVector[i]->getX()==x&&plantVector[i]->getY()==y)//tracks if a peashooter has been placed on the grid. if you click the same spot, a repeater will be placed and the peashooter gets removed { plantVector[i]->setPosition(-1,-1); scene()->removeItem(plantVector[i]); p = new Plants(8,x,y); plantVector.push_back(p); plantType=0; subtractSunPoints(p->getCost()); scene()->addItem(p); cooldowns[7].restart(); emit disableButton(8); break; } } } } grid[click->y()/100][click->x()/90]=false; break; } } for(int i=0;i<sunVector.size();i++) { if(sunVector[i]->areaX(click->x())&&sunVector[i]->areaY(click->y()))//checks if you have clicked on a sun { sunVector[i]->setClicked();//moves the sun so you cant click on it again (if you dont do this, you wont see the sun but you can still get sun points) scene()->removeItem(sunVector[i]); addSunPoints(25); } } }