bool InputHandler::isHeldDown(const sf::Input& input, std::string control) { //b1 : a corresponding key has been pressed //b2 : a corresponding mouse button has been pressed bool b1 = false, b2 = false; unsigned int i; std::map<std::string, std::vector<sf::Key::Code>>::iterator it1 = boundKeys.find(control); if(it1 != boundKeys.end()) //if the control is found in the map { i = 0; while(i < it1->second.size() && !b1) { b1 = input.IsKeyDown(it1->second.at(i)); i++; } } std::map<std::string, std::vector<sf::Mouse::Button>>::iterator it2 = boundMouseButtons.find(control); if(it2 != boundMouseButtons.end()) //if the control is found in the map { i = 0; while(i < it2->second.size() && !b2) { b2 = input.IsMouseButtonDown(it2->second.at(i)); i++; } } return (b1 || b2); }
void Train::Update(const sf::Input &input) { static sf::Clock clock; static bool waiting = false; acceleration = (1/mass) *(push_force - speed * brakes_factor); speed += acceleration; position.z += speed; if (position.z > 900 || position.z < -900) { position.z = -position.z; //comingSound.SetPosition(this->position); comingSound.Play(); StopGas(); } if (!user_controlled){ if (position.z < -250 && position.z > -300){ StopGas(); Brakes(); } if (IsStopped() && !waiting){ clock.Reset(); alarm=false; waiting = true; printf("\n\n\n waiting\n"); } if (waiting && clock.GetElapsedTime() > waiting_time) { Gas(); waiting = false; printf(" timed out\n"); } if (waiting && clock.GetElapsedTime() > waiting_time-4 && !alarm) { alarm=true; sound.SetBuffer(Buffer); //sound.SetPitch(0.6); //sound.SetAttenuation(0.4); sound.SetRelativeToListener(true); sound.SetPosition(this->position); sound.SetMinDistance(100); sound.Play(); //movmetnSound.SetPosition(this->position); movmetnSound.Play(); } light->position = sf::Vector3f(-40.900002,10,position.z + 5) ; if(camera !=NULL) camera->SetOrigin(position.x+8.3,position.y+15,position.z); }else { if (input.IsKeyDown(sf::Key::Z)) { push_force = 0.1f; }else if (input.IsKeyDown(sf::Key::X)){ brakes_factor = 0.05f; } } }
void ShipInput::handleInput(const sf::Input& Input) { ShipState& state = static_cast<ShipState&>(mObj.getComponent("state")); state.setState(ShipState::THRUST, Input.IsKeyDown(sf::Key::Up)); state.setState(ShipState::BRAKE, Input.IsKeyDown(sf::Key::Down)); state.setState(ShipState::TURN_LEFT, Input.IsKeyDown(sf::Key::Left)); state.setState(ShipState::TURN_RIGHT, Input.IsKeyDown(sf::Key::Right)); }
bool QuitGame::handleEvent(const sf::Input &Input) { if(Input.IsKeyDown(sf::Key::Y)) { setFinished(); if(m_panel != NULL) { m_panel->setQuit(); } } if(Input.IsKeyDown(sf::Key::N)) { setFinished(); } return true; }
void Paddle::GetInput(const sf::Input& Input, float FrameTime) { if(Input.IsKeyDown(sf::Key::Up)) { Y -= Speed * FrameTime; } if(Input.IsKeyDown(sf::Key::Down)) { Y += Speed * FrameTime; } }
GS_Return GS_Controls::Move(float secsPassed, const sf::Input& input) { Gamestate::Move(secsPassed, input); if((GS_FOCUS & GetState()) && input.IsJoystickButtonDown(0, 3)) { return GS_POP; } return GS_NOP; }
sf::Vector2f cHero::getMovement(const sf::Input & theInput, sf::Clock & jumpClock) { sf::Vector2f movementVector; xSpeed = 0; ySpeed = 0; // horizontal movement if (theInput.IsKeyDown(sf::Key::Left)) { xSpeed += -5; xAccel += -.2; } else if (theInput.IsKeyDown(sf::Key::Right)) { xSpeed += 5; xAccel += .2; } // vertical movement if (theInput.IsKeyDown(sf::Key::Up)) { if (isOnGround()) { jumpClock.Reset(); //ySpeed = -8; } if (jumpClock.GetElapsedTime() < .3) { yAccel -= .12 / (jumpClock.GetElapsedTime()); ySpeed = 30 * (jumpClock.GetElapsedTime() - .3); } } if (isOnGround()) { yAccel = 0; if (xAccel > .2) xAccel -= .05; else if (xAccel < -.2) xAccel += .05; } else { yAccel += 1 + (jumpClock.GetElapsedTime() / 20); } movementVector.x = xSpeed + xAccel; movementVector.y = ySpeed + yAccel; return movementVector; }
void Avatar::HandleSfmlRealtimeInput(const sf::Input& Input) { bool mouvementDetecte = false; if (Input.IsKeyDown(sf::Key::D)) { StrafeDroite(); mouvementDetecte = true; } if (Input.IsKeyDown(sf::Key::Q)) { StrafeGauche(); mouvementDetecte = true; } if(Input.IsKeyDown(sf::Key::E)) { Tourne(vitesseRotation); mouvementDetecte = true; } if(Input.IsKeyDown(sf::Key::A)) { Tourne(-vitesseRotation); mouvementDetecte = true; } if (Input.IsKeyDown(sf::Key::Z)) { Avance(); mouvementDetecte = true; } if (Input.IsKeyDown(sf::Key::Space)) { Saute(); } if (Input.IsKeyDown(sf::Key::S)) { Recule(); mouvementDetecte = true; } if(mouvementDetecte) { if(enMouvement == false) { objet3D.PlayAnimation(1,true); enMouvement = true; } } else { enMouvement = false; objet3D.PlayAnimation(0, true); } }
bool Slider::relayEvents(sf::Event const &event, sf::Input const &input) { if (WidgetBase::relayEvents(event, input)) { // evenement capture : traitement if (input.IsMouseButtonDown(sf::Mouse::Left)) { // on doit deplacer le curseur en consequence et modifier la valeur du slider float px = input.GetMouseX(); float d = _max - _min; float w = _frame.getWidth() / d; int value = (px - _frame.getPX() + w/2) / w + _min; // valeur pointee par le curseur setValue(value); // notification if (_pListener) _pListener->notifyChangedValue(*this); } return true; } return false; }
void GUIPage::HandleInput(const sf::Input& input) { int x = input.GetMouseX(); int y = input.GetMouseY(); focusedElement = NULL; // TODO: This loop is also repeated in GUIPage::Click(). // bring it out into a separate method. for(unsigned int i = 0; i < elements.size(); ++i) { if(elements[i]->shape.Contains(x, y)) { elements[i]->focused = true; focusedElement = elements[i]; break; } else { elements[i]->focused = false; } } }
void FreeFlyCamera::Animate(float dt, const sf::Input & input) { if(input.IsKeyDown(sf::Key::Left)) { position += speed * left * dt; } if(input.IsKeyDown(sf::Key::Right)) { position += speed * -left * dt; } if(input.IsKeyDown(sf::Key::Up)) { position += speed * forward * dt; } if(input.IsKeyDown(sf::Key::Down)) { position += speed * -forward * dt; } CompileVectors(); SetCameraInternals(); }
void updateInput(const sf::Input& input) { if (input.IsKeyDown(sf::Key::Left)) { if (sprite::player.GetPosition().x > 0) sprite::player.Move(-1*game::MOVE_RATE,0); } if (input.IsKeyDown(sf::Key::Right)) { if (sprite::player.GetPosition().x < 1280) { sprite::player.Move(1*game::MOVE_RATE,0); } } if (input.IsKeyDown(sf::Key::Up)) { if (sprite::player.GetPosition().y > 0) { sprite::player.Move(0,-1*game::MOVE_RATE); } } if (input.IsKeyDown(sf::Key::Down)) { if (sprite::player.GetPosition().y < 1024) { sprite::player.Move(0,game::MOVE_RATE); } } if (input.IsKeyDown(sf::Key::Z)) { float curtime = game::clock.GetElapsedTime(); if (curtime - game::playerSGTime > game::SG_COOLDOWN) { fireSG(); game::playerSGTime = curtime; } } // if (input.IsKeyDown(sf::Key::A)) // { // if (sprite::baddie.GetPosition().x > 0) // sprite::baddie.Move(-1*game::MOVE_RATE,0); // } // if (input.IsKeyDown(sf::Key::D)) // { // sprite::baddie.Move(1*game::MOVE_RATE,0); // } // if (input.IsKeyDown(sf::Key::W)) // { // sprite::baddie.Move(0,-1*game::MOVE_RATE); // } // if (input.IsKeyDown(sf::Key::S)) // { // sprite::baddie.Move(0,game::MOVE_RATE); // } if (input.IsKeyDown(sf::Key::K)) { std::cout << "Bullets size" << game::bullets.size() << std::endl; std::cout << "Baddies are " << game::baddies.size() << std::endl; } }
bool GS_Game::Move(float secsPassed, const sf::Input& input) { #ifdef _WIN32 XBoxCtrl::GetInstance()->Update(secsPassed); #endif _expManager.Update(secsPassed); SoundManager::GetInstance()->Update(); _timePassed += secsPassed; _fallTimer += secsPassed; if(Collision()) { if(_activeBlock.position.y <= 0) { _next = MB_ENTER_SCORE; return false; } _activeBlock.position.y--; AddToField(); DropNewBlock(); } if(input.IsKeyDown(sf::Key::Escape) || input.IsJoystickButtonDown(0, 3)) { _next = MB_MENU; // _next = MB_ENTER_SCORE; return false; } if(_canDrop && _falling) { if(input.IsKeyDown(sf::Key::Down) || input.IsJoystickButtonDown(0, 0)) { #ifdef _WIN32 XBoxCtrl::GetInstance()->Rumble(10000, 20000, 0.3f); #endif while(!Collision()) _activeBlock.position.y++; _activeBlock.position.y--; if(_activeBlock.position.y <= 0) { _next = MB_ENTER_SCORE; return false; } AddToField(); DropNewBlock(); _fallTimer = 0.0f; _canDrop = false; } } if(!_canDrop) { if(!input.IsKeyDown(sf::Key::Down) && !input.IsJoystickButtonDown(0, 0)) _canDrop = true; } if(_canRotate) { if(input.IsKeyDown(sf::Key::Up) || input.IsJoystickButtonDown(0, 1)) { int current = _activeBlock.index; int next = _activeBlock.index + s_blocks[current].next; int px1, py1, px2, py2; for(int i = 0; i < 4; ++i) { for(int j = 0; j < 4; ++j) { if(BLOCK_PIVOT == s_blocks[current].map[MAPSIZE * i + j]) { px1 = i; py1 = j; } if(BLOCK_PIVOT == s_blocks[next].map[MAPSIZE * i + j]) { px2 = i; py2 = j; } } } _activeBlock.index = next; sf::Vector2<int> pos = _activeBlock.position; int x = _activeBlock.position.x += (px2 - px1); int y = _activeBlock.position.y += (py2 - py1); if(OutOfField() || Collision()) { int i; for(i = 1; i < 4; ++i) { _activeBlock.position.x = x - i; if(!OutOfField()) break; _activeBlock.position.x = x + i; if(!OutOfField()) break; } if(3 <= i || Collision()) { _activeBlock.position = pos; _activeBlock.index = current; } } #ifdef _WIN32 XBoxCtrl::GetInstance()->Rumble(10000, 1000, 0.5f); #endif _zoom->Play(); _canRotate = false; } } if(!_canRotate) { if(!input.IsKeyDown(sf::Key::Up) && !input.IsJoystickButtonDown(0, 1)) _canRotate = true; } if(_receivesInput) { if(_falling) { if(50.0f < input.GetJoystickAxis(0, sf::Joy::AxisY)) { _activeBlock.position.y++; if(Collision()) _activeBlock.position.y--; _fallTimer = 0.0f; _delayInput = 0.0f; _receivesInput = false; } if(-50.0f > input.GetJoystickAxis(0, sf::Joy::AxisY)) { for(int i = 0; i < 2; ++i) { _activeBlock.position.y++; if(Collision()) _activeBlock.position.y--; } _fallTimer = 0.0f; _delayInput = 0.0f; _receivesInput = false; } } if(input.IsKeyDown(sf::Key::Left) || -50.0f > input.GetJoystickAxis(0, sf::Joy::AxisX)) { _activeBlock.position.x--; if(OutOfField() || Collision()) _activeBlock.position.x++; _receivesInput = false; _delayInput = 0.0f; } if(input.IsKeyDown(sf::Key::Right) || 50.0f < input.GetJoystickAxis(0, sf::Joy::AxisX)) { _activeBlock.position.x++; if(OutOfField() || Collision()) _activeBlock.position.x--; _receivesInput = false; _delayInput = 0.0f; } } else { _delayInput += secsPassed; if(0.1f < _delayInput) _receivesInput = true; } if(_falling) { if(_fallTimer > 0.5f) { _activeBlock.position.y++; _fallTimer = 0.0f; } } else { _blendTimer += secsPassed; if(2.0f < _blendTimer) { for(int i = 19; i >= 1; --i) { if(_field[i].remFlag) { for(int j = 0; j < i; ++j) { for(int k = 0; k < 16; ++k) { _field[i - j].stack[k] = _field[i - j - 1].stack[k]; _field[i - j].remFlag = _field[i - j - 1].remFlag; } } ++i; } } _falling = true; } } _fieldFrame.Move(secsPassed); return true; }