Skip to content

Commit

Permalink
clean up code; remove irrelevant comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuang97 authored and Ralith committed Jan 10, 2024
1 parent 5093b3d commit a3046cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
44 changes: 21 additions & 23 deletions client/src/graphics/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,20 @@ 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);
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 @@ -366,19 +364,19 @@ impl Window {
}
}

fn number_key_to_index(key: VirtualKeyCode) -> usize {
fn number_key_to_index(key: VirtualKeyCode) -> Option<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,
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,
}
}

Expand Down
23 changes: 14 additions & 9 deletions 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 @@ -60,7 +73,6 @@ pub struct Sim {
break_block_pressed: bool,

selected_material: Material,
material_palette: [Material; 10],

prediction: PredictedMotion,
local_character_controller: LocalCharacterController,
Expand Down Expand Up @@ -92,9 +104,6 @@ impl Sim {
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 @@ -147,11 +156,7 @@ impl Sim {
}

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]
};
self.selected_material = *MATERIAL_PALETTE.get(idx).unwrap_or(&MATERIAL_PALETTE[0]);
}

pub fn set_break_block_pressed_true(&mut self) {
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_or(na::Vector3::x_axis()),
up: graph.get_relative_up(position).unwrap(),
dt_seconds,
movement_input: sanitize_motion_input(input.movement),
jump_input: input.jump,
Expand Down

0 comments on commit a3046cc

Please sign in to comment.