-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are to benefits for doing this: * Simplification of systems which need access to pointed to entities. * Intersection is computed only once and used from multiple places. The full benefit described above will be seized once there are new systems working with pointed to entities (and terrain). An example is a system which changes displayed cursor (for example: attack vs move) based on pointed to object / terrain. Relates to #11.
- Loading branch information
Showing
3 changed files
with
107 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use super::{collisions::Intersector, objects::Playable, GameStates, Labels}; | ||
use crate::math::ray::Ray; | ||
use bevy::{ | ||
ecs::system::SystemParam, | ||
input::mouse::MouseMotion, | ||
prelude::{ | ||
App, Camera, Entity, EventReader, GlobalTransform, ParallelSystemDescriptorCoercion, | ||
Plugin, Query, Res, ResMut, SystemSet, With, | ||
}, | ||
window::Windows, | ||
}; | ||
use glam::{Vec2, Vec3}; | ||
|
||
pub struct PointerPlugin; | ||
|
||
impl Plugin for PointerPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.init_resource::<Pointer>().add_system_set( | ||
SystemSet::on_update(GameStates::Playing) | ||
.with_system(mouse_move_handler.label(Labels::PreInputUpdate)), | ||
); | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Pointer { | ||
entity: Option<Entity>, | ||
} | ||
|
||
impl Pointer { | ||
pub fn entity(&self) -> Option<Entity> { | ||
self.entity | ||
} | ||
|
||
fn set_entity(&mut self, entity: Option<Entity>) { | ||
self.entity = entity; | ||
} | ||
} | ||
|
||
#[derive(SystemParam)] | ||
struct MouseInWorld<'w, 's> { | ||
windows: Res<'w, Windows>, | ||
cameras: Query<'w, 's, (&'static GlobalTransform, &'static Camera)>, | ||
} | ||
|
||
impl<'w, 's> MouseInWorld<'w, 's> { | ||
fn mouse_ray(&self) -> Option<Ray> { | ||
let window = self.windows.get_primary().unwrap(); | ||
|
||
// Normalized to values between -1.0 to 1.0 with (0.0, 0.0) in the | ||
// middle of the screen. | ||
let cursor_position = match window.cursor_position() { | ||
Some(position) => { | ||
let screen_size = Vec2::new(window.width() as f32, window.height() as f32); | ||
(position / screen_size) * 2.0 - Vec2::ONE | ||
} | ||
None => return None, | ||
}; | ||
|
||
let (camera_transform, camera) = self.cameras.single(); | ||
let camera_transform_mat = camera_transform.compute_matrix(); | ||
let camera_projection = camera.projection_matrix; | ||
|
||
let screen_to_world = camera_transform_mat * camera_projection.inverse(); | ||
let world_to_screen = camera_projection * camera_transform_mat; | ||
|
||
// Depth of camera near plane in screen coordinates. | ||
let near_plane = world_to_screen.transform_point3(-Vec3::Z * camera.near).z; | ||
let ray_origin = screen_to_world.transform_point3(cursor_position.extend(near_plane)); | ||
let ray_direction = ray_origin - camera_transform.translation; | ||
Some(Ray::new(ray_origin, ray_direction)) | ||
} | ||
} | ||
|
||
fn mouse_move_handler( | ||
mut resource: ResMut<Pointer>, | ||
event: EventReader<MouseMotion>, | ||
mouse: MouseInWorld, | ||
playable: Intersector<With<Playable>>, | ||
) { | ||
if event.len() == 0 { | ||
return; | ||
} | ||
|
||
let entity = mouse | ||
.mouse_ray() | ||
.and_then(|ray| playable.ray_intersection(&ray)) | ||
.map(|(entity, _)| entity); | ||
resource.set_entity(entity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters