Raycaster is smoother without changing much. Big debate is should left-right and forward-back at the same time cancel out motion or should they be exclusive since you can't do both.
This commit is contained in:
parent
bf77723f70
commit
cf539296a5
1 changed files with 35 additions and 32 deletions
|
@ -112,47 +112,50 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
|||
}
|
||||
}
|
||||
|
||||
void draw_ceiling_floor(sf::RenderWindow &window) {
|
||||
draw_rect(window,
|
||||
{SCREEN_HEIGHT, SCREEN_HEIGHT /2},
|
||||
{SCREEN_HEIGHT, SCREEN_HEIGHT},
|
||||
100);
|
||||
draw_rect(window,
|
||||
{SCREEN_HEIGHT, (SCREEN_HEIGHT * -1) / 2},
|
||||
{SCREEN_HEIGHT, SCREEN_HEIGHT},
|
||||
200);
|
||||
}
|
||||
|
||||
void draw_everything(sf::RenderWindow &window) {
|
||||
draw_map(window, MAP);
|
||||
draw_ceiling_floor(window);
|
||||
ray_casting(window, MAP);
|
||||
window.display();
|
||||
}
|
||||
|
||||
int main() {
|
||||
using KB = sf::Keyboard;
|
||||
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Raycaster");
|
||||
window.setVerticalSyncEnabled(true);
|
||||
|
||||
while(window.isOpen()) {
|
||||
draw_everything(window);
|
||||
|
||||
if(KB::isKeyPressed(KB::A)) {
|
||||
player_angle -= 0.1;
|
||||
} else if(KB::isKeyPressed(KB::D)) {
|
||||
player_angle += 0.1;
|
||||
}
|
||||
|
||||
if(KB::isKeyPressed(KB::W)) {
|
||||
player_x += -1 * std::sin(player_angle) * 5;
|
||||
player_y += std::cos(player_angle) * 5;
|
||||
} else if(KB::isKeyPressed(KB::S)) {
|
||||
player_x -= -1 * std::sin(player_angle) * 5;
|
||||
player_y -= std::cos(player_angle) * 5;
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
|
||||
draw_map(window, MAP);
|
||||
|
||||
draw_rect(window,
|
||||
{480, SCREEN_HEIGHT /2},
|
||||
{SCREEN_HEIGHT, SCREEN_HEIGHT},
|
||||
100);
|
||||
|
||||
draw_rect(window,
|
||||
{480, (SCREEN_HEIGHT * -1) / 2},
|
||||
{SCREEN_HEIGHT, SCREEN_HEIGHT},
|
||||
200);
|
||||
|
||||
ray_casting(window, MAP);
|
||||
|
||||
window.display();
|
||||
|
||||
while(window.pollEvent(event)) {
|
||||
using KB = sf::Keyboard;
|
||||
|
||||
if(event.type == sf::Event::Closed) {
|
||||
window.close();
|
||||
} else if(event.type == sf::Event::KeyPressed) {
|
||||
if(KB::isKeyPressed(KB::Left)) {
|
||||
player_angle -= 0.1;
|
||||
} else if(KB::isKeyPressed(KB::Right)) {
|
||||
player_angle += 0.1;
|
||||
} else if(KB::isKeyPressed(KB::Up)) {
|
||||
player_x += -1 * std::sin(player_angle) * 5;
|
||||
player_y += std::cos(player_angle) * 5;
|
||||
} else if(KB::isKeyPressed(KB::Down)) {
|
||||
player_x -= -1 * std::sin(player_angle) * 5;
|
||||
player_y -= std::cos(player_angle) * 5;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue