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;
}
}
}
}
}
- 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) {
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});
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;
}
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(_);
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);
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 :: () {
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 {
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);
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;
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};