- // Update camera
- float ratio = (float)vg_window_y / (float)vg_window_x;
- float const size = 7.5f;
- glm_ortho( -size, size, -size*ratio, size*ratio, 0.1f, 100.f, m_projection );
-
- glm_mat4_identity( m_view );
- glm_translate_z( m_view, -10.f );
- glm_rotate_x( m_view, 1.0f, m_view );
-
- glm_mat4_mul( m_projection, m_view, vg_pv );
-
- // Compute map update
- map.frame ^= 0x1;
- for( int y = 0; y < map.y; y ++ )
- {
- for( int x = 0; x < map.x; x ++ )
- {
- // Cell is a connector if it has at least 3 connections
- int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0}, {0,1} };
- u32 output_count = 0;
- struct cell *tile, *thistile;
- thistile = map_tile_at( (int [2]){x,y} );
-
- if( thistile->flags & CELL_FLAG_CANAL )
- {
- for( int i = 0; i < vg_list_size( output_dirs ); i ++ )
- {
- tile = map_tile_at( (int [2]){ x+output_dirs[i][0], y+output_dirs[i][1] } );
-
- if( tile && tile->flags & CELL_FLAG_CANAL )
- output_count ++;
- }
-
- if( output_count >= 3 )
- thistile->flags |= CELL_FLAG_CONNECTOR;
- else
- thistile->flags &= ~CELL_FLAG_CONNECTOR;
- }
- }
- }
-
- // Get mouse ray
- vec3 ray_origin;
- vec3 ray_dir;
-
- mat4 pv_inverse;
- vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
- glm_mat4_inv( vg_pv, pv_inverse );
- glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
- glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
- glm_vec3_sub( ray_dir, ray_origin, ray_dir );
-
- // Get floor tile intersection
- float ray_t = -ray_origin[1] / ray_dir[1];
-
- vec3 tile_pos;
- glm_vec3_copy( ray_origin, tile_pos );
- glm_vec3_muladds( ray_dir, ray_t, tile_pos );
- glm_vec3_sub( tile_pos, map.origin, tile_pos );
-
- int tile_x = floorf( tile_pos[0] );
- int tile_y = floorf( tile_pos[2] );
-
- map.selected = map_tile_at( (int [2]){tile_x, tile_y} );
-
- if( map.selected )
- {
- // Check if valid
- int validation[][2] = { {1,1}, {-1,1}, {-1,-1}, {1,-1} };
- struct cell *a, *b, *c;
-
- map.select_valid = 1;
- for( int i = 0; i < vg_list_size( validation ); i ++ )
- {
- a = map_tile_at( (int [2]){ tile_x+validation[i][0], tile_y } );
- b = map_tile_at( (int [2]){ tile_x, tile_y+validation[i][1] } );
-
- if( a && b && (a->flags & b->flags & CELL_FLAG_CANAL) )
- {
- c = map_tile_at( (int [2]){ tile_x+validation[i][0], tile_y+validation[i][1] } );
-
- if( c && (c->flags & CELL_FLAG_CANAL ) )
- {
- map.select_valid = 0;
- break;
- }
- }
- }
-
- if( map.select_valid )
- {
- if( vg_get_button_down("primary") )
- {
- if( map.selected->flags & CELL_FLAG_CANAL )
- {
- map.selected->flags &= ~(CELL_FLAG_CANAL | CELL_FLAG_CONNECTOR);
- }
- else
- {
- map.selected->flags |= CELL_FLAG_CANAL;
- }
- }
- }
- }