flowing water between cells
authorhgn <hgodden00@gmail.com>
Tue, 21 Sep 2021 23:42:07 +0000 (00:42 +0100)
committerhgn <hgodden00@gmail.com>
Tue, 21 Sep 2021 23:42:07 +0000 (00:42 +0100)
fishladder.c

index 462b26891d4d4e7fe12d45538b0ff957380dbd52..b19f58523b2161207c4d370156aaeb9e35e82ddf 100644 (file)
@@ -41,7 +41,8 @@ struct world
        struct cell
        {
                u32 state;
-       } 
+               u8 water;
+       }
        *data;
        
        struct cell_terminal
@@ -168,6 +169,7 @@ static int map_load( const char *str )
                        
                        // Tile initialization
                        // row[ cx ] .. etc
+                       row[ cx ].water = 0;
                        
                        if( *c == '+' || *c == '-' )
                        {
@@ -274,10 +276,56 @@ void vg_update(void)
        int tile_x = floorf( tile_pos[0] );
        int tile_y = floorf( tile_pos[1] );
        
-       if( tile_x >= 0 && tile_x < world.w && tile_y >= 0 && tile_y <= world.h )
+       if( tile_x >= 2 && tile_x < world.w-2 && tile_y >= 2 && tile_y <= world.h-2 )
+       {
                world.selected = tile_y * world.w + tile_x;
+               
+               struct cell *cell = &world.data[tile_y*world.w+tile_x];
+               if( cell->state & (FLAG_WALL|FLAG_INPUT|FLAG_OUTPUT) )
+               {
+                       world.selected = -1;
+               }
+               else
+               {
+                       if( vg_get_button_down("primary") )
+                       {
+                               cell->state ^= FLAG_CANAL;
+                       }
+               }
+       }
        else
                world.selected = -1;
+       
+       for( int y = 1; y < world.h-1; y ++ )
+       {
+               for( int x = 1; x < world.w-1; x ++ )
+               {
+                       struct cell *cell = &world.data[y*world.w+x];
+
+                       if( !(cell->state & FLAG_CANAL) )
+                               cell->water = 0;
+                       
+                       if( cell->state & FLAG_INPUT )
+                               cell->water = 8;
+                       
+                       if( cell->water )
+                       {
+                               v2i dirs[] = {{1,0},{0,1},{-1,0},{0,-1}};
+                               
+                               for( int i = 0; i < vg_list_size( dirs ); i ++ )
+                               {
+                                       struct cell *neighbour = &world.data[(y+dirs[i][1])*world.w+x+dirs[i][0]];
+                                       
+                                       if( neighbour->state & FLAG_CANAL )
+                                       {
+                                               neighbour->water = vg_max( neighbour->water, cell->water-1 );
+                                       }
+                               }
+                               
+                               cell->water --;
+                       }
+               }
+       }
 }
 
 void vg_render(void)
@@ -302,12 +350,15 @@ void vg_render(void)
                        
                        struct cell *cell = &world.data[y*world.w+x];
                        
-                       if( cell->state & FLAG_WALL ) { v4_copy( (v4f){ 0.4f, 0.4f, 0.4f, 1.0f }, colour ); }
+                       if( cell->state & FLAG_WALL ) { v4_copy( (v4f){ 0.2f, 0.2f, 0.2f, 1.0f }, colour ); }
                        else if( cell->state & FLAG_CANAL ) { v4_copy( (v4f){ 0.6f, 0.6f, 0.6f, 1.0f }, colour ); }
                        else if( cell->state & FLAG_INPUT ) { v4_copy( (v4f){ 0.2f, 0.3f, 0.7f, 1.0f }, colour ); }
                        else if( cell->state & FLAG_OUTPUT ) { v4_copy( (v4f){ 0.2f, 0.7f, 0.3f, 1.0f }, colour ); }
                        else v4_copy( (v4f){ 0.9f, 0.9f, 0.9f, 1.0f }, colour );
                        
+                       if( cell->water )
+                               v4_copy( (v4f){ 0.2f, 0.3f, 0.7f * (float)(cell->water) * (1.0f/8.0f), 1.0f }, colour );
+                       
                        if( world.selected == y*world.w + x )
                                v3_muls( colour, sinf( vg_time )*0.25f + 0.5f, colour );