Compare commits

...

2 Commits

Author SHA1 Message Date
Alexandre 5e3ff72046 added shooting mines 2025-01-25 22:08:42 +01:00
Alexandre 286d4ecbce adde projectiles 2025-01-24 21:00:32 +01:00
22 changed files with 311 additions and 71 deletions

View File

@ -11,6 +11,9 @@
"sdl.h": "c", "sdl.h": "c",
"triangles.h": "c", "triangles.h": "c",
"base.h": "c", "base.h": "c",
"move.h": "c" "move.h": "c",
"hash.h": "c",
"proj.h": "c",
"entities.h": "c"
} }
} }

View File

@ -7,7 +7,7 @@ all: bin/back
test: bin/back test: bin/back
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 mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@ $(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -23,6 +23,7 @@ obj/triangles.o: src/triangles.c
obj/bullets.o: src/bullets.c obj/bullets.o: src/bullets.c
obj/move.o: src/move.c obj/move.o: src/move.c
obj/base.o: src/base.c obj/base.o: src/base.c
obj/proj.o: src/proj.c
obj/menus.o: src/menus.c obj/menus.o: src/menus.c
obj/hash.o: src/hash.c obj/hash.o: src/hash.c

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
obj/proj.o Normal file

Binary file not shown.

Binary file not shown.

View File

@ -16,6 +16,7 @@
#include "base.h" #include "base.h"
#include "triangles.h" #include "triangles.h"
#include "move.h" #include "move.h"
#include "proj.h"
#include "entities.h" #include "entities.h"
#include "generation.h" #include "generation.h"
#include "display.h" #include "display.h"
@ -663,8 +664,10 @@ void addTriangleRotated(
rotate_cube(x0, y0, z0, &px0, &py0, &pz0, cb); rotate_cube(x0, y0, z0, &px0, &py0, &pz0, cb);
rotate_cube(x1, y1, z1, &px1, &py1, &pz1, cb); rotate_cube(x1, y1, z1, &px1, &py1, &pz1, cb);
rotate_cube(x2, y2, z2, &px2, &py2, &pz2, cb); rotate_cube(x2, y2, z2, &px2, &py2, &pz2, cb);
if(triangles_i < MAX_SIZE -1) {
addTriangle(px0, py0, pz0, px1, py1, pz1, px2, py2, pz2, red, green, blue); addTriangle(px0, py0, pz0, px1, py1, pz1, px2, py2, pz2, red, green, blue);
} }
}
// -------------------------------------------------------------------------------------------------------------------------------- // // -------------------------------------------------------------------------------------------------------------------------------- //
@ -801,20 +804,13 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
add_triangles_cb(current_room->map, current_room->map_size); add_triangles_cb(current_room->map, current_room->map_size);
add_triangles_tp(current_room->tps, current_room->tps_size); add_triangles_tp(current_room->tps, current_room->tps_size);
add_triangles_ent(current_room->ents, current_room->ent_len); add_triangles_ent(current_room->ents, current_room->ent_len);
drawProj();
topological_sort(); topological_sort();
//remove_hidden(renderer); //remove_hidden(renderer);
//topological_sort(); //topological_sort();
for(int k = 0; k < triangles_i; k++) { for(int k = 0; k < triangles_i; k++) {
renderTriangleFull(renderer, triangles_order[k]); renderTriangleFull(renderer, triangles_order[k]);
} }
/*for(int k = 0; k < triangles_i; k++) {
drawNumberToRenderer(renderer, digits,
(int)(3.3*(proj_pt_distance_to_camera(triangles_og_coords[k][0])+proj_pt_distance_to_camera(triangles_og_coords[k][1])+proj_pt_distance_to_camera(triangles_og_coords[k][2]))),
(int)(0.33*(triangles_to_render[k][0].x+triangles_to_render[k][1].x+triangles_to_render[k][2].x)),
(int)(0.33*(triangles_to_render[k][0].y+triangles_to_render[k][1].y+triangles_to_render[k][2].y)),
75/4, 105/4, 0
);
}*/
for(int k = 0; k < 0*triangles_i; k++) { for(int k = 0; k < 0*triangles_i; k++) {
for(int l = 0; l < 3; l++) { for(int l = 0; l < 3; l++) {
drawNumberToRenderer(renderer, digits, drawNumberToRenderer(renderer, digits,

View File

@ -29,6 +29,13 @@ double square_z_distance_to_camera(
double x2, double y2, double z2 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_X0(double* y, double* z, double theta);
void axialRotation_X(double* y, double* z, double theta, double cst_y, double cst_z); void axialRotation_X(double* y, double* z, double theta, double cst_y, double cst_z);
void axialRotation_Y0(double* x, double* z, double theta); void axialRotation_Y0(double* x, double* z, double theta);

View File

@ -14,6 +14,8 @@
#include "hash.h" #include "hash.h"
#include "structure.h" #include "structure.h"
#include "base.h" #include "base.h"
#include "display.h"
#include "proj.h"
#include "entities.h" #include "entities.h"
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
@ -58,11 +60,28 @@ 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 speen(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)*15.0; ret->hz_angle += ((double)dtime)*15.0;
} }
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 speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) {
ret->hz_angle += ((double)dtime)*15.0; ret->hz_angle += ((double)dtime)*22.5;
if((int)(10.0*ret->hz_angle) != (int)(10.0*(ret->hz_angle - ((double)dtime)*22.5))) {
double dx = (x+w/2 - camx);
double dy = (y+h/2 - camy);
double dz = (z+d/2 - camz);
double total = sqrt(dx*dx + dy*dy + dz*dz) ;
dx = 170.0*dx/total;
dy = 170.0*dy/total;
dz = 170.0*dz/total;
appendProj(x+w/2, y+h/2, z+d/2, -dx, -dy, -dz, 0.0, 0.0, 0.0, 192, 32, 192, 10, 2.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+w/2, y+h/2, z+d/2, 10.0 + rand()%15, 10.0 + rand()%15, 0.0, 0.0, 0.0, 0.0, 32, 32, 255, 1, 5.0);
}
} }
void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) { void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
@ -84,20 +103,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 dy = (y+h/2 - camy);
double dz = (z+d/2 - camz); double dz = (z+d/2 - camz);
double total = sqrt(dx*dx + dy*dy + dz*dz) ; double total = sqrt(dx*dx + dy*dy + dz*dz) ;
dx = 7.0*dx/total; dx = 110.0*dx/total;
dy = 7.0*dy/total; dy = 110.0*dy/total;
dz = 7.0*dz/total; dz = 110.0*dz/total;
ret->x -=12.0*dtime*dx ; ret->x -= dtime*dx ;
if(is_colliding_with_map(*ret) || is_colliding_with_tp(*ret)) { 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)) { 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)) { 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)) {
} }
} }

View File

@ -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 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 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); 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);

View File

@ -41,7 +41,7 @@ void init_ent_generator(int n) {
hashtbl_entities[0].id = 0; hashtbl_entities[0].id = 0;
hashtbl_entities[0].name = "Coin"; // 0 = default hashtbl_entities[0].name = "Coin"; // 0 = default
hashtbl_entities[0].updatePos = &speen2 ; hashtbl_entities[0].updatePos = &speen ;
hashtbl_entities[0].onHit = &detectHit ; hashtbl_entities[0].onHit = &detectHit ;
hashtbl_entities[0].onDeath = NULL ; hashtbl_entities[0].onDeath = NULL ;
@ -56,6 +56,12 @@ void init_ent_generator(int n) {
hashtbl_entities[2].updatePos = &go_to_player ; hashtbl_entities[2].updatePos = &go_to_player ;
hashtbl_entities[2].onHit = &explodeOnHit ; hashtbl_entities[2].onHit = &explodeOnHit ;
hashtbl_entities[2].onDeath = NULL ; 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) { fct_entry* get_entry(int k0) {

View File

@ -16,6 +16,7 @@
#include "base.h" #include "base.h"
#include "move.h" #include "move.h"
#include "triangles.h" #include "triangles.h"
#include "proj.h"
#include "entities.h" #include "entities.h"
#include "display.h" #include "display.h"
#include "generation.h" #include "generation.h"
@ -63,6 +64,7 @@ int main(int argc, char** argv) {
init_draworder(); init_draworder();
init_ent_generator(10); init_ent_generator(10);
trInit(); trInit();
init_proj();
parse_rooms(5); parse_rooms(5);
import_digits(rend) ; import_digits(rend) ;
import_letters(rend) ; import_letters(rend) ;
@ -81,49 +83,56 @@ int main(int argc, char** argv) {
entend = clock(); entend = clock();
//printf("00\n"); //printf("00\n");
//printf("%s\n", SDL_GetError()); //printf("%s\n", SDL_GetError());
fflush(stdout); //fflush(stdout);
playerActions(((float)entend - (float)entstart)/CLOCKS_PER_SEC) ; playerActions(((float)entend - (float)entstart)/CLOCKS_PER_SEC) ;
//printf("01\n"); //printf("01\n");
fflush(stdout); //fflush(stdout);
generate_nearby_chunks(1); generate_nearby_chunks(1);
//printf("02\n"); //printf("02\n");
fflush(stdout); //fflush(stdout);
entend = clock(); entend = clock();
update_entities(((float)entend - (float)entstart)/CLOCKS_PER_SEC); update_entities(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
updateAllProj(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
//printf("03\n"); //printf("03\n");
fflush(stdout); //fflush(stdout);
entstart = clock(); entstart = clock();
//printf("-->%d\n", triangles_i);
drawCurrentRoom(rend); drawCurrentRoom(rend);
//printf("-->%d\n", triangles_i);
//printf("-->%d\n", triangles_i);
//printf("04\n"); //printf("04\n");
//printf("%s\n", SDL_GetError()); //printf("%s\n", SDL_GetError());
fflush(stdout); //fflush(stdout);
drawData(rend) ; drawData(rend) ;
//printf("05\n"); //printf("05\n");
fflush(stdout); //fflush(stdout);
drawHPbar(rend); drawHPbar(rend);
//printf("06\n"); //printf("06\n");
fflush(stdout); //fflush(stdout);
finish = clock(); finish = clock();
fade_dmg = max(fade_dmg-5, 0); fade_dmg = max(fade_dmg-5, 0);
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC; delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
//printf("07\n"); //printf("07\n");
fflush(stdout); //fflush(stdout);
drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0); drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0); drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, coins, 1500/2-55, 1000 - 70, 75/3, 105/3, 0); drawNumberToRenderer(rend, digits, coins, 1500/2-55, 1000 - 70, 75/3, 105/3, 0);
//printf("08\n"); //printf("08\n");
fflush(stdout); //fflush(stdout);
//printf("%s\n", SDL_GetError()); //printf("%s\n", SDL_GetError());
updateRenderer(rend) ; updateRenderer(rend) ;
sim_time += delta + intervalf ; sim_time += delta + intervalf ;
//printf("09\n"); //printf("09\n");
fflush(stdout); //fflush(stdout);
if(player_hp <= 0) {
stop_evetything = true ;
}
usleep(interval) ; usleep(interval) ;
} }
//printf("GPNE\n"); //printf("GPNE\n");
fflush(stdout); //fflush(stdout);
free_digits(digits) ; free_digits(digits) ;
for(int k = 0; k < MAX_SIZE; k++) { for(int k = 0; k < MAX_SIZE; k++) {
@ -138,8 +147,9 @@ int main(int argc, char** argv) {
free(blues); free(blues);
free(triangles_order); free(triangles_order);
free(visited_tri); free(visited_tri);
free_proj();
//printf("10\n"); //printf("10\n");
fflush(stdout); //fflush(stdout);
hashtbl_free(visited); hashtbl_free(visited);
free_pool(); free_pool();
@ -147,17 +157,17 @@ int main(int argc, char** argv) {
SDL_DestroyRenderer(rend); SDL_DestroyRenderer(rend);
//printf("10\n"); //printf("10\n");
fflush(stdout); //fflush(stdout);
SDL_DestroyWindow(win); SDL_DestroyWindow(win);
//printf("11\n"); //printf("11\n");
fflush(stdout); //fflush(stdout);
SDL_Quit(); SDL_Quit();
//printf("12\n"); //printf("12\n");
fflush(stdout); //fflush(stdout);
/* -------------------------------------------------------- */ /* -------------------------------------------------------- */
//printf("Done\n") ; //printf("Done\n") ;
fflush(stdout); //fflush(stdout);
return 0; return 0;
} }

View File

@ -15,6 +15,7 @@
#include "structure.h" #include "structure.h"
#include "base.h" #include "base.h"
#include "entities.h" #include "entities.h"
#include "proj.h"
#include "move.h" #include "move.h"
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
@ -84,6 +85,7 @@ bool is_colliding(float dtime) {
player_chy = current_room->tps[k].dest_chy ; player_chy = current_room->tps[k].dest_chy ;
current_room = hashtbl_find_opt(visited, player_chx, player_chy); current_room = hashtbl_find_opt(visited, player_chx, player_chy);
set_player_coords(old_chx, old_chy); set_player_coords(old_chx, old_chy);
resetProj();
return true ; return true ;
} }
} }
@ -182,13 +184,10 @@ void playerActions(float dtime) {
if(state[SDL_SCANCODE_SPACE] == 1) { if(state[SDL_SCANCODE_SPACE] == 1) {
stop_evetything = true ; stop_evetything = true ;
} else {
pass = true ;
} }
if(state[SDL_SCANCODE_T] == 1) { if(state[SDL_SCANCODE_T] == 1) {
fprintf(stderr, "Killed.\n") ; stop_evetything = true ;
exit(1) ;
} }
if(state[SDL_SCANCODE_A] == 1) { if(state[SDL_SCANCODE_A] == 1) {

180
src/proj.c Normal file
View File

@ -0,0 +1,180 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#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]->ttl = 1.0;
bullets[k]->red = 0;
bullets[k]->green = 0;
bullets[k]->blue = 0;
bullets[k]->damage = 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 ;
}
bool is_proj_colliding_w_player(double x, double y, double z, double size) {
return (distance_pt_pt_3d(x, y, z, camx, camy, camz) <= size);
}
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.07, 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, int dmg, double ttl) {
if(bullets_id < MAX_SIZE) {
bullets[bullets_id]->red = r ;
bullets[bullets_id]->green = g ;
bullets[bullets_id]->blue = b ;
bullets[bullets_id]->size = 1.0 ;
bullets[bullets_id]->damage = dmg ;
bullets[bullets_id]->ttl = ttl ;
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[k]->ttl = bullets[bullets_id]->ttl;
bullets[k]->damage = bullets[bullets_id]->damage;
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);
bullets[k]->ttl -= 20.0*(double)dtime;
//if(k==0) printf("[%d] %lf\n", k, bullets[k]->ttl);
if(is_proj_colliding_w_player(bullets[k]->pos.x - bullets[k]->size/2.0, bullets[k]->pos.y - bullets[k]->size/2.0, bullets[k]->pos.z - bullets[k]->size/2.0, bullets[k]->size)) {
if(bullets[k]->damage != 0) {
player_hp -= bullets[k]->damage ;
fade_dmg = 255 ;
}
removeProj(k);
k -= 1;
} else if(bullets[k]->ttl <= 0.0 || (
is_proj_colliding_with_map(bullets[k]->pos.x - bullets[k]->size/2.0, bullets[k]->pos.y - bullets[k]->size/2.0, bullets[k]->pos.z - bullets[k]->size/2.0, bullets[k]->size) ||
is_proj_colliding_with_tp(bullets[k]->pos.x - bullets[k]->size/2.0, bullets[k]->pos.y - bullets[k]->size/2.0, bullets[k]->pos.z - bullets[k]->size/2.0, bullets[k]->size)
)) {
removeProj(k);
k -= 1;
}
}
}
void resetProj() {
bullets_id = 0;
}
void free_proj() {
for(int k = 0; k < MAX_SIZE; k++) {
free(bullets[k]);
}
free(bullets);
}

26
src/proj.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef PROJ_H
#define PROJ_H
typedef struct projectile {
pt_2d pos ;
pt_2d vel ;
pt_2d acc ;
double size;
double ttl;
int red;
int green;
int blue;
int damage;
} 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, int dmg, double ttl);
void removeProj(int k);
void updateAllProj(float dtime);
void resetProj();
#endif

