Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for number keys to select type of block to be placed #340

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions client/src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ impl Window {
sim.toggle_no_clip();
}
}
VirtualKeyCode::Key1 | VirtualKeyCode::Key2 | VirtualKeyCode::Key3
| VirtualKeyCode::Key4 | VirtualKeyCode::Key5 | VirtualKeyCode::Key6
| VirtualKeyCode::Key7 | VirtualKeyCode::Key8 | VirtualKeyCode::Key9
| VirtualKeyCode::Key0 => {
if state == ElementState::Pressed {
if let Some(sim) = self.sim.as_mut() {
sim.select_material(number_key_to_index(key));
}
}
}
Ralith marked this conversation as resolved.
Show resolved Hide resolved
VirtualKeyCode::Escape => {
let _ = self.window.set_cursor_grab(CursorGrabMode::None);
self.window.set_cursor_visible(true);
Expand Down Expand Up @@ -356,6 +366,22 @@ impl Window {
}
}

fn number_key_to_index(key: VirtualKeyCode) -> usize {
match key {
VirtualKeyCode::Key1 => 0,
VirtualKeyCode::Key2 => 1,
VirtualKeyCode::Key3 => 2,
VirtualKeyCode::Key4 => 3,
VirtualKeyCode::Key5 => 4,
VirtualKeyCode::Key6 => 5,
VirtualKeyCode::Key7 => 6,
VirtualKeyCode::Key8 => 7,
VirtualKeyCode::Key9 => 8,
VirtualKeyCode::Key0 => 9,
_ => 0,
}
}

impl Drop for Window {
fn drop(&mut self) {
self.draw.take();
Expand Down
18 changes: 17 additions & 1 deletion client/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ pub struct Sim {
place_block_pressed: bool,
/// Whether the break-block button has been pressed since the last step
break_block_pressed: bool,

selected_material: Material,
material_palette: [Material; 10],
Ralith marked this conversation as resolved.
Show resolved Hide resolved

prediction: PredictedMotion,
local_character_controller: LocalCharacterController,
}
Expand Down Expand Up @@ -87,6 +91,10 @@ impl Sim {
jump_held: false,
place_block_pressed: false,
break_block_pressed: false,
selected_material: Material::WoodPlanks,
material_palette: [Material::WoodPlanks, Material::Grass, Material::Dirt,
Material::Sand, Material::Snow, Material::WhiteBrick,
Material::GreyBrick, Material::Basalt, Material::Water, Material::Lava],
prediction: PredictedMotion::new(proto::Position {
node: NodeId::ROOT,
local: na::one(),
Expand Down Expand Up @@ -138,6 +146,14 @@ impl Sim {
self.place_block_pressed = true;
}

pub fn select_material(&mut self, idx: usize) {
self.selected_material = if idx < self.material_palette.len() {
self.material_palette[idx]
} else {
self.material_palette[0]
};
Ralith marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn set_break_block_pressed_true(&mut self) {
self.break_block_pressed = true;
}
Expand Down Expand Up @@ -476,7 +492,7 @@ impl Sim {
};

let material = if placing {
Material::WoodPlanks
self.selected_material
} else {
Material::Void
};
Expand Down
2 changes: 1 addition & 1 deletion common/src/character_controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run_character_step(
graph,
radius: sim_config.character.character_radius,
},
up: graph.get_relative_up(position).unwrap(),
up: graph.get_relative_up(position).unwrap(), // up: graph.get_relative_up(position).unwrap_or(na::Vector3::x_axis()),
Ralith marked this conversation as resolved.
Show resolved Hide resolved
dt_seconds,
movement_input: sanitize_motion_input(input.movement),
jump_input: input.jump,
Expand Down
Loading