From f86dc6281977e11bdcdfa28557a90bacb9bb42aa Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 9 Jun 2022 11:45:05 +0200 Subject: input --- client/src/client/mod.rs | 68 ++++++++++++------------------------------------ renderer/src/main.rs | 48 +++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 60 deletions(-) diff --git a/client/src/client/mod.rs b/client/src/client/mod.rs index c72cc0b..c779317 100644 --- a/client/src/client/mod.rs +++ b/client/src/client/mod.rs @@ -1,7 +1,6 @@ pub mod helper; use self::helper::get_map_path; - use super::gamenet::{ enums::{Team, VERSION}, msg::{ @@ -11,7 +10,7 @@ use super::gamenet::{ system::{Ready, RequestMapData}, Game, System, SystemOrGame, }, - snap_obj::{obj_size, PlayerInput}, + snap_obj::obj_size, SnapObj, }; use crate::client::helper::{check_dummy_map, hexdump, WarnSnap}; @@ -38,6 +37,8 @@ use std::{ use tempfile::{NamedTempFile, NamedTempFileOptions}; use warn::Log; +pub use super::gamenet::snap_obj::PlayerInput; + pub struct ClientConfig { pub nick: String, pub clan: String, @@ -55,7 +56,9 @@ pub struct Client { state: ClientState, download: Option, - _interface_receive: Receiver, + input: PlayerInput, + + interface_receive: Receiver, interface_send: Sender, } @@ -64,7 +67,9 @@ pub struct ClientInterface { pub receive: Receiver, } -pub enum ClientMesgIn {} +pub enum ClientMesgIn { + Input(PlayerInput), +} pub enum ClientMesgOut { MapChange { name: String, crc: i32 }, Snaps(Vec<(u16, SnapObj)>), @@ -110,8 +115,9 @@ impl Client { dummy_map: false, state: ClientState::Connection, download: None, - _interface_receive: b, + interface_receive: b, interface_send: c, + input: PlayerInput::default(), }, ClientInterface { receive: d, @@ -133,6 +139,11 @@ impl<'a, L: Loop> Application for Client { warn!("exiting peer {}", self.pid); evloop.disconnect(self.pid, b"error"); } + for m in self.interface_receive.try_iter() { + match m { + ClientMesgIn::Input(i) => self.input = i, + } + } } fn on_packet(&mut self, evloop: &mut L, chunk: Chunk) { let pid = chunk.pid; @@ -227,7 +238,6 @@ impl<'a, L: Loop> Application for Client { } System::Snap(_) | System::SnapEmpty(_) | System::SnapSingle(_) => { self.num_snaps_since_reset += 1; - let mut input = PlayerInput::default(); { let res = match *msg { System::Snap(s) => self.snaps.snap(&mut Log, obj_size, s), @@ -253,50 +263,6 @@ impl<'a, L: Loop> Application for Client { self.interface_send .send(ClientMesgOut::Snaps(snaps.clone())) .unwrap(); - - let client_id = snaps - .iter() - .filter_map(|(_id, i)| { - if let SnapObj::PlayerInfo(p) = i { - if p.local != 0 { - Some(p.client_id) - } else { - None - } - } else { - None - } - }) - .next() - .unwrap_or(1 << 15) - as u16; - - let mut target_position = None; - let mut my_position = (0, 0); - for (id, snap) in &snaps { - match snap { - SnapObj::Character(c) => { - let c = c.character_core; - if *id == client_id { - my_position = (c.x, c.y) - } else if c.hook_state != 0 { - target_position = Some((c.x, c.y)) - } - } - _ => {} - } - } - if let Some(target) = target_position { - if target.0 < my_position.0 { - input.direction = -1; - } - if target.0 > my_position.0 { - input.direction = 1; - } - if target.1 - 16 < my_position.1 { - input.jump = 1; - } - } } Ok(None) => { self.num_snaps_since_reset -= 1; @@ -314,7 +280,7 @@ impl<'a, L: Loop> Application for Client { ack_snapshot: tick, intended_tick: tick, input_size: mem::size_of::().assert_i32(), - input, + input: self.input, }, ); } diff --git a/renderer/src/main.rs b/renderer/src/main.rs index 6847a2e..d4ee57d 100644 --- a/renderer/src/main.rs +++ b/renderer/src/main.rs @@ -1,7 +1,7 @@ pub mod map; use glutin::{ - event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::{Window, WindowBuilder}, ContextWrapper, GlProfile, PossiblyCurrent, @@ -17,11 +17,11 @@ use skia_safe::{ Canvas, Color, Color4f, ColorSpace, ColorType, Paint, Point, Surface, }; use std::{ - convert::TryInto, net::IpAddr, process::exit, str::FromStr, sync::atomic::Ordering, thread, - time::Duration, + collections::HashSet, convert::TryInto, net::IpAddr, process::exit, str::FromStr, + sync::atomic::Ordering, thread, time::Duration, }; use twclient::{ - client::{Client, ClientConfig, ClientInterface}, + client::{Client, ClientConfig, ClientInterface, ClientMesgIn, PlayerInput}, world::World, SHOULD_EXIT, }; @@ -113,8 +113,11 @@ fn main() { client_interface, map_renderer: MapRenderer::new(), world: World::new(), + input: PlayerInput::default(), }; + let mut keys_down = HashSet::::new(); + event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; @@ -146,17 +149,43 @@ fn main() { input: KeyboardInput { virtual_keycode, - modifiers, + state, .. }, .. } => { - if modifiers.logo() { - if let Some(VirtualKeyCode::Q) = virtual_keycode { - *control_flow = ControlFlow::Exit; + if let Some(k) = virtual_keycode { + let sk = if state == ElementState::Pressed { + 1 + } else { + -1 + }; + let sa = if state == ElementState::Pressed { 1 } else { 0 }; + + let repeat = match state { + ElementState::Pressed => !keys_down.insert(k), + ElementState::Released => !keys_down.remove(&k), + }; + if !repeat { + match k { + VirtualKeyCode::A => { + renderer.input.direction += sk * -1; + } + VirtualKeyCode::D => { + renderer.input.direction += sk * 1; + } + VirtualKeyCode::Space => { + renderer.input.jump = sa; + } + _ => (), + } + renderer + .client_interface + .send + .send(ClientMesgIn::Input(renderer.input)) + .unwrap(); } } - env.windowed_context.window().request_redraw(); } _ => (), }, @@ -178,6 +207,7 @@ pub struct Renderer { client_interface: ClientInterface, map_renderer: MapRenderer, world: World, + input: PlayerInput, } impl Renderer { -- cgit v1.2.3-70-g09d2