adde projectiles

This commit is contained in:
Alexandre 2025-01-24 21:00:32 +01:00
parent 1578d6682d
commit 286d4ecbce
18 changed files with 238 additions and 16 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.

BIN
obj/proj.o Normal file

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,6 +804,7 @@ 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();

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"
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
@ -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; 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) { void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
if(ret->red == 193) { if(ret->red == 193) {
ret->red = 0; 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 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

@ -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) ;
@ -91,11 +93,15 @@ int main(int argc, char** argv) {
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);
@ -138,6 +144,7 @@ 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);

158
src/proj.c Normal file
View File

@ -0,0 +1,158 @@
#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]->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);
}

22
src/proj.h Normal file
View File

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

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 10
$ $