DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

d4d7329962488255db8355fa00356da742abd19c — Jonni Liljamo 1 year, 7 months ago a09ccd0
feat(client): basic camera controls
1 files changed, 38 insertions(+), 3 deletions(-)

M client/src/main.rs
M client/src/main.rs => client/src/main.rs +38 -3
@@ 10,7 10,7 @@
#![allow(clippy::derivable_impls)]

use api::user::User;
use bevy::prelude::*;
use bevy::{prelude::*, input::mouse::{MouseWheel, MouseScrollUnit}};
use bevy_assets_bundler::*;
use bevy_editor_pls::EditorPlugin;
use bevy_egui::EguiPlugin;


@@ 102,7 102,7 @@ fn main() {
    app.add_plugin(plugins::MenuPlugin);
    app.add_plugin(plugins::GamePlugin);

    app.add_startup_system(setup);
    app.add_startup_system(setup).add_system(move_camera);
    app.run();

    info!("goodbye");


@@ 117,7 117,7 @@ fn setup(mut commands: Commands) {
            Camera3dBundle {
                transform: Transform {
                    translation: Vec3::new(0., 5.5, 5.8),
                    rotation: Quat::from_rotation_x(-1.0),
                    rotation: Quat::from_rotation_x(-1.2),
                    ..Default::default()
                },
                ..Default::default()


@@ 127,6 127,41 @@ fn setup(mut commands: Commands) {
        .insert(PlayerCamera);
}

fn move_camera(
    time: Res<Time>,
    input: Res<Input<KeyCode>>,
    mut mouse_wheel_ev_r: EventReader<MouseWheel>,
    mut camera_query: Query<(&PlayerCamera, &mut Transform)>,
) {
    for (_camera, mut transform) in &mut camera_query {
        let mut direction = Vec3::ZERO;

        let y_modifier = 3.;
        for ev in mouse_wheel_ev_r.iter() {
            match ev.unit {
                MouseScrollUnit::Line => direction.y -= 20.0 * ev.y.signum() * y_modifier * time.delta_seconds(),
                MouseScrollUnit::Pixel => direction.y -= ev.y * y_modifier * time.delta_seconds(),
            }
        }

        if input.pressed(KeyCode::A) {
            direction.x -= 1.;
        }
        if input.pressed(KeyCode::D) {
            direction.x += 1.;
        }
        if input.pressed(KeyCode::W) {
            direction.z -= 1.;
        }
        if input.pressed(KeyCode::S) {
            direction.z += 1.;
        }

        let cam_speed = 4.;
        transform.translation += direction * cam_speed * time.delta_seconds();
    }
}

fn editor_controls() -> bevy_editor_pls::controls::EditorControls {
    use bevy_editor_pls::controls;