View File

@ -318,17 +318,6 @@ bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og
return false; return false;
} }
get_barycentric(p, tri, &u, &v, &w); get_barycentric(p, tri, &u, &v, &w);
/*if(renderer != NULL && (u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) {
if(mid.z >= 0.4) {
SDL_Rect r;
r.x = (int)(1500.0 * (1.0 + (mid.x / (1.5 * mid.z * tan_fov))) / 2.0) -2;
r.y = (int)(1000.0 * (1.0 + (mid.y / (mid.z * tan_fov))) / 2.0) -2;
r.w = 4 ;
r.h = 4 ;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &r);
}
}*/
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) { if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
pt_2d mid = convex_pt2d_tri(og[0], u, og[1], v, og[2], w); pt_2d mid = convex_pt2d_tri(og[0], u, og[1], v, og[2], w);
dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(ogp); dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(ogp);
@ -341,6 +330,19 @@ bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og
} }
bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) { bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) {
for(int k1 = 0; k1 < 3; k1++) {
for(int k2 = 0; k2 < 3; k2++) {
if(seg_seg_inter(tri1[k1], tri1[(k1+1)%3], tri2[k2], tri2[(k2+1)%3], &th1, &th2)) {
pt_2d mid1 = convex_pt2d(tri1[k1], tri1[(k1+1)%3], th1);
pt_2d mid2 = convex_pt2d(tri2[k2], tri2[(k2+1)%3], th2);
dist = proj_pt_distance_to_camera(mid1) - proj_pt_distance_to_camera(mid2);
if(absf(dist) >= 0.0001) {
//return (proj_pt_distance_to_camera(mid1) <= proj_pt_distance_to_camera(mid2));
return (dist <= 0.0);
}
}
}
}
for(int k = 0; k < 3; k++) { for(int k = 0; k < 3; k++) {
pt_2d p = tri1[k]; pt_2d p = tri1[k];
if(pt_equal(p, tri2[0], 0.0001) || pt_equal(p, tri2[1], 0.0001) || pt_equal(p, tri2[2], 0.0001)) { if(pt_equal(p, tri2[0], 0.0001) || pt_equal(p, tri2[1], 0.0001) || pt_equal(p, tri2[2], 0.0001)) {
@ -371,18 +373,5 @@ bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) {
} }
} }
} }
for(int k1 = 0; k1 < 3; k1++) {
for(int k2 = 0; k2 < 3; k2++) {
if(seg_seg_inter(tri1[k1], tri1[(k1+1)%3], tri2[k2], tri2[(k2+1)%3], &th1, &th2)) {
pt_2d mid1 = convex_pt2d(tri1[k1], tri1[(k1+1)%3], th1);
pt_2d mid2 = convex_pt2d(tri2[k2], tri2[(k2+1)%3], th2);
dist = proj_pt_distance_to_camera(mid1) - proj_pt_distance_to_camera(mid2);
if(absf(dist) >= 0.0001) {
//return (proj_pt_distance_to_camera(mid1) <= proj_pt_distance_to_camera(mid2));
return (dist <= 0.0);
}
}
}
}
return false; return false;
} }

View File

@ -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] [-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Entities : 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, 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 : Weight :
20 50
$ $

View File

@ -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] [0.0, 40.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 64, 0, 5, 100, 2]
Weight : Weight :
100 500
$ $