More cleanup, mostly removing variables and simplifying the math.
This commit is contained in:
parent
113df851af
commit
033d5cdfec
3 changed files with 41 additions and 51 deletions
39
main.cpp
39
main.cpp
|
@ -3,30 +3,30 @@
|
||||||
static const int SCREEN_HEIGHT=720;
|
static const int SCREEN_HEIGHT=720;
|
||||||
static const int SCREEN_WIDTH=1280;
|
static const int SCREEN_WIDTH=1280;
|
||||||
|
|
||||||
Matrix MAP{{8,8,8,8,8,8,8,8,8},
|
Matrix MAP{
|
||||||
{8,0,2,0,0,0,0,0,8},
|
{8,8,8,8,8,8,8,8,8},
|
||||||
{8,0,7,0,0,5,6,0,8},
|
{8,0,2,0,0,0,0,0,8},
|
||||||
{8,0,0,0,0,0,0,0,8},
|
{8,0,7,0,0,5,6,0,8},
|
||||||
{8,8,0,0,0,0,0,8,8},
|
{8,0,0,0,0,0,0,0,8},
|
||||||
{8,0,0,1,3,4,0,0,8},
|
{8,8,0,0,0,0,0,8,8},
|
||||||
{8,0,0,0,0,0,8,8,8},
|
{8,0,0,1,3,4,0,0,8},
|
||||||
{8,0,0,0,0,0,0,0,8},
|
{8,0,0,0,0,0,8,8,8},
|
||||||
{8,8,8,8,8,8,8,8,8}
|
{8,0,0,0,0,0,0,0,8},
|
||||||
};
|
{8,8,8,8,8,8,8,8,8}
|
||||||
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
using KB = sf::Keyboard;
|
using KB = sf::Keyboard;
|
||||||
|
|
||||||
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Zed's Ray Caster Game Thing");
|
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Zed's Ray Caster Game Thing");
|
||||||
|
|
||||||
int TILE_SIZE = RAY_VIEW_HEIGHT / matrix::width(MAP);
|
|
||||||
|
|
||||||
//ZED this should set with a function
|
//ZED this should set with a function
|
||||||
float player_x = RAY_VIEW_HEIGHT / 2;
|
float player_x = matrix::width(MAP) / 2;
|
||||||
float player_y = RAY_VIEW_HEIGHT / 2;
|
float player_y = matrix::height(MAP) / 2;
|
||||||
|
|
||||||
Raycaster rayview(window, MAP);
|
Raycaster rayview(window, MAP);
|
||||||
rayview.position_camera(player_x, player_y, TILE_SIZE);
|
|
||||||
|
rayview.position_camera(player_x, player_y);
|
||||||
|
|
||||||
double moveSpeed = 0.1;
|
double moveSpeed = 0.1;
|
||||||
double rotSpeed = 0.1;
|
double rotSpeed = 0.1;
|
||||||
|
@ -37,15 +37,15 @@ int main() {
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
if(KB::isKeyPressed(KB::W)) {
|
if(KB::isKeyPressed(KB::W)) {
|
||||||
rayview.move_forward(moveSpeed);
|
rayview.run(moveSpeed, 1);
|
||||||
} else if(KB::isKeyPressed(KB::S)) {
|
} else if(KB::isKeyPressed(KB::S)) {
|
||||||
rayview.move_backward(moveSpeed);
|
rayview.run(moveSpeed, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(KB::isKeyPressed(KB::D)) {
|
if(KB::isKeyPressed(KB::D)) {
|
||||||
rayview.rotate_right(rotSpeed);
|
rayview.rotate(rotSpeed, -1);
|
||||||
} else if(KB::isKeyPressed(KB::A)) {
|
} else if(KB::isKeyPressed(KB::A)) {
|
||||||
rayview.rotate_left(rotSpeed);
|
rayview.rotate(rotSpeed, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
|
@ -54,7 +54,6 @@ int main() {
|
||||||
window.close();
|
window.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -41,6 +41,7 @@ void TexturePack::load_image(std::vector<uint32_t>& texture, const char *filenam
|
||||||
sf::Image img;
|
sf::Image img;
|
||||||
bool good = img.loadFromFile(filename);
|
bool good = img.loadFromFile(filename);
|
||||||
dbc::check(good, format("failed to load {}", filename));
|
dbc::check(good, format("failed to load {}", filename));
|
||||||
|
|
||||||
uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr();
|
uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr();
|
||||||
std::copy_n(pixbuf, texture.size(), texture.begin());
|
std::copy_n(pixbuf, texture.size(), texture.begin());
|
||||||
}
|
}
|
||||||
|
@ -63,10 +64,10 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map) :
|
||||||
textures.load_textures();
|
textures.load_textures();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Raycaster::position_camera(float player_x, float player_y, int tile_size) {
|
void Raycaster::position_camera(float player_x, float player_y) {
|
||||||
// x and y start position
|
// x and y start position
|
||||||
posX = player_x / tile_size;
|
posX = player_x;
|
||||||
posY = player_y / tile_size;
|
posY = player_y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Raycaster::draw_pixel_buffer() {
|
void Raycaster::draw_pixel_buffer() {
|
||||||
|
@ -370,32 +371,24 @@ void Raycaster::sort_sprites(int* order, double* dist, int amount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Raycaster::move_forward(double moveSpeed) {
|
void Raycaster::run(double speed, int dir) {
|
||||||
if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed;
|
double speed_and_dir = speed * dir;
|
||||||
if(empty_space(int(posX), int(posY + dirY * moveSpeed))) posY += dirY * moveSpeed;
|
if(empty_space(int(posX + dirX * speed_and_dir), int(posY))) {
|
||||||
|
posX += dirX * speed_and_dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(empty_space(int(posX), int(posY + dirY * speed_and_dir))) {
|
||||||
|
posY += dirY * speed_and_dir;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Raycaster::move_backward(double moveSpeed) {
|
void Raycaster::rotate(double speed, int dir) {
|
||||||
if(empty_space(int(posX - dirX * moveSpeed), int(posY))) posX -= dirX * moveSpeed;
|
double speed_and_dir = speed * dir;
|
||||||
if(empty_space(int(posX), int(posY - dirY * moveSpeed))) posY -= dirY * moveSpeed;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Raycaster::rotate_right(double rotSpeed) {
|
|
||||||
double oldDirX = dirX;
|
double oldDirX = dirX;
|
||||||
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
dirX = dirX * cos(speed_and_dir) - dirY * sin(speed_and_dir);
|
||||||
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
dirY = oldDirX * sin(speed_and_dir) + dirY * cos(speed_and_dir);
|
||||||
|
|
||||||
double oldPlaneX = planeX;
|
double oldPlaneX = planeX;
|
||||||
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
planeX = planeX * cos(speed_and_dir) - planeY * sin(speed_and_dir);
|
||||||
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
planeY = oldPlaneX * sin(speed_and_dir) + planeY * cos(speed_and_dir);
|
||||||
}
|
|
||||||
|
|
||||||
void Raycaster::rotate_left(double rotSpeed) {
|
|
||||||
double oldDirX = dirX;
|
|
||||||
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
|
|
||||||
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
|
|
||||||
|
|
||||||
double oldPlaneX = planeX;
|
|
||||||
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
|
||||||
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,9 +86,7 @@ struct Raycaster {
|
||||||
void sort_sprites(int* order, double* dist, int amount);
|
void sort_sprites(int* order, double* dist, int amount);
|
||||||
|
|
||||||
// ZED these can be one or two functions
|
// ZED these can be one or two functions
|
||||||
void move_forward(double moveSpeed);
|
void run(double speed, int dir);
|
||||||
void move_backward(double moveSpeed);
|
void rotate(double speed, int dir);
|
||||||
void rotate_right(double rotSpeed);
|
void position_camera(float player_x, float player_y);
|
||||||
void rotate_left(double rotSpeed);
|
|
||||||
void position_camera(float player_x, float player_y, int tile_size);
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue