Better movement code, 8 directions.

This commit is contained in:
KikooDX 2021-01-28 16:52:49 +01:00
parent 0519e630d8
commit 1750fe8cd0
2 changed files with 80 additions and 39 deletions

View File

@ -26,7 +26,7 @@ pub fn main() !void {
ray.SetTargetFPS(60);
// Create level.
var level: Level = try Level.init(allocator, 128, 128);
var level: Level = try Level.init(allocator, 16, 16);
defer level.deinit(allocator);
// Create camera.
@ -34,7 +34,7 @@ pub fn main() !void {
// Scale, used by drawing code.
const scale_type = u16;
var scale: scale_type = 4;
var scale: scale_type = 16;
// Create cursor.
var cursor: Vec2 = Vec2.init(0, 0);
@ -73,13 +73,27 @@ pub fn main() !void {
var selection_update: movement.SelectionUpdate = undefined;
// Zoom. TODO: move to functions.
switch (action) {
// Movement.
'h' => selection_update = movement.move_left(&cursor, 1, true),
'j' => selection_update = movement.move_down(&cursor, 1, true),
'k' => selection_update = movement.move_up(&cursor, 1, true),
'l' => selection_update = movement.move_right(&cursor, 1, true),
'H' => selection_update = movement.move_left(&cursor, 1, false),
'J' => selection_update = movement.move_down(&cursor, 1, false),
'K' => selection_update = movement.move_up(&cursor, 1, false),
'L' => selection_update = movement.move_right(&cursor, 1, false),
// Zoom. TODO: move to functions.
// Diagonals and rectangle selection.
'y' => selection_update = movement.move_up_left(&cursor, 1, true),
'u' => selection_update = movement.move_up_right(&cursor, 1, true),
'b' => selection_update = movement.move_down_left(&cursor, 1, true),
'n' => selection_update = movement.move_down_right(&cursor, 1, true),
'Y' => selection_update = movement.move_up_left(&cursor, 1, false),
'U' => selection_update = movement.move_up_right(&cursor, 1, false),
'B' => selection_update = movement.move_down_left(&cursor, 1, false),
'N' => selection_update = movement.move_down_right(&cursor, 1, false),
// Zoom (pog feature).
'-' => if (scale > 3) {
scale -= 1;
},
@ -88,10 +102,7 @@ pub fn main() !void {
},
// Reset zoom.
'0' => scale = 4,
else => {
log("No action mapped to key {}.", .{action});
break undefined;
},
else => log("No action mapped to key {}.", .{action}),
}
if (selection_update.exclusive != undefined)

View File

@ -15,20 +15,34 @@ pub const SelectionUpdate = struct {
state: bool,
};
/// Try to move the cursor `n` times to the left.
pub fn move_left(cursor: *Vec2, n: u64, exclusive_selection: bool) SelectionUpdate {
/// Universal move system, prefer direction wrappers.
fn move(cursor: *Vec2, n: u32, exclusive_selection: bool, dx: i2, dy: i2) SelectionUpdate {
const before: Vec2 = cursor.*;
if (n > maxIntVec2) {
cursor.x = 0;
} else {
const steps = @intCast(Vec2.int_type, n);
if (cursor.x >= steps)
cursor.x -= steps
else
cursor.x = 0;
if (dx > 0) {
var i: u32 = 0;
while (i != n and cursor.x < maxIntVec2) : (i += 1) {
cursor.x += 1;
}
} else if (dx < 0) {
var i: u32 = 0;
while (i != n and cursor.x > 0) : (i += 1) {
cursor.x -= 1;
}
}
return SelectionUpdate {
if (dy > 0) {
var i: u32 = 0;
while (i != n and cursor.y < maxIntVec2) : (i += 1) {
cursor.y += 1;
}
} else if (dy < 0) {
var i: u32 = 0;
while (i != n and cursor.y > 0) : (i += 1) {
cursor.y -= 1;
}
}
return SelectionUpdate{
.exclusive = exclusive_selection,
.area = if (exclusive_selection)
Rect.init_from_vec2(cursor.*, cursor.*)
@ -38,28 +52,44 @@ pub fn move_left(cursor: *Vec2, n: u64, exclusive_selection: bool) SelectionUpda
};
}
/// Try to move the cursor `n` times to the right.
pub fn move_right(cursor: *Vec2, n: u64, exclusive_selection: bool) SelectionUpdate {
const before: Vec2 = cursor.*;
if (n > maxIntVec2) {
cursor.x = maxIntVec2;
} else {
const steps = @intCast(Vec2.int_type, n);
/// Try to move the cursor `n` times left.
pub fn move_left(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, -1, 0);
}
if (maxIntVec2 - cursor.x >= steps)
cursor.x += steps
else
cursor.x = maxIntVec2;
}
// TODO: remove code duplication.
return SelectionUpdate {
.exclusive = exclusive_selection,
.area = if (exclusive_selection)
Rect.init_from_vec2(cursor.*, cursor.*)
else
Rect.init_from_vec2(before, cursor.*),
.state = true,
};
/// Try to move the cursor `n` times right.
pub fn move_right(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, 1, 0);
}
/// Try to move the cursor `n` times up.
pub fn move_up(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, 0, -1);
}
/// Try to move the cursor `n` times down.
pub fn move_down(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, 0, 1);
}
/// Try to move the cursor `n` times up and left.
pub fn move_up_left(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, -1, -1);
}
/// Try to move the cursor `n` times up and right.
pub fn move_up_right(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, 1, -1);
}
/// Try to move the cursor `n` times down and left.
pub fn move_down_left(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, -1, 1);
}
/// Try to move the cursor `n` times down and right.
pub fn move_down_right(cursor: *Vec2, n: u32, exclusive_selection: bool) SelectionUpdate {
return move(cursor, n, exclusive_selection, 1, 1);
}
test "move left" {