void Creature::Update(sf::RenderWindow &app, sf::View &camera) { xSpeed += horizontal * app.GetFrameTime(); ySpeed += vertical * app.GetFrameTime(); xSpeed *= 1 - tan(friction*PI/2) * app.GetFrameTime(); ySpeed *= 1 - tan(friction*PI/2) * app.GetFrameTime(); move(xSpeed * app.GetFrameTime(), ySpeed * app.GetFrameTime()); }
void df::Context::ExtractRenderWindowStatus(sf::RenderWindow const &renderWindow) { _ellapsedTime = renderWindow.GetFrameTime(); _frameRate = 1.f/_ellapsedTime; _windowSize.Width = renderWindow.GetWidth(); _windowSize.Height = renderWindow.GetHeight(); }
void Player::draw(sf::RenderWindow& window) { if (isAnimated) { //Anim has completed? if(!saiyan.Update(window.GetFrameTime())) { isAnimated = false; saiyan.Reset(); //Play song Music::GetInstance().playRageMode(); } //Find sprite center float centerX = avatar.GetPosition().x + avatar.GetSize().x / 2; float centerY = avatar.GetPosition().y + avatar.GetSize().y / 2; float x = centerX - saiyanSprite.GetSize().x /2; float y = centerY - saiyanSprite.GetSize().y /2; saiyanSprite.SetPosition(x, y); window.Draw(saiyanSprite); } else { window.Draw(avatar); } rageMeter = sf::Shape::Rectangle(0, 0, rage, 15, sf::Color::Red); rageMeter.SetPosition(25,75); window.Draw(rageMeterBox); window.Draw(rageMeter); window.Draw(rageStr); }
void backgroundManager::update(sf::RenderWindow &Screen){ if(sprite.GetPosition().y+(sprite.GetSize().y/2)>=screenSize.y){ sprite.SetPosition(0,-screenSize.y); } sprite.Move(0,Screen.GetFrameTime()*scrollSpeed); draw(Screen); }
void Visual::textDisplay(sf::RenderWindow& mainWindow) { //draw text sf::String text("", font, 15); text.SetPosition(5, 5); std::ostringstream sstream; int fps = static_cast<int>(1 / mainWindow.GetFrameTime()); sstream << "FPS: "; sstream << fps; if (totalFPS > 4000000000 || frameCount > 700) { totalFPS = 0; frameCount = 0; } if (fps >= 0) totalFPS += fps; frameCount++; sstream << "\nAvg FPS: "; sstream << totalFPS / frameCount; text.SetText(sstream.str()); sstream.end; text.SetColor(sf::Color::Green); mainWindow.Draw(text); }
void Player::Draw(sf::RenderWindow& W){ if(lives > 3) lives = 3; Anim->Animate(W.GetFrameTime()); Anim->SetPosition(left, up); Anim->Draw(W); }
bool Player::playDeath(sf::RenderWindow& window) { window.Clear(); bool result = death.Update(window.GetFrameTime()); window.Draw(death_sprite); return result; }
// Actions joueur void Player::evolue(sf::RenderWindow& app) { const sf::Input& input = app.GetInput(); m_frame = app.GetFrameTime(); // Déplacement à la souris if(m_sprite.GetPosition().y > input.GetMouseY()-PLAYER_SIZE/2) Move(Player::TOP); else if(m_sprite.GetPosition().y < input.GetMouseY()-PLAYER_SIZE/2) Move(Player::BOTTOM); if(m_sprite.GetPosition().x > input.GetMouseX()-PLAYER_SIZE/2) Move(Player::LEFT); else if(m_sprite.GetPosition().x < input.GetMouseX()-PLAYER_SIZE/2) Move(Player::RIGHT); }
void AddHelpText(sf::RenderWindow &App, sf::Font &f, GroupManager &gm) { sf::View v = App.GetView(); sf::Vector2f center = v.GetCenter(), half = v.GetHalfSize(); string s; s = "Operations available:\n"; s += "Move: Left, Right, Up, Down\n"; s += "Speed: s (decrease), d (increase)\n"; s += "Step size: w (decrease), e (increase)\n"; s += "Zoom: z (in), x (out)\n"; s += "Steps mode: a (switch between modes)\n"; s += "Quit: Esc\n"; sf::String text(s, f, 12); text.SetColor(sf::Color(128, 200, 40)); text.Move(center.x - half.x + half.x/20, center.y - half.y + half.y/20); App.Draw(text); ostringstream step_per_second_str; step_per_second_str << "~" << step_per_second << "\n"; ostringstream step_time_str; step_time_str << "~" << step_time << "\n"; ostringstream framerate_str; framerate_str << 1.0/App.GetFrameTime() << "\n"; ostringstream step_size_str; step_size_str << gm.GetStepSize() << "\n"; s = "Steps per second: "; s += step_per_second_str.str() ; s += "Step time: "; s += step_time_str.str(); s += "Framerate: "; s += framerate_str.str(); s += "Step size: "; s += step_size_str.str(); s += "Steps mode: "; switch (step_mode) { case SM_HARD_STEPS_CNT: s += "SM_HARD_STEPS_CNT\n"; break; case SM_FREE_STEPS_CNT: s += "SM_FREE_STEPS_CNT\n"; break; default: break; } sf::String inf(s.c_str(), f, 20); inf.SetColor(sf::Color(230, 200, 180)); inf.Move(center.x - half.x + half.x/20, center.y + half.y - half.y/2); App.Draw(inf); }
void Animation::Update(sf::RenderWindow &Window) { if(active) frameCounter += (int)Window.GetFrameTime(); else frameCounter = 0; if (frameCounter >= switchFrame) { frameCounter = 0; currentFrameX += getFrameWidth(); if(currentFrameX >= spriteImage.GetImage()->GetWidth()) currentFrameX = 0; } spriteImage.SetSubRect(sf::IntRect(currentFrameX, currentFrameY * getFrameHeight(), currentFrameX +getFrameWidth(), currentFrameY + getFrameHeight())); spriteImage.SetPosition(x, y); }
void Paddle::Update(sf::RenderWindow& app) { float speed = 0; // get player input if(app.GetInput().IsKeyDown(sf::Key::Up)) { if(Y > 0) speed = -defaultSpeed; } if(app.GetInput().IsKeyDown(sf::Key::Down)) { if(Y + H < screenHeight) speed = defaultSpeed; } Y += speed * app.GetFrameTime(); SetPosition(X, Y); }
void Player::Update(sf::RenderWindow & App, class Physic & physic) { if(!m_inited) Init(physic); const sf::Input & _input = App.GetInput(); physic.GetPlayer(0)->vel = physic.GetPlayer(0)->body->GetLinearVelocity(); physic.GetPlayer(0)->rotation = physic.GetPlayer(0)->body->GetAngle(); if(_input.IsKeyDown(sf::Key::A)) { physic.GetPlayer(0)->rotation += -1.0f * App.GetFrameTime(); physic.GetPlayer(0)->body->SetTransform(physic.GetPlayer(0)->body->GetPosition(), physic.GetPlayer(0)->rotation); /*m_rotationDegree -= 3; m_rotation = 3; m_player.Rotate( (float)m_rotation );*/ } // turn LEFT if(_input.IsKeyDown(sf::Key::D)) { physic.GetPlayer(0)->rotation += 1.0f * App.GetFrameTime(); physic.GetPlayer(0)->body->SetTransform(physic.GetPlayer(0)->body->GetPosition(), physic.GetPlayer(0)->rotation); /*m_rotationDegree += 3; m_rotation = -3; m_player.Rotate( (float)m_rotation );*/ } // turn RIGHT if(_input.IsKeyDown(sf::Key::W)) { m_V += 2.00 ; } if(_input.IsKeyDown(sf::Key::S)) { m_V -= 1.00 ; } if(_input.IsKeyDown(sf::Key::Space)) { m_V = 0.0 ; } physic.GetPlayer(0)->body->SetAngularVelocity(0.f); /* if(m_clock2.GetElapsedTime() > 1.0f) { if(_input.IsMouseButtonDown(sf::Mouse::Left)) { Shoot(App); m_reload = true; m_clock2.Reset(); } m_reload = false; } else { m_reload = true; } */ // UpdateBullets(App); // m_mousePozVec.x = _input.GetMouseX(); // m_mousePozVec.y = _input.GetMouseY(); m_mousePozVec = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY()); m_xH = m_cannon.GetPosition().x - m_mousePozVec.x; m_yH = m_cannon.GetPosition().y - m_mousePozVec.y; m_cannonRotation =(float)(atan2(m_yH,m_xH) * 180.f / b2_pi + 180.f); // if(physic.GetPlayer(0)->rotation > 359) physic.GetPlayer(0)->rotation = 0; // if(physic.GetPlayer(0)->rotation < 0) physic.GetPlayer(0)->rotation = 359; if( m_V > m_maxPlayerSpeed ) m_V = m_maxPlayerSpeed; if( m_V < m_maxPlayerBackSpeed ) m_V = m_maxPlayerBackSpeed; physic.GetPlayer(0)->vel.x = m_V * cos((b2_pi*(physic.GetPlayer(0)->rotation * RADTODEG ))/180.f); physic.GetPlayer(0)->vel.y = m_V * sin((b2_pi*(physic.GetPlayer(0)->rotation * RADTODEG ))/180.f); // if( m_disVec.x >= 1 || m_disVec.x <= - 1 ) { m_playerPozVec.x += int(m_disVec.x) * App.GetFrameTime(); m_disVec.x = m_disVec.x - int( m_disVec.x ); } // if( m_disVec.y >= 1 || m_disVec.y <= - 1 ) { m_playerPozVec.y += int(m_disVec.y) * App.GetFrameTime(); m_disVec.y = m_disVec.y - int( m_disVec.y ); } physic.GetPlayer(0)->body->SetLinearVelocity(physic.GetPlayer(0)->vel); m_player.SetPosition( physic.GetPlayer(0)->GetPosition() ); m_player.SetRotation( -physic.GetPlayer(0)->rotation * RADTODEG ); m_cannon.SetPosition(m_player.GetPosition()); m_cannon.SetRotation(-m_cannonRotation); /* #pragma region DEBUG INFO std::ostringstream bufor; sf::String tdebug; tdebug.SetFont(LoadContent::GetInstance()->m_font2); tdebug.SetSize(20); bufor << "\nFPS = " << _Framerate; tdebug.SetText(bufor.str()); App.Draw(tdebug); #pragma endregion */ App.Draw(m_player); App.Draw(m_cannon); }
void Player::Gravity(sf::RenderWindow &app){ m_vely+=GameConfig::g_config["gravity"]/1000.f*app.GetFrameTime(); }
int GameScreen::update (sf::RenderWindow &app) { int nextScreen = myID; float elapsedTime = app.GetFrameTime(); sf::Event event; while (app.GetEvent(event)) // Boucle des évènements en attente { if (event.Type == sf::Event::Closed) { return EXIT; } else if(m_iHandler->testEvent(event, "Quit")) { return EXIT; } else if (event.Type == sf::Event::MouseMoved) { sf::Vector2f mouseCoords = app.ConvertCoords(event.MouseMove.X, event.MouseMove.Y, &ConfigOptions::getView()); m_cursor.moveOnSquare(BoardAlignedSprite::toSquares(mouseCoords), false); } else if(m_iHandler->testEvent(event, "Up")) { sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(0, -1); if(isValid(s)) m_cursor.moveOnSquare(s, false); } else if(m_iHandler->testEvent(event, "Left")) { sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(-1, 0); if(isValid(s)) m_cursor.moveOnSquare(s, false); } else if(m_iHandler->testEvent(event, "Down")) { sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(0, 1); if(isValid(s)) m_cursor.moveOnSquare(s, false); } else if(m_iHandler->testEvent(event, "Right")) { sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(1, 0); if(isValid(s)) m_cursor.moveOnSquare(s, false); } else if(playerHasHand()) { if(m_iHandler->testEvent(event, "Save")) { m_game.saveToFile(BACKUP_FILE); } else if(m_iHandler->testEvent(event, "Load")) { m_game.loadFromFile(BACKUP_FILE); refreshAll(); } else if (m_iHandler->testEvent(event, "LClick")) { sf::Vector2f mouseCoords = app.ConvertCoords(event.MouseButton.X, event.MouseButton.Y, &ConfigOptions::getView()); sf::Vector2i s = BoardAlignedSprite::toSquares(mouseCoords); if(isValid(s)) clickOn(s); else if(!m_game.hasStarted()) { PieceType type = m_placementUI.click(mouseCoords); if(type == NB_PIECES) //end of turn tryAndEndTurn(); else m_selectedType = type; } } if (m_iHandler->testEvent(event, "Choose")) { sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()); if(isValid(s)) clickOn(s); } else if (m_iHandler->testEvent(event, "RClick")) { sf::Vector2f mouseCoords = app.ConvertCoords(event.MouseButton.X, event.MouseButton.Y, &ConfigOptions::getView()); sf::Vector2i s = BoardAlignedSprite::toSquares(mouseCoords); if(!m_game.hasStarted()) remove(s); } else if (m_iHandler->testEvent(event, "EndTurn")) { tryAndEndTurn(); } else if (event.Type == sf::Event::MouseWheelMoved) { if(event.MouseWheel.Delta > 0) { m_selectedType = m_placementUI.goUp(); } else { m_selectedType = m_placementUI.goDown(); } } else if (m_iHandler->testEvent(event, "Rabbit")) { selectPieceType(RABBIT); } else if (m_iHandler->testEvent(event, "Cat")) { selectPieceType(CAT); } else if (m_iHandler->testEvent(event, "Dog")) { selectPieceType(DOG); } else if (m_iHandler->testEvent(event, "Horse")) { selectPieceType(HORSE); } else if (m_iHandler->testEvent(event, "Camel")) { selectPieceType(CAMEL); } else if (m_iHandler->testEvent(event, "Elephant")) { selectPieceType(ELEPHANT); } } //end of if(playerHasHand) else if(isOver() && m_victorySign.GetColor().a == 255 && (m_iHandler->testEvent(event, "EndTurn") || m_iHandler->testEvent(event, "Choose") || m_iHandler->testEvent(event, "LClick"))) { //if we do something after the match ends, remove the victory sign m_victorySign.SetColor(sf::Color(255,255,255,0)); } } //end of event loop m_turnSign.update(elapsedTime); m_cursor.update(elapsedTime); for(std::map<PiecePtr, PieceSprite>::iterator it = m_pieces.begin(); it != m_pieces.end(); ++it) it->second.update(elapsedTime); //removing pieces that have finished disappearing for (auto it = m_disappearingPieces.begin(); it != m_disappearingPieces.end(); ) //++it done below if necessary { PiecePtr p = *it; if (m_pieces[p].hasDisappeared()) { m_pieces.erase(p); it = m_disappearingPieces.erase(it); //gives it the value of the next element, thus no need to increase it again } else ++it; } if(isOver()) //manages the appearance of the victory sign { int alpha = m_victorySign.GetColor().a; if(alpha > 0 && alpha < 255) //0 means it's disappeared already { alpha += (int) (elapsedTime * VICT_APP_SPD * 255); if(alpha > 255) alpha = 255; m_victorySign.SetColor(sf::Color(255,255,255,alpha)); } } return nextScreen; }
SBI gra_screen::Animate(sf::RenderWindow &App) { if (Timer_c.GetElapsedTime()-osttime-korektatime>1) { korektatime=Timer_c.GetElapsedTime()-osttime; } osttime=Timer_c.GetElapsedTime()-korektatime; for (int i=0;i<obiekty_il;i++) { (obiekty+i)->ZasTarcie(App.GetFrameTime(),ProgTarcia); } const sf::Input& Input = App.GetInput(); if (Input.IsKeyDown(sf::Key::B)) { for (int i=0;i<obiekty_il;i++) { (obiekty+i)->SetV(0,0); } } for (int i=0; i<gracz_il; i++) { bool cx=false; bool cy=false; int przyspGDoc; bool czyuderz=(Input.IsKeyDown(gracze_ev[i][(int)Uderz].KeyCode)); if (czyuderz) przyspGDoc=PrzyspGKop; else przyspGDoc=PrzyspG; if (Input.IsKeyDown(gracze_ev[i][(int)Lewo].KeyCode)) { gracze_ob[i].SetVX(gracze_ob[i].GetVX()-App.GetFrameTime()*przyspGDoc); cx=true; } if (Input.IsKeyDown(gracze_ev[i][(int)Prawo].KeyCode)) { gracze_ob[i].SetVX(gracze_ob[i].GetVX()+App.GetFrameTime()*przyspGDoc); if (cx) cx=false; else cx=true; } if (Input.IsKeyDown(gracze_ev[i][(int)Gora].KeyCode)) { gracze_ob[i].SetVY(gracze_ob[i].GetVY()-App.GetFrameTime()*przyspGDoc); cy=true; } if (Input.IsKeyDown(gracze_ev[i][(int)Dol].KeyCode)) { gracze_ob[i].SetVY(gracze_ob[i].GetVY()+App.GetFrameTime()*przyspGDoc); if (cy) cy=false; else cy=true; } if (Input.IsKeyDown(gracze_ev[i][(int)Uderz].KeyCode)) { if (GraczPowLock[i]) gracze_ob[i].PopV(MaxVGraczNorm); else gracze_ob[i].PopV(MaxVGraczKop); } else { gracze_ob[i].PopV(MaxVGraczNorm); GraczPowLock[i]=false; } } /*for (int i=0; i<obiekty_il; i++) { (obiekty+i)->Animate(App.GetFrameTime(),bg,bd,bl,bp,bramka); } for (int i=0; i<obiekty_il-1; i++) for (int j=i+1; j<obiekty_il; j++) if (czy&&(obiekty+i)->ZderzenieAnaliz(App.GetFrameTime(),obiekty+j,bg,bd,bl,bp,bramka)) { cout << "ZDERZENIE!!!"; czy=true; }*/ double anisum=0; double tmp; double mintmp=10000000; double mini; int dodnum1,dodnum2; int num1,num2; num1=0; while(num1!=-1) { num1=-1; num2=-1; mini=1000000; for (int i=0; i<obiekty_il-1; i++) for (int j=i+1; j<obiekty_il; j++) { tmp=(obiekty+i)->TZderzenia(obiekty+j); if (tmp<mintmp) { mintmp=tmp; dodnum1=i; dodnum2=j; } if (tmp<mini&&tmp<=App.GetFrameTime()-anisum) { cout << "tmp = " << tmp << endl; mini=tmp; num1=i; num2=j; } } mini-=0.000001; if (mini<0) mini=0; if (num1!=-1) { anisum+=mini; for (int i=0; i<obiekty_il; i++) { (obiekty+i)->Animate(mini,bg,bd,bl,bp,bramka); } cout << "Przeliczanie zderzenia: " << num1 << " i " << num2 << endl; double mocude; if (num1==0&&num2>=5&&Input.IsKeyDown(gracze_ev[num2-5][(int)Uderz].KeyCode)&&!GraczPowLock[num2-5]) { mocude=(obiekty+num1)->PrzeliczZderzenie(obiekty+num2,MocKop); GraczPowLock[num2-5]=true; } else mocude=(obiekty+num1)->PrzeliczZderzenie(obiekty+num2); if (num1==0&&num2>=5) { if (mocude>10&&(kops_p.GetStatus()==sf::Sound::Stopped||kops_p.GetPlayingOffset()>0.2)) { cout << "KOPNIECIE DZWIEK"; kops_p.SetVolume(mocude/2); kops_p.Play(); } } } } for (int i=0; i<obiekty_il; i++) { (obiekty+i)->Animate(App.GetFrameTime()-anisum,bg,bd,bl,bp,bramka); } string tmpek; if (mintmp<100000) { tmpek+="czas do zderzenia: "; tmpek+=DoubleToString(mintmp); tmpek+=" obiektu: "; tmpek+=IntToString(dodnum1); tmpek+=" z: "; tmpek+=IntToString(dodnum2); } else { tmpek="brak zderzen..."; } Techdata.SetText(tmpek); string czas; if ((int)(Timer_c.GetElapsedTime()-korektatime)/60<10) czas+="0"; czas+=IntToString((int)(Timer_c.GetElapsedTime()-korektatime)/60); czas+=":"; if ((int)(Timer_c.GetElapsedTime()-korektatime)%60<10) czas+="0"; czas+=IntToString((int)(Timer_c.GetElapsedTime()-korektatime)%60); Timer_ob1.SetText(czas); //Timer_ob2.SetText(czas); return SBI(); }
void AnimatedTile::Draw(sf::RenderWindow& W){ anim.SetPosition(left, up); anim.Animate(W.GetFrameTime()); anim.Draw(W); }