place / break blocks

This commit is contained in:
onezplpl 2024-07-18 11:06:32 -04:00
parent fb639f96c3
commit 227d642be6
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
7 changed files with 191 additions and 55 deletions

View file

@ -2,6 +2,7 @@
#include "gen.h"
#include "linmath.h"
#include <GLFW/glfw3.h>
#include <stdio.h>
int is_space;
@ -33,7 +34,7 @@ int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) {
}
struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
GLFWwindow *window, struct chunk chunk) {
GLFWwindow *window, struct chunk chunk) {
vec3 direction = {cosf(cy) * sinf(cx) * 0.1, sinf(cy) * 0.1,
cosf(cy) * cosf(cx) * 0.1};
vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.1, 0,
@ -52,10 +53,6 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
vec3_add(dir_temp, dir_temp, front);
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
vec3_add(dir_temp, dir_temp, direction);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
vec3_add(dir_temp, dir_temp, right);
}
@ -71,7 +68,7 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
for (int a = -2; a < 0; a++) {
off[1] = a;
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2],
chunk, 0);
chunk, 0, -1);
}
vec3_add(pos, pos, dir_temp);
@ -80,8 +77,8 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
col = 0;
for (int a = -2; a < 0; a++) {
off[1] = a;
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1],
pos[2] + off[2], chunk, 0);
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2],
chunk, 0, -1);
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
@ -101,4 +98,66 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
vec3_dup(dirOut.pos, direction);
return dirOut;
}
struct v3f control_rtrace(double cx, double cy, vec3 pos, int mode) {
struct chunk placeChunk;
vec3 direction = {cosf(cy) * sinf(cx) * 0.1, sinf(cy) * 0.1,
cosf(cy) * cosf(cx) * 0.1};
vec3 temp_pos;
vec3 temp_dir;
vec3_dup(temp_pos, pos);
for (int i = 0; i < CHUNK_LENGTH * 4; i++) {
double xd =
fabs(direction[0]) / (((direction[0] > 0) ? (1.0 - fmod(temp_pos[0], 1))
: fmod(temp_pos[0], 1)) +
0.01);
double yd =
fabs(direction[1]) / (((direction[1] > 0) ? (1.0 - fmod(temp_pos[1], 1))
: fmod(temp_pos[1], 1)) +
0.01);
double zd =
fabs(direction[2]) / (((direction[2] > 0) ? (1.0 - fmod(temp_pos[2], 1))
: fmod(temp_pos[2], 1)) +
0.01);
if (xd > yd && xd > zd && !isnan(xd)) {
vec3_scale(temp_dir, direction, fabs(1 / xd + 0.01));
} else if (yd > zd && !isnan(yd)) {
vec3_scale(temp_dir, direction, fabs(1 / yd + 0.01));
} else if (!isnan(zd)) {
vec3_scale(temp_dir, direction, fabs(1 / zd + 0.01));
}
vec3_add(temp_pos, temp_pos, temp_dir);
if (gen_cube(temp_pos[0],temp_pos[1],temp_pos[2],placeChunk,0,-1) != 0) {
if (mode == 0) vec3_sub(temp_pos,temp_pos,temp_dir);
break;
}
}
struct v3f out_pos;
vec3_dup(out_pos.pos, temp_pos);
return out_pos;
}
void control_lclick(double cx, double cy, vec3 pos) {
struct v3f highlighted = control_rtrace(cx, cy, pos, 1);
struct chunk placeChunk;
int cube = gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,-1);
if (cube == 0) return;
gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,0);
}
void control_rclick(double cx, double cy, vec3 pos) {
struct v3f highlighted = control_rtrace(cx, cy, pos, 0);
struct chunk placeChunk;
int cube = gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,-1);
if (cube != 0) return;
gen_cube(highlighted.pos[0],highlighted.pos[1],highlighted.pos[2],placeChunk,0,1);
}