From: Brendan Hansen Date: Thu, 29 Apr 2021 16:08:41 +0000 (-0500) Subject: better mouse controlling X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=32dba52411a573745ce0e0829bdefb4b6a64577b;p=sand-toy.git better mouse controlling --- diff --git a/site/sand_toy.wasm b/site/sand_toy.wasm index 49bc6f4..1081a10 100644 Binary files a/site/sand_toy.wasm and b/site/sand_toy.wasm differ diff --git a/src/sand_toy.onyx b/src/sand_toy.onyx index 7d717de..ee56ab4 100644 --- a/src/sand_toy.onyx +++ b/src/sand_toy.onyx @@ -9,13 +9,16 @@ particle_board: ParticleBoard; world_texture : gl.GLTexture world_texture_data : [] u8 -world_width := 256 +world_width := 512 world_height := 256 window_width := 0 window_height := 0 selected_particle := Particle.Type.Empty +mouse_down := false +mouse_x := 0.0f +mouse_y := 0.0f init :: () { events.init(); @@ -57,10 +60,10 @@ poll_events :: () { } if event.mouse.button == .Left { - mx := (cast(f32) event.mouse.pos_x / cast(f32) window_width) * ~~world_width; - my := ((~~event.mouse.pos_y - BAR_HEIGHT) / cast(f32) window_height) * ~~world_height; + mouse_x = (cast(f32) event.mouse.pos_x / cast(f32) window_width) * ~~world_width; + mouse_y = ((~~event.mouse.pos_y - BAR_HEIGHT) / cast(f32) window_height) * ~~world_height; - spawn_particles(~~mx, ~~my, 20, selected_particle); + mouse_down = true; } if event.mouse.button == .Right { @@ -70,10 +73,25 @@ poll_events :: () { remove_particles(~~mx, ~~my, 10); } } + + case .MouseUp { + mouse_down = false; + } + + case .MouseMove { + if event.mouse.pos_y < ~~BAR_HEIGHT do return; + + if mouse_down { + mouse_x = (cast(f32) event.mouse.pos_x / cast(f32) window_width) * ~~world_width; + mouse_y = ((~~event.mouse.pos_y - BAR_HEIGHT) / cast(f32) window_height) * ~~world_height; + } + } } } handle_ui_mouse :: (event: ^events.MouseEvent) { + if event.kind != .MouseDown do return; + if event.button == .Left { selected_particle = Particle.types[cast(u32) math.floor(~~event.pos_x / BAR_HEIGHT)]; } @@ -104,6 +122,10 @@ remove_particles :: (x: i32, y: i32, radius: i32) { } update :: () { + if mouse_down { + spawn_particles(~~mouse_x, ~~mouse_y, 20, selected_particle); + } + update_particles(^particle_board); } @@ -111,7 +133,9 @@ prepare_world_texture :: () { for y: particle_board.height { for x: particle_board.width { index := get_index(^particle_board, x, y); - color := particle_type_to_color(particle_board.particles[index].type); + type := particle_board.particles[index].type; + color := particle_type_to_color(type); + world_texture_data[index * 3 + 0] = color.r; world_texture_data[index * 3 + 1] = color.g; world_texture_data[index * 3 + 2] = color.b; diff --git a/src/simulation.onyx b/src/simulation.onyx index 93a12fa..6057b1f 100644 --- a/src/simulation.onyx +++ b/src/simulation.onyx @@ -29,7 +29,7 @@ ParticleBoard :: struct { } Particle :: struct { - Type :: enum { + Type :: enum (u8) { Empty; Sand; Water; @@ -147,6 +147,17 @@ first_empty :: (use board: ^ParticleBoard, indicies: ..i32) -> i32 { return -1; } +#private_file +all_are :: (use board: ^ParticleBoard, type: Particle.Type, indicies: ..i32) -> bool { + for index: indicies { + if index < 0 do continue; + + if particles[index].type != type do return false; + } + + return true; +} + #private_file can_sink :: (use board: ^ParticleBoard, below_index: i32) -> bool { return particle_is_liquid(particles[below_index].type); @@ -160,12 +171,7 @@ update_sand :: (use board: ^ParticleBoard, x: i32, y: i32) { bl_index := get_index(board, x - 1, y + 1); br_index := get_index(board, x + 1, y + 1); - move_to_index := -1; - if time % 2 == 0 { - move_to_index = first_empty(board, b_index, bl_index, br_index); - } else { - move_to_index = first_empty(board, b_index, br_index, bl_index); - } + move_to_index := first_empty(board, b_index, bl_index, br_index); if move_to_index != -1 { particle_set(board, move_to_index, particles[index]); @@ -183,17 +189,12 @@ update_water :: (use board: ^ParticleBoard, x: i32, y: i32) { index := get_index(board, x, y); b_index := get_index(board, x, y + 1); - bl_index := get_index(board, x - 1, y + 1); - br_index := get_index(board, x + 1, y + 1); l_index := get_index(board, x - 1, y); r_index := get_index(board, x + 1, y); + bl_index := get_index(board, x - 1, y + 1); + br_index := get_index(board, x + 1, y + 1); - move_to_index := -1; - if time % 2 == 1 { - move_to_index = first_empty(board, b_index, bl_index, br_index, l_index, r_index); - } else { - move_to_index = first_empty(board, b_index, br_index, bl_index, r_index, l_index); - } + move_to_index := first_empty(board, b_index, br_index, bl_index, r_index, l_index); if move_to_index != -1 { particle_set(board, move_to_index, particles[index]);