Add fast forwarding

cpp
Elnu 3 years ago
parent 96a0254208
commit 3c861bd5e2

@ -1,3 +1,4 @@
#include <SFML/Window/Keyboard.hpp>
#define WINDOW_WIDTH 400 #define WINDOW_WIDTH 400
#define WINDOW_HEIGHT 400 #define WINDOW_HEIGHT 400
@ -82,12 +83,19 @@ int main()
sf::Event event; sf::Event event;
while (window.pollEvent(event)) while (window.pollEvent(event))
{ {
if (event.type == sf::Event::Closed) switch (event.type) {
window.close(); case sf::Event::Closed:
window.close();
default:
break;
}
} }
window.clear(); window.clear();
// Fast forward
window.setFramerateLimit(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down) ? 16 : 8);
// Horizontal movement // Horizontal movement
int movement = 0; int movement = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
@ -116,27 +124,30 @@ int main()
} }
// Snapping // Snapping
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) { int snap_y = block.position.y;
while (true) { while (true) {
for (int y = 0; y < 2; y++) { for (int y = 0; y < 2; y++) {
for (int x = 0; x < 4; x++) { for (int x = 0; x < 4; x++) {
if (!block.type->grid[y][x]) { if (!block.type->grid[y][x]) {
continue; continue;
} }
int global_x = x + block.position.x; int global_x = x + block.position.x;
int global_y = y + block.position.y; int global_y = y + snap_y;
if (global_y == GRID_HEIGHT - 1 || grid[global_y + 1][global_x] != nullptr) { if (global_y == GRID_HEIGHT - 1 || grid[global_y + 1][global_x] != nullptr) {
goto after_snap_loop; goto after_snap_loop;
}
} }
} }
block.position.y++;
} }
snap_y++;
} }
after_snap_loop: after_snap_loop:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
block.position.y = snap_y;
}
// Drawing block and land checking // Drawing block and land checking
shape.setFillColor(block.type->tile_type->color); sf::Color ghost_color = block.type->tile_type->color;
ghost_color.a = 64;
bool landed = false; bool landed = false;
for (int y = 0; y < 2; y++) { for (int y = 0; y < 2; y++) {
for (int x = 0; x < 4; x++) { for (int x = 0; x < 4; x++) {
@ -145,11 +156,16 @@ int main()
} }
int global_x = x + block.position.x; int global_x = x + block.position.x;
int global_y = y + block.position.y; int global_y = y + block.position.y;
int global_snap_y = snap_y + y;
if (global_y == GRID_HEIGHT - 1 || grid[global_y + 1][global_x] != nullptr) { if (global_y == GRID_HEIGHT - 1 || grid[global_y + 1][global_x] != nullptr) {
landed = true; landed = true;
} }
shape.setFillColor(block.type->tile_type->color);
shape.setPosition(global_x * shape_width, global_y * shape_height); shape.setPosition(global_x * shape_width, global_y * shape_height);
window.draw(shape); window.draw(shape);
shape.setFillColor(ghost_color);
shape.setPosition(global_x * shape_width, global_snap_y * shape_height);
window.draw(shape);
} }
} }

Loading…
Cancel
Save