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();
}
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 {
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)];
}
}
update :: () {
+ if mouse_down {
+ spawn_particles(~~mouse_x, ~~mouse_y, 20, selected_particle);
+ }
+
update_particles(^particle_board);
}
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;
}
Particle :: struct {
- Type :: enum {
+ Type :: enum (u8) {
Empty;
Sand;
Water;
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);
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]);
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]);