Skip to content

Commit

Permalink
Implement impulse command
Browse files Browse the repository at this point in the history
  • Loading branch information
cormac-obrien committed Aug 30, 2018
1 parent 8fcb7c9 commit 76a43a5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/bin/quake-client/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ impl Game {
pub fn frame(&mut self, frame_duration: Duration) {
self.client.frame(frame_duration).unwrap();

if let Some(ref game_input) = self.input.borrow().game_input() {
if let Some(ref mut game_input) = self.input.borrow_mut().game_input_mut() {
self.client
.handle_input(game_input, frame_duration, 0)
.handle_input(game_input, frame_duration)
.unwrap();
}

Expand Down
4 changes: 0 additions & 4 deletions src/bin/quake-client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ impl Program for ClientProgram {
}
}

if let Some(ref mut game_input) = self.input.borrow_mut().game_input_mut() {
game_input.clear_mouse().unwrap();
}

flame::start("EventsLoop::poll_events");
self.events_loop
.borrow_mut()
Expand Down
51 changes: 48 additions & 3 deletions src/client/input/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::Rc;
use std::str::FromStr;
Expand Down Expand Up @@ -452,6 +452,7 @@ pub struct GameInput {
bindings: Rc<RefCell<HashMap<BindInput, BindTarget>>>,
action_states: Rc<RefCell<[bool; ACTION_COUNT]>>,
mouse_delta: (f64, f64),
impulse: Rc<Cell<u8>>,
}

impl GameInput {
Expand All @@ -461,13 +462,18 @@ impl GameInput {
bindings: Rc::new(RefCell::new(HashMap::new())),
action_states: Rc::new(RefCell::new([false; ACTION_COUNT])),
mouse_delta: (0.0, 0.0),
impulse: Rc::new(Cell::new(0)),
}
}

pub fn mouse_delta(&self) -> (f64, f64) {
self.mouse_delta
}

pub fn impulse(&self) -> u8 {
self.impulse.get()
}

/// Bind the default controls.
pub fn bind_defaults(&mut self) {
self.bind(Key::W, BindTarget::from_str("+forward").unwrap());
Expand All @@ -482,6 +488,15 @@ impl GameInput {
self.bind(Key::LControl, BindTarget::from_str("+attack").unwrap());
self.bind(Key::E, BindTarget::from_str("+use").unwrap());
self.bind(Key::Grave, BindTarget::from_str("toggleconsole").unwrap());
self.bind(Key::Key1, BindTarget::from_str("impulse 1").unwrap());
self.bind(Key::Key2, BindTarget::from_str("impulse 2").unwrap());
self.bind(Key::Key3, BindTarget::from_str("impulse 3").unwrap());
self.bind(Key::Key4, BindTarget::from_str("impulse 4").unwrap());
self.bind(Key::Key5, BindTarget::from_str("impulse 5").unwrap());
self.bind(Key::Key6, BindTarget::from_str("impulse 6").unwrap());
self.bind(Key::Key7, BindTarget::from_str("impulse 7").unwrap());
self.bind(Key::Key8, BindTarget::from_str("impulse 8").unwrap());
self.bind(Key::Key9, BindTarget::from_str("impulse 9").unwrap());
}

/// Bind a `BindInput` to a `BindTarget`.
Expand Down Expand Up @@ -847,16 +862,46 @@ impl GameInput {
}
}),
).unwrap();

// "impulse"
let impulse = self.impulse.clone();
cmds.insert_or_replace(
"impulse",
Box::new(move |args| {
println!("args: {}", args.len());
match args.len() {
1 => match u8::from_str(args[0]) {
Ok(i) => impulse.set(i),
Err(_) => println!("Impulse must be a number between 0 and 255"),
}

_ => println!("impulse [number]"),
}
})
).unwrap();
}

// must be called every frame!
pub fn refresh(&mut self) -> Result<(), Error> {
self.clear_mouse()?;
self.clear_impulse()?;

Ok(())
}

// must be called at the beginning of every frame!
pub fn clear_mouse(&mut self) -> Result<(), Error> {
fn clear_mouse(&mut self) -> Result<(), Error> {
self.handle_input(MouseWheel::Up, ElementState::Released)?;
self.handle_input(MouseWheel::Down, ElementState::Released)?;
self.mouse_delta = (0.0, 0.0);

Ok(())
}

fn clear_impulse(&mut self) -> Result<(), Error> {
self.impulse.set(0);

Ok(())
}
}

#[cfg(test)]
Expand Down
8 changes: 5 additions & 3 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,8 @@ impl Client {

pub fn handle_input(
&mut self,
game_input: &GameInput,
game_input: &mut GameInput,
frame_time: Duration,
impulse: u8,
) -> Result<(), Error> {
self.adjust_angles(game_input, frame_time);

Expand Down Expand Up @@ -691,14 +690,17 @@ impl Client {
side_move: sidemove as i16,
up_move: upmove as i16,
button_flags,
impulse,
impulse: game_input.impulse(),
};
// debug!("Sending move command: {:?}", move_cmd);

let mut msg = Vec::new();
move_cmd.serialize(&mut msg)?;
self.qsock.send_msg_unreliable(&msg)?;

// clear mouse and impulse
game_input.refresh()?;

Ok(())
}

Expand Down

0 comments on commit 76a43a5

Please sign in to comment.