Improve input system

cpp
Elnu 3 years ago
parent 4f53a00f41
commit 18253d188e

@ -2,6 +2,7 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
#include <iostream> #include <iostream>
@ -152,7 +153,9 @@ int main()
int shape_height = WINDOW_HEIGHT / GRID_HEIGHT; int shape_height = WINDOW_HEIGHT / GRID_HEIGHT;
sf::RectangleShape shape(sf::Vector2f(shape_width, shape_height)); sf::RectangleShape shape(sf::Vector2f(shape_width, shape_height));
bool snap, rotate; bool snap, rotate, move_left, move_right;
sf::Clock update_clock;
sf::Clock move_clock;
while (window.isOpen()) while (window.isOpen())
{ {
@ -171,6 +174,12 @@ int main()
case sf::Keyboard::Up: case sf::Keyboard::Up:
rotate = true; rotate = true;
break; break;
case sf::Keyboard::Left:
move_left = true;
break;
case sf::Keyboard::Right:
move_right = true;
break;
default: default:
break; break;
} }
@ -180,13 +189,20 @@ int main()
} }
} }
window.clear(); bool is_update_frame = update_clock.getElapsedTime().asMilliseconds() > 250;
if (is_update_frame) {
update_clock.restart();
}
bool is_move_frame = move_clock.getElapsedTime().asMilliseconds() > 125;
if (is_move_frame) {
move_clock.restart();
}
// Fast forward window.clear();
window.setFramerateLimit(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down) ? 8 : 6);
// Rotation // Rotation
if (rotate) { if (rotate && is_move_frame) {
block.rotation_state++; block.rotation_state++;
// Check to see if new rotation state is overlapping any tiles // Check to see if new rotation state is overlapping any tiles
for (auto tile : block.get_tiles()) { for (auto tile : block.get_tiles()) {
@ -199,15 +215,20 @@ int main()
} }
// Horizontal movement // Horizontal movement
if (is_move_frame) {
int movement = 0; int movement = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) if (move_left || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
movement--; movement--;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) move_left = false;
}
if (move_right ||sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) {
movement++; movement++;
move_right = false;
}
bool obstructed = false; bool obstructed = false;
if (movement != 0) { if (movement != 0) {
for (auto tile : block.get_tiles()) { for (auto tile : block.get_tiles()) {
if (tile.x <= 0 || tile.x >= GRID_WIDTH - 1 || grid[tile.y][tile.x + movement]) { if (tile.x + movement < 0 || tile.x + movement >= GRID_WIDTH || grid[tile.y][tile.x + movement]) {
obstructed = true; obstructed = true;
goto after_movement_loop; goto after_movement_loop;
} }
@ -217,6 +238,7 @@ int main()
if (!obstructed) { if (!obstructed) {
block.position.x += movement; block.position.x += movement;
} }
}
// Snapping // Snapping
int snap_offset = 0; int snap_offset = 0;
@ -278,7 +300,7 @@ int main()
} }
} }
block = Block(); block = Block();
} else { } else if(is_update_frame) {
block.position.y++; block.position.y++;
} }

Loading…
Cancel
Save