From: Brendan Hansen Date: Sat, 2 Apr 2022 19:44:32 +0000 (-0500) Subject: bugfixes with textbox controls; separate player logic X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a2ccd3e160cc0f9229730c12fcf1083615ac3976;p=voxel-shooter.git bugfixes with textbox controls; separate player logic --- diff --git a/src/client/gfx/ui.onyx b/src/client/gfx/ui.onyx index 570a2ef..985fc66 100644 --- a/src/client/gfx/ui.onyx +++ b/src/client/gfx/ui.onyx @@ -291,37 +291,31 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th textbox_editing_state.cursor_position = screen_to_cursor(^font, text_x, text_y, text, ~~mx, ~~my); } - move_towards(^textbox_editing_state.action_key_timeout, 0, 0.05f); - if textbox_editing_state.action_key_timeout == 0 { - keys := input_get_keys_this_frame(); - for key: iter.as_iterator(^keys) { - textbox_editing_state.action_key_timeout = 0.17f; - - switch key.key { - case GLFW_KEY_ESCAPE { - set_active_item(0); - input_release_keys(); - } + for key: input_get_keys_this_frame() { + switch key.key { + case GLFW_KEY_ESCAPE { + set_active_item(0); + input_release_keys(); + } - case GLFW_KEY_LEFT do textbox_editing_state.cursor_position -= 1; - case GLFW_KEY_RIGHT do textbox_editing_state.cursor_position += 1; - case GLFW_KEY_END do textbox_editing_state.cursor_position = text_buffer.count; - case GLFW_KEY_HOME do textbox_editing_state.cursor_position = 0; + case GLFW_KEY_LEFT do textbox_editing_state.cursor_position -= 1; + case GLFW_KEY_RIGHT do textbox_editing_state.cursor_position += 1; + case GLFW_KEY_END do textbox_editing_state.cursor_position = text_buffer.count; + case GLFW_KEY_HOME do textbox_editing_state.cursor_position = 0; - case GLFW_KEY_BACKSPACE { - if textbox_editing_state.cursor_position > 0 { - array.delete(text_buffer, textbox_editing_state.cursor_position - 1); - } - textbox_editing_state.cursor_position = math.max(~~0, textbox_editing_state.cursor_position - 1); + case GLFW_KEY_BACKSPACE { + if textbox_editing_state.cursor_position > 0 { + array.delete(text_buffer, textbox_editing_state.cursor_position - 1); } + textbox_editing_state.cursor_position = math.max(~~0, textbox_editing_state.cursor_position - 1); + } - case GLFW_KEY_DELETE { - array.delete(text_buffer, textbox_editing_state.cursor_position); - } + case GLFW_KEY_DELETE { + array.delete(text_buffer, textbox_editing_state.cursor_position); + } - case GLFW_KEY_ENTER { - result = .Enter_Pressed; - } + case GLFW_KEY_ENTER { + result = .Enter_Pressed; } } } diff --git a/src/client/main.onyx b/src/client/main.onyx index 95c545a..d55d151 100644 --- a/src/client/main.onyx +++ b/src/client/main.onyx @@ -121,19 +121,14 @@ update :: (dt: f32) { } } - if !cursor_grabbed { - if Rect.contains(.{ 200, 200, 200, 200 }, mouse_get_position_vector()) { - if is_button_just_down(GLFW_MOUSE_BUTTON_LEFT) { - toggle_cursor_grabbed(); - } - } - - } else { + if cursor_grabbed { + player_update_controls(^player, dt); player_update(^player, dt); - player_chunk := world_position_to_chunk(world, player.body.pos); - world_move_center(world, player_chunk); } + player_update_physics(^player, dt); + player_chunk := world_position_to_chunk(world, player.body.pos); + world_move_center(world, player_chunk); world_update(world, dt); if is_key_just_down(GLFW_KEY_F7) { @@ -157,6 +152,12 @@ draw :: () { ww, wh: i32; glfwGetWindowSize(window, ^ww, ^wh); font_print(font, ~~(ww / 2), ~~(wh / 2), "."); + + if !cursor_grabbed { + if draw_button(.{ 200, 200, 200, 200 }, "Resume") { + toggle_cursor_grabbed(); + } + } if debug_screen { immediate_set_color(.{1, 0, 1, 0.5}); @@ -201,6 +202,7 @@ draw_chat :: () { immediate_set_color(.{0.2, 0.2, 0.2, 0.8}); immediate_rectangle(0, ~~(wh - 300), 400, 300); + font_set_color(.{1, 1, 1}); y := cast(f32) (wh - 32); while i := cast(i32) chat_messages.count - 1; i >= 0 { defer i -= 1; @@ -267,7 +269,7 @@ main :: (args) => { } addr: net.Socket_Address; - addr.addr = 0x7f000001; + addr.addr = net.str_to_ipv4("127.0.0.1"); addr.port = 5123; host', _ := onet.host_create(null, 1); println(_); diff --git a/src/client/utils/input.onyx b/src/client/utils/input.onyx index 798eefe..f3bc20b 100644 --- a/src/client/utils/input.onyx +++ b/src/client/utils/input.onyx @@ -34,7 +34,7 @@ Key_Descriptor :: struct { mod: u32 = 0; } #match hash.to_u32 (use x: Key_Descriptor) => hash.to_u32(key); -#operator == (x, y: Key_Descriptor) => x.key == y.key; +#operator == macro (x, y: Key_Descriptor) => x.key == y.key; input_update :: () { glfwGetCursorPos(window, ^mouse_x, ^mouse_y); @@ -58,8 +58,23 @@ input_get_chars_this_frame :: () -> [] u32 { return key_codepoints; } -input_get_keys_this_frame :: () -> Set(Key_Descriptor) { - return keys_this_frame; +input_get_keys_this_frame :: () -> Iterator(Key_Descriptor) { + #persist index := 0; + + next :: (_: rawptr) -> (Key_Descriptor, bool) { + if index >= keys_this_frame.entries.count do return .{0}, false; + + while keys_last_frame->has(keys_this_frame.entries[index].value) { + index += 1; + if index >= keys_this_frame.entries.count do return .{0}, false; + } + + defer index += 1; + return keys_this_frame.entries[index].value, true; + } + + index = 0; + return .{ null, next }; } input_capture_keys :: () { diff --git a/src/client/world/player.onyx b/src/client/world/player.onyx index 7b93b22..b2a3beb 100644 --- a/src/client/world/player.onyx +++ b/src/client/world/player.onyx @@ -4,6 +4,8 @@ use package glfw3 Player :: struct { camera: ^Camera; // Should the camera exist on the player? Or should the player just control the camera? body: PhysicsBody; + + direction_to_move: Vector3; } player_make :: () -> Player { @@ -21,17 +23,7 @@ player_make :: () -> Player { return player; } -player_update :: (use player: ^Player, dt: f32) { - if world_get_chunk(world, world_position_to_chunk(world, body.pos)) == null do return; - - speed := 6f; - if is_key_down(GLFW_KEY_LEFT_CONTROL) { - speed = 10; - } - if is_key_down(GLFW_KEY_LEFT_SHIFT) { - speed = 1; - } - +player_update_controls :: (use player: ^Player, dt: f32) { mdx, mdy := mouse_get_delta(); camera.y_rot += ~~(-mdx / 400); camera.x_rot += ~~( mdy / 400); @@ -49,6 +41,21 @@ player_update :: (use player: ^Player, dt: f32) { if is_key_down(GLFW_KEY_S) do xz_vel -= facing; if is_key_down(GLFW_KEY_D) do xz_vel += Vector3.norm(Vector3.cross(facing, .{0,1,0})); if is_key_down(GLFW_KEY_A) do xz_vel -= Vector3.norm(Vector3.cross(facing, .{0,1,0})); + direction_to_move = xz_vel; +} + +player_update_physics :: (use player: ^Player, dt: f32) { + if world_get_chunk(world, world_position_to_chunk(world, body.pos)) == null do return; + + speed := 6f; + if is_key_down(GLFW_KEY_LEFT_CONTROL) { + speed = 10; + } + if is_key_down(GLFW_KEY_LEFT_SHIFT) { + speed = 1; + } + + xz_vel := direction_to_move; if Vector3.mag(xz_vel) > 0 do xz_vel = Vector3.norm(xz_vel) * speed; body.vel.x = xz_vel.x; @@ -71,9 +78,14 @@ player_update :: (use player: ^Player, dt: f32) { if body.pos.y < -100 do body.pos = .{0,48,0}; camera.position = body.pos; +} + +player_update :: (use player: ^Player, dt: f32) { + if world_get_chunk(world, world_position_to_chunk(world, body.pos)) == null do return; ray_cast_to_block :: (_, p) => world_get_block(world, p.x, p.y, p.z) != Block_Empty; + forward := camera_get_forward(camera); dir: Vector3i; if !ray_cast(.{ camera.position, forward }, 10, null, ray_cast_to_block, ^selected_block, ^dir) { selected_block = .{0,0,0};