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
};
float sim_start;
int simulating;
+ float frame_lerp;
+
struct cell_terminal
{
// TODO: Split into input/output structures
v2i dir;
int alive;
char payload;
+ float death_time;
+ v2f physics_v;
+ v2f physics_co;
}
fishes[16];
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 )
{
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 ++ )
{
}
}
+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;
// 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 ++ )
{
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
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;
+ }
+ }
+ }
}
}
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];
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
{
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
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;
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 )
{