better mouse controlling
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 29 Apr 2021 16:08:41 +0000 (11:08 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 29 Apr 2021 16:08:41 +0000 (11:08 -0500)
site/sand_toy.wasm
src/sand_toy.onyx
src/simulation.onyx

index 49bc6f47d2957d2ef9e05b45fad247eaa12d20a2..1081a108e6c200ab3ed6264b047f9230a8a39a7a 100644 (file)
Binary files a/site/sand_toy.wasm and b/site/sand_toy.wasm differ
index 7d717de8e309b0b17d0914600f23580c41497b83..ee56ab4f8ba2361b18e30bf41c6b4fa5b70e2025 100644 (file)
@@ -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;
index 93a12faf1d6e00fb19d5123c020f3111febbb5e1..6057b1fb4fdd5fbb68c728810ab6e3e1e96f7c9c 100644 (file)
@@ -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]);