Skip to content

Commit

Permalink
added level intro and player indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirezmike committed Mar 3, 2022
1 parent 54e3a74 commit 5d1b2be
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/bullet.rs
Expand Up @@ -8,6 +8,11 @@ impl Plugin for BulletPlugin {
.add_system_set(
SystemSet::on_exit(AppState::InGame).with_system(cleanup::<CleanupMarker>),
)
.add_system_set(
SystemSet::on_update(AppState::MechaPicker)
.with_system(handle_bullet_events)
.with_system(handle_bullets),
)
.add_system_set(
SystemSet::on_update(AppState::InGame)
.with_system(handle_bullet_events)
Expand Down
41 changes: 41 additions & 0 deletions src/follow_text.rs
@@ -0,0 +1,41 @@
use crate::game_camera::PanOrbitCamera;
use bevy::prelude::*;
use bevy::render::camera::*;

pub struct FollowTextPlugin;
impl Plugin for FollowTextPlugin {
fn build(&self, app: &mut App) {
app.add_system(update_text_position);
}
}

#[derive(Component)]
pub struct FollowText {
pub following: Entity,
}

fn update_text_position(
windows: Res<Windows>,
mut text_query: Query<(&mut Style, &CalculatedSize, &FollowText)>,
mesh_query: Query<&Transform>,
camera_query: Query<(&Camera, &GlobalTransform), With<PanOrbitCamera>>,
) {
for (mut style, calculated, follow) in text_query.iter_mut() {
if let Ok(mesh_position) = mesh_query.get(follow.following) {
for (camera, camera_transform) in camera_query.iter() {
match camera.world_to_screen(&windows, camera_transform, mesh_position.translation)
{
Some(coords) => {
style.position.left = Val::Px(coords.x - calculated.size.width / 2.0);
style.position.bottom =
Val::Px((coords.y * 1.4) - calculated.size.height / 2.0);
}
None => {
// A hack to hide the text when the it's behind the camera
style.position.bottom = Val::Px(-1000.0);
}
}
}
}
}
}
103 changes: 99 additions & 4 deletions src/game_camera.rs
Expand Up @@ -8,8 +8,11 @@ pub struct GameCameraPlugin;
impl Plugin for GameCameraPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(Perlin::new())
.add_system(handle_camera_shake)
.add_system(pan_orbit_camera);
.insert_resource(CameraSettings::default())
//.add_system(handle_camera_shake)
.add_system(update_camera)
//.add_system(pan_orbit_camera);
;
}
}

Expand All @@ -30,6 +33,88 @@ impl Default for PanOrbitCamera {
}
}

#[derive(Default)]
pub struct CameraSettings {
look_at: Vec3,
height: f32,
orbit: bool,
speed: f32,
distance: f32,
target_distance: f32,
}

impl CameraSettings {
pub fn set_camera(
&mut self,
height: f32,
look_at: Vec3,
speed: f32,
orbit: bool,
distance: f32,
target_distance: f32,
) {
self.height = height;
self.look_at = look_at;
self.speed = speed;
self.orbit = orbit;
self.distance = distance;
self.target_distance = target_distance;
}
}

fn update_camera(
mut cameras: Query<&mut Transform, With<PanOrbitCamera>>,
mut camera_settings: ResMut<CameraSettings>,
time: Res<Time>,
) {
let mut c = camera_settings;

if (c.distance - c.target_distance).abs() > 0.1 {
if c.distance > c.target_distance {
c.distance -= time.delta_seconds() * 8.0;
} else {
c.distance += time.delta_seconds() * 8.0;
}
}

for mut transform in cameras.iter_mut() {
if transform.translation.is_nan() {
transform.translation = Vec3::new(0.1, 0.1, 0.1);
}
let height_difference = transform.translation.y - c.height;
if height_difference.abs() > 0.1 {
transform.translation.y +=
(c.height - transform.translation.y) * c.speed * time.delta_seconds();
// if height_difference > 0.0 {
// transform.translation.y -=
// (c.height - transform.translation.y)
// * c.speed
// * time.delta_seconds();
// } else {
// transform.translation.y +=
// (c.height - transform.translation.y)
// * c.speed
// * time.delta_seconds();
// }
}

if c.orbit {
let yaw = Quat::from_rotation_y(time.delta_seconds() as f32 * 0.3);
transform.rotation *= yaw; // rotate around global y axis
} else {
transform.rotation = Quat::from_rotation_y((3.0 * std::f32::consts::PI) / 2.0);
}

let rot_matrix = Mat3::from_quat(transform.rotation);
let new_translation = c.look_at + rot_matrix.mul_vec3(Vec3::new(0.0, 0.0, c.distance));

transform.translation.x = new_translation.x;
transform.translation.z = new_translation.z;

transform.look_at(c.look_at, Vec3::Y);
}
}

