void WGM63::loop() { if(_currentState == STOP && _destState == STOP) return; if(_currentState == STOP) { changeDirection( _destState ); } if(_currentState != _destState) { _currentDelay += ACCELERATION_STEP; _currentDelay = constrain(_currentDelay, DRIVING_DELAY, START_DELAY); if(_currentDelay == START_DELAY) { changeDirection( _destState ); } } else { if(_currentDelay != DRIVING_DELAY) { _currentDelay -= ACCELERATION_STEP; _currentDelay = constrain(_currentDelay, DRIVING_DELAY, START_DELAY); } } digitalWrite(_stepperPin, HIGH); delayMicroseconds(_currentDelay); digitalWrite(_stepperPin, LOW); delayMicroseconds(_currentDelay); }
bool Ghost::makeDecision() { if ( !mCountdown && rand() % 3 == 0 ) { if ( changeDirection() ) if ( !mGame->moveGhost(*this, mDirection) ) return false; mCountdown = 4; return true; } else { Pos newPos = mPos + mDirection; bool canMove = true; if ( !mGame->canMoveGhost(newPos) ) if ( !changeDirection() ) canMove = false; if ( canMove ) if ( !mGame->moveGhost(*this, mDirection) ) return false; if ( mCountdown ) --mCountdown; return true; } }
void Enemy1::update(float time){ turnaroundTimer += time; sprite.update(time); // Test if it's time to, and actually do, turn around. if (turnaroundTimer > 2 && getDirection() && body->GetLinearVelocity().x < 0.2f){ changeDirection(0); turnaroundTimer = 0; } else if (turnaroundTimer > 2 && !getDirection() && body->GetLinearVelocity().x > -0.2f){ changeDirection(1); turnaroundTimer = 0; } // Move the slug. if (getDirection() && body->GetLinearVelocity().x < 1){ b2Vec2 force = b2Vec2(0.3f, 0.0f); b2Vec2 position = body->GetPosition(); body->ApplyLinearImpulse(force, position,true); } else if(!getDirection() && body->GetLinearVelocity().x > -1){ b2Vec2 force = b2Vec2(-0.3f, 0.0f); b2Vec2 position = body->GetPosition(); body->ApplyLinearImpulse(force, position,true); } if (health <= 0){ manager->addCorpse(body->GetPosition().x, body->GetPosition().y,1,1,5,5,true,"resources/textures/enemy1_corpse.png"); manager->addItemCrate(body->GetPosition().x+2, body->GetPosition().y-0.2, 0); manager->remove(ID); } }
void onMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT && state == GLUT_DOWN) { changeDirection(centerHansel, angleHansel, x, y); } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { changeDirection(centerGreta, angleGreta, x, y); } glutPostRedisplay(); }
void Snarlbot::doSomething() { if(!isAlive()) return; if(getTicks() != getTempTicks()) //turn to rest { increaseTick(); return; } if(fire(getX(), getY(), getDirection())) //function to check if it can fire and then fire the bullet { setTicksToZero(); return; } int x = getWorld()->getXcoordOfNext(getX(), getDirection()); int y = getWorld()->getYcoordOfNext(getY(), getDirection()); if(getWorld()->canMove(this,x, y,getDirection())) //else move in the adjacent direction if possible moveTo(x, y); else setDirection(changeDirection()); //else change the direction setTicksToZero(); //set ticks to 0 after moving }
void CentipedeSegment::updateDirection() { Direction newDirection; switch (direction) { case LEFT: if ((GLuint)Position.y1 < FIELDSIZE - 1) newDirection = DOWN; else newDirection = UP; break; case RIGHT: if ((GLuint)Position.y1 < FIELDSIZE - 1) newDirection = DOWN; else newDirection = UP; break; case DOWN: newDirection = getOppositeDirection(oldDirection); Position.y1 = (GLfloat)((GLuint)getCenter().y); //Align to y axis Position.y2 = Position.y1 + CENTISEGMENT_HEIGHT; break; case UP: newDirection = getOppositeDirection(oldDirection); Position.y1 = (GLfloat)((GLuint)getCenter().y); //Align to y axis Position.y2 = Position.y1 + CENTISEGMENT_HEIGHT; break; default: break; } changeDirection(newDirection); }
void PMBrushContainer2::draw() { // cout << "Offset: " << offset << endl; ofSetColor(color); ofSetCircleResolution(32); // Interpolate brush positions float offsetX=0.0f, offsetY=0.0f; if ((direction==LEFT) || (direction==RIGHT)) offsetY = offset; else offsetX = offset; double deltaX = (x - oldX) / double(speed); double deltaY = (y - oldY) / double(speed); for (int i=0; i<speed; ++i) { oldX += deltaX + offsetX; oldY += deltaY + offsetY; ofDrawCircle(oldX, oldY, size / 2.0f); } if (shouldChangeDirection) changeDirection(); // cout << "Pos: " << x << ", " << y << endl; }
// Main game function void run() { // Initialize the map initMap(); running = true; while (running) { // If a key is pressed if (_kbhit()) { // Change to direction determined by key pressed changeDirection(_getch()); } // Upate the map update(); // Clear the screen clearScreen(); // Print the map printMap(); // wait 0.5 seconds Sleep(500); } // Print out game over text std::cout << "\t\t!!!Game over!" << std::endl << "\t\tYour score is: " << food; // Stop console from closing instantly std::cin.ignore(); }
/** * This function processes the movement commands that the behaviours generate. * Depending on the values in the behaviour_command_t struct, it sets motor * speed, moves a given distance or rotates. */ void moveCommand(behaviour_command_t * cmd) { if(cmd->move_value > 0) // move or rotate? { writeString("cmd->move_value > 0\n"); if(cmd->rotate){ rotate(cmd->speed_left, cmd->dir, cmd->move_value, false); writeString("rotate\n"); }else if(cmd->move){ move(cmd->speed_left, cmd->dir, DIST_MM(cmd->move_value), false); writeString("move\n"); } cmd->move_value = 0; // clear move value - the move commands are only // given once and then runs in background. } else if(!(cmd->move || cmd->rotate)) // just move at speed? { changeDirection(cmd->dir); moveAtSpeed(cmd->speed_left,cmd->speed_right); } else if(isMovementComplete()) // movement complete? --> clear flags! { writeString("mouvement complete\n"); cmd->rotate = false; cmd->move = false; } }
void MonoMissile::calculate() { STACKTRACE; Missile::calculate(); if ( target != last_target ) set_up_beacons(); last_target = target; if ( !(target && target->exists()) || target->isInvisible() ) { target = NULL; return; } if ( distance( from_beacon ) > distance( to_beacon )) { play_sound2( data->sampleWeapon[1] ); if ( from_beacon ) delete from_beacon; from_beacon = new SpaceLocation( this, pos, 0.0 ); double tangle = trajectory_angle( target ); int itangle = (int)((tangle + PI/4) / (PI/2)) % 4; changeDirection( (PI/2) * itangle ); sprite_index = itangle; if ( itangle % 2 == 0 ) { if ( to_beacon ) delete to_beacon; to_beacon = new SpaceLocation( this, Vector2(2*target->normal_pos().x-pos.x, pos.y), 0.0 ); } else if ( itangle % 2 == 1 ) { if ( to_beacon ) delete to_beacon; to_beacon = new SpaceLocation( this, Vector2(pos.x, 2*target->normal_pos().y-pos.y), 0.0 ); } } }
void SnakeNpcControlManager::AsyncChangeNpcDirection(int64_t diff_in_ms) { //std::lock_guard<std::recursive_mutex> lock(snake_npcs_mutex_); strand_.post( [this, diff_in_ms]() { npc_change_dir_timer_.expires_from_now(boost::posix_time::milliseconds(diff_in_ms)); // 임시: // 랜덤하게 방향을 변경. // UpdatteMove 가 불린 횟수와 관계없이, // 시간당 방향전환 횟수가 랜덤하도록 방향을 설정. if (checkChangeDirection(diff_in_ms)) { if (auto game_session = game_session_wp_.lock()) { changeDirection(random_engine_, game_session, snake_npc_handles_, diff_in_ms); } } npc_change_dir_timer_.async_wait( [this, diff_in_ms](boost::system::error_code ec) { if (!ec) { AsyncChangeNpcDirection(diff_in_ms); } }); }); }
void AutoTank::shot() { if(!life) return; changeDirection(targetObject->getPosition()); float radGunAngle = M_PI*15.0/180.0; float radTankAngle = M_PI*angleWithX/180.0; bombStartPos.setX(currentPosition.X()+ 14.0*cos(radGunAngle)*cos(radTankAngle)); bombStartPos.setY(targetObject->getPosition().Y()); bombStartPos.setZ(currentPosition.Z()- 14.0*cos(radGunAngle)*sin(radTankAngle)); float s = bombStartPos.dist(targetObject->getPosition()); bombStartPos.setY(currentPosition.Y()+ 14.0*sin(radGunAngle) +2.0); float h = bombStartPos.Y() - targetObject->getPosition().Y(); float x = bombAcceleMag /(0.5*s + 1.9318*h); bombVelocityMag = s*sqrt(x); bombVelocity.setX(bombVelocityMag*cos(radGunAngle)*cos(radTankAngle)); bombVelocity.setY(bombVelocityMag*sin(radGunAngle)); bombVelocity.setZ(-bombVelocityMag*cos(radGunAngle)*sin(radTankAngle)); bombArray->addTankBomb(bombStartPos, bombVelocity); shotted = true; shotTime = glutGet(GLUT_ELAPSED_TIME); //runAway(); }
Ghost::Ghost(Game *game, const Pos &pos) :mGame(game), mPos(pos), mDirection(Pos(1, 0)), mCountdown(4) { if ( rand() % 4 ) changeDirection(); }
void JyglarStrayShot::calculate() { STACKTRACE; Shot::calculate(); double r = random(minturn* ANGLE_RATIO, maxturn* ANGLE_RATIO); changeDirection( angle + r * frame_time ); }
static void moveRight() { if (x != SCREEN_WIDTH - PACMAN_WIDTH) { changeDirection(4); x += 1; } }
static void moveDown() { if (y != SCREEN_HEIGHT - PACMAN_HEIGHT) { changeDirection(2); y += 1; } }
static void moveUp() { if (y != 0) { changeDirection(1); y -= 1; } }
Entity Entity::move(int enumDirection, sf::RenderWindow *win){ if (enumDirection == UP){ if (direction == enumDirection){ position.y -= (speed / 16); sprite.setPosition(position); } else { changeDirection(enumDirection); } } if (enumDirection == RIGHT){ if (direction == enumDirection){ position.x += (speed / 16); sprite.setPosition(position); } else { changeDirection(enumDirection); } } if (enumDirection == DOWN){ if (direction == enumDirection){ for (int i = 0; i < speed; i++) { position.y += (speed / 16); sprite.setPosition(position); win->clear(); win->draw(sprite); win->display(); } } else { changeDirection(enumDirection); } } if (enumDirection == LEFT){ if (direction == enumDirection){ position.x -= (speed / 16); sprite.setPosition(position); } else { changeDirection(enumDirection); } } return *this; }
static void moveLeft() { if (x != 0) { changeDirection(3); x -= 1; } }
void Missile::initialize() { explosion.turnOffExplosion(); resetSounds(); setAnimation(eFlying); if(!facingRight) changeDirection(); x = dbRnd(100) - 300; speed = maxSpeed; if(dbRnd(1)) { speed = maxSpeed * -1; x = dbRnd(100) + 1100; changeDirection(); } y = 163; }
void MonoMissile::calculate_index() { STACKTRACE; while (angle < 0) angle += PI2; int iangle = (int)((angle + PI/4) / (PI/2)) % 4; angle = iangle * PI/2; changeDirection( angle ); sprite_index = iangle; }
NPC::NPC(Board* board, uint32_t rootX, uint32_t rootY) : m_rootX(rootX) , m_rootY(rootY) , m_cx(rootX) , m_cy(rootY) , m_board(board) { m_board->setField(rootX, rootY, Board::FIELD_NPC); changeDirection(); }
void Pursued::update() { if(!hasDestination) setRandomDestination();/// FOR NO OTHER TARGETS changeDirection(); walls(); if(hasDestination) move(); move(); hasDestination = false; }
void runAStep(struct Ball* ball, int posYJ1, int posYJ2, struct Score* score) { if(colideRWall(*ball) == 1) { if (colideRaquette(posYJ1, *ball)) { changeDirection(ball); ball->varY = ((ball->coord.y - posYJ1) - 20)/2.3; mouve(ball); } else { score->J2++; displayScore(*score); initBall(ball); } } else if(colideRWall(*ball) == 2) { if (colideRaquette(posYJ2, *ball)) { changeDirection(ball); ball->varY = ((ball->coord.y - posYJ2) - 20)/2.3; mouve(ball); } else { score->J1++; displayScore(*score); initBall(ball); } } else { if (colideWall(*ball) > 0) ball->varY = - ball->varY; mouve(ball); } }
Virus::Virus(Frame const &limits) : _incub(0), _hp(100) { int minx = limits.getPX() + PADDING_SPAWN; int maxx = limits.getPX() + limits.getWidth() - PADDING_SPAWN; int miny = limits.getPY() + PADDING_SPAWN; int maxy = limits.getPY() + limits.getHeight() - PADDING_SPAWN; _position.x = rand() % (maxx - minx + 1) + minx; _position.y = rand() % (maxy - miny + 1) + miny; changeDirection(); }
void wait(int i, int timer, char *keyflag, char *last_direction, struct snakeNode *snakeHead) { int j; if (i > 0) { api_settimer(timer, i); i = 128; } else { i = 0x0a; /* Enter */ } for (;;) { j = api_getkey(1); if (i == j) { break; } if (j == '4') { keyflag[0 /* left */] = 1; *last_direction = '4'; changeDirection(snakeHead, 4); } if (j == '6') { keyflag[1 /* right */] = 1; *last_direction = '6'; changeDirection(snakeHead, 6); } if (j == '8') { keyflag[2 /* up */] = 1; *last_direction = '8'; changeDirection(snakeHead, 8); } if (j == '2') { keyflag[3 /* down */] = 1; *last_direction = '2'; changeDirection(snakeHead, 2); } } return; }
void Virus::move(Frame const &limits) { int minx = limits.getPX() + PADDING_SPAWN; int maxx = limits.getPX() + limits.getWidth() - PADDING_SPAWN; int miny = limits.getPY() + PADDING_SPAWN; int maxy = limits.getPY() + limits.getHeight() - PADDING_SPAWN; _position += _direction; fixPosition(minx, maxx, miny, maxy); if (_clock.GetElapsedTime() >= _freq) { changeDirection(); _clock.Reset(); } }
void SnakeNetworkClientMainWindow::keyPressEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_Up: case Qt::Key_W: emit changeDirection(CMD_UP); break; case Qt::Key_Down: case Qt::Key_S: emit changeDirection(CMD_DOWN); break; case Qt::Key_Left: case Qt::Key_A: emit changeDirection(CMD_LEFT); break; case Qt::Key_Right: case Qt::Key_D: emit changeDirection(CMD_RIGHT); break; default: break; } }
/** Hace que el ascensor se mueva al siguiente piso que tiene que visitar */ void executeTask(){ //Serial.println("executeTask"); if (!exteriorSelected[currentFloor]) { if (taskInDirection(currentDirection)) { //Serial.println("quedan pisos por visitar en esta dirección"); startElevator(); } else if (taskInDirection (!currentDirection)) { //Si hay tareas en sentido contrario, ir para allá changeDirection(); startElevator(); } } else { //Serial.println("viene por dooropen"); doorOpen(); } }
void updateDirection(int i) { for (int i = 0; i<particleAmount; i++) { for (int j = 0; j<objectAmount; j++) { bool check = checkIfWithin(i,j); if (check){ changeDirection(i,j); } else{ particles[i].direction[2] = particles[i].direction[2] + gravity; // gravity update } } } }