From 5276db4a15a6565813bdde22198f8aee4030e56f Mon Sep 17 00:00:00 2001 From: hgn Date: Fri, 3 Dec 2021 15:34:05 +0000 Subject: [PATCH] fix movement bugs --- build.sh | 4 +-- fishladder.c | 73 ++++++++++++++++++++++++++++++++++-------- fishladder_resources.h | 41 +----------------------- maps/thirds.map | 33 ++++++++++++------- vg/vg.h | 2 +- vg/vg_m.h | 5 +++ vg/vg_steamworks.h | 0 7 files changed, 89 insertions(+), 69 deletions(-) create mode 100644 vg/vg_steamworks.h diff --git a/build.sh b/build.sh index 485a8c1..63891f6 100755 --- a/build.sh +++ b/build.sh @@ -3,7 +3,7 @@ src="fishladder.c" target="fishladder" lib="-I. -L./lib -L./" -libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -lpthread -lXi -ldl" # -l:steam/libsteam_api.so" +libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -lpthread -lXi -ldl -l:steam/libsteam_api.so" flags="-fsanitize=address -ggdb3 -Wno-unused-function -DNO_STEAM" build_dir="build.linux" @@ -24,7 +24,7 @@ compile_models=false while (( "$#" )); do case $1 in -r|--release) - flags="-O3 -Wno-unused-function -DNO_STEAM" + flags="-O3 -Wno-unused-function" echo "Release mode" ;; -p|--play) diff --git a/fishladder.c b/fishladder.c index 4ff7458..73c9210 100644 --- a/fishladder.c +++ b/fishladder.c @@ -1,6 +1,7 @@ // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved //#define VG_STEAM +//#define VG_STEAM_APPID 1218140U #include "vg/vg.h" #include "fishladder_resources.h" @@ -245,7 +246,8 @@ enum e_fish_state k_fish_state_soon_dead = -1, k_fish_state_dead = 0, k_fish_state_alive, - k_fish_state_bg + k_fish_state_bg, + k_fish_state_soon_alive }; struct world @@ -270,6 +272,7 @@ struct world int simulating; int sim_run, max_runs; + float sim_speed; float frame_lerp; struct cell_terminal @@ -1356,6 +1359,7 @@ void vg_update(void) world.sim_frame = 0; world.sim_start = vg_time; world.sim_run = 0; + world.sim_speed = 2.5f; for( int i = 0; i < world.w*world.h; i ++ ) world.data[ i ].state &= ~FLAG_FLIP_FLOP; @@ -1367,7 +1371,7 @@ void vg_update(void) // Fish ticks if( world.simulating ) { - while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) ) + while( world.sim_frame < (int)((vg_time-world.sim_start)*world.sim_speed) ) { //vg_info( "frame: %u\n", world.sim_frame ); sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 ); @@ -1394,6 +1398,9 @@ void vg_update(void) if( fish->state == k_fish_state_soon_dead ) fish->state = k_fish_state_dead; + if( fish->state == k_fish_state_soon_alive ) + fish->state = k_fish_state_alive; + if( fish->state < k_fish_state_alive ) continue; @@ -1488,7 +1495,7 @@ void vg_update(void) fish->state = world_check_pos_ok( fish->pos )? k_fish_state_bg: k_fish_state_dead; } - v2i_add( fish->pos, fish->dir, fish->pos ); + //v2i_add( fish->pos, fish->dir, fish->pos ); } else if( fish->state == k_fish_state_bg ) { @@ -1505,7 +1512,7 @@ void vg_update(void) if( cell_entry->config == k_cell_type_con_r || cell_entry->config == k_cell_type_con_u || cell_entry->config == k_cell_type_con_l || cell_entry->config == k_cell_type_con_d ) { - fish->state = k_fish_state_alive; + fish->state = k_fish_state_soon_alive; fish->dir[0] = 0; fish->dir[1] = 0; @@ -1535,6 +1542,7 @@ void vg_update(void) if( fish->state == k_fish_state_alive ) { + v2i_add( fish->pos, fish->dir, fish->pos ); struct cell *cell_current = pcell( fish->pos ); if( cell_current->state & FLAG_IS_TRIGGER ) @@ -1559,23 +1567,56 @@ void vg_update(void) } // Third pass (collisions) + struct fish *fi, *fj; + for( int i = 0; i < world.num_fishes; i ++ ) { - if( world.fishes[i].state == k_fish_state_alive ) + fi = &world.fishes[i]; + + if( fi->state == k_fish_state_alive ) { + int continue_again = 0; + for( int j = i+1; j < world.num_fishes; j ++ ) { - if( (world.fishes[j].state == k_fish_state_alive) && - (world.fishes[i].pos[0] == world.fishes[j].pos[0]) && - (world.fishes[i].pos[1] == world.fishes[j].pos[1]) ) + fj = &world.fishes[j]; + + if( (fj->state == k_fish_state_alive) ) { - // Shatter death (+0.5s) - world.fishes[i].state = k_fish_state_soon_dead; - world.fishes[j].state = k_fish_state_soon_dead; - world.fishes[i].death_time = 0.5f; - world.fishes[j].death_time = 0.5f; + v2i fi_prev; + v2i fj_prev; + + v2i_sub( fi->pos, fi->dir, fi_prev ); + v2i_sub( fj->pos, fj->dir, fj_prev ); + + int + collide_next_frame = ( + (fi->pos[0] == fj->pos[0]) && + (fi->pos[1] == fj->pos[1]))? 1: 0, + collide_this_frame = ( + (fi_prev[0] == fj->pos[0]) && + (fi_prev[1] == fj->pos[1]) && + (fj_prev[0] == fi->pos[0]) && + (fj_prev[1] == fi->pos[1]) + )? 1: 0; + + if( collide_next_frame || collide_this_frame ) + { + // Shatter death (+0.5s) + float death_time = collide_this_frame? 0.0f: 0.5f; + + fi->state = k_fish_state_soon_dead; + fj->state = k_fish_state_soon_dead; + fi->death_time = death_time; + fj->death_time = death_time; + + continue_again = 1; + break; + } } } + if( continue_again ) + continue; } } @@ -1654,6 +1695,10 @@ void vg_update(void) world.sim_frame = 0; world.sim_start = vg_time; world.num_fishes = 0; + + for( int i = 0; i < world.w*world.h; i ++ ) + world.data[ i ].state &= ~FLAG_FLIP_FLOP; + continue; } else @@ -1690,7 +1735,7 @@ void vg_update(void) } float scaled_time = 0.0f; - scaled_time = (vg_time-world.sim_start)*2.0f; + scaled_time = (vg_time-world.sim_start)*world.sim_speed; world.frame_lerp = scaled_time - (float)world.sim_frame; // Update positions diff --git a/fishladder_resources.h b/fishladder_resources.h index e3abf07..4e48534 100644 --- a/fishladder_resources.h +++ b/fishladder_resources.h @@ -4,11 +4,10 @@ vg_tex2d tex_tile_data = { .path = "textures/tileset.qoi" }; vg_tex2d tex_tile_detail = { .path = "textures/tile_overlays.qoi" }; vg_tex2d tex_wood = { .path = "textures/wood.qoi" }; -vg_tex2d tex_ball = { .path = "textures/ball.qoi", .flags = VG_TEXTURE_CLAMP }; vg_tex2d tex_background = { .path = "textures/background.qoi" }; vg_tex2d tex_ball_noise = { .path = "textures/bnoise.qoi" }; -vg_tex2d *texture_list[] = { &tex_tile_detail, &tex_tile_data, &tex_wood, &tex_ball, &tex_background, &tex_ball_noise }; +vg_tex2d *texture_list[] = { &tex_tile_detail, &tex_tile_data, &tex_wood, &tex_background, &tex_ball_noise }; // AUDIO // =========================================================================================================== @@ -139,43 +138,6 @@ SHADER_DEFINE( shader_tile_colour, UNIFORMS({ "uPv", "uOffset", "uColour" }) ) -/* -SHADER_DEFINE( shader_ball, - // VERTEX - "layout (location=0) in vec2 a_co;" - "uniform vec2 uOffset;" - "uniform mat3 uPv;" - "" - "out vec2 aTexCoords;" - "" - "void main()" - "{" - // Create texture coords - "aTexCoords = a_co;" - - // Vertex transform - "vec3 worldpos = vec3( a_co * 0.5 - 0.25 + uOffset, 1.0 );" - "gl_Position = vec4( uPv * worldpos, 1.0 );" - "}", - - // FRAGMENT - "out vec4 FragColor;" - "" - "uniform sampler2D uTexMain;" - "uniform vec3 uColour;" - "" - "in vec2 aTexCoords;" - "" - "void main()" - "{" - "vec4 glyph = texture( uTexMain, aTexCoords );" - "FragColor = vec4( uColour + glyph.rgb * 0.2, glyph.a );" - "}" - , - UNIFORMS({ "uTexMain", "uColour", "uOffset", "uPv" }) -) -*/ - SHADER_DEFINE( shader_ball, // VERTEX "layout (location=0) in vec2 a_co;" @@ -217,7 +179,6 @@ SHADER_DEFINE( shader_ball, "vec2 shadow_coords = center_coords + vec2(0.02,0.07);" "vec2 shadow_coords_sqr = shadow_coords*shadow_coords;" - //"float shadow = exp(-abs(shadow_coords_sqr.x+shadow_coords_sqr.y)*20.0);" "float shadow = exp(-((shadow_coords_sqr.x+shadow_coords_sqr.y)-0.0125)*15.0);" "vec3 marble_comp = uColour*0.9 + (noise_sample.x*0.7+pow(rim_light,3.0)*2.0) * 0.1;" diff --git a/maps/thirds.map b/maps/thirds.map index ab14e51..4111bef 100644 --- a/maps/thirds.map +++ b/maps/thirds.map @@ -1,12 +1,21 @@ -#############; -###-##-##-###;a:aa:aaa,a:aa:aaa,a:aa:aaa -## ##; -## ##; -## ##; -## ##; -## ##; -## ##; -## ##; -## ##; -######+######;aaa:aaaaaa:aaaaaaaaa -#############; +#####################; +#######-##-##-#######;a:aa:aaa,a:aa:aaa,a:aa:aaa +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +## ##; +##########+##########;aaa:aaaaaa:aaaaaaaaa +#####################; diff --git a/vg/vg.h b/vg/vg.h index 22cd0a7..53670b2 100644 --- a/vg/vg.h +++ b/vg/vg.h @@ -139,7 +139,7 @@ static void vg_init( int argc, char *argv[], const char *window_name ) { #ifdef VG_STEAM // Initialize steamworks - if( !sw_init( 1218140U ) ) + if( !sw_init( VG_STEAM_APPID ) ) { vg_exiterr( "Steamworks failed to initialize" ); } diff --git a/vg/vg_m.h b/vg/vg_m.h index dd7f49b..a0c0107 100644 --- a/vg/vg_m.h +++ b/vg/vg_m.h @@ -60,6 +60,11 @@ static inline void v2i_add( v2i a, v2i b, v2i d ) d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; } +static inline void v2i_sub( v2i a, v2i b, v2i d ) +{ + d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; +} + static inline void v2_minv( v2f a, v2f b, v2f dest ) { dest[0] = vg_minf(a[0], b[0]); diff --git a/vg/vg_steamworks.h b/vg/vg_steamworks.h new file mode 100644 index 0000000..e69de29 -- 2.25.1