diff --git a/.vscode/settings.json b/.vscode/settings.json index 74f4b13..0ba4fe1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,9 @@ "sdl.h": "c", "triangles.h": "c", "base.h": "c", - "move.h": "c" + "move.h": "c", + "hash.h": "c", + "proj.h": "c", + "entities.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 1e3762e..80cc013 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ all: bin/back test: bin/back bin/back -bin/back: obj/main.o obj/generation.o obj/display.o obj/entities.o obj/bullets.o obj/menus.o obj/triangles.o obj/move.o obj/base.o obj/hash.o +bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/triangles.o obj/move.o obj/base.o obj/hash.o mkdir -p bin $(CC) $(FLAGS) $^ $(LFLAGS) -o $@ @@ -23,6 +23,7 @@ obj/triangles.o: src/triangles.c obj/bullets.o: src/bullets.c obj/move.o: src/move.c obj/base.o: src/base.c +obj/proj.o: src/proj.c obj/menus.o: src/menus.c obj/hash.o: src/hash.c diff --git a/bin/back b/bin/back index e8845c7..0b6b91d 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/display.o b/obj/display.o index b490315..2f7305e 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/entities.o b/obj/entities.o index b6f148f..a8b8346 100644 Binary files a/obj/entities.o and b/obj/entities.o differ diff --git a/obj/generation.o b/obj/generation.o index 42073c9..fb99e0b 100644 Binary files a/obj/generation.o and b/obj/generation.o differ diff --git a/obj/main.o b/obj/main.o index 6666d2c..7935b6a 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/proj.o b/obj/proj.o new file mode 100644 index 0000000..6dc5384 Binary files /dev/null and b/obj/proj.o differ diff --git a/src/display.c b/src/display.c index 02c96ec..06f8d5a 100644 --- a/src/display.c +++ b/src/display.c @@ -16,6 +16,7 @@ #include "base.h" #include "triangles.h" #include "move.h" +#include "proj.h" #include "entities.h" #include "generation.h" #include "display.h" @@ -663,7 +664,9 @@ void addTriangleRotated( rotate_cube(x0, y0, z0, &px0, &py0, &pz0, cb); rotate_cube(x1, y1, z1, &px1, &py1, &pz1, cb); rotate_cube(x2, y2, z2, &px2, &py2, &pz2, cb); - addTriangle(px0, py0, pz0, px1, py1, pz1, px2, py2, pz2, red, green, blue); + if(triangles_i < MAX_SIZE -1) { + addTriangle(px0, py0, pz0, px1, py1, pz1, px2, py2, pz2, red, green, blue); + } } // -------------------------------------------------------------------------------------------------------------------------------- // @@ -801,6 +804,7 @@ void drawCurrentRoom(SDL_Renderer* renderer) { add_triangles_cb(current_room->map, current_room->map_size); add_triangles_tp(current_room->tps, current_room->tps_size); add_triangles_ent(current_room->ents, current_room->ent_len); + drawProj(); topological_sort(); //remove_hidden(renderer); //topological_sort(); diff --git a/src/display.h b/src/display.h index 74bdbd5..53296d1 100644 --- a/src/display.h +++ b/src/display.h @@ -29,6 +29,13 @@ double square_z_distance_to_camera( double x2, double y2, double z2 ); +void addTriangle( + double x0, double y0, double z0, + double x1, double y1, double z1, + double x2, double y2, double z2, + int red, int green, int blue +); + void axialRotation_X0(double* y, double* z, double theta); void axialRotation_X(double* y, double* z, double theta, double cst_y, double cst_z); void axialRotation_Y0(double* x, double* z, double theta); diff --git a/src/entities.c b/src/entities.c index 99a3eea..1d06454 100644 --- a/src/entities.c +++ b/src/entities.c @@ -14,6 +14,8 @@ #include "hash.h" #include "structure.h" #include "base.h" +#include "display.h" +#include "proj.h" #include "entities.h" // ------------------------------------------------------------------------------------------------------------------------------------------------ // @@ -65,6 +67,14 @@ void speen2(double x, double y, double z, double w, double h, double d, double h ret->hz_angle += ((double)dtime)*15.0; } +void speen3(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) { + ret->vt_angle += ((double)dtime)*22.5; + if((int)(ret->vt_angle) != (int)(ret->vt_angle - ((double)dtime)*22.5)) { + appendProj(x, y, z, 10.0 + rand()%15, 10.0 + rand()%15, 0.0, 0.0, 0.0, 0.0, 32, 32, 255); + //printf("app\n"); + } +} + void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) { if(ret->red == 193) { ret->red = 0; @@ -84,20 +94,23 @@ void go_to_player(double x, double y, double z, double w, double h, double d, do double dy = (y+h/2 - camy); double dz = (z+d/2 - camz); double total = sqrt(dx*dx + dy*dy + dz*dz) ; - dx = 7.0*dx/total; - dy = 7.0*dy/total; - dz = 7.0*dz/total; - ret->x -=12.0*dtime*dx ; + dx = 110.0*dx/total; + dy = 110.0*dy/total; + dz = 110.0*dz/total; + ret->x -= dtime*dx ; if(is_colliding_with_map(*ret) || is_colliding_with_tp(*ret)) { - ret->x +=12.0*dtime*dx ; + ret->x += dtime*dx ; } - ret->y -=12.0*dtime*dy ; + ret->y -= dtime*dy ; if(is_colliding_with_map(*ret) || is_colliding_with_tp(*ret)) { - ret->y +=12.0*dtime*dy ; + ret->y += dtime*dy ; } - ret->z -=12.0*dtime*dz ; + ret->z -= dtime*dz ; if(is_colliding_with_map(*ret) || is_colliding_with_tp(*ret)) { - ret->z +=12.0*dtime*dz ; + ret->z += dtime*dz ; + } + if((int)(ret->x+ret->y+ret->z) != (int)(ret->x+ret->y+ret->z-dx-dy-dz)) { + } } diff --git a/src/entities.h b/src/entities.h index 173c1c5..139761e 100644 --- a/src/entities.h +++ b/src/entities.h @@ -9,6 +9,7 @@ void update_entities(float dtime); void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret); void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret); +void speen3(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret); void go_to_player(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret); diff --git a/src/generation.c b/src/generation.c index 5ddb130..2a2433a 100644 --- a/src/generation.c +++ b/src/generation.c @@ -56,6 +56,12 @@ void init_ent_generator(int n) { hashtbl_entities[2].updatePos = &go_to_player ; hashtbl_entities[2].onHit = &explodeOnHit ; hashtbl_entities[2].onDeath = NULL ; + + hashtbl_entities[3].id = 3; + hashtbl_entities[3].name = "ExplosiveShoot"; + hashtbl_entities[3].updatePos = &speen3 ; + hashtbl_entities[3].onHit = &explodeOnHit ; + hashtbl_entities[3].onDeath = NULL ; } fct_entry* get_entry(int k0) { diff --git a/src/main.c b/src/main.c index 8faab32..7bf2df0 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ #include "base.h" #include "move.h" #include "triangles.h" +#include "proj.h" #include "entities.h" #include "display.h" #include "generation.h" @@ -63,6 +64,7 @@ int main(int argc, char** argv) { init_draworder(); init_ent_generator(10); trInit(); + init_proj(); parse_rooms(5); import_digits(rend) ; import_letters(rend) ; @@ -91,11 +93,15 @@ int main(int argc, char** argv) { entend = clock(); update_entities(((float)entend - (float)entstart)/CLOCKS_PER_SEC); + updateAllProj(((float)entend - (float)entstart)/CLOCKS_PER_SEC); //printf("03\n"); fflush(stdout); entstart = clock(); + //printf("-->%d\n", triangles_i); drawCurrentRoom(rend); + //printf("-->%d\n", triangles_i); + //printf("-->%d\n", triangles_i); //printf("04\n"); //printf("%s\n", SDL_GetError()); fflush(stdout); @@ -138,6 +144,7 @@ int main(int argc, char** argv) { free(blues); free(triangles_order); free(visited_tri); + free_proj(); //printf("10\n"); fflush(stdout); hashtbl_free(visited); diff --git a/src/proj.c b/src/proj.c new file mode 100644 index 0000000..5082292 --- /dev/null +++ b/src/proj.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "structure.h" +#include "hash.h" +#include "base.h" +#include "move.h" +#include "entities.h" +#include "proj.h" + +projectile** bullets ; +int bullets_id ; + +double psize = 1.5; + +pt_2d build_pt(double x, double y, double z) { + pt_2d res; + res.x = x ; + res.y = y ; + res.z = z ; + return res; +} + +void init_proj() { + bullets = malloc(sizeof(projectile*)*MAX_SIZE); + for(int k = 0; k < MAX_SIZE; k++) { + bullets[k] = malloc(sizeof(projectile)); + bullets[k]->pos = build_pt(0.0, 0.0, 0.0); + bullets[k]->vel = build_pt(0.0, 0.0, 0.0); + bullets[k]->acc = build_pt(0.0, 0.0, 0.0); + bullets[k]->size = 1.0; + bullets[k]->red = 0; + bullets[k]->green = 0; + bullets[k]->blue = 0; + } + bullets_id = 0; +} + +bool is_proj_colliding_with_map(double x, double y, double z, double size) { + for(int k = 0; k < current_room->map_size; k++) { + if(distance_pt_cube_0_3d(x+size/2, y+size/2, z+size/2, current_room->map[k]) <= size/2) { + return true ; + } + } + return false ; +} + +bool is_proj_colliding_with_tp(double x, double y, double z, double size) { + for(int k = 0; k < current_room->tps_size; k++) { + for(int d = 0; d < 8; d++) { + if(distance_pt_cube_0_3d(x+size/2, y+size/2, z+size/2, current_room->tps[k].hitbox) <= size/2) { + return true ; + } + } + } + return false ; +} + +double ppx, ppy, ppz; +double projx, projy; +void addProjectileToDraw(projectile* proj) { + if(triangles_i < MAX_SIZE-1) { + project_to_camera(proj->pos.x, proj->pos.y, proj->pos.z, &ppx, &ppy, &ppz); + if(ppz >= draw_constant) { + projx = 1500.0 * (1.0 + (ppx / (1.5 * ppz * tan_fov))) / 2.0; + projy = 1000.0 * (1.0 + (ppy / (ppz * tan_fov))) / 2.0; + double rpsize = (40.0*psize/maxd(0.5, ppz)); + //printf("%lf\n", rpsize); + triangles_to_render[triangles_i][0] = build_pt(projx-rpsize/2, projy-rpsize/2, 0.0); + triangles_to_render[triangles_i][1] = build_pt(projx-rpsize/2, projy+rpsize/2, 0.0); + triangles_to_render[triangles_i][2] = build_pt(projx+rpsize/2, projy+rpsize/2, 0.0); + triangles_og_coords[triangles_i][0] = build_pt(ppx, ppy, ppz); + triangles_og_coords[triangles_i][1] = build_pt(ppx, ppy, ppz); + triangles_og_coords[triangles_i][2] = build_pt(ppx, ppy, ppz); + reds[triangles_i] = proj->red; + greens[triangles_i] = proj->green; + blues[triangles_i] = proj->blue; + triangles_i += 1; + + triangles_to_render[triangles_i][0] = build_pt(projx-rpsize/2, projy-rpsize/2, 0.0); + triangles_to_render[triangles_i][1] = build_pt(projx+rpsize/2, projy-rpsize/2, 0.0); + triangles_to_render[triangles_i][2] = build_pt(projx+rpsize/2, projy+rpsize/2, 0.0); + triangles_og_coords[triangles_i][0] = build_pt(ppx, ppy, ppz); + triangles_og_coords[triangles_i][1] = build_pt(ppx, ppy, ppz); + triangles_og_coords[triangles_i][2] = build_pt(ppx, ppy, ppz); + reds[triangles_i] = proj->red; + greens[triangles_i] = proj->green; + blues[triangles_i] = proj->blue; + triangles_i += 1; + } + } +} + +void drawProj() { + for(int k = 0; k < bullets_id; k++) { + addProjectileToDraw(bullets[k]); + } +} + +void appendProj(double x, double y, double z, double vx, double vy, double vz, double ax, double ay, double az, int r, int g, int b) { + if(bullets_id < MAX_SIZE) { + bullets[bullets_id]->red = r ; + bullets[bullets_id]->green = g ; + bullets[bullets_id]->blue = b ; + bullets[bullets_id]->size = 0.5 ; + bullets[bullets_id]->pos = build_pt(x, y, z) ; + bullets[bullets_id]->vel = build_pt(vx, vy, vz) ; + bullets[bullets_id]->acc = build_pt(ax, ay, az) ; + bullets_id += 1; + } +} + +void removeProj(int k) { + bullets[k]->red = bullets[bullets_id]->red; + bullets[k]->green = bullets[bullets_id]->green; + bullets[k]->blue = bullets[bullets_id]->blue; + bullets[k]->size = bullets[bullets_id]->size; + bullets[k]->pos = bullets[bullets_id]->pos; + bullets[k]->vel = bullets[bullets_id]->vel; + bullets[k]->acc = bullets[bullets_id]->acc; + bullets_id -= 1; +} + +void updateAllProj(float dtime) { + for(int k = 0; k < bullets_id; k++) { + bullets[k]->vel.x += ((double)dtime)*(bullets[k]->acc.x) ; + bullets[k]->vel.y += ((double)dtime)*(bullets[k]->acc.y) ; + bullets[k]->vel.z += ((double)dtime)*(bullets[k]->acc.z) ; + bullets[k]->pos.x += ((double)dtime)*(bullets[k]->vel.x) ; + bullets[k]->pos.y += ((double)dtime)*(bullets[k]->vel.y) ; + bullets[k]->pos.z += ((double)dtime)*(bullets[k]->vel.z) ; + //if(k==0) printf("[%d] %lf, %lf, %lf\n", k, bullets[k]->pos.x, bullets[k]->pos.y, bullets[k]->pos.z); + if( + is_proj_colliding_with_map(bullets[k]->pos.x - bullets[k]->size/2, bullets[k]->pos.y - bullets[k]->size/2, bullets[k]->pos.z - bullets[k]->size/2, bullets[k]->size) || + is_proj_colliding_with_tp(bullets[k]->pos.x - bullets[k]->size/2, bullets[k]->pos.y - bullets[k]->size/2, bullets[k]->pos.z - bullets[k]->size/2, bullets[k]->size) + ) { + removeProj(k); + k -= 1; + } + } +} + +void free_proj() { + for(int k = 0; k < MAX_SIZE; k++) { + free(bullets[k]); + } + free(bullets); +} \ No newline at end of file diff --git a/src/proj.h b/src/proj.h new file mode 100644 index 0000000..86cb3ea --- /dev/null +++ b/src/proj.h @@ -0,0 +1,22 @@ +#ifndef PROJ_H +#define PROJ_H + +typedef struct projectile { + pt_2d pos ; + pt_2d vel ; + pt_2d acc ; + double size; + int red; + int green; + int blue; +} projectile ; + +void init_proj(); +void free_proj(); + +void drawProj(); +void appendProj(double x, double y, double z, double vx, double vy, double vz, double ax, double ay, double az, int r, int g, int b); +void removeProj(int k); +void updateAllProj(float dtime); + +#endif \ No newline at end of file diff --git a/templates/room_2 b/templates/room_2 index aacd2b7..22af1e6 100644 --- a/templates/room_2 +++ b/templates/room_2 @@ -14,12 +14,12 @@ Teleporters : [-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0] Entities : -[3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] +[3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 192, 0, 1, 0, 3] [-3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] [3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] -[-3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0] +[-3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 192, 0, 1, 0, 3] Weight : -20 +50 $ \ No newline at end of file diff --git a/templates/room_4 b/templates/room_4 index 253b526..f7db6bb 100644 --- a/templates/room_4 +++ b/templates/room_4 @@ -13,6 +13,6 @@ Entities : [0.0, 40.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 64, 0, 5, 100, 2] Weight : -100 +10 $ \ No newline at end of file