adding gravity NOT DONE
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 30 Apr 2021 03:59:10 +0000 (22:59 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 30 Apr 2021 03:59:10 +0000 (22:59 -0500)
site/sand_toy.wasm
src/simulation.onyx

index 1081a108e6c200ab3ed6264b047f9230a8a39a7a..9b792b797343a297d9874c67729f83303862f131 100644 (file)
Binary files a/site/sand_toy.wasm and b/site/sand_toy.wasm differ
index 6057b1fb4fdd5fbb68c728810ab6e3e1e96f7c9c..21f7d3f66b299ba743c0b3692281042243f325b2 100644 (file)
@@ -1,10 +1,14 @@
 use package core
 
+Gravity :: 0.3f
+
+
+
 Color :: struct {
     Empty :: Color.{  22,  22,  22 };
     Sand  :: Color.{ 226, 214, 173 };
     Water :: Color.{ 100, 100, 255 };
-    Wall  :: Color.{  60,  60,  30 };
+    Wood  :: Color.{  60,  60,  30 };
 
     r, g, b: u8;
 }
@@ -33,10 +37,10 @@ Particle :: struct {
         Empty;
         Sand;
         Water;
-        Wall;
+        Wood;
     }
     
-    types :: Type.[ .Sand, .Water, .Wall ];
+    types :: Type.[ .Sand, .Water, .Wood ];
 
     type: Type = .Empty;
     life: f32 = 0;
@@ -47,7 +51,7 @@ particle_type_to_color :: (t: Particle.Type) -> Color {
     switch t {
         case .Sand    do return .Sand;
         case .Water   do return .Water;
-        case .Wall    do return .Wall;
+        case .Wood    do return .Wood;
         case #default do return .Empty;
     }
 }
@@ -101,7 +105,7 @@ update_particles :: (use board: ^ParticleBoard) {
             switch particle_type {
                 case .Sand  do update_sand(board, x, y);
                 case .Water do update_water(board, x, y);
-                case .Wall  --- // Walls do not need updating
+                case .Wood  --- // Woods do not need updating
                 case .Empty ---
             }
         }
@@ -122,7 +126,7 @@ particle_switch :: (use board: ^ParticleBoard, a_index: i32, b_index: i32) {
 
 // This should get inlined by Onyx, when inlining is a thing.
 get_index :: (use board: ^ParticleBoard, x: i32, y: i32) -> i32 {
-    if x < 0 || y < 0 || x >= width || y >= height do return -1;
+    if x < 0 || y < 0 || x >= ~~width || y >= ~~height do return -1;
     return y * width + x;
 }
 
@@ -139,9 +143,7 @@ first_empty :: (use board: ^ParticleBoard, indicies: ..i32) -> i32 {
     for index: indicies {
         if index < 0 do continue;
 
-        if particles[index].type == .Empty {
-            return index;
-        }
+        if particles[index].type == .Empty do return index;
     }
 
     return -1;
@@ -167,18 +169,28 @@ can_sink :: (use board: ^ParticleBoard, below_index: i32) -> bool {
 update_sand :: (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);
+    particles[index].velocity.y += Gravity;
 
-    move_to_index := first_empty(board, b_index, bl_index, br_index); 
+    if particles[get_index(board, x, y + 1)].type != .Empty {
+        particles[index].velocity.y = 0;
+    }
 
-    if move_to_index != -1 {
-        particle_set(board, move_to_index, particles[index]);
-        particle_set(board, index, .{ .Empty });
-        return;
+    vy := cast(i32) particles[index].velocity.y;
+    for yy: y + 1 .. y + vy + 2 {
+        b_index  := get_index(board, x, yy);
+        bl_index := get_index(board, x - 1, yy);
+        br_index := get_index(board, x + 1, yy);
+
+        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]);
+            particle_set(board, index, .{ .Empty });
+            return;
+        }
     }
 
+    b_index := get_index(board, x, y + 1);
     if can_sink(board, b_index) {
         particle_switch(board, index, b_index);
     }
@@ -188,16 +200,36 @@ update_sand :: (use board: ^ParticleBoard, x: i32, y: i32) {
 update_water :: (use board: ^ParticleBoard, x: i32, y: i32) {
     index := get_index(board, x, y);
 
-    b_index  := get_index(board, x, 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);
+    particles[index].velocity.y += Gravity;
+
+    if particles[get_index(board, x, y + 1)].type != .Empty {
+        particles[index].velocity.y = 0;
+    }
+
+    vy       := cast(i32) particles[index].velocity.y;
+    b_index  := get_index(board, x, y + vy + 1);
+    bl_index := get_index(board, x - 1, y + vy + 1);
+    br_index := get_index(board, x + 1, y + vy + 1);
 
-    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);
 
     if move_to_index != -1 {
         particle_set(board, move_to_index, particles[index]);
         particle_set(board, index, .{ .Empty });
+        return;
+    }
+
+    l_index  := get_index(board, x - 1, y);
+    r_index  := get_index(board, x + 1, y);
+    if random.int() % 4 >= 2 {
+        if l_index >= 0 && particles[l_index].type == .Empty {
+            particle_set(board, l_index, particles[index]);
+            particle_set(board, index, .{ .Empty });
+        }
+    } else {
+        if r_index >= 0 && particles[r_index].type == .Empty {
+            particle_set(board, r_index, particles[index]);
+            particle_set(board, index, .{ .Empty });
+        }
     }
 }