From 8ac68e31e7e5bc342db42c6fe97d3dde470e9e60 Mon Sep 17 00:00:00 2001 From: koma Date: Thu, 5 May 2022 17:20:15 +0200 Subject: [PATCH] block placing --- include/app.h | 3 ++ include/camera.h | 2 +- src/app.c | 76 ++++++++++++++++++++++++++++++++++++++++++------ src/chunk.c | 2 +- 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/include/app.h b/include/app.h index e51bb4f..6081f62 100644 --- a/include/app.h +++ b/include/app.h @@ -39,8 +39,11 @@ void app_load_textures(App *app); GLuint app_get_texture(App *app, Block_type type); int check_intersection_block(App *app, vec3i *retv, int *chunk_index); + void app_break_block(App *app); +void app_place_block(App *app); + bool app_check_collision(vec3f camera_pos, ChunkManager *cm); void init_app(App *app, uint w, uint h); diff --git a/include/camera.h b/include/camera.h index 38e14b3..b63f110 100644 --- a/include/camera.h +++ b/include/camera.h @@ -6,7 +6,7 @@ #include #include "chunk.h" -#define GRAVITY 1 +#define GRAVITY 5 /** * Camera, as a moving point with direction */ diff --git a/src/app.c b/src/app.c index e3dcfa5..8c38c13 100644 --- a/src/app.c +++ b/src/app.c @@ -57,7 +57,7 @@ void app_load_textures(App *app) app->surfaces[1].surface = IMG_Load("assets/block.png"); app->surfaces[2].surface = IMG_Load("assets/block.png"); - for (int i = 0; i < app->surface_count-2; i++) { + for (int i = 0; i < app->surface_count-1; i++) { if(app->surfaces[i].surface == NULL) { printf("error loading texture\n"); exit(1); @@ -78,15 +78,12 @@ void app_load_textures(App *app) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid); + //glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid); } } - - void update_app(App* app) { - update_camera(&(app->camera), app->frame_time/1000); } @@ -194,8 +191,13 @@ void handle_events(App* app) } break; case SDL_MOUSEBUTTONDOWN: - app_break_block(app); - break; + if (event.button.button == SDL_BUTTON_LEFT) { + app_break_block(app); + break; + } else if (event.button.button == SDL_BUTTON_RIGHT) { + app_place_block(app); + break; + } case SDL_MOUSEMOTION: SDL_GetMouseState(&x, &y); rotate_camera(&(app->camera), mouse_x - x, mouse_y - y); @@ -336,7 +338,7 @@ int check_intersection_block(App *app, vec3i *retv, int *chunk_index) for (int y = 0; y < CHUNK_MAX_Y; y++) { for (int z = 0; z < CHUNK_MAX_Z; z++) { if (app->cm->chunks[i].blocks[x][y][z].visible) { - for (float j = 0; j < 8; j+=0.2) { + for (float j = 0; j < 5; j+=0.2) { vec3f ray = {(c_pos.x - app->cm->chunks[i].start_pos.x) + c_dir.x * j, (c_pos.y - app->cm->chunks[i].start_pos.y) + c_dir.y * j, (c_pos.z - app->cm->chunks[i].start_pos.z) + c_dir.z * j}; @@ -369,10 +371,66 @@ void app_break_block(App *app) //printf("chunk %d. min x: %f y: %f max x: %f y: %f\n", i, app->chunks[i].start_pos.x, // app->chunks[i].start_pos.y, app->chunks[i].start_pos.x+CHUNK_MAX_X, app->chunks[i].start_pos.y+CHUNK_MAX_Y); //printf("broken block x: %f y: %f z: %f\n", block.x+app->chunks[i].start_pos.x, block.y+app->chunks[i].start_pos.y, block.z+app->chunks[i].start_pos.z); - if(ret < 0) + if (ret < 0) return; app->cm->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR; chunk_update(app->cm, &(app->cm->chunks[i])); } +void app_place_block(App *app) +{ + float dist = 5.0; + vec3f cam_pos = app->camera.position; + vec3i block; + int i; + int ret = check_intersection_block(app, &block, &i); + + if (ret < 0) + return; + + vec3f t = { (float) cam_pos.x - block.x, + (float) cam_pos.y - block.y, + (float) cam_pos.z - block.z }; + if (t.x > 0) { + if (t.y < t.x && t.y > -t.x) { + app->cm->chunks[i].blocks[block.x+1][block.y][block.z].type = BLOCKTYPE_STONE; + chunk_update(app->cm, &(app->cm->chunks[i])); + return; + } + } else { + if (t.y < -t.x && t.y > t.x) { + app->cm->chunks[i].blocks[block.x-1][block.y][block.z].type = BLOCKTYPE_STONE; + chunk_update(app->cm, &(app->cm->chunks[i])); + return; + } + } + + if (t.y > 0) { + app->cm->chunks[i].blocks[block.x][block.y+1][block.z].type = BLOCKTYPE_STONE; + chunk_update(app->cm, &(app->cm->chunks[i])); + return; + } else { + app->cm->chunks[i].blocks[block.x][block.y-1][block.z].type = BLOCKTYPE_STONE; + chunk_update(app->cm, &(app->cm->chunks[i])); + return; + } + + //if (cam_pos.x < (float)block.x+dist/2 && + // cam_pos.x > (float)block.x-dist/2) { + // if (cam_pos.x > block.x) + // app->cm->chunks[i].blocks[block.x+1][block.y][block.z].type = BLOCKTYPE_STONE; + // if (cam_pos.x < block.x) + // app->cm->chunks[i].blocks[block.x-1][block.y][block.z].type = BLOCKTYPE_STONE; + // chunk_update(app->cm, &(app->cm->chunks[i])); + //} + //if (cam_pos.y < (float)block.y+dist/2 && + // cam_pos.y > (float)block.y-dist/2) { + // if (cam_pos.y > block.y) + // app->cm->chunks[i].blocks[block.x][block.y+1][block.z].type = BLOCKTYPE_STONE; + // if (cam_pos.y < block.y) + // app->cm->chunks[i].blocks[block.x][block.y-1][block.z].type = BLOCKTYPE_STONE; + // chunk_update(app->cm, &(app->cm->chunks[i])); + //} +} + diff --git a/src/chunk.c b/src/chunk.c index 3762c85..11e1b92 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -100,7 +100,7 @@ void draw_cube(ChunkManager *cm, Block_type type, float x, float y, float z) void chunk_create_displayl(ChunkManager *cm, Chunk *chunk) { chunk->id = glGenLists(1); - glBindTexture(GL_TEXTURE_2D, cm->textures[0].tid); + //glBindTexture(GL_TEXTURE_2D, cm->textures[0].tid); glNewList(chunk->id, GL_COMPILE); for (int x = 0; x < CHUNK_MAX_X; x++) { for (int y = 0; y < CHUNK_MAX_Y; y++) {