falling animations
authorhgn <hgodden00@gmail.com>
Fri, 29 Oct 2021 22:12:56 +0000 (23:12 +0100)
committerhgn <hgodden00@gmail.com>
Fri, 29 Oct 2021 22:12:56 +0000 (23:12 +0100)
fishladder.c
textures/tileset.png
vg/vg.h
vg/vg_m.h

index e024fca71987265a050db88d2a0be6b4f99317de..f4afab1714af2a915a1167a278329867e49ad901 100644 (file)
@@ -94,6 +94,8 @@ m3x3f m_mdl;
 
 enum cell_type
 {
+       k_cell_type_ramp_right = 3,
+       k_cell_type_ramp_left = 6,
        k_cell_type_split = 7,
        k_cell_type_merge = 13
 };
@@ -172,6 +174,8 @@ struct world
        float sim_start;
        int simulating;
        
+       float frame_lerp;
+       
        struct cell_terminal
        {
                // TODO: Split into input/output structures
@@ -195,6 +199,9 @@ struct world
                v2i dir;
                int alive;
                char payload;
+               float death_time;
+               v2f physics_v;
+               v2f physics_co;
        }
        fishes[16];
        
@@ -498,8 +505,8 @@ static int cell_interactive( v2i co )
 
 static void map_reclassify( v2i start, v2i end )
 {
-       v2i full_start = { 2,2 };
-       v2i full_end = { world.w-2, world.h-2 };
+       v2i full_start = { 1,1 };
+       v2i full_end = { world.w-1, world.h-1 };
        
        if( !start || !end )
        {
@@ -515,7 +522,7 @@ static void map_reclassify( v2i start, v2i end )
                        
                        u8 config = 0x00;
                        
-                       if( pcell((v2i){x,y})->state & FLAG_CANAL )
+                       if( pcell((v2i){x,y})->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
                        {
                                for( int i = 0; i < vg_list_size( dirs ); i ++ )
                                {
@@ -531,6 +538,16 @@ static void map_reclassify( v2i start, v2i end )
        }
 }
 
+v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
+v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
+v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
+v2f const curve_12[]= {{0.0f,0.5f},{0.375f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
+
+v2f const curve_7[] = {{0.5f,0.8438f},{0.875f,0.8438f},{0.625f,0.5f},{1.0f,0.5f}};
+v2f const curve_7_1[] = {{0.5f,0.8438f},{1.0f-0.875f,0.8438f},{1.0-0.625f,0.5f},{0.0f,0.5f}};
+
+float const curve_7_linear_section = 0.1562f;
+
 void vg_update(void)
 {
        static int curlevel = 0;
@@ -638,47 +655,12 @@ void vg_update(void)
 
        // Fish ticks
        if( world.simulating )
-       {
+       {       
                while( world.sim_frame < (int)((vg_time-world.sim_start)*2.0f) )
                {
                        //vg_info( "frame: %u\n", world.sim_frame );
                        sfx_set_playrnd( &audio_random, &audio_system_balls_switching, 0, 9 );
-                       
-                       for( int i = 0; i < arrlen( world.io ); i ++ )
-                       {
-                               struct cell_terminal *term = &world.io[ i ];
-                               int posx = term->id % world.w;
-                               int posy = (term->id - posx)/world.w;
-                               int is_input = world.data[ term->id ].state & FLAG_INPUT;
-                               
-                               if( is_input )
-                               {
-                                       if( world.sim_frame < arrlen( term->conditions ) )
-                                       {
-                                               struct fish *fish = &world.fishes[world.num_fishes++];
-                                               fish->pos[0] = posx;
-                                               fish->pos[1] = posy;
-                                               fish->alive = 1;
-                                               fish->payload = term->conditions[world.sim_frame];
-                                               
-                                               int can_spawn = 0;
-                                               
-                                               v2i dirs[] = {{1,0},{-1,0},{0,-1}};
-                                               for( int j = 0; j < vg_list_size(dirs); j ++ )
-                                                       if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
-                                                       {
-                                                               fish->dir[0] = dirs[j][0];
-                                                               fish->dir[1] = dirs[j][1];
-                                                               can_spawn = 1;
-                                                               break;
-                                                       }
-                                               
-                                               if( !can_spawn )
-                                                       world.num_fishes--;
-                                       }
-                               }
-                       }
-                       
+
                        // Update splitter deltas
                        for( int i = 0; i < world.h*world.w; i ++ )
                        {
@@ -688,13 +670,17 @@ void vg_update(void)
                                        cell->state &= ~FLAG_FLIP_ROTATING;
                                }
                        }
-
+                       
+                       // Update fish positions
                        for( int i = 0; i < world.num_fishes; i ++ )
                        {
                                struct fish *fish = &world.fishes[i];
                                struct cell *cell_current = pcell( fish->pos );
                                
-                               if( !fish->alive )
+                               if( fish->alive == -1 )
+                                       fish->alive = 0;
+                               
+                               if( fish->alive != 1 )
                                        continue;
                                
                                // Apply to output
@@ -760,15 +746,169 @@ void vg_update(void)
                                if( !(cell_entry->state & (FLAG_INPUT|FLAG_CANAL|FLAG_OUTPUT) ))
                                        fish->alive = 0;
                                else
+                               {
+                                       if( fish->dir[0] )
+                                       {
+                                               if( cell_entry->config == k_cell_type_split ||
+                                                        cell_entry->config == k_cell_type_ramp_right ||
+                                                        cell_entry->config == k_cell_type_ramp_left )
+                                               {
+                                                       // Special death (FALL)                                                 
+                                                       v2_sub( fish->physics_co, fish->physics_v, fish->physics_v );
+                                                       v2_divs( fish->physics_v, vg_time_delta, fish->physics_v );
+
+                                                       fish->alive = -2;
+                                                       vg_warn( "Special death (fall)\n" );
+                                                       continue;
+                                               }
+                                       }
+                               
                                        if( cell_entry->config == k_cell_type_split )
                                        {
                                                sfx_set_playrnd( &audio_splitter, &audio_system_balls_important, 0, 1 );
                                                cell_entry->state |= FLAG_FLIP_ROTATING;
                                        }
+                               }
+                       }
+                       
+                       // Check for collisions
+                       for( int i = 0; i < world.num_fishes; i ++ )
+                       {
+                               if( world.fishes[i].alive == 1 )
+                               {
+                                       for( int j = i+1; j < world.num_fishes; j ++ )
+                                       {
+                                               if( (world.fishes[j].alive == 1) && (world.fishes[i].pos[0] == world.fishes[j].pos[0]) &&
+                                                        (world.fishes[i].pos[1] == world.fishes[j].pos[1]) )
+                                               {
+                                                       // Shatter death (+0.5s)
+                                                       world.fishes[i].alive = -1;
+                                                       world.fishes[j].alive = -1;
+                                                       world.fishes[i].death_time = 0.5f;
+                                                       world.fishes[j].death_time = 0.5f;
+                                               }
+                                       }
+                               }
+                       }
+                       
+                       // Spawn fishes
+                       for( int i = 0; i < arrlen( world.io ); i ++ )
+                       {
+                               struct cell_terminal *term = &world.io[ i ];
+                               int posx = term->id % world.w;
+                               int posy = (term->id - posx)/world.w;
+                               int is_input = world.data[ term->id ].state & FLAG_INPUT;
+                               
+                               if( is_input )
+                               {
+                                       if( world.sim_frame < arrlen( term->conditions ) )
+                                       {
+                                               struct fish *fish = &world.fishes[world.num_fishes++];
+                                               fish->pos[0] = posx;
+                                               fish->pos[1] = posy;
+                                               fish->alive = 1;
+                                               fish->payload = term->conditions[world.sim_frame];
+                                               
+                                               int can_spawn = 0;
+                                               
+                                               v2i dirs[] = {{1,0},{-1,0},{0,-1}};
+                                               for( int j = 0; j < vg_list_size(dirs); j ++ )
+                                                       if( pcell( (v2i){ posx+dirs[j][0], posy+dirs[j][1] } )->state & FLAG_CANAL )
+                                                       {
+                                                               fish->dir[0] = dirs[j][0];
+                                                               fish->dir[1] = dirs[j][1];
+                                                               can_spawn = 1;
+                                                               break;
+                                                       }
+                                               
+                                               if( !can_spawn )
+                                                       world.num_fishes--;
+                                       }
+                               }
                        }
                        
                        world.sim_frame ++;
                }
+               
+               float scaled_time = 0.0f;
+               scaled_time = (vg_time-world.sim_start)*2.0f;
+               world.frame_lerp = scaled_time - (float)world.sim_frame;
+               
+               // Update positions
+               for( int i = 0; i < world.num_fishes; i ++ )
+               {
+                       struct fish *fish = &world.fishes[i];
+                       
+                       if( fish->alive == 0 )
+                               continue;
+                       
+                       if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
+                               continue; // Todo: particle thing?
+                       
+                       if( fish->alive == -2 )
+                       {
+                               v2_muladds( fish->physics_v, (v2f){ 0.0, -9.8f }, vg_time_delta, fish->physics_v );
+                               v2_muladds( fish->physics_co, fish->physics_v, vg_time_delta, fish->physics_co );
+                       }
+                       else
+                       {               
+                               struct cell *cell = pcell(fish->pos);
+                               v2f const *curve;
+                               
+                               float t = world.frame_lerp;
+                               
+                               v2_copy( fish->physics_co, fish->physics_v );
+                               
+                               switch( cell->config )
+                               {
+                                       case 13:
+                                               if( fish->dir[0] == 1 )
+                                                       curve = curve_12;
+                                               else
+                                                       curve = curve_9;
+                                       break;
+                                       case 3: curve = curve_3; break;
+                                       case 6: curve = curve_6; break;
+                                       case 9: curve = curve_9; break;
+                                       case 12: curve = curve_12; break;
+                                       case 7:
+                                               if( t > curve_7_linear_section )
+                                               {
+                                                       t -= curve_7_linear_section;
+                                                       t *= (1.0f/(1.0f-curve_7_linear_section));
+                                                       
+                                                       curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
+                                               }
+                                               else curve = NULL;
+                                       break;
+                                       default: curve = NULL; break;
+                               }
+
+                               if( curve )
+                               {
+                                       float t2 = t * t;
+                                       float t3 = t * t * t;
+
+                                       float cA = 3.0f*t2 - 3.0f*t3;
+                                       float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
+                                       float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
+
+                                       fish->physics_co[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
+                                       fish->physics_co[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
+                                       fish->physics_co[0] += (float)fish->pos[0];
+                                       fish->physics_co[1] += (float)fish->pos[1];
+                               }
+                               else
+                               {
+                                       v2f origin;
+                                       origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
+                                       origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
+                                       
+                                       fish->physics_co[0] = origin[0] + (float)fish->dir[0]*t;
+                                       fish->physics_co[1] = origin[1] + (float)fish->dir[1]*t;
+                               }
+                       }
+               }
        }
 }
 
@@ -802,7 +942,7 @@ static void render_tiles( v2i start, v2i end, v4f const regular_colour, v4f cons
                        
                        int uv[2] = { 3, 0 };
                        
-                       if( cell->state & FLAG_CANAL )
+                       if( cell->state & (FLAG_CANAL|FLAG_INPUT|FLAG_OUTPUT) )
                        {
                                uv[0] = tile_offsets[ cell->config ][0];
                                uv[1] = tile_offsets[ cell->config ][1];
@@ -829,27 +969,9 @@ void vg_render(void)
        glClearColor( 0.8f, 0.8f, 0.8f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        
-       float scaled_time = 0.0f, frame_lerp = 0.0f;
-       
-       if( world.simulating )
-       {
-               scaled_time = (vg_time-world.sim_start)*2.0f;
-               frame_lerp = scaled_time - (float)world.sim_frame;
-       }
-       
        v4f const colour_default = {1.0f, 1.0f, 1.0f, 1.0f};
        v4f const colour_selected = {0.90f, 0.92f, 1.0f, 1.0f};
-       
-       v2f const curve_3[] = {{0.5f,1.0f},{0.5f,0.625f},{0.625f,0.5f},{1.0f,0.5f}};
-       v2f const curve_6[] = {{0.5f,1.0f},{0.5f,0.625f},{0.375f,0.5f},{0.0f,0.5f}};
-       v2f const curve_9[] = {{1.0f,0.5f},{0.625f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
-       v2f const curve_12[]= {{0.0f,0.5f},{0.375f,0.5f},{0.5f,0.375f},{0.5f,0.0f}};
-       
-       v2f const curve_7[] = {{0.5f,0.8438f},{0.875f,0.8438f},{0.625f,0.5f},{1.0f,0.5f}};
-       v2f const curve_7_1[] = {{0.5f,0.8438f},{1.0f-0.875f,0.8438f},{1.0-0.625f,0.5f},{0.0f,0.5f}};
-       
-       float const curve_7_linear_section = 0.1562f;
-       
+
        // TILE SET RENDERING 
        //      todo: just slam everything into a mesh...
        //          when user modifies a tile the neighbours can be easily uploaded to gpu mem
@@ -894,76 +1016,21 @@ void vg_render(void)
                {
                        struct fish *fish = &world.fishes[i];
                        
-                       if( !fish->alive )
+                       if( fish->alive == 0 )
                                continue;
-
-                       // Evaluate position
-                       struct cell *cell = pcell(fish->pos);
-                       v2f fish_pos;
-                       
-                       v2f const *curve;
-                       
-                       float t = frame_lerp;
-                       
-                       switch( cell->config )
-                       {
-                               case 13:
-                                       if( fish->dir[0] == 1 )
-                                               curve = curve_12;
-                                       else
-                                               curve = curve_9;
-                               break;
-                               case 3: curve = curve_3; break;
-                               case 6: curve = curve_6; break;
-                               case 9: curve = curve_9; break;
-                               case 12: curve = curve_12; break;
-                               case 7:
-                                       if( t > curve_7_linear_section )
-                                       {
-                                               t -= curve_7_linear_section;
-                                               t *= (1.0f/(1.0f-curve_7_linear_section));
-                                               
-                                               curve = cell->state & FLAG_FLIP_FLOP? curve_7: curve_7_1;
-                                       }
-                                       else curve = NULL;
-                               break;
-                               default: curve = NULL; break;
-                       }
                        
-                       if( curve )
-                       {
-                               float t2 = t * t;
-                               float t3 = t * t * t;
-
-                               float cA = 3.0f*t2 - 3.0f*t3;
-                               float cB = 3.0f*t3 - 6.0f*t2 + 3.0f*t;
-                               float cC = 3.0f*t2 - t3 - 3.0f*t + 1.0f;
-
-                               fish_pos[0] = t3*curve[3][0] + cA*curve[2][0] + cB*curve[1][0] + cC*curve[0][0];
-                               fish_pos[1] = t3*curve[3][1] + cA*curve[2][1] + cB*curve[1][1] + cC*curve[0][1];
-                               fish_pos[0] += (float)fish->pos[0];
-                               fish_pos[1] += (float)fish->pos[1];
-                       }
-                       else
-                       {
-                               v2f origin;
-                               origin[0] = (float)fish->pos[0] + (float)fish->dir[0]*-0.5f + 0.5f;
-                               origin[1] = (float)fish->pos[1] + (float)fish->dir[1]*-0.5f + 0.5f;
-                               
-                               fish_pos[0] = origin[0] + (float)fish->dir[0]*t;
-                               fish_pos[1] = origin[1] + (float)fish->dir[1]*t;
-                       }
+                       if( fish->alive == -1 && (world.frame_lerp > fish->death_time) )
+                               continue;
                        
                        v4f dot_colour = { 0.0f, 0.0f, 0.0f, 1.0f };
                        colour_code_v3( fish->payload, dot_colour );
                        
                        glUniform3fv( SHADER_UNIFORM( shader_ball, "uColour" ), 1, dot_colour );
-                       glUniform2f( SHADER_UNIFORM( shader_ball, "uOffset" ), fish_pos[0], fish_pos[1] );
+                       glUniform2fv( SHADER_UNIFORM( shader_ball, "uOffset" ), 1, fish->physics_co );
                        draw_mesh( 0, 32 );
                }
        }
        
-       
        SHADER_USE( shader_tile_main );
 
        // Bind textures
@@ -989,12 +1056,12 @@ void vg_render(void)
                                
                                if( cell->state & FLAG_FLIP_ROTATING )
                                {
-                                       if( (frame_lerp > curve_7_linear_section) )
+                                       if( (world.frame_lerp > curve_7_linear_section) )
                                        {
                                                float const rotation_speed = 0.4f;
-                                               if( (frame_lerp < 1.0f-rotation_speed) )
+                                               if( (world.frame_lerp < 1.0f-rotation_speed) )
                                                {
-                                                       float t = frame_lerp - curve_7_linear_section;
+                                                       float t = world.frame_lerp - curve_7_linear_section;
                                                        t *= -2.0f * (1.0f/(1.0f-(curve_7_linear_section+rotation_speed)));
                                                        t += 1.0f;
                                                        
@@ -1059,7 +1126,8 @@ void vg_render(void)
                
                for( int j = 0; j < arrlen( term->conditions ); j ++ )
                {
-                       glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + 0.2f, 0.1f );
+                       float y_offset = is_input? 0.8f: 0.2f;
+                       glUniform3f( SHADER_UNIFORM( shader_tile_colour, "uOffset" ), (float)posx + 0.2f + 0.2f * (float)j, (float)posy + y_offset, 0.1f );
                        
                        if( is_input )
                        {
index 5e4ce73f1f39cc02437fe5b0fa33ebd209cd57d4..aa41e19761f2904fbfb2f86592488bfe6e8cef44 100644 (file)
Binary files a/textures/tileset.png and b/textures/tileset.png differ
diff --git a/vg/vg.h b/vg/vg.h
index 33f177cedb5e210a691351551efe9bdae59e7c7b..74a8775cf8c10e11e8d3ca57fe7ba202b92184f4 100644 (file)
--- a/vg/vg.h
+++ b/vg/vg.h
@@ -233,7 +233,7 @@ static void vg_init( int argc, char *argv[], const char *window_name )
                        
                        vg_time_last = vg_time;
                        vg_time = glfwGetTime();
-                       vg_time_delta = vg_min( vg_time - vg_time_last, 0.1f );
+                       vg_time_delta = vg_minf( vg_time - vg_time_last, 0.1f );
                        
                        vg_update_inputs();
                        vg_update();
index cb98db2d1a5a64f060a5c150b9bdc9d663a6b3d3..690683710d816a9bec4ec8444b29d54b58b9e007 100644 (file)
--- a/vg/vg_m.h
+++ b/vg/vg_m.h
@@ -77,6 +77,12 @@ static inline void v2_muls( v2f a, float s, v2f d )
        d[0] = a[0]*s; d[1] = a[1]*s;
 }
 
+static inline void v2_divs( v2f a, float s, v2f d )
+{
+       d[0] = a[0]/s; d[1] = a[1]/s;
+}
+
+
 static inline void v2_mul( v2f a, v2f b, v2f d )
 {
        d[0] = a[0]*b[0]; d[1] = a[1]*b[1];
@@ -87,6 +93,11 @@ static inline void v2_div( v2f a, v2f b, v2f d )
        d[0] = a[0]/b[0]; d[1] = a[1]/b[1];
 }
 
+static inline void v2_muladds( v2f a, v2f b, float s, v2f d )
+{
+       d[0] = a[0]+b[0]*s; d[1] = a[1]+b[1]*s;
+}
+
 // Vector 3
 // ==================================================================================================================