SFML и C++ Уроки \ Разработка игр › Форумы › Логика игр › Стрельба по клику мыши › Ответ в теме: Стрельба по клику мыши
Для начала разберитесь как вы вобще заставили всё это работать через select case. По сути вы можете вовсе без него прожить передавая ускорение напрямую с каждой кнопки
C++
1 2 3 4 5 6 7 8 9 |
if (Keyboard::isKeyPressed(Keyboard::Right)) dx += -0.1; //Когда зажата стрелка вправо мы придаём импульс движения вправо public void onUpdate(Time delta) { //... sprite.move(dx * delta.asSeconds(), dy * delta.asSeconds());//Применяем смещение //... } |
Ну а направить пулю проще простого:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Vector2f vectorNormalize(Vector2f vector, Vector2f vector2) { float velocity = 1 / std::sqrt((abs((vector - vector2).x * 5)*abs((vector - vector2).x) * 5) + (abs((vector - vector2).y * 5)*abs((vector - vector2).y * 5))); return Vector2f(vector.x * velocity, vector.y * velocity); } //... if (event.type == Event::MouseButtonPressed) { if (event.key.code == Mouse::Left) { entities.push_back(new Bullet(BulletImage, "Bullet", lvl, p.x, p.y, 16, 16, p.state)); Vector2f direction = Vector2f(Mouse::getPosition(window).x - p.sprite.getPosition().x, Mouse::getPosition(window).y - p.sprite.getPosition().y); direction = vectorNormilize(direction, Vector(0, 0)); entities.back().velocity = Vector2f(direction.x * speed, direction.y * speed); } } //... //... public void onUpdate(Time delta) { sprite.move(velocity.x * delta.asSeconds(), velocity.y * delta.asSeconds()); } //... |