Skip to content

Commit

Permalink
allow for number keys to select type of block to be placed
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuang97 authored and Ralith committed Jan 10, 2024
1 parent 664a2f4 commit 5093b3d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
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));
}
}
}
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],

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]
};
}

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()),
dt_seconds,
movement_input: sanitize_motion_input(input.movement),
jump_input: input.jump,
Expand Down

0 comments on commit 5093b3d

Please sign in to comment.