Add clear button

This commit is contained in:
2026-03-15 16:32:01 +01:00
parent d239d818eb
commit 4e27783354
2 changed files with 82 additions and 11 deletions
+26
View File
@@ -62,3 +62,29 @@ pub fn hadamard(self: V2, other: V2) V2 {
return result;
}
pub fn lengthSq(self: V2) f32 {
const result = inner(self, self);
return result;
}
pub fn length(self: V2) f32 {
const result = @sqrt(lengthSq(self));
return result;
}
pub fn norm(self: V2) V2 {
var result = V2.zero;
const len = length(self);
if (len > 0.0) {
result = .{
.x = self.x / len,
.y = self.y / len,
};
}
return result;
}
+56 -11
View File
@@ -717,6 +717,8 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
state.draw_bounding_rects = !state.draw_bounding_rects;
}
const clear_pressed = keyPressed(&input.key.q);
if (!input.mouse_left_down) {
state.grabbed_brick_index = null;
}
@@ -731,6 +733,10 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
}
for (&state.bricks, 0..) |*brick, brick_index| {
var new_pos = brick.pos;
const bounding_box = findBrickBoundingBox(brick);
if (brick.rotating) {
brick.rotating_time += input.delta_time;
if (brick.rotating_time > 0.05) {
@@ -742,7 +748,7 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
for (brick.tiles, 0..) |row, i| {
for (row, 0..) |tile, j| {
if (tile > 0) {
const pos = getTilePos(i, j).add(brick.pos);
const pos = getTilePos(i, j).add(new_pos);
const tile_mid = pos.add(tile_size.mul(0.5));
const tile_board_index = &brick.board_indices[i][j];
@@ -779,7 +785,7 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
if (brick_index == state.grabbed_brick_index) {
const diff = mouse_pos.sub(state.mouse_pos);
brick.pos = brick.pos.add(diff);
new_pos = new_pos.add(diff);
if (keyPressed(&input.key.e) or keyPressed(&input.key.f)) {
brick.rotating = true;
@@ -787,8 +793,8 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
brick.tiles = rotated;
const bbox = findBrickBoundingBox(brick);
const center = brick.pos.sub(bbox.center);
brick.pos = mouse_world_pos.add(center);
const center = new_pos.sub(bbox.center);
new_pos = mouse_world_pos.add(center);
}
// Flip
@@ -798,23 +804,36 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
brick.tiles = flipped;
const bbox = findBrickBoundingBox(brick);
const center = brick.pos.sub(bbox.center);
brick.pos = mouse_world_pos.add(center);
const center = new_pos.sub(bbox.center);
new_pos = mouse_world_pos.add(center);
}
}
const valid_pos = brickHasValidPos(brick);
if (!input.mouse_left_down) {
const valid_pos = brickHasValidPos(brick);
if (valid_pos) {
// TODO: Snap to board
const min = findBrickMinBoardIdx(brick);
const board_tile_pos = state.board.getTilePos(min.i, min.j);
const bbox = findBrickBoundingBox(brick);
const diff = brick.pos.sub(bbox.min);
brick.pos = board_tile_pos.add(diff);
const diff = new_pos.sub(bbox.min);
new_pos = board_tile_pos.add(diff);
}
}
if (clear_pressed) {
if (valid_pos) {
const board = state.board;
const board_size = V2.initUSize(board.width * TILE_SIZE, board.height * TILE_SIZE);
const board_half = board_size.mul(0.5);
const board_half_len = board_half.length();
const board_mid = board.pos.add(board_half);
const diff = bounding_box.center.sub(board_mid).norm();
new_pos = new_pos.add(diff.mul(board_half_len));
}
}
brick.pos = new_pos;
}
// NOTE: Move the grabbed brick to the end of the array. This makes become
@@ -841,7 +860,7 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
},
}
};
// xor bby
// xor bby: valid if tile is occupied, or if it's the current day and month, it should not be
const valid = (board_tile.taken and !is_day_or_month) or (!board_tile.taken and is_day_or_month);
has_won = has_won and valid;
}
@@ -963,6 +982,30 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
}
}
}
if (comptime builtin.mode == std.builtin.OptimizeMode.Debug) {
if (state.draw_bounding_rects) {
const board = state.board;
const board_size = V2.initUSize(board.width * TILE_SIZE, board.height * TILE_SIZE);
{
const camera_pos = board.pos.add(state.camera.pos);
drawRect(buffer, camera_pos, board_size, 0xFFFF0000, 1);
}
{
const board_size_half = board_size.mul(0.5);
const camera_pos = board.pos.add(board_size_half).add(state.camera.pos);
const size = 8;
const half = @divTrunc(size, 2);
const x: i32 = @intFromFloat(camera_pos.x);
const y: i32 = @intFromFloat(camera_pos.y);
fillSquare(buffer, x - half, y - half, 8, 0xFFFF0000);
}
}
}
//
// UI
//
@@ -980,6 +1023,8 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
const scale = 0.5;
const height = buffer.height - state.font.glyph_height;
drawString(buffer, state.font, V2.initI32(8, height - 2 * state.font.glyph_height + 8), scale, "Clear: q");
drawString(buffer, state.font, V2.initI32(8, height - state.font.glyph_height), scale, "Flip: w");
drawString(buffer, state.font, V2.initI32(8, height - 8), scale, "Rotate: e");