void EnemyBase::runFollowPoint() { auto point = currPoint(); auto prevP = point; setPosition(point->getPosition()); point = nextPoint(); auto nextP = point; if (point != nullptr) { float distancePrevToNext; //计算2个点的距离差,保证匀速运动 if (abs(prevP->getPosition().x - nextP->getPosition().x) <= abs(prevP->getPosition().y - nextP->getPosition().y)) { distancePrevToNext = abs(prevP->getPosition().y - nextP->getPosition().y); } else { distancePrevToNext = abs(prevP->getPosition().x - nextP->getPosition().x); } float timeToMove = distancePrevToNext / (getRunSpeed()*20.0); runAction(Sequence::create(MoveTo::create(timeToMove, point->getPosition()) , CallFuncN::create(CC_CALLBACK_0(EnemyBase::runFollowPoint, this)) , NULL)); } else { //setEnemySuccessful(true); } }
void EnemyTest::changeDirection(float dt) { auto curr = currPoint(); if (curr->getPositionX() > this->getPosition().x) { sprite->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("monster1_right"))); //从cache里取出动画播放 } else{ sprite->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("monster1_left"))); } }
void EnemyBase::changeDirection(float dt) //子类要重写 { auto curr = currPoint(); if (curr->getPositionX() > this->getPosition().x) { runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runright"))); //从cache里取出动画播放 } else{ runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runleft"))); } }
void Thief::changeDirection(float dt) { auto curr = currPoint(); if(nullptr == curr) { return ; } if(curr->getPositionX() > _spriteBody->getPositionX()) { _spriteBody->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runright1"))); } else { _spriteBody->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runleft1"))); } }
void IntonationWidget::smoothPoints(QPainter& painter) { if (intonationPointList_.size() < 2U) return; for (unsigned int i = 0, end = intonationPointList_.size() - 1; i < end; ++i) { const auto& point1 = intonationPointList_[i]; const auto& point2 = intonationPointList_[i + 1]; double x1 = point1.absoluteTime(); double y1 = point1.semitone(); double m1 = point1.slope(); double x2 = point2.absoluteTime(); double y2 = point2.semitone(); double m2 = point2.slope(); double x12 = x1 * x1; double x13 = x12 * x1; double x22 = x2 * x2; double x23 = x22 * x2; double denominator = x2 - x1; denominator = denominator * denominator * denominator; double d = (-(y2 * x13) + 3.0 * y2 * x12 * x2 + m2 * x13 * x2 + m1 * x12 * x22 - m2 * x12 * x22 - 3.0 * x1 * y1 * x22 - m1 * x1 * x23 + y1 * x23) / denominator; double c = (-(m2 * x13) - 6.0 * y2 * x1 * x2 - 2.0 * m1 * x12 * x2 - m2 * x12 * x2 + 6.0 * x1 * y1 * x2 + m1 * x1 * x22 + 2.0 * m2 * x1 * x22 + m1 * x23) / denominator; double b = (3.0 * y2 * x1 + m1 * x12 + 2.0 * m2 * x12 - 3.0 * x1 * y1 + 3.0 * x2 * y2 + m1 * x1 * x2 - m2 * x1 * x2 - 3.0 * y1 * x2 - 2.0 * m1 * x22 - m2 * x22) / denominator; double a = (-2.0 * y2 - m1 * x1 - m2 * x1 + 2.0 * y1 + m1 * x2 + m2 * x2) / denominator; QPointF prevPoint(0.5 + timeToX(x1), 0.5 + valueToY(y1)); for (unsigned int j = static_cast<unsigned int>(x1); j <= static_cast<unsigned int>(x2); j += SMOOTH_POINTS_X_INCREMENT) { double x = j; double y = x * (x * (x * a + b) + c) + d; QPointF currPoint(0.5 + timeToX(x), 0.5 + valueToY(y)); painter.drawLine(prevPoint, currPoint); prevPoint = currPoint; } QPointF lastPoint(0.5 + timeToX(x2), 0.5 + valueToY(y2)); painter.drawLine(prevPoint, lastPoint); } }
void Thief::changeDirection(float dt) { hpBgSprite->setPosition(Point(sprite->getContentSize().width / 2, sprite->getContentSize().height +15 )+sprite->getPosition()); auto curr = currPoint(); if( curr == NULL ) { return; } if(curr->getPositionX() > sprite->getPosition().x ) { sprite->setRotation3D(Vec3(0,-90,0)); }else{ sprite->setRotation3D(Vec3(0,90,0)); } }
// Note: with no antialiasing, the coordinates in QPointF are rounded to the nearest integer. void IntonationWidget::paintEvent(QPaintEvent*) { if (eventList_ == nullptr || eventList_->list().empty()) { return; } QPainter painter(this); painter.setFont(QFont("monospace")); if (modelUpdated_) { QFontMetrics fm = painter.fontMetrics(); textYOffset_ = 0.5 * fm.ascent(); leftMargin_ = MARGIN + TEXT_MARGIN + fm.width(yLabels[0]); modelUpdated_ = false; } totalWidth_ = std::ceil(leftMargin_ + graphWidth_ + 6.0 * MARGIN); if (totalWidth_ < MININUM_WIDTH) { totalWidth_ = MININUM_WIDTH; } totalHeight_ = std::ceil(2.0 * MARGIN + 3.0 * TRACK_HEIGHT + 15.0 * yStep_); if (totalHeight_ < MININUM_HEIGHT) { totalHeight_ = MININUM_HEIGHT; } setMinimumWidth(totalWidth_); setMinimumHeight(totalHeight_); double xStart = timeToX(0.0); double yStart = valueToY(MIN_VALUE); double xEnd = timeToX(maxTime_); double yEnd = valueToY(MAX_VALUE); // Y labels. for (unsigned int i = 0; i < yLabels.size(); ++i) { double y = MARGIN + (i + 3) * yStep_ + textYOffset_; painter.drawText(QPointF(MARGIN, y), yLabels[i]); } // Horizontal lines. painter.setPen(Qt::lightGray); for (int i = 1; i < 15; ++i) { double y = MARGIN + (i + 3) * yStep_; painter.drawLine(QPointF(xStart, y), QPointF(xEnd, y)); } painter.setPen(Qt::black); postureTimeList_.clear(); double yPosture = MARGIN + 3.0 * TRACK_HEIGHT - 0.5 * (TRACK_HEIGHT - 1.0) + textYOffset_; unsigned int postureIndex = 0; for (const TRMControlModel::Event_ptr& ev : eventList_->list()) { double x = timeToX(ev->time); if (ev->flag) { postureTimeList_.push_back(ev->time); const TRMControlModel::Posture* posture = eventList_->getPostureAtIndex(postureIndex++); if (posture) { painter.setPen(Qt::black); // Posture name. painter.drawText(QPointF(x, yPosture), posture->name().c_str()); } painter.setPen(Qt::lightGray); // Event vertical line. painter.drawLine(QPointF(x, yStart), QPointF(x, yEnd)); } } painter.setPen(Qt::black); // Frame. painter.drawRect(QRectF(QPointF(xStart, yStart), QPointF(xEnd, yEnd))); double yRuleText = MARGIN + TRACK_HEIGHT - 0.5 * (TRACK_HEIGHT - 1.0) + textYOffset_; double yRuleText2 = yRuleText + TRACK_HEIGHT; for (int i = 0; i < eventList_->numberOfRules(); ++i) { auto* ruleData = eventList_->getRuleAtIndex(i); if (ruleData) { unsigned int firstPosture = ruleData->firstPosture; unsigned int lastPosture = ruleData->lastPosture; int postureTime1, postureTime2; if (firstPosture < postureTimeList_.size()) { postureTime1 = postureTimeList_[firstPosture]; } else { postureTime1 = 0; // invalid } if (lastPosture < postureTimeList_.size()) { postureTime2 = postureTimeList_[lastPosture]; } else { postureTime2 = postureTime1 + ruleData->duration; } // Rule frame. painter.drawRect(QRectF( QPointF(timeToX(postureTime1), MARGIN), QPointF(timeToX(postureTime2), MARGIN + 2.0 * TRACK_HEIGHT))); // Rule number. painter.drawText(QPointF(timeToX(postureTime1) + TEXT_MARGIN, yRuleText), QString::number(ruleData->number)); // Rule duration. painter.drawText(QPointF(timeToX(postureTime1) + TEXT_MARGIN, yRuleText2), QString::number(ruleData->duration, 'f', 0)); } } QPen pen; QPen pen2; pen2.setWidth(2); painter.setRenderHint(QPainter::Antialiasing); painter.setBrush(QBrush(Qt::black)); // Lines between intonation points. QPointF prevPoint(0.5 + xStart, 0.5 + yStart); for (unsigned int i = 0, size = intonationPointList_.size(); i < size; ++i) { QPointF currPoint( 0.5 + timeToX(intonationPointList_[i].absoluteTime()), 0.5 + valueToY(intonationPointList_[i].semitone())); painter.setPen(pen2); painter.drawLine(prevPoint, currPoint); painter.setPen(pen); drawPointMarker(painter, 0.5 + currPoint.x(), 0.5 + currPoint.y()); prevPoint = currPoint; } painter.setBrush(QBrush()); painter.setPen(pen2); smoothPoints(painter); painter.setPen(pen); painter.setRenderHint(QPainter::Antialiasing, false); // Point selection. if (selectedPoint_ >= 0) { double x = timeToX(intonationPointList_[selectedPoint_].absoluteTime()); double y = valueToY(intonationPointList_[selectedPoint_].semitone()); painter.drawRect(QRectF( QPointF(x - SELECTION_SIZE, y - SELECTION_SIZE), QPointF(x + SELECTION_SIZE, y + SELECTION_SIZE))); } // Beat lines. for (unsigned int i = 0, size = intonationPointList_.size(); i < size; ++i) { double x = timeToX(intonationPointList_[i].beatTime()); painter.drawLine(QPointF(x, yStart), QPointF(x, yEnd)); } // Time scale. double yTick1 = yStart + TIME_DIVISION_MARK_SIZE; double yTick2 = yStart + 2.0 * TIME_DIVISION_MARK_SIZE; double yTimeText = yTick2 + 0.5 * TRACK_HEIGHT + textYOffset_; for (int time = 0, end = static_cast<int>(maxTime_); time <= end; time += 10) { double x = timeToX(time); if (time % 100 == 0) { painter.drawLine(QPointF(x, yStart), QPointF(x, yTick2)); painter.drawText(QPointF(x, yTimeText), QString::number(time)); } else { painter.drawLine(QPointF(x, yStart), QPointF(x, yTick1)); } } }