void PlayerInputController::keysProcessing(const float dt) const
{
for (const auto& tank : tanks)
{
tank->updateDirect();
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveUpCommand->getKey()))
{
tank->keysAssignment.moveUpCommand->execute(tank, dt);
}
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveDownCommand->getKey()))
{
tank->keysAssignment.moveDownCommand->execute(tank, dt);
}
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveLeftCommand->getKey()))
{
tank->keysAssignment.moveLeftCommand->execute(tank, dt);
}
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveRightCommand->getKey()))
{
tank->keysAssignment.moveRightCommand->execute(tank, dt);
}
}
}
void Game::updateEvents()
{
sf::Event ev;
std::cout << "t1 = " << _t1->_sprite.getRotation() << '\n';
std::cout << "t2 = " << _t2->_sprite.getRotation() << '\n';
while (_window->pollEvent(ev))
{
if (ev.type == sf::Event::Closed)
{
_window->close();
}
}
inputController_->keysProcessing(_deltaTime);
}
void PlayerInputController::keysProcessing(const float dt) const
{
for (const auto& tank : tanks)
{
tank->updateDirect();
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveUpCommand->getKey()))
{
tank->keysAssignment.moveUpCommand->execute(tank, dt);
}
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveDownCommand->getKey()))
{
tank->keysAssignment.moveDownCommand->execute(tank, dt);
}
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveLeftCommand->getKey()))
{
tank->keysAssignment.moveLeftCommand->execute(tank, dt);
}
if (sf::Keyboard::isKeyPressed(tank->keysAssignment.moveRightCommand->getKey()))
{
tank->keysAssignment.moveRightCommand->execute(tank, dt);
}
}
}
if (isPressedUp && !isReleasedUp)
{
_t2->keysAssignment.moveUpCommand->execute(_t2, _deltaTime);
std::cout << "move t2" << '\n';
}
if (isPressedW && !isReleasedW)
{
_t1->keysAssignment.moveUpCommand->execute(_t1, _deltaTime);
std::cout << "move t1" << '\n';
}
Game::Game()
{
_window = new sf::RenderWindow(sf::VideoMode(WIDTH, HEIGHT), TITLE, sf::Style::Close);
_window->setFramerateLimit(FPS);
_window->setVerticalSyncEnabled(true);
_bg = new BackGround();
_t1 = new Tank(TypeTank::RedBlack, sf::Keyboard::Key::A, sf::Keyboard::Key::D, sf::Keyboard::Key::W,
sf::Keyboard::Key::S);
_t2 = new Tank(TypeTank::RedBlack, sf::Keyboard::Key::Left, sf::Keyboard::Key::Right, sf::Keyboard::Key::Up,
sf::Keyboard::Key::Down);
_t1->_sprite.setPosition(WIDTH / 2, HEIGHT / 2);
_t2->_sprite.setPosition(WIDTH / 2, HEIGHT / 2);
inputController_ = new PlayerInputController(std::vector<Tank*>{_t1, _t2});
}
void Game::updateEvents()
{
sf::Event ev;
while (_window->pollEvent(ev))
{
if (ev.type == sf::Event::Closed)
{
_window->close();
}
// Обработка нажатий игрока
if (ev.type == sf::Event::KeyPressed)
{
inputController_->keysProcessing(ev.key.code, _deltaTime);
}
}
}
void PlayerInputController::keysProcessing(const sf::Keyboard::Key key, float dt) const
{
for (const auto& tank : tanks)
{
if ((tank->keysAssignment).moveRightCommand->getKey() == key)
(tank->keysAssignment).moveRightCommand->execute(tank, dt);
if ((tank->keysAssignment).moveLeftCommand->getKey() == key)
(tank->keysAssignment).moveLeftCommand->execute(tank, dt);
if ((tank->keysAssignment).moveUpCommand->getKey() == key)
(tank->keysAssignment).moveUpCommand->execute(tank, dt);
if ((tank->keysAssignment).moveDownCommand->getKey() == key)
(tank->keysAssignment).moveDownCommand->execute(tank, dt);
}
}
public class InputControllerWASD : MonoBehaviour
{
void Update()
{
if(Input.GetKey(KeyCode.W))
{
// какое-то действите
}
}
}
public class InputControllerArrows : MonoBehaviour
{
void Update()
{
if(Input.GetKey(KeyCode.LeftArrow))
{
// какое-то действите
}
}
}
Просто сейчас хотелось бы узнать, почему isKeyPressed и вынесение обработки вне цикла, сделало то, что сейчас происходит, а т.е Почему скорость персонажей возросла ?