void Player::killPlayer() { if (fireReady == false) { delete reloadAnim; fireReady = true; } deleteTank(); // координата Х в центре экрана int x1 = centralX(); // координата Y в центре экрана int y1 = centralY(); // плашка смерти QString image1 = ":/images/images/died.png"; died = new Caution(image1, "DEAD"); died->setPos(x1-200,y1-88); game->scene->addItem(died); QTimer::singleShot(2000, this, SLOT(spawnPlayer())); }
void SwipeGestureRecognizer::onTouchMoved(const Touch *touch) { if (numTouches() < numTouchesRequired()) { return; } uint64_t deltaTime = touch->timestamp() - m_startTime; if (deltaTime == 0) { return; } if (state() != State::Possible) { return; } float prevCentralX = centralX(); float prevCentralY = centralY(); updateCentralPoint(); float deltaX = centralX() - prevCentralX; float deltaY = centralY() - prevCentralY; m_cumulativeDeltaX += deltaX; m_cumulativeDeltaY += deltaY; float avrgVelocityX = m_cumulativeDeltaX / deltaTime; float avrgVelocityY = m_cumulativeDeltaY / deltaTime; float cumulativeDeltaSquared = SQUARED_PYTHAGOREAN(m_cumulativeDeltaY, m_cumulativeDeltaX); float avrgVelocitySquared = SQUARED_PYTHAGOREAN(avrgVelocityY, avrgVelocityX); float thresholdSquared = SQUARED(recognitionThreshold()); float minDisplacementSquared = SQUARED(minDisplacement()); float minVelocitySquared = SQUARED(m_minVelocity); // qDebug() << m_minVelocity << " " << minDisplacement() << " " << maxDuration(); // qDebug() << sqrtf(avrgVelocitySquared) << " " << sqrtf(cumulativeDeltaSquared) << " " << deltaTime; if (m_noDirection) { if (cumulativeDeltaSquared >= thresholdSquared && avrgVelocitySquared >= minVelocitySquared && cumulativeDeltaSquared >= minDisplacementSquared) { setState(State::Recognized); } } else { float absVelocityX = fabsf(avrgVelocityX); float absVelocityY = fabsf(avrgVelocityY); if (absVelocityX > absVelocityY) { float absCumulativeDeltaX = fabsf(m_cumulativeDeltaX); if (absCumulativeDeltaX > recognitionThreshold()) { if ((deltaX < 0.0f && (m_direction & Left) == 0) || ((deltaX > 0.0f) && (m_direction & Right) == 0) || fabsf(atanf(m_cumulativeDeltaY /m_cumulativeDeltaX)) >= m_maxAngle) { setState(State::Failed); } else if (absVelocityX >= m_minVelocity && absCumulativeDeltaX >= minDisplacement()) { setState(State::Recognized); } } } else if (absVelocityY > absVelocityX) { float absCumulativeDeltaY = fabsf(m_cumulativeDeltaY); if (absCumulativeDeltaY > recognitionThreshold()) { if ((deltaY < 0.0f && (m_direction & Up) == 0) || ((deltaY > 0.0f) && (m_direction & Down) == 0) || fabsf(atanf(m_cumulativeDeltaX / m_cumulativeDeltaY)) >= m_maxAngle) { setState(State::Failed); } else if (absVelocityY >= m_minVelocity && absCumulativeDeltaY >= minDisplacement()) { setState(State::Recognized); } } } else if (cumulativeDeltaSquared > thresholdSquared) { setState(State::Failed); } } }
void Player::keyActions() // player действия при нажатии клавиши { game->centerOn(this); lastX = x(); lastY = y(); lastDeg = degree; lastHDeg = hdegree; lastHealth = health; // эффект получения урона (должен стоять перед движением) if (wou) { /* int x1 = centralX(); int y1 = centralY(); wound->setPos(x1-game->width()/2,y1-game->height()/2); */ } // движение вперед, назад if (mf) { moveForward(); if (game->createBots) *bot << "mf "; } if (mb) { moveBack(); if (game->createBots) *bot << "mb "; } // если player вышел за границы if (this->x() < game->dop - pixsize/2 || this->x() > game->scene->width()-game->dop*2 + 20 || this->y() < game->dop - pixsize/2 || this->y() > game->scene->height()-game->dop*2 + 20) { // координата Х в центре экрана int x1 = centralX(); // координата Y в центре экрана int y1 = centralY(); if (out == false) { out = true; outTime = QTime::currentTime(); outTimer = outMaxTime; QString image = ":/images/images/out.png"; caution = new Caution(image, "CAUTION", 5); caution->setPos(x1-200,y1-88); game->scene->addItem(caution); } caution->setPos(x1-200, y1-88); if (outTime.msecsTo(QTime::currentTime()) > 1000) { if (outTimer > 0) { outTimer--; outTime = outTime.addSecs(1); caution->decTimer(); } else { // DIE M**********R! //qDebug() << "you died"; out = false; outTimer = outMaxTime; delete caution; // убить killPlayer(); // BUHAHA } } } else { if (out == true) { out = false; outTimer = outMaxTime; delete caution; } } // универсальный поворот if (rl || rr) { playerRotate(); spin++; } // ты ничего не видел if (rl == false && rr == false) spin = 0; // совершенно ничего if (spin == 540) { QMediaPlayer *sound = new QMediaPlayer(); sound->setMedia(QUrl("qrc:/sounds/sounds/spin.mp3")); sound->play(); } // поворот башни if (hl) { headLeft(); if (game->createBots) *bot << "hl "; } if (hr) { headRight(); if (game->createBots) *bot << "hr "; } // звук поворота башни if ((hr || hl) && tankhrotate->state() == QMediaPlayer::StoppedState) // если звук поворота еще не проигрывался tankhrotate->play(); if (hr == false && hl == false && tankhrotate->state() == QMediaPlayer::PlayingState) tankhrotate->stop(); // стрельба + звуки стрельбы if (fr && fireReady) { playerFire(); reloading = false; // звук перезарядки еще на начат isFiring = 1; // анимация перезарядки reloadAnim = new QLabel(); reloadAnim->setStyleSheet("background-color: transparent;"); QMovie *movie = new QMovie(":/images/images/anim/Reload.gif"); reloadAnim->setMovie(movie); reloadAnim->move(x()+20,y()+20); movie->setScaledSize(QSize(180,180)); //movie->setSpeed(100); movie->start(); QGraphicsProxyWidget *proxy = game->scene->addWidget(reloadAnim); if (game->createBots) *bot << "fr "; } if (fireReady == false) // счетчик для ожидания между выстрелами { fireCount += keyDelay; reloadAnim->move(x()+20,y()+20); } if ((fireCount > fireTime - bulletready->duration()) && reloading == false) // звук перезарядки { reloading = true; // перезарядка уже начата (защита от нескольких) bulletready->play(); } if (fireCount > fireTime) // готовность выстрела { fireReady = true; fireCount = 0; delete reloadAnim; } if (game->createBots) *bot << endl; if (lastX != x() || lastY != y() || lastDeg != degree || lastHDeg != hdegree || lastHealth != health) emit KeyPressed(); if (alive == false || (mf == false && mb == false && rl == false && rr == false && hl == false && hr == false && fr == false && fireReady == true && tankhrotate->state() == QMediaPlayer::StoppedState && out == false)) { action = false; timer->stop(); } }