fn handle_camera_shake(
time: Res<Time>,
perlin: Res<Perlin>,
Expand Down Expand Up @@ -70,6 +155,8 @@ pub fn pan_orbit_camera(
input_mouse: Res<Input<MouseButton>>,
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut PanOrbitCamera, &mut Transform, &PerspectiveProjection)>,
camera_settings: Res<CameraSettings>,
time: Res<Time>,
) {
// change input mapping for orbit and panning here
let orbit_button = MouseButton::Right;
Expand All @@ -82,6 +169,10 @@ pub fn pan_orbit_camera(
let mut scroll = 0.0;
let mut orbit_button_changed = false;

if camera_settings.orbit {
rotation_move = Vec2::new(2.0, 0.0);
}

if input_mouse.pressed(orbit_button) || keyboard_input.pressed(orbit_key) {
for ev in ev_motion.iter() {
rotation_move += ev.delta;
Expand All @@ -95,6 +186,7 @@ pub fn pan_orbit_camera(
for ev in ev_scroll.iter() {
scroll += ev.y;
}

if input_mouse.just_released(orbit_button)
|| input_mouse.just_pressed(orbit_button)
|| keyboard_input.just_released(orbit_key)
Expand Down Expand Up @@ -151,8 +243,10 @@ pub fn pan_orbit_camera(
// parent = x and y rotation
// child = z-offset
let rot_matrix = Mat3::from_quat(transform.rotation);
transform.translation =
let new_translation =
pan_orbit.focus + rot_matrix.mul_vec3(Vec3::new(0.0, 0.0, pan_orbit.radius));
transform.translation.x = new_translation.x;
transform.translation.z = new_translation.z;
}
}
}
Expand All @@ -163,7 +257,8 @@ fn get_primary_window_size(windows: &Res<Windows>) -> Vec2 {
}

pub fn spawn_camera(mut commands: Commands, game_assets: Res<assets::GameAssets>) {
let translation = Vec3::new(-25.0, 25.0, 0.0);
//let translation = Vec3::new(-25.0, 25.0, 0.0);
let translation = Vec3::new(0.1, 0.1, 0.1);

let radius = translation.length();

Expand Down
2 changes: 1 addition & 1 deletion src/inspect.rs
@@ -1,4 +1,4 @@
use crate::{asset_loading, assets::GameAssets, mesh, player};
use crate::{asset_loading, assets::GameAssets, game_camera, mesh, player};
use bevy::prelude::*;
use bevy::window::WindowResized;
use bevy_inspector_egui::bevy_egui::EguiSettings;
Expand Down
44 changes: 40 additions & 4 deletions src/levels/debug.rs
@@ -1,6 +1,6 @@
use crate::{
asset_loading, assets::GameAssets, bot, burro::Burro, cleanup, collision, game_camera,
game_state, mesh, player, AppState,
asset_loading, assets::GameAssets, bot, burro::Burro, cleanup, collision, follow_text,
game_camera, game_state, mesh, player, AppState,
};
use bevy::gltf::Gltf;
use bevy::prelude::*;
Expand Down Expand Up @@ -143,7 +143,10 @@ fn setup(
mut clear_color: ResMut<ClearColor>,
mut meshes: ResMut<Assets<Mesh>>,
mut images: ResMut<Assets<Image>>,
mut camera_settings: ResMut<game_camera::CameraSettings>,
) {
camera_settings.set_camera(20.0, Vec3::ZERO, 0.4, false, 0.5, 30.0);

*clear_color = ClearColor(Color::rgb(1.0, 0.65, 0.62));
commands.insert_resource(AmbientLight {
color: Color::WHITE,
Expand Down Expand Up @@ -199,10 +202,43 @@ fn setup(
.insert(CleanupMarker)
.insert_bundle(bot::BotBundle::new(b.skin));
} else {
commands
let entity = commands
.spawn_bundle(burro_bundle)
.insert(CleanupMarker)
.insert_bundle(player::PlayerBundle::new(b.skin));
.insert_bundle(player::PlayerBundle::new(b.skin))
.id();

commands
.spawn_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: Rect {
bottom: Val::Px(5.0),
left: Val::Px(15.0),
..Default::default()
},
size: Size {
width: Val::Px(200.0),
..Default::default()
},
..Default::default()
},
text: Text::with_section(
"P1".to_string(),
TextStyle {
font: game_assets.font.clone(),
font_size: 40.0,
color: Color::YELLOW,
},
TextAlignment {
..Default::default()
},
),
..Default::default()
})
.insert(CleanupMarker)
.insert(follow_text::FollowText { following: entity });
}
});

Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Expand Up @@ -12,6 +12,7 @@ mod bullet;
mod burro;
mod collision;
mod direction;
mod follow_text;
mod game_camera;
mod game_state;
mod ingame_ui;
Expand All @@ -35,6 +36,7 @@ fn main() {
.add_plugin(audio::GameAudioPlugin)
.add_plugin(bot::BotPlugin)
.add_plugin(bullet::BulletPlugin)
.add_plugin(follow_text::FollowTextPlugin)
.add_plugin(game_camera::GameCameraPlugin)
.add_plugin(game_state::GameStatePlugin)
.add_plugin(mesh::MeshPlugin)
Expand Down

0 comments on commit 5d1b2be

Please sign in to comment.