Field::Field(QGraphicsScene *scene) //: QObject() { m_scene = scene; m_animating = false; m_pathItems.fill(NULL, W * H); ResourceLoader *loader = ResourceLoader::instance(); for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { QGraphicsObject *item = new SvgRendererItem(loader->tile()); item->setPos(i * CELL_SIZE, j * CELL_SIZE); #ifdef QT_DEBUG //if (j % 2 && i % 2) // item->setScale(0.5); #endif item->setZValue(0); m_scene->addItem(item); item = new SvgRendererItem(loader->pathmarker()); item->setPos(i * CELL_SIZE, j * CELL_SIZE); item->setZValue(2); item->setVisible(false); m_scene->addItem(item); setVectorAt(m_pathItems, i, j, item); } } m_sel = QPoint(-1, -1); m_selItem = new SvgRendererItem(loader->select()); m_selItem->setZValue(1); m_selItem->setVisible(false); m_scene->addItem(m_selItem); m_field.fill(0, W * H); m_fieldItems.fill(NULL, W * H); m_next.fill({ QPoint(-1, -1), 0, NULL, NULL }, 3); }
void Field::throwBalls(int num, bool directly) { QVector<QPoint> free; free.fill(QPoint(0, 0), W * H); int nFree = 0; for (int i = 0; i < W; i++) for (int j = 0; j < H; j++) if (at(i, j) == 0) { free[nFree] = QPoint(i, j); nFree++; } #ifdef QT_DEBUG for (int i = 0; i < W * H; i++) { //qDebug("%d, %d", free[i].x(), free[i].y()); } #endif //return; QList<SuggestedBall> ballsToThrow; for (int n = 0; n < num; n++) { if (nFree > 0) { int at = qrand() % nFree; ballsToThrow.append({ free[at], qrand() % COLORS + 1, NULL, NULL }); free.removeAt(at); nFree--; } else ballsToThrow.append({ QPoint(-1, -1), qrand() % COLORS + 1, NULL, NULL }); } if (directly) { // This is needed on startup only, therefore do nothing except placing balls foreach (auto ball, ballsToThrow) { setAt(ball.pt, ball.color); QGraphicsObject *item = new SvgRendererItem(ResourceLoader::instance()->ball(ball.color)); item->setPos(ball.pt * CELL_SIZE); item->setZValue(3); m_scene->addItem(item); setVectorAt(m_fieldItems, ball.pt, item); } }
//! [10] Robot::Robot(QGraphicsItem *parent) : RobotPart(parent) { setFlag(ItemHasNoContents); QGraphicsObject *torsoItem = new RobotTorso(this); QGraphicsObject *headItem = new RobotHead(torsoItem); QGraphicsObject *upperLeftArmItem = new RobotLimb(torsoItem); QGraphicsObject *lowerLeftArmItem = new RobotLimb(upperLeftArmItem); QGraphicsObject *upperRightArmItem = new RobotLimb(torsoItem); QGraphicsObject *lowerRightArmItem = new RobotLimb(upperRightArmItem); QGraphicsObject *upperRightLegItem = new RobotLimb(torsoItem); QGraphicsObject *lowerRightLegItem = new RobotLimb(upperRightLegItem); QGraphicsObject *upperLeftLegItem = new RobotLimb(torsoItem); QGraphicsObject *lowerLeftLegItem = new RobotLimb(upperLeftLegItem); //! [10] //! [11] headItem->setPos(0, -18); upperLeftArmItem->setPos(-15, -10); lowerLeftArmItem->setPos(30, 0); upperRightArmItem->setPos(15, -10); lowerRightArmItem->setPos(30, 0); upperRightLegItem->setPos(10, 32); lowerRightLegItem->setPos(30, 0); upperLeftLegItem->setPos(-10, 32); lowerLeftLegItem->setPos(30, 0); //! [11] //! [12] QParallelAnimationGroup *animation = new QParallelAnimationGroup(this); QPropertyAnimation *headAnimation = new QPropertyAnimation(headItem, "rotation"); headAnimation->setStartValue(20); headAnimation->setEndValue(-20); QPropertyAnimation *headScaleAnimation = new QPropertyAnimation(headItem, "scale"); headScaleAnimation->setEndValue(1.1); animation->addAnimation(headAnimation); animation->addAnimation(headScaleAnimation); //! [12] QPropertyAnimation *upperLeftArmAnimation = new QPropertyAnimation(upperLeftArmItem, "rotation"); upperLeftArmAnimation->setStartValue(190); upperLeftArmAnimation->setEndValue(180); animation->addAnimation(upperLeftArmAnimation); QPropertyAnimation *lowerLeftArmAnimation = new QPropertyAnimation(lowerLeftArmItem, "rotation"); lowerLeftArmAnimation->setStartValue(50); lowerLeftArmAnimation->setEndValue(10); animation->addAnimation(lowerLeftArmAnimation); QPropertyAnimation *upperRightArmAnimation = new QPropertyAnimation(upperRightArmItem, "rotation"); upperRightArmAnimation->setStartValue(300); upperRightArmAnimation->setEndValue(310); animation->addAnimation(upperRightArmAnimation); QPropertyAnimation *lowerRightArmAnimation = new QPropertyAnimation(lowerRightArmItem, "rotation"); lowerRightArmAnimation->setStartValue(0); lowerRightArmAnimation->setEndValue(-70); animation->addAnimation(lowerRightArmAnimation); QPropertyAnimation *upperLeftLegAnimation = new QPropertyAnimation(upperLeftLegItem, "rotation"); upperLeftLegAnimation->setStartValue(150); upperLeftLegAnimation->setEndValue(80); animation->addAnimation(upperLeftLegAnimation); QPropertyAnimation *lowerLeftLegAnimation = new QPropertyAnimation(lowerLeftLegItem, "rotation"); lowerLeftLegAnimation->setStartValue(70); lowerLeftLegAnimation->setEndValue(10); animation->addAnimation(lowerLeftLegAnimation); QPropertyAnimation *upperRightLegAnimation = new QPropertyAnimation(upperRightLegItem, "rotation"); upperRightLegAnimation->setStartValue(40); upperRightLegAnimation->setEndValue(120); animation->addAnimation(upperRightLegAnimation); QPropertyAnimation *lowerRightLegAnimation = new QPropertyAnimation(lowerRightLegItem, "rotation"); lowerRightLegAnimation->setStartValue(10); lowerRightLegAnimation->setEndValue(50); animation->addAnimation(lowerRightLegAnimation); QPropertyAnimation *torsoAnimation = new QPropertyAnimation(torsoItem, "rotation"); torsoAnimation->setStartValue(5); torsoAnimation->setEndValue(-20); animation->addAnimation(torsoAnimation); //! [13] for (int i = 0; i < animation->animationCount(); ++i) { QPropertyAnimation *anim = qobject_cast<QPropertyAnimation *>(animation->animationAt(i)); anim->setEasingCurve(QEasingCurve::SineCurve); anim->setDuration(2000); } animation->setLoopCount(-1); animation->start(); //! [13] }