Fix frame and grabbing
This commit is contained in:
+39
-29
@@ -87,6 +87,7 @@ const State = struct {
|
||||
frame_count: i32,
|
||||
mouse_pos: V2,
|
||||
camera: Camera,
|
||||
grabbed_brick_index: ?usize,
|
||||
bricks: [8]Brick,
|
||||
};
|
||||
|
||||
@@ -238,6 +239,8 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
|
||||
if (!memory.initialized) {
|
||||
state.camera.offset = mid;
|
||||
|
||||
state.grabbed_brick_index = null;
|
||||
|
||||
state.bricks[0] = Brick{
|
||||
.rotating = false,
|
||||
.rotating_time = 0,
|
||||
@@ -350,8 +353,11 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
|
||||
|
||||
const mouse_world_pos = mouse_pos.sub(state.camera.pos);
|
||||
|
||||
var mouse_on_brick = false;
|
||||
for (&state.bricks) |*brick| {
|
||||
if (!input.mouse_left_down) {
|
||||
state.grabbed_brick_index = null;
|
||||
}
|
||||
|
||||
for (&state.bricks, 0..) |*brick, brick_index| {
|
||||
if (brick.rotating) {
|
||||
brick.rotating_time += input.delta_time;
|
||||
if (brick.rotating_time > 0.25) {
|
||||
@@ -370,41 +376,45 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
|
||||
const pos = tile_pos.add(brick.pos);
|
||||
|
||||
const camera_pos = pos.add(state.camera.pos);
|
||||
if (pointInsideRect(state.mouse_pos, camera_pos, tile_size)) {
|
||||
if (input.mouse_left_down) {
|
||||
const diff = mouse_pos.sub(state.mouse_pos);
|
||||
brick.pos = brick.pos.add(diff);
|
||||
|
||||
if (keyPressed(&input.key.space)) {
|
||||
brick.rotating = true;
|
||||
const rotated = brick.rotate();
|
||||
brick.tiles = rotated;
|
||||
|
||||
const bbox = findBrickBoundingBox(brick);
|
||||
const center = brick.pos.sub(bbox.center);
|
||||
brick.pos = mouse_world_pos.add(center);
|
||||
if (input.mouse_left_down) {
|
||||
if (pointInsideRect(state.mouse_pos, camera_pos, tile_size)) {
|
||||
if (state.grabbed_brick_index == null) {
|
||||
state.grabbed_brick_index = brick_index;
|
||||
}
|
||||
|
||||
// Flip
|
||||
if (keyPressed(&input.key.f)) {
|
||||
brick.rotating = true;
|
||||
const flipped = brick.transpose();
|
||||
brick.tiles = flipped;
|
||||
|
||||
const bbox = findBrickBoundingBox(brick);
|
||||
const center = brick.pos.sub(bbox.center);
|
||||
brick.pos = mouse_world_pos.add(center);
|
||||
}
|
||||
|
||||
mouse_on_brick = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (brick_index == state.grabbed_brick_index) {
|
||||
const diff = mouse_pos.sub(state.mouse_pos);
|
||||
brick.pos = brick.pos.add(diff);
|
||||
|
||||
if (keyPressed(&input.key.space)) {
|
||||
brick.rotating = true;
|
||||
const rotated = brick.rotate();
|
||||
brick.tiles = rotated;
|
||||
|
||||
const bbox = findBrickBoundingBox(brick);
|
||||
const center = brick.pos.sub(bbox.center);
|
||||
brick.pos = mouse_world_pos.add(center);
|
||||
}
|
||||
|
||||
// Flip
|
||||
if (keyPressed(&input.key.f)) {
|
||||
brick.rotating = true;
|
||||
const flipped = brick.transpose();
|
||||
brick.tiles = flipped;
|
||||
|
||||
const bbox = findBrickBoundingBox(brick);
|
||||
const center = brick.pos.sub(bbox.center);
|
||||
brick.pos = mouse_world_pos.add(center);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (input.mouse_left_down and !mouse_on_brick) {
|
||||
if (input.mouse_left_down and state.grabbed_brick_index == null) {
|
||||
const diff = mouse_pos.sub(state.mouse_pos);
|
||||
state.camera.pos = state.camera.pos.add(diff);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user