implemented camera tracking for an object
This commit is contained in:
commit
6684029804
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
4912
Cargo.lock
generated
Normal file
4912
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "testing-collisions"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bevy = "0.15.3"
|
||||||
39
src/components.rs
Normal file
39
src/components.rs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Player;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Speed {
|
||||||
|
pub speed : f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Object;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Component, Default)]
|
||||||
|
pub struct Tracking {
|
||||||
|
entity : u64,
|
||||||
|
is_tracking : bool,
|
||||||
|
}
|
||||||
|
impl Tracking {
|
||||||
|
|
||||||
|
pub fn track(mut self, entity : u64) {
|
||||||
|
self.entity = entity;
|
||||||
|
self.is_tracking = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ignore(mut self) {
|
||||||
|
self.entity = 0;
|
||||||
|
self.is_tracking = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_lock(&mut self) {
|
||||||
|
self.is_tracking = !self.is_tracking;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_tracking(&self) -> bool {
|
||||||
|
self.is_tracking
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/entities.rs
Normal file
29
src/entities.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
use super::components::*;
|
||||||
|
|
||||||
|
#[derive(Bundle)]
|
||||||
|
pub struct PlayerBundle {
|
||||||
|
pub marker : Player,
|
||||||
|
pub speed : Speed,
|
||||||
|
pub pos : Transform,
|
||||||
|
pub mesh : Mesh2d,
|
||||||
|
pub color: MeshMaterial2d<ColorMaterial>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Bundle)]
|
||||||
|
pub struct Box {
|
||||||
|
pub pos : Transform,
|
||||||
|
marker : Object,
|
||||||
|
mesh : Mesh2d,
|
||||||
|
color: MeshMaterial2d<ColorMaterial>,
|
||||||
|
}
|
||||||
|
impl Box {
|
||||||
|
pub fn new(meshes: &mut ResMut<Assets<Mesh>>, materials : &mut ResMut<Assets<ColorMaterial>>, x: f32, y: f32) -> Self {
|
||||||
|
Box {
|
||||||
|
marker : Object,
|
||||||
|
mesh : Mesh2d(meshes.add(Rectangle::new(10.0, 10.0))),
|
||||||
|
pos : Transform::from_xyz(x, y, 0.0),
|
||||||
|
color: MeshMaterial2d(materials.add(Color::hsl(360. , 0.95, 0.7)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
56
src/main.rs
Normal file
56
src/main.rs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
use components::{Player, Speed};
|
||||||
|
use entities::*;
|
||||||
|
|
||||||
|
pub mod entities;
|
||||||
|
pub mod components;
|
||||||
|
pub mod systems;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut app = App::new();
|
||||||
|
app.add_plugins(DefaultPlugins);
|
||||||
|
app.insert_resource(Time::<Fixed>::from_hz(60.));
|
||||||
|
app.add_systems(Startup, setup);
|
||||||
|
app.add_systems(FixedUpdate, (
|
||||||
|
systems::camera_lock,
|
||||||
|
systems::player_movement,
|
||||||
|
));
|
||||||
|
app.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fn setup(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<ColorMaterial>>
|
||||||
|
) {
|
||||||
|
// Setup a world camera with a Transform & tracking
|
||||||
|
commands.spawn((
|
||||||
|
Camera{ ..default() },
|
||||||
|
Camera2d,
|
||||||
|
));
|
||||||
|
|
||||||
|
let shape = meshes.add(Rectangle::new(10.0, 10.0));
|
||||||
|
let color = materials.add(Color::hsl(360., 0.95, 0.7));
|
||||||
|
|
||||||
|
// Setup a player in the world
|
||||||
|
commands.spawn(PlayerBundle {
|
||||||
|
marker: Player,
|
||||||
|
pos: Transform::default(),
|
||||||
|
mesh: Mesh2d(shape),
|
||||||
|
color: MeshMaterial2d(color),
|
||||||
|
speed: Speed { speed: 20.0 }
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup a few boxes
|
||||||
|
commands.spawn(Box::new(&mut meshes, &mut materials, 20.0, 20.0));
|
||||||
|
commands.spawn(Box::new(&mut meshes, &mut materials, 40.0, -20.0));
|
||||||
|
commands.spawn(Box::new(&mut meshes, &mut materials, 100.0, 320.0));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Systems
|
||||||
|
|
||||||
|
//toggle_tracking : toggles tracking the player throughout the world
|
||||||
44
src/systems.rs
Normal file
44
src/systems.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::components::{Player, Speed};
|
||||||
|
|
||||||
|
pub fn camera_lock(
|
||||||
|
camera_query: Single<(&mut Camera, &mut Transform), Without<Player>>,
|
||||||
|
player_query: Single<(&Player, &Transform), Without<Camera2d>>
|
||||||
|
) {
|
||||||
|
let (_, mut camera_pos) = camera_query.into_inner();
|
||||||
|
let (_, player_pos) = player_query.into_inner();
|
||||||
|
camera_pos.translation = player_pos.translation;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn player_movement (
|
||||||
|
keyboard: Res<ButtonInput<KeyCode>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
player_query: Single<(&Player, &mut Transform, &Speed)>
|
||||||
|
) {
|
||||||
|
let (_player, mut transform, speed) = player_query.into_inner();
|
||||||
|
|
||||||
|
let mut translation_delta : Vec3 = Vec3::default();
|
||||||
|
|
||||||
|
// Check WASD for key presses, and update the transform_delta as necessary
|
||||||
|
if keyboard.pressed(KeyCode::KeyW) {
|
||||||
|
translation_delta += Vec3::default().with_y(1.);
|
||||||
|
}
|
||||||
|
if keyboard.pressed(KeyCode::KeyA) {
|
||||||
|
translation_delta += Vec3::default().with_x(-1.);
|
||||||
|
}
|
||||||
|
if keyboard.pressed(KeyCode::KeyS) {
|
||||||
|
translation_delta += Vec3::default().with_y(-1.);
|
||||||
|
}
|
||||||
|
if keyboard.pressed(KeyCode::KeyD) {
|
||||||
|
translation_delta += Vec3::default().with_x(1.);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scale the delta by the player's speed component times the interval between the recent frame updates
|
||||||
|
translation_delta *= speed.speed * time.delta_secs();
|
||||||
|
|
||||||
|
|
||||||
|
// Finally, update player's position vector
|
||||||
|
|
||||||
|
transform.translation += translation_delta;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user