Updated the mouse system so it picks touch inputs as well
parent
886c89bb96
commit
c192973410
|
@ -4,6 +4,7 @@ target/
|
|||
Cargo.lock
|
||||
**/*.rs.bk
|
||||
*.pdb
|
||||
jniLibs/
|
||||
|
||||
# Gradle files
|
||||
.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(
|
||||
mut mouse: ResMut<Mouse>,
|
||||
touches: Res<Touches>,
|
||||
window: Query<&Window>,
|
||||
camera: Query<(&Camera, &GlobalTransform)>,
|
||||
) {
|
||||
|
@ -22,7 +23,15 @@ fn mouse_position(
|
|||
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()
|
||||
else {
|
||||
return;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use std::f32::consts::PI;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy::{input::touch::Touch, prelude::*};
|
||||
|
||||
use crate::{
|
||||
common::{animation::SpriteAnimation, velocity::Velocity},
|
||||
common::{animation::SpriteAnimation, mouse::Mouse, velocity::Velocity},
|
||||
level::Unit,
|
||||
GameState, START_POSITION,
|
||||
};
|
||||
|
@ -55,6 +55,7 @@ fn setup(
|
|||
fn move_player(
|
||||
keys: Res<ButtonInput<KeyCode>>,
|
||||
gamepads: Query<&Gamepad>,
|
||||
mouse: Res<Mouse>,
|
||||
mut query: Query<(&mut Transform, &mut Velocity), With<Player>>,
|
||||
window: Query<&Window>,
|
||||
) {
|
||||
|
@ -66,6 +67,10 @@ fn move_player(
|
|||
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) {
|
||||
velocity.x += 1.0;
|
||||
}
|
||||
|
@ -79,6 +84,8 @@ fn move_player(
|
|||
velocity.y -= 1.0;
|
||||
}
|
||||
|
||||
let distance_to_target = position - mouse.0;
|
||||
if distance_to_target.length() > 10.0 {
|
||||
if velocity.length() > 0.0 {
|
||||
velocity.0 = velocity.normalize() * 400.0;
|
||||
let angle = velocity.to_angle() - PI / 2.0;
|
||||
|
@ -89,6 +96,7 @@ fn move_player(
|
|||
let window = window.single();
|
||||
let screen_size: Vec3 = window.physical_size().as_vec2().extend(1.0);
|
||||
transform.translation = transform.translation.clamp(Vec3::ZERO, screen_size);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_gamepad_movement(gamepad: &Gamepad, velocity: &mut Velocity) {
|
||||
|
|
Loading…
Reference in New Issue