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 all commits
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: 25 additions & 1 deletion client/src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,15 @@ impl Window {
self.window.set_cursor_visible(true);
mouse_captured = false;
}
_ => {}
_ => {
if let Some(material_idx) = number_key_to_index(key) {
if state == ElementState::Pressed {
if let Some(sim) = self.sim.as_mut() {
sim.select_material(material_idx);
}
}
}
}
},
WindowEvent::Focused(focused) => {
if !focused {
Expand Down Expand Up @@ -356,6 +364,22 @@ impl Window {
}
}

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

impl Drop for Window {
fn drop(&mut self) {
self.draw.take();
Expand Down
23 changes: 22 additions & 1 deletion client/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ use common::{
EntityId, GraphEntities, SimConfig, Step,
};

const MATERIAL_PALETTE: [Material; 10] = [
Material::WoodPlanks,
Material::Grass,
Material::Dirt,
Material::Sand,
Material::Snow,
Material::WhiteBrick,
Material::GreyBrick,
Material::Basalt,
Material::Water,
Material::Lava,
];

/// Game state
pub struct Sim {
// World state
Expand Down Expand Up @@ -58,6 +71,9 @@ 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,

prediction: PredictedMotion,
local_character_controller: LocalCharacterController,
}
Expand Down Expand Up @@ -87,6 +103,7 @@ impl Sim {
jump_held: false,
place_block_pressed: false,
break_block_pressed: false,
selected_material: Material::WoodPlanks,
prediction: PredictedMotion::new(proto::Position {
node: NodeId::ROOT,
local: na::one(),
Expand Down Expand Up @@ -138,6 +155,10 @@ impl Sim {
self.place_block_pressed = true;
}

pub fn select_material(&mut self, idx: usize) {
self.selected_material = *MATERIAL_PALETTE.get(idx).unwrap_or(&MATERIAL_PALETTE[0]);
}

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

let material = if placing {
Material::WoodPlanks
self.selected_material
} else {
Material::Void
};
Expand Down
Loading