bugfixes with textbox controls; separate player logic
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 2 Apr 2022 19:44:32 +0000 (14:44 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 2 Apr 2022 19:44:32 +0000 (14:44 -0500)
src/client/gfx/ui.onyx
src/client/main.onyx
src/client/utils/input.onyx
src/client/world/player.onyx

index 570a2ef68408683cea3f8b94c11d481c8cf72234..985fc66eed862704c4d5b8b2b5a8dcd9e52cf836 100644 (file)
@@ -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;
                 }
             }
         }
index 95c545a24087befa4d07eca028da4ae4cccfcf57..d55d1519af558f104e72dbef77e944d25500b87c 100644 (file)
@@ -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(_);
index 798eefefba76806526dbf2b1e576e167c3665040..f3bc20b8084007be0dc6123420114264336fd297 100644 (file)
@@ -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 :: () {
index 7b93b2218ded7bf32313f82f80de2c6f3f1d8a29..b2a3bebd0044463821cf7afb9f5ef21e243c8e6a 100644 (file)
@@ -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};