-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c64ccc2
commit d605473
Showing
7 changed files
with
179 additions
and
30 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
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,27 @@ | ||
use bevy::ecs::system::EntityCommand; | ||
use bevy::prelude::*; | ||
|
||
use crate::game::actor::actor; | ||
use crate::game::actor::facing::FacePlayer; | ||
use crate::util::prelude::*; | ||
|
||
pub(super) fn plugin(app: &mut App) { | ||
app.configure::<IsEnemy>(); | ||
} | ||
|
||
#[derive(Component, Reflect, Default)] | ||
#[reflect(Component)] | ||
pub struct IsEnemy; | ||
|
||
impl Configure for IsEnemy { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Self>(); | ||
} | ||
} | ||
|
||
pub fn enemy(key: impl Into<String>) -> impl EntityCommand<World> { | ||
let key = key.into(); | ||
move |mut entity: EntityWorldMut| { | ||
entity.add(actor(key)).insert((IsEnemy, FacePlayer)); | ||
} | ||
} |
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,80 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::core::camera::CameraRoot; | ||
use crate::core::window::WindowRoot; | ||
use crate::core::UpdateSet; | ||
use crate::game::actor::player::IsPlayer; | ||
use crate::util::prelude::*; | ||
|
||
pub(super) fn plugin(app: &mut App) { | ||
app.configure::<(Facing, FacePlayer, FaceCursor)>(); | ||
} | ||
|
||
#[derive(Component, Reflect)] | ||
#[reflect(Component)] | ||
pub struct Facing(pub Dir2); | ||
|
||
impl Configure for Facing { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Facing>(); | ||
} | ||
} | ||
|
||
impl Default for Facing { | ||
fn default() -> Self { | ||
Self(Dir2::EAST) | ||
} | ||
} | ||
|
||
#[derive(Component, Reflect, Default)] | ||
#[reflect(Component)] | ||
pub struct FacePlayer; | ||
|
||
impl Configure for FacePlayer { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Self>(); | ||
app.add_systems(Update, face_player.in_set(UpdateSet::SyncEarly)); | ||
} | ||
} | ||
|
||
fn face_player( | ||
player_query: Query<&GlobalTransform, With<IsPlayer>>, | ||
mut facing_query: Query<(&mut Facing, &GlobalTransform), With<FacePlayer>>, | ||
) { | ||
let target_pos = r!(player_query.get_single()).translation().xy(); | ||
|
||
for (mut facing, gt) in &mut facing_query { | ||
let pos = gt.translation().xy(); | ||
facing.0 = c!(Dir2::new(target_pos - pos)); | ||
} | ||
} | ||
|
||
#[derive(Component, Reflect, Default)] | ||
#[reflect(Component)] | ||
pub struct FaceCursor; | ||
|
||
impl Configure for FaceCursor { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Self>(); | ||
app.add_systems(Update, face_cursor.in_set(UpdateSet::SyncEarly)); | ||
} | ||
} | ||
|
||
fn face_cursor( | ||
window_root: Res<WindowRoot>, | ||
window_query: Query<&Window>, | ||
camera_root: Res<CameraRoot>, | ||
camera_query: Query<(&Camera, &GlobalTransform)>, | ||
mut facing_query: Query<(&mut Facing, &GlobalTransform), With<FaceCursor>>, | ||
) { | ||
let window = r!(window_query.get(window_root.primary)); | ||
let (camera, camera_gt) = r!(camera_query.get(camera_root.primary)); | ||
let target_pos = r!(window | ||
.cursor_position() | ||
.and_then(|cursor| camera.viewport_to_world_2d(camera_gt, cursor))); | ||
|
||
for (mut facing, gt) in &mut facing_query { | ||
let pos = gt.translation().xy(); | ||
facing.0 = c!(Dir2::new(target_pos - pos)); | ||
} | ||
} |
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,23 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::game::actor::actor_helper; | ||
use crate::game::actor::facing::FaceCursor; | ||
use crate::util::prelude::*; | ||
|
||
pub(super) fn plugin(app: &mut App) { | ||
app.configure::<IsPlayer>(); | ||
} | ||
|
||
#[derive(Component, Reflect, Default)] | ||
#[reflect(Component)] | ||
pub struct IsPlayer; | ||
|
||
impl Configure for IsPlayer { | ||
fn configure(app: &mut App) { | ||
app.register_type::<Self>(); | ||
} | ||
} | ||
|
||
pub fn player(entity: EntityWorldMut) { | ||
actor_helper(entity, None).insert((IsPlayer, FaceCursor)); | ||
} |
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