Use press/release actions for the break/place buttons

Fixes #391
This commit is contained in:
IntegratedQuantum 2024-05-26 13:31:52 +02:00
parent 6013b579d9
commit ca37eb97d7
2 changed files with 22 additions and 18 deletions

View File

@ -219,6 +219,26 @@ pub var fog = Fog{.color=.{0, 1, 0.5}, .density=1.0/15.0/128.0}; // TODO: Make t
var nextBlockPlaceTime: ?i64 = null;
var nextBlockBreakTime: ?i64 = null;
pub fn pressPlace() void {
const time = std.time.milliTimestamp();
nextBlockPlaceTime = time + main.settings.updateRepeatDelay;
Player.placeBlock();
}
pub fn releasePlace() void {
nextBlockPlaceTime = null;
}
pub fn pressBreak() void {
const time = std.time.milliTimestamp();
nextBlockBreakTime = time + main.settings.updateRepeatDelay;
Player.breakBlock();
}
pub fn releaseBreak() void {
nextBlockBreakTime = null;
}
pub fn update(deltaTime: f64) void {
var movement = Vec3d{0, 0, 0};
const forward = vec.rotateZ(Vec3d{0, 1, 0}, -camera.rotation[2]);
@ -270,22 +290,6 @@ pub fn update(deltaTime: f64) void {
}
const time = std.time.milliTimestamp();
if(KeyBoard.key("placeBlock").pressed != (nextBlockPlaceTime != null)) {
if(nextBlockPlaceTime != null) {
nextBlockPlaceTime = null;
} else {
nextBlockPlaceTime = time + main.settings.updateRepeatDelay;
Player.placeBlock();
}
}
if(KeyBoard.key("breakBlock").pressed != (nextBlockBreakTime != null)) {
if(nextBlockBreakTime != null) {
nextBlockBreakTime = null;
} else {
nextBlockBreakTime = time + main.settings.updateRepeatDelay;
Player.breakBlock();
}
}
if(nextBlockPlaceTime) |*placeTime| {
if(time -% placeTime.* >= 0) {
placeTime.* += main.settings.updateRepeatSpeed;

View File

@ -294,8 +294,8 @@ pub const KeyBoard = struct {
.{.name = "jump", .key = c.GLFW_KEY_SPACE},
.{.name = "fall", .key = c.GLFW_KEY_LEFT_SHIFT},
.{.name = "fullscreen", .key = c.GLFW_KEY_F11, .releaseAction = &Window.toggleFullscreen},
.{.name = "placeBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT},
.{.name = "breakBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_LEFT},
.{.name = "placeBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT, .pressAction = &game.pressPlace, .releaseAction = &game.releasePlace},
.{.name = "breakBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_LEFT, .pressAction = &game.pressBreak, .releaseAction = &game.releaseBreak},
.{.name = "takeBackgroundImage", .key = c.GLFW_KEY_PRINT_SCREEN, .releaseAction = &takeBackgroundImageFn},