aboutsummaryrefslogtreecommitdiff
path: root/renderer/src/main.rs
diff options
context:
space:
mode:
authormetamuffin <yvchraiqi@protonmail.com>2022-06-09 11:45:05 +0200
committermetamuffin <yvchraiqi@protonmail.com>2022-06-09 11:45:05 +0200
commitf86dc6281977e11bdcdfa28557a90bacb9bb42aa (patch)
tree230719bad80c51ea907ba64401ccd2eee4cd4886 /renderer/src/main.rs
parentb71d1e8272e35aeb6b186740b604dfb26f316827 (diff)
downloadtwclient-f86dc6281977e11bdcdfa28557a90bacb9bb42aa.tar
twclient-f86dc6281977e11bdcdfa28557a90bacb9bb42aa.tar.bz2
twclient-f86dc6281977e11bdcdfa28557a90bacb9bb42aa.tar.zst
input
Diffstat (limited to 'renderer/src/main.rs')
-rw-r--r--renderer/src/main.rs48
1 files changed, 39 insertions, 9 deletions
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::<VirtualKeyCode>::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 {