Skip to content

Commit

Permalink
add joystick
Browse files Browse the repository at this point in the history
  • Loading branch information
stutxo committed Sep 4, 2023
1 parent 90b67f7 commit 8335299
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 49 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bevy_egui = "0.21.0"
names = { version = "0.14.0", default-features = false }
gloo-timers = { version = "0.3.0", features = ["futures"] }
bevy_ecs_ldtk = "0.8.0"
virtual_joystick = "2.0.1"


[profile.release]
Expand Down
Binary file added assets/Knob.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/Knob.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/Outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>☔ rain.run</title>
<script type="module">import init from './satrunner-62e669801ff536db.js';init('./satrunner-62e669801ff536db_bg.wasm');</script>
<script type="module">import init from './satrunner-fd648276f38806ba.js';init('./satrunner-fd648276f38806ba_bg.wasm');</script>


<style>
Expand All @@ -17,8 +17,8 @@
}
</style>

<link rel="preload" href="./satrunner-62e669801ff536db_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="./satrunner-62e669801ff536db.js"></head>
<link rel="preload" href="./satrunner-fd648276f38806ba_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
<link rel="modulepreload" href="./satrunner-fd648276f38806ba.js"></head>

<body>
<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,13 @@ function __wbg_get_imports() {
imports.wbg.__wbg_removeEventListener_5de660c02ed784e4 = function() { return handleError(function (arg0, arg1, arg2, arg3) {
getObject(arg0).removeEventListener(getStringFromWasm0(arg1, arg2), getObject(arg3));
}, arguments) };
imports.wbg.__wbg_new_b66404b6322c59bf = function() { return handleError(function (arg0, arg1) {
const ret = new WebSocket(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_setbinaryType_096c70c4a9d97499 = function(arg0, arg1) {
getObject(arg0).binaryType = takeObject(arg1);
};
imports.wbg.__wbg_readyState_b25418fd198bf715 = function(arg0) {
const ret = getObject(arg0).readyState;
return ret;
Expand All @@ -1123,13 +1130,6 @@ function __wbg_get_imports() {
imports.wbg.__wbg_send_1a008ea2eb3a1951 = function() { return handleError(function (arg0, arg1, arg2) {
getObject(arg0).send(getArrayU8FromWasm0(arg1, arg2));
}, arguments) };
imports.wbg.__wbg_new_b66404b6322c59bf = function() { return handleError(function (arg0, arg1) {
const ret = new WebSocket(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_setbinaryType_096c70c4a9d97499 = function(arg0, arg1) {
getObject(arg0).binaryType = takeObject(arg1);
};
imports.wbg.__wbg_measure_aa7a73f17813f708 = function() { return handleError(function (arg0, arg1, arg2, arg3) {
let deferred0_0;
let deferred0_1;
Expand Down Expand Up @@ -1885,64 +1885,64 @@ function __wbg_get_imports() {
const ret = getObject(arg0).fullscreenElement;
return isLikeNone(ret) ? 0 : addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper2927 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_34);
imports.wbg.__wbindgen_closure_wrapper2761 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 115, __wbg_adapter_34);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper43898 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper43702 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 115, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper43902 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper43706 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 115, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper43905 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper43709 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 115, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper43908 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper43712 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 115, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper43911 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper43715 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 115, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper43914 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 109, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper43718 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 115, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper43917 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper43721 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper44266 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper44068 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper45389 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper45155 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper45392 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper45158 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper45395 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_34);
imports.wbg.__wbindgen_closure_wrapper45161 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_34);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper45397 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper45163 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_37);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper49217 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_34);
imports.wbg.__wbindgen_closure_wrapper48983 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_34);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper50524 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2650, __wbg_adapter_37);
imports.wbg.__wbindgen_closure_wrapper51500 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 2655, __wbg_adapter_37);
return addHeapObject(ret);
};

Expand Down Expand Up @@ -1986,7 +1986,7 @@ async function __wbg_init(input) {
if (wasm !== undefined) return wasm;

if (typeof input === 'undefined') {
input = new URL('satrunner-62e669801ff536db_bg.wasm', import.meta.url);
input = new URL('satrunner-fd648276f38806ba_bg.wasm', import.meta.url);
}
const imports = __wbg_get_imports();

Expand Down
Binary file not shown.
67 changes: 62 additions & 5 deletions src/game_core/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use bevy::prelude::*;
use virtual_joystick::{
TintColor, VirtualJoystickEvent, VirtualJoystickEventType, VirtualJoystickNode,
};

use crate::{
game_core::player::PLAYER_SPEED,
game_util::resources::{ClientTick, NetworkStuff},
network::messages::{ClientMessage, PlayerInput},
};
Expand All @@ -12,7 +16,7 @@ pub fn input(
mouse: Res<Input<MouseButton>>,
camera_query: Query<(&Camera, &GlobalTransform)>,
windows: Query<&Window>,
touches: Res<Touches>,
// touches: Res<Touches>,
mut outgoing: ResMut<NetworkStuff>,
client_tick: Res<ClientTick>,
) {
Expand Down Expand Up @@ -73,12 +77,65 @@ pub fn input(
}
}

for touch in touches.iter_just_pressed() {
if let Some(window) = windows.iter().next() {
let position = get_position(touch.position(), window);
handle_input(position, &mut player);
// for touch in touches.iter_just_pressed() {
// if let Some(window) = windows.iter().next() {
// let position = get_position(touch.position(), window);
// handle_input(position, &mut player);
// }
// }
}
}
}

pub fn update_joystick(
mut joystick: EventReader<VirtualJoystickEvent<String>>,
mut joystick_color: Query<(&mut TintColor, &VirtualJoystickNode<String>)>,
mut query: Query<&mut Player>,
mut outgoing: ResMut<NetworkStuff>,
client_tick: Res<ClientTick>,
) {
for j in joystick.iter() {
let Vec2 { x, y } = j.axis();
match j.get_type() {
VirtualJoystickEventType::Press | VirtualJoystickEventType::Drag => {
let (mut color, node) = joystick_color.single_mut();
if node.id == j.id() {
*color = TintColor(Color::WHITE);
}
}
VirtualJoystickEventType::Up => {
let (mut color, node) = joystick_color.single_mut();
if node.id == j.id() {
*color = TintColor(Color::WHITE.with_a(0.2));
}
}
}

if client_tick.tick.unwrap_or(0) % 2 == 0 {
for mut player in query.iter_mut() {
player.target += Vec2::new(x, y) * 10.;

info!("player target: {:?}", player.target);

let input = PlayerInput::new(
[player.target.x, player.target.y],
player.id,
client_tick.tick.unwrap(),
true,
);

player.pending_inputs.push(input.clone());

match outgoing
.write
.as_mut()
.unwrap()
.try_send(ClientMessage::PlayerInput(input))
{
Ok(()) => {}
Err(e) => error!("Error sending message: {} CHANNEL FULL???", e),
};
}
}
}
}
27 changes: 27 additions & 0 deletions src/game_core/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use bevy::{prelude::*, time::Stopwatch, utils::HashMap};

use bevy_ecs_ldtk::LdtkWorldBundle;
use uuid::Uuid;
use virtual_joystick::{
TintColor, VirtualJoystickAxis, VirtualJoystickBundle, VirtualJoystickInteractionArea,
VirtualJoystickNode, VirtualJoystickType,
};

use crate::{
game_util::{
Expand Down Expand Up @@ -44,6 +48,29 @@ pub fn spawn_player(
},
KeyboardNode,
));

commands
.spawn(
VirtualJoystickBundle::new(VirtualJoystickNode {
border_image: asset_server.load("Outline.png"),
knob_image: asset_server.load("Knob.png"),
knob_size: Vec2::new(40., 40.),
dead_zone: 0.,
id: "UniqueJoystick".to_string(),
axis: VirtualJoystickAxis::Both,
behaviour: VirtualJoystickType::Fixed,
})
.set_color(TintColor(Color::WHITE.with_a(0.2)))
.set_style(Style {
width: Val::Px(75.),
height: Val::Px(75.),
position_type: PositionType::Absolute,
right: Val::Percent(5.),
bottom: Val::Percent(5.),
..default()
}),
)
.insert(VirtualJoystickInteractionArea);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/keyboard/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn setup_keyboard(
asset_server: Res<AssetServer>,
placement_query: Query<Entity, With<KeyboardNode>>,
) {
info!("keyboard setup!");
// info!("keyboard setup!");
for ent in placement_query.iter() {
// let mut parent_node = commands.entity(ent);
// parent_node.commands().spawn(bundle)
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use game_core::{
game_loop::{enemy_loop, player_loop, tick},
gui::{check_disconnected, disconnected, game_over, score_board, setup_menu},
handle::handle_server,
input::input,
input::{input, update_joystick},
objects::{handle_bolt, handle_rain},
sprites::{pool_bolt, pool_rain, spawn_ldtk},
};
Expand All @@ -17,6 +17,7 @@ use game_util::resources::{
use keyboard::KeyboardPlugin;
use network::websockets::websocket;
use std::collections::VecDeque;
use virtual_joystick::VirtualJoystickPlugin;

mod game_core;
mod game_util;
Expand All @@ -42,6 +43,7 @@ fn main() {
EguiPlugin,
LdtkPlugin,
KeyboardPlugin,
VirtualJoystickPlugin::<String>::default(),
))
.insert_resource(LevelSelection::Index(0))
.insert_resource(LdtkSettings {
Expand All @@ -58,7 +60,10 @@ fn main() {
.add_systems(Update, setup_menu.run_if(in_state(GameStage::Menu)))
.add_systems(Update, (handle_server, score_board, check_disconnected))
.add_systems(FixedUpdate, (tick, enemy_loop, handle_rain, handle_bolt))
.add_systems(Update, (input).run_if(in_state(GameStage::InGame)))
.add_systems(
Update,
(input, update_joystick).run_if(in_state(GameStage::InGame)),
)
.add_systems(
Update,
(disconnected).run_if(in_state(GameStage::Disconnected)),
Expand Down

0 comments on commit 8335299

Please sign in to comment.