From 00bd0600086421b4e1a24cd7e5d44729b8ebb9f4 Mon Sep 17 00:00:00 2001 From: hgn Date: Tue, 20 Jul 2021 19:14:46 +0100 Subject: [PATCH] basic tile editing --- fishladder.c | 106 +++++++++++++++++++++++++++++++++++++++++++-------- vg/config.h | 8 ++-- 2 files changed, 95 insertions(+), 19 deletions(-) diff --git a/fishladder.c b/fishladder.c index 12628ca..70448bc 100644 --- a/fishladder.c +++ b/fishladder.c @@ -44,6 +44,7 @@ int main( int argc, char *argv[] ) #define CELL_FLAG_HOVER 0x8 #define CELL_FLAG_ITER 0x10 #define CELL_FLAG_CANAL 0x20 +#define CELL_FLAG_CONNECTOR 0x40 /* Does this cell split and have an incoming vertical connection? */ static struct { @@ -55,12 +56,16 @@ static struct u32 model_id; char *conditions; + + int level; + int diff[2]; } * cells; vec3 origin; struct cell *selected; int select_valid; + u32 frame; u32 *io; @@ -238,6 +243,36 @@ void vg_update(void) 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; @@ -262,7 +297,45 @@ void vg_update(void) map.selected = map_tile_at( (int [2]){tile_x, tile_y} ); - vg_line2( (vec3){0.f,0.f,0.f}, (vec3){0.f,3.f,0.f}, 0xff0000ff, 0xff00ff00 ); + 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; + } + } + } + } } GLuint tile_vao; @@ -297,23 +370,26 @@ void vg_render(void) struct cell *cell = &map.cells[ y*map.x+x ]; - if( map.selected != cell ) - { - if( cell->flags & CELL_FLAG_INPUT ) - glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.9f, 0.5f, 0.5f, 1.0f ); - else if( cell->flags & CELL_FLAG_OUTPUT ) - glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.5f, 0.9f, 0.5f, 1.0f ); - else if( cell->flags & CELL_FLAG_WALL ) - glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.1f, 0.1f, 0.1f, 1.0f ); - else - glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), 0.7f, 0.7f, 0.7f, 1.0f ); - } - else + vec4 colour = { 0.7f, 0.7f, 0.7f, 1.f }; + + if( cell->flags & CELL_FLAG_INPUT ) glm_vec3_copy( (vec3){ 0.9f,0.5f,0.5f }, colour ); + else if( cell->flags & CELL_FLAG_OUTPUT ) glm_vec3_copy( (vec3){ 0.5f,0.9f,0.5f }, colour ); + else if( cell->flags & CELL_FLAG_WALL ) glm_vec3_copy( (vec3){ 0.1f,0.1f,0.1f }, colour ); + else if( cell->flags & CELL_FLAG_CANAL ) glm_vec3_copy( (vec3){ 0.5f,0.5f,0.8f }, colour ); + + if( cell->flags & CELL_FLAG_CONNECTOR ) + glm_vec3_copy( (vec3){ 0.6f, 0.f, 0.9f }, colour ); + + if( map.selected == cell ) { + if( !map.select_valid ) + glm_vec3_copy( (vec3){ 1.f, 0.f, 0.f }, colour ); + float flash = sinf( vg_time*2.5f ) * 0.25f + 0.75f; - glUniform4f( SHADER_UNIFORM( colour_shader, "uColour" ), flash,flash,flash, 1.0f ); + glm_vec3_scale( colour, flash, colour ); } - + + glUniform4fv( SHADER_UNIFORM( colour_shader, "uColour" ), 1, colour ); glDrawArrays( GL_TRIANGLES, 0, 6 ); } } diff --git a/vg/config.h b/vg/config.h index 01c9b0a..d8a9246 100644 --- a/vg/config.h +++ b/vg/config.h @@ -2,16 +2,16 @@ static struct button_binding vg_button_binds[] = { - { .name = "fire0", .bind = GLFW_MOUSE_BUTTON_LEFT }, - { .name = "fire1", .bind = GLFW_MOUSE_BUTTON_RIGHT }, + { .name = "primary", .bind = GLFW_MOUSE_BUTTON_LEFT }, + { .name = "secondary", .bind = GLFW_MOUSE_BUTTON_RIGHT }, { .name = "noclip", .bind = GLFW_KEY_V, }, { .name = "jump", .bind = GLFW_KEY_SPACE } }; static struct axis_binding vg_axis_binds[] = { - { .name = "fire0", .positive = GLFW_MOUSE_BUTTON_LEFT, .negative = -1 }, - { .name = "fire1", .positive = GLFW_MOUSE_BUTTON_RIGHT, .negative = -1 }, + { .name = "primary", .positive = GLFW_MOUSE_BUTTON_LEFT, .negative = -1 }, + { .name = "secondary", .positive = GLFW_MOUSE_BUTTON_RIGHT, .negative = -1 }, { .name = "horizontal", .positive = GLFW_KEY_D, .negative = GLFW_KEY_A }, { .name = "vertical", .positive = GLFW_KEY_W, .negative = GLFW_KEY_S } }; -- 2.25.1