Updated the mouse system so it picks touch inputs as well
parent
886c89bb96
commit
c192973410
|
@ -4,6 +4,7 @@ target/
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
*.pdb
|
*.pdb
|
||||||
|
jniLibs/
|
||||||
|
|
||||||
# Gradle files
|
# Gradle files
|
||||||
.gradle/
|
.gradle/
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
@ -0,0 +1,23 @@
|
||||||
|
|
||||||
|
run platform="pc":
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
if [ "{{platform}}" = "pc" ]; then
|
||||||
|
cargo run
|
||||||
|
elif [ "{{platform}}" = "mobile" ]; then
|
||||||
|
is_paired="$(adb get-state 1> /dev/null 2>&1 && echo "device attached" || echo "no device found")"
|
||||||
|
if [ "$is_paired" = "no device found" ]; then
|
||||||
|
echo "no device paired! run 'adb connect <target>'"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
adb install ./app/build/outputs/apk/debug/app-debug.apk
|
||||||
|
adb shell monkey -p xyz.pixelatedw.avoid 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
build platform="pc":
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
cargo build
|
||||||
|
if [ "{{platform}}" = "mobile" ]; then
|
||||||
|
cargo ndk -t arm64-v8a -o ./app/src/main/jniLibs build --release --package avoid-rs
|
||||||
|
./gradlew build
|
||||||
|
fi
|
||||||
|
|
|
@ -14,6 +14,7 @@ pub struct Mouse(pub Vec2);
|
||||||
|
|
||||||
fn mouse_position(
|
fn mouse_position(
|
||||||
mut mouse: ResMut<Mouse>,
|
mut mouse: ResMut<Mouse>,
|
||||||
|
touches: Res<Touches>,
|
||||||
window: Query<&Window>,
|
window: Query<&Window>,
|
||||||
camera: Query<(&Camera, &GlobalTransform)>,
|
camera: Query<(&Camera, &GlobalTransform)>,
|
||||||
) {
|
) {
|
||||||
|
@ -22,7 +23,15 @@ fn mouse_position(
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(position) = window.cursor_position() {
|
let mut position = window.cursor_position();
|
||||||
|
|
||||||
|
if position.is_none() {
|
||||||
|
for touch in touches.iter() {
|
||||||
|
position = Some(touch.position())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(position) = position {
|
||||||
let Ok((camera, camera_transform)) = camera.get_single()
|
let Ok((camera, camera_transform)) = camera.get_single()
|
||||||
else {
|
else {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::{input::touch::Touch, prelude::*};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
common::{animation::SpriteAnimation, velocity::Velocity},
|
common::{animation::SpriteAnimation, mouse::Mouse, velocity::Velocity},
|
||||||
level::Unit,
|
level::Unit,
|
||||||
GameState, START_POSITION,
|
GameState, START_POSITION,
|
||||||
};
|
};
|
||||||
|
@ -55,6 +55,7 @@ fn setup(
|
||||||
fn move_player(
|
fn move_player(
|
||||||
keys: Res<ButtonInput<KeyCode>>,
|
keys: Res<ButtonInput<KeyCode>>,
|
||||||
gamepads: Query<&Gamepad>,
|
gamepads: Query<&Gamepad>,
|
||||||
|
mouse: Res<Mouse>,
|
||||||
mut query: Query<(&mut Transform, &mut Velocity), With<Player>>,
|
mut query: Query<(&mut Transform, &mut Velocity), With<Player>>,
|
||||||
window: Query<&Window>,
|
window: Query<&Window>,
|
||||||
) {
|
) {
|
||||||
|
@ -66,6 +67,10 @@ fn move_player(
|
||||||
handle_gamepad_movement(gamepad, &mut velocity);
|
handle_gamepad_movement(gamepad, &mut velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let position = Vec2::new(transform.translation.x, transform.translation.y);
|
||||||
|
|
||||||
|
velocity.0 = (mouse.0 - position).normalize() * 0.8;
|
||||||
|
|
||||||
if keys.pressed(KeyCode::ArrowRight) {
|
if keys.pressed(KeyCode::ArrowRight) {
|
||||||
velocity.x += 1.0;
|
velocity.x += 1.0;
|
||||||
}
|
}
|
||||||
|
@ -79,16 +84,19 @@ fn move_player(
|
||||||
velocity.y -= 1.0;
|
velocity.y -= 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if velocity.length() > 0.0 {
|
let distance_to_target = position - mouse.0;
|
||||||
velocity.0 = velocity.normalize() * 400.0;
|
if distance_to_target.length() > 10.0 {
|
||||||
let angle = velocity.to_angle() - PI / 2.0;
|
if velocity.length() > 0.0 {
|
||||||
let angle = Quat::from_rotation_z(angle);
|
velocity.0 = velocity.normalize() * 400.0;
|
||||||
transform.rotation = angle;
|
let angle = velocity.to_angle() - PI / 2.0;
|
||||||
}
|
let angle = Quat::from_rotation_z(angle);
|
||||||
|
transform.rotation = angle;
|
||||||
|
}
|
||||||
|
|
||||||
let window = window.single();
|
let window = window.single();
|
||||||
let screen_size: Vec3 = window.physical_size().as_vec2().extend(1.0);
|
let screen_size: Vec3 = window.physical_size().as_vec2().extend(1.0);
|
||||||
transform.translation = transform.translation.clamp(Vec3::ZERO, screen_size);
|
transform.translation = transform.translation.clamp(Vec3::ZERO, screen_size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_gamepad_movement(gamepad: &Gamepad, velocity: &mut Velocity) {
|
fn handle_gamepad_movement(gamepad: &Gamepad, velocity: &mut Velocity) {
|
||||||
|
|
Loading…
Reference in New Issue