diff --git a/bin/back b/bin/back index b9419ba..a5f5970 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/base.o b/obj/base.o index e32cba1..a3932ca 100644 Binary files a/obj/base.o and b/obj/base.o differ diff --git a/obj/display.o b/obj/display.o index 626502e..7ccb769 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/entities.o b/obj/entities.o index c833649..7c54a64 100644 Binary files a/obj/entities.o and b/obj/entities.o differ diff --git a/obj/generation.o b/obj/generation.o index 514676b..d39b8d7 100644 Binary files a/obj/generation.o and b/obj/generation.o differ diff --git a/obj/main.o b/obj/main.o index c0a62b5..075c34f 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/move.o b/obj/move.o index 07e8c13..715fff6 100644 Binary files a/obj/move.o and b/obj/move.o differ diff --git a/src/entities.c b/src/entities.c index 4c7a72c..12a65a2 100644 --- a/src/entities.c +++ b/src/entities.c @@ -58,5 +58,18 @@ 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) { - ret->vt_angle += ((double)dtime)*3.0; + ret->vt_angle += ((double)dtime)*5.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) { + ret->hz_angle += ((double)dtime)*5.0; +} + +void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) { + if(ret->red == 255) { + ret->red = 0; + ret->green = 192; + ret->blue = 0; + coins += 1; + } } \ No newline at end of file diff --git a/src/entities.h b/src/entities.h index 31e1c71..ea8946e 100644 --- a/src/entities.h +++ b/src/entities.h @@ -9,4 +9,8 @@ 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 detectHit(float dtime, int* hp, int* dmg, cube_0* ret); + #endif \ No newline at end of file diff --git a/src/generation.c b/src/generation.c index 5ce7527..0f508a2 100644 --- a/src/generation.c +++ b/src/generation.c @@ -27,6 +27,8 @@ entry* pool ; int pool_size ; int total_weight ; +int coins ; + void copy_room(room* src, room* dest, int chx, int chy) { // considering dest has already been malloc'd dest->chunk_x = chx ; @@ -64,7 +66,9 @@ void copy_room(room* src, room* dest, int chx, int chy) { (*(src->ents[k].pos)).w, (*(src->ents[k].pos)).h, (*(src->ents[k].pos)).d, (*(src->ents[k].pos)).hz_angle, (*(src->ents[k].pos)).vt_angle, (*(src->ents[k].pos)).red, (*(src->ents[k].pos)).green, (*(src->ents[k].pos)).blue ); - dest->ents[k].updatePos = *speen ; + dest->ents[k].updatePos = src->ents[k].updatePos; + dest->ents[k].onHit = src->ents[k].onHit; + dest->ents[k].onDeath = src->ents[k].onDeath; } dest->tps_size = src->tps_size; } @@ -92,8 +96,15 @@ void build_starting_chunk(int chx, int chy) { new->tps[2] = create_teleporter(5.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 0, 255, 0 , chx+1, chy , 2.5, 2.5, 2.5); new->tps[3] = create_teleporter(2.0, 1.0, 5.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 0, 0, 255 , chx , chy+1, 2.5, 2.5, 2.5); - new->ents = malloc(sizeof(entity)*0); - new->ent_len = 0 ; + new->ents = malloc(sizeof(entity)*1); + new->ent_len = 1 ; + new->ents[0].pos = malloc(sizeof(cube_0)); + *(new->ents[0].pos) = create_cube_0(-0.25, -2.25, -0.25, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 128, 0); + new->ents[0].damage = 0 ; + new->ents[0].hitpoints = 900 ; + new->ents[0].onDeath = NULL ; + new->ents[0].onHit = *detectHit ; + new->ents[0].updatePos = *speen2 ; hashtbl_add(visited, chx, chy, new); } @@ -105,6 +116,7 @@ void init_hashtbl() { player_chx = 0 ; player_chy = 0 ; total_weight = 0 ; + coins = 0 ; } void get_number_blocks(int* ret_cubes, int* ret_tps, int* ret_ent, FILE* ptr) { @@ -262,6 +274,8 @@ void parse_one_room(int id, char* filename) { pool[id].area->ents[k].hitpoints = malloc(sizeof(int)); *(pool[id].area->ents[k].hitpoints) = hp ; pool[id].area->ents[k].updatePos = *speen ; + pool[id].area->ents[k].onHit = *detectHit ; + pool[id].area->ents[k].onDeath = NULL ; //printf("\n"); } diff --git a/src/main.c b/src/main.c index 8f5d2ff..206b5f4 100644 --- a/src/main.c +++ b/src/main.c @@ -69,7 +69,8 @@ int main(int argc, char** argv) { origin = clock(); SDL_SetRenderDrawColor(rend, 255, 255, 255, SDL_ALPHA_OPAQUE) ; - playerActions() ; + entend = clock(); + playerActions(((float)entend - (float)entstart)/CLOCKS_PER_SEC) ; generate_nearby_chunks(1); entend = clock(); @@ -83,6 +84,7 @@ int main(int argc, char** argv) { delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC; 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, coins, 1500/2-55, 1000 - 70, 75/3, 105/3, 0); updateRenderer(rend) ; sim_time += delta + intervalf ; usleep(interval) ; diff --git a/src/move.c b/src/move.c index b8195b8..e0d1c47 100644 --- a/src/move.c +++ b/src/move.c @@ -14,6 +14,7 @@ #include "hash.h" #include "structure.h" #include "base.h" +#include "entities.h" #include "move.h" // ---------------------------------------------------------------------------------------------------- // @@ -56,7 +57,7 @@ void set_player_coords(int old_chx, int old_chy) { } } -bool is_colliding() { +bool is_colliding(float dtime) { for(int k = 0; k < current_room->map_size; k++) { double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ; if(dist <= min_dist) { @@ -75,10 +76,19 @@ bool is_colliding() { return true ; } } + for(int k = 0; k < current_room->ent_len; k++) { + double dist = distance_pt_cube_0_3d(camx, camy, camz, *(current_room->ents[k].pos)) ; + if(dist <= min_dist) { + if(current_room->ents[k].onHit != NULL) { + (*current_room->ents[k].onHit)(dtime, current_room->ents[k].hitpoints, ¤t_room->ents[k].damage, &(*(current_room->ents[k].pos))); + } + return true ; + } + } return false ; } -void playerActions() { +void playerActions(float dtime) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { @@ -107,7 +117,7 @@ void playerActions() { camz += speed*cos(rot_hz)*cos(rot_vt)/10; camx -= speed*sin(rot_hz)*cos(rot_vt)/10; camy += speed*sin(rot_vt)/10; - if(is_colliding()) { + if(is_colliding(dtime)) { camz -= speed*cos(rot_hz)*cos(rot_vt)/10; camx += speed*sin(rot_hz)*cos(rot_vt)/10; camy -= speed*sin(rot_vt)/10; @@ -119,7 +129,7 @@ void playerActions() { for(int k = 0; k < 10; k++) { camx -= speed*cos(rot_hz)/10; camz -= speed*sin(rot_hz)/10; - if(is_colliding()) { + if(is_colliding(dtime)) { camx += speed*cos(rot_hz)/10; camz += speed*sin(rot_hz)/10; k=11; @@ -131,7 +141,7 @@ void playerActions() { camz -= speed*cos(rot_hz)*cos(rot_vt)/10; camx += speed*sin(rot_hz)*cos(rot_vt)/10; camy -= speed*sin(rot_vt)/10; - if(is_colliding()) { + if(is_colliding(dtime)) { camz += speed*cos(rot_hz)*cos(rot_vt)/10; camx -= speed*sin(rot_hz)*cos(rot_vt)/10; camy += speed*sin(rot_vt)/10; @@ -143,7 +153,7 @@ void playerActions() { for(int k = 0; k < 10; k++) { camx += speed*cos(rot_hz)/10; camz += speed*sin(rot_hz)/10; - if(is_colliding()) { + if(is_colliding(dtime)) { camx -= speed*cos(rot_hz)/10; camz -= speed*sin(rot_hz)/10; k=11; diff --git a/src/move.h b/src/move.h index 375ee1d..dd5f9b0 100644 --- a/src/move.h +++ b/src/move.h @@ -2,7 +2,7 @@ #define MOVE_H void init_csts(); -bool is_colliding(); -void playerActions(); +bool is_colliding(float dtime); +void playerActions(float dtime); #endif \ No newline at end of file diff --git a/src/structure.h b/src/structure.h index cd281f0..2f78f13 100644 --- a/src/structure.h +++ b/src/structure.h @@ -31,7 +31,15 @@ typedef struct teleporter { typedef struct entity { cube_0* pos ; - void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) ; // act as velocity function + void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) ; + // act as velocity function + + void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret) ; + // triggers when object is hit + + void (*onDeath)(float dtime) ; + // triggers when goes negative + int damage ; int* hitpoints ; } entity ; @@ -94,4 +102,6 @@ extern int player_chy ; extern int* drawOrder ; +extern int coins; + #endif \ No newline at end of file