bool StelMovementMgr::handleMouseMoves(int x, int y, Qt::MouseButtons) { // Turn if the mouse is at the edge of the screen unless config asks otherwise if (flagEnableMoveAtScreenEdge) { if (x <= 1) { turnLeft(1); isMouseMovingHoriz = true; } else if (x >= core->getProjection2d()->getViewportWidth() - 2) { turnRight(1); isMouseMovingHoriz = true; } else if (isMouseMovingHoriz) { turnLeft(0); isMouseMovingHoriz = false; } if (y <= 1) { turnUp(1); isMouseMovingVert = true; } else if (y >= core->getProjection2d()->getViewportHeight() - 2) { turnDown(1); isMouseMovingVert = true; } else if (isMouseMovingVert) { turnUp(0); isMouseMovingVert = false; } } // We can hardly use the mouse exactly enough to go to the zenith/pole. Any mouse motion can safely reset the simplified up vector. setViewUpVector(Vec3d(0., 0., 1.)); if (isDragging && flagEnableMouseNavigation) { if (hasDragged || (std::sqrt((float)((x-previousX)*(x-previousX) +(y-previousY)*(y-previousY)))>dragTriggerDistance)) { hasDragged = true; setFlagTracking(false); dragView(previousX, previousY, x, y); previousX = x; previousY = y; return true; } } return false; }
bool Character::moveUp(bool isDirFixed){ if (!isDirFixed) { turnUp(); } if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_UP)){ _destPosition.y += 32; return true; } else{ /*switch (_type){ case (int)Type::HAMSTER: return false; break; case (int)Type::MASTER: _destPosition.y += 32; do{ if (moveUp() == true){ break; } } while (1); break; }*/ return false; } }
void MainWindow::createMenus() { exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); startAct = new QAction(tr("Start animation"), this); startAct->setShortcut(tr("Ctrl+p")); connect(startAct, SIGNAL(triggered()), this, SLOT(startAnimation())); stopAct = new QAction(tr("St&op animation"), this); stopAct->setShortcut(tr("Ctrl+l")); connect(stopAct, SIGNAL(triggered()), this, SLOT(stopAnimation())); toggleF = new QAction(tr("Toggle flying"), this); toggleF->setShortcut(tr("Ctrl+t")); connect(toggleF, SIGNAL(triggered()), this, SLOT(toggleFly())); turnL = new QAction(tr("Turn left"), this); turnL->setShortcut(tr("Ctrl+a")); connect(turnL, SIGNAL(triggered()), this, SLOT(turnLeft())); turnR = new QAction(tr("Turn right"), this); turnR->setShortcut(tr("Ctrl+d")); connect(turnR, SIGNAL(triggered()), this, SLOT(turnRight())); turnU = new QAction(tr("Turn up"), this); turnU->setShortcut(tr("Ctrl+w")); connect(turnU, SIGNAL(triggered()), this, SLOT(turnUp())); turnD = new QAction(tr("Turn down"), this); turnD->setShortcut(tr("Ctrl+s")); connect(turnD, SIGNAL(triggered()), this, SLOT(turnDown())); speedU = new QAction(tr("Speed up"), this); speedU->setShortcut(tr("Ctrl+i")); connect(speedU, SIGNAL(triggered()), this, SLOT(speedUp())); speedD = new QAction(tr("Speed down"), this); speedD->setShortcut(tr("Ctrl+k")); connect(speedD, SIGNAL(triggered()), this, SLOT(speedDown())); fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(exitAct); animationMenu = menuBar()->addMenu(tr("&Animation")); animationMenu->addAction(startAct); animationMenu->addAction(stopAct); flyingMenu = menuBar()->addMenu(tr("&Flying")); flyingMenu->addAction(toggleF); flyingMenu->addAction(turnL); flyingMenu->addAction(turnR); flyingMenu->addAction(turnU); flyingMenu->addAction(turnD); flyingMenu->addAction(speedU); flyingMenu->addAction(speedD); }
void Character::turnToward(Character* aim){ int offX = aim->getPositionX() - this->getPositionX(); int offY = aim->getPositionY() - this->getPositionY(); if (abs(offY) >= abs(offX)){ if (offY < 0){ turnDown(); } else if (offY > 0){ turnUp(); } } else{ if (offX < 0){ turnLeft(); } else if (offX > 0){ turnRight(); } } }
//数组里面是tilePos void ShowPlayerJump::play(const vector<CCPoint>& vecPath) { if (vecPath.size() == 0) return; // find tile path NDTileMap* tilemap = NDTileMap::findMe(); if (!tilemap) return; // find tile player NDTilePlayer* tilePlayer = tilemap->getTilePlayer(); if (!tilePlayer) return; // reset fglayer zorder this->FgLayerZOrder_reset(); // get player dir & pos PLAYER_DIR playerDir = tilePlayer->getDir(); CCPoint playerPos = tilePlayer->getTilePos(); // push commands with path nodes for (int i = 0; i < vecPath.size(); i++) { const CCPoint& kDestPosition = vecPath[i]; PLAYER_DIR eNewDir = calcDir(playerPos, kDestPosition); float playTurnDuration = GStr.getFloat("player_turn_duration", 0.5f); float playJumpDuration = GStr.getFloat("player_jump_duration", 0.5f); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) playTurnDuration = TMax<float>(playTurnDuration, 0.3f); playJumpDuration = TMax<float>(playJumpDuration, 0.3f); #endif // turn if (playerDir != eNewDir) { if (eNewDir == E_DIR_LEFT) turnLeft(playTurnDuration, tilePlayer); else if (eNewDir == E_DIR_RIGHT) turnRight(playTurnDuration, tilePlayer); else if (eNewDir == E_DIR_UP) turnUp(playTurnDuration, tilePlayer); else if (eNewDir == E_DIR_DOWN) turnDown(playTurnDuration, tilePlayer); } // jump playSound(); jump(playJumpDuration, tilePlayer, kDestPosition, eNewDir); // idle idle(0.05); // update player pos & dir playerPos = kDestPosition; playerDir = eNewDir; } // save last tile pos for notify lastTilePos = vecPath.back(); }
void StelMovementMgr::handleKeys(QKeyEvent* event) { if (event->type() == QEvent::KeyPress) { // Direction and zoom deplacements switch (event->key()) { case Qt::Key_Left: turnLeft(true); break; case Qt::Key_Right: turnRight(true); break; case Qt::Key_Up: if (event->modifiers().testFlag(Qt::ControlModifier)){ zoomIn(true); } else { turnUp(true); } break; case Qt::Key_Down: if (event->modifiers().testFlag(Qt::ControlModifier)) { zoomOut(true); } else { turnDown(true); } break; case Qt::Key_PageUp: zoomIn(true); break; case Qt::Key_PageDown: zoomOut(true); break; case Qt::Key_Shift: moveSlow(true); break; case Qt::Key_Space: if (event->modifiers().testFlag(Qt::ControlModifier)) setDragTimeMode(true); break; default: return; } } else { // When a deplacement key is released stop moving switch (event->key()) { case Qt::Key_Left: turnLeft(false); break; case Qt::Key_Right: turnRight(false); break; case Qt::Key_Up: zoomIn(false); turnUp(false); break; case Qt::Key_Down: zoomOut(false); turnDown(false); break; case Qt::Key_PageUp: zoomIn(false); break; case Qt::Key_PageDown: zoomOut(false); break; case Qt::Key_Shift: moveSlow(false); break; case Qt::Key_Control: // This can be all that is seen for anything with control, so stop them all. // This is true for 4.8.1 turnRight(false); turnLeft(false); zoomIn(false); zoomOut(false); turnDown(false); turnUp(false); setDragTimeMode(false); break; default: return; } } event->accept(); }
void Predator::updateAI(){ if (!isMoving()){ _moveTrigger = 0; if (((Hamster *)CharacterManager::getInstance()->_player)->_caught == true \ ||GameManager::getInstance()->_caughtNum >=7){ return; } Hamster * nearestSurvivedHamster = nullptr; int nearestDistanceSq=0; for (int i = 0 ;i < CharacterManager::getInstance()->_hamsters.size(); i++){ if (CharacterManager::getInstance()->_hamsters.at(i) != nullptr\ && ((Hamster *)CharacterManager::getInstance()->_hamsters.at(i))->_caught == false\ && CharacterManager::getInstance()->_hamsters.at(i)->_state != (int)Hamster::State::LOSE){ int offTileX = (int)CharacterManager::getInstance()->_hamsters.at(i)->getPositionX() - (int)this->getPositionX(); int offTileY = (int)CharacterManager::getInstance()->_hamsters.at(i)->getPositionY() - (int)this->getPositionY(); if (nearestSurvivedHamster == nullptr){ nearestSurvivedHamster = (Hamster *)CharacterManager::getInstance()->_hamsters.at(i); nearestDistanceSq = offTileX*offTileX + offTileY*offTileY; } else{ if (offTileX*offTileX + offTileY*offTileY<nearestDistanceSq){ nearestSurvivedHamster = (Hamster *)CharacterManager::getInstance()->_hamsters.at(i); nearestDistanceSq = offTileX*offTileX + offTileY*offTileY; } /*if (((Hamster *)CharacterManager::getInstance()->_hamsters.at(i))->_ranking \ >nearestSurvivedHamster->_ranking){ nearestSurvivedHamster = (Hamster *)CharacterManager::getInstance()->_hamsters.at(i); }*/ } } } if (nearestSurvivedHamster != nullptr){ if (nearestSurvivedHamster->_caught == false\ && nearestSurvivedHamster->_state != (int)Hamster::State::LOSE) { int offTileX = (int)nearestSurvivedHamster->getPositionX() / 32 - (int)this->getPositionX() / 32; int offTileY = (int)nearestSurvivedHamster->getPositionY() / 32 - (int)this->getPositionY() / 32; if (offTileY > 0){ if (offTileY <= _vision){ if (offTileX<0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_LEFT)){ _moveTrigger = CHARACTER_DIRECTION_LEFT; } } else if (offTileX > 0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_RIGHT)) { _moveTrigger = CHARACTER_DIRECTION_RIGHT; } } else if (offTileX == 0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_UP)) { _moveTrigger = CHARACTER_DIRECTION_UP; } else{ turnUp(); if (abs(nearestSurvivedHamster->getPositionY() - this->getPositionY()) <= 32){ //MessageBox("Caught a hamster!", "test"); nearestSurvivedHamster->_caught = true; } } } } else{ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_UP)) { _moveTrigger = CHARACTER_DIRECTION_UP; } else if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_LEFT)){ _moveTrigger = CHARACTER_DIRECTION_LEFT; } else if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_RIGHT)){ _moveTrigger = CHARACTER_DIRECTION_RIGHT; } } } else if (offTileY < 0){ if (-offTileY <= _vision){ if (offTileX<0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_LEFT)){ _moveTrigger = CHARACTER_DIRECTION_LEFT; } } else if (offTileX > 0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_RIGHT)) { _moveTrigger = CHARACTER_DIRECTION_RIGHT; } } else if (offTileX == 0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_DOWN)) { _moveTrigger = CHARACTER_DIRECTION_DOWN; } else{ turnDown(); if (abs(nearestSurvivedHamster->getPositionY() - this->getPositionY()) <= 32){ //MessageBox("Caught a hamster!", "test"); nearestSurvivedHamster->_caught = true; } } } } else{ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_DOWN)) { _moveTrigger = CHARACTER_DIRECTION_DOWN; } else if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_LEFT)){ _moveTrigger = CHARACTER_DIRECTION_LEFT; } else if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_RIGHT)){ _moveTrigger = CHARACTER_DIRECTION_RIGHT; } } } else if (offTileY == 0){ if (offTileX<0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_LEFT)){ _moveTrigger = CHARACTER_DIRECTION_LEFT; } else{ turnLeft(); if (abs(nearestSurvivedHamster->getPositionX() - this->getPositionX()) <= 32){ nearestSurvivedHamster->_caught = true; } } } else if (offTileX > 0){ if (canMove(_destPosition.x, _destPosition.y, CHARACTER_DIRECTION_RIGHT)) { _moveTrigger = CHARACTER_DIRECTION_RIGHT; } else{ turnRight(); if (abs(nearestSurvivedHamster->getPositionX() - this->getPositionX()) <= 32){ nearestSurvivedHamster->_caught = true; } } } else if (offTileX == 0){ //impossible } } } } } }