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
21
main.cpp
21
main.cpp
|
@ -3,7 +3,8 @@
|
|||
static const int SCREEN_HEIGHT=720;
|
||||
static const int SCREEN_WIDTH=1280;
|
||||
|
||||
Matrix MAP{{8,8,8,8,8,8,8,8,8},
|
||||
Matrix MAP{
|
||||
{8,8,8,8,8,8,8,8,8},
|
||||
{8,0,2,0,0,0,0,0,8},
|
||||
{8,0,7,0,0,5,6,0,8},
|
||||
{8,0,0,0,0,0,0,0,8},
|
||||
|
@ -19,14 +20,13 @@ int main() {
|
|||
|
||||
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
|
||||
float player_x = RAY_VIEW_HEIGHT / 2;
|
||||
float player_y = RAY_VIEW_HEIGHT / 2;
|
||||
float player_x = matrix::width(MAP) / 2;
|
||||
float player_y = matrix::height(MAP) / 2;
|
||||
|
||||
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 rotSpeed = 0.1;
|
||||
|
@ -37,15 +37,15 @@ int main() {
|
|||
window.display();
|
||||
|
||||
if(KB::isKeyPressed(KB::W)) {
|
||||
rayview.move_forward(moveSpeed);
|
||||
rayview.run(moveSpeed, 1);
|
||||
} else if(KB::isKeyPressed(KB::S)) {
|
||||
rayview.move_backward(moveSpeed);
|
||||
rayview.run(moveSpeed, -1);
|
||||
}
|
||||
|
||||
if(KB::isKeyPressed(KB::D)) {
|
||||
rayview.rotate_right(rotSpeed);
|
||||
rayview.rotate(rotSpeed, -1);
|
||||
} else if(KB::isKeyPressed(KB::A)) {
|
||||
rayview.rotate_left(rotSpeed);
|
||||
rayview.rotate(rotSpeed, 1);
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
|
@ -54,7 +54,6 @@ int main() {
|
|||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -41,6 +41,7 @@ void TexturePack::load_image(std::vector<uint32_t>& texture, const char *filenam
|
|||
sf::Image img;
|
||||
bool good = img.loadFromFile(filename);
|
||||
dbc::check(good, format("failed to load {}", filename));
|
||||
|
||||
uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr();
|
||||
std::copy_n(pixbuf, texture.size(), texture.begin());
|
||||
}
|
||||
|
@ -63,10 +64,10 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map) :
|
|||
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
|
||||
posX = player_x / tile_size;
|
||||
posY = player_y / tile_size;
|
||||
posX = player_x;
|
||||
posY = player_y;
|
||||
}
|
||||
|
||||
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) {
|
||||
if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed;
|
||||
if(empty_space(int(posX), int(posY + dirY * moveSpeed))) posY += dirY * moveSpeed;
|
||||
void Raycaster::run(double speed, int dir) {
|
||||
double speed_and_dir = speed * dir;
|
||||
if(empty_space(int(posX + dirX * speed_and_dir), int(posY))) {
|
||||
posX += dirX * speed_and_dir;
|
||||
}
|
||||
|
||||
void Raycaster::move_backward(double moveSpeed) {
|
||||
if(empty_space(int(posX - dirX * moveSpeed), int(posY))) posX -= dirX * moveSpeed;
|
||||
if(empty_space(int(posX), int(posY - dirY * moveSpeed))) posY -= dirY * moveSpeed;
|
||||
if(empty_space(int(posX), int(posY + dirY * speed_and_dir))) {
|
||||
posY += dirY * speed_and_dir;
|
||||
}
|
||||
}
|
||||
|
||||
void Raycaster::rotate_right(double rotSpeed) {
|
||||
void Raycaster::rotate(double speed, int dir) {
|
||||
double speed_and_dir = speed * dir;
|
||||
double oldDirX = dirX;
|
||||
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
||||
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
||||
dirX = dirX * cos(speed_and_dir) - dirY * sin(speed_and_dir);
|
||||
dirY = oldDirX * sin(speed_and_dir) + dirY * cos(speed_and_dir);
|
||||
|
||||
double oldPlaneX = planeX;
|
||||
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
||||
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
||||
}
|
||||
|
||||
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);
|
||||
planeX = planeX * cos(speed_and_dir) - planeY * sin(speed_and_dir);
|
||||
planeY = oldPlaneX * sin(speed_and_dir) + planeY * cos(speed_and_dir);
|
||||
}
|
||||
|
|
|
@ -86,9 +86,7 @@ struct Raycaster {
|
|||
void sort_sprites(int* order, double* dist, int amount);
|
||||
|
||||
// ZED these can be one or two functions
|
||||
void move_forward(double moveSpeed);
|
||||
void move_backward(double moveSpeed);
|
||||
void rotate_right(double rotSpeed);
|
||||
void rotate_left(double rotSpeed);
|
||||
void position_camera(float player_x, float player_y, int tile_size);
|
||||
void run(double speed, int dir);
|
||||
void rotate(double speed, int dir);
|
||||
void position_camera(float player_x, float player_y);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue