added initial zoom out timer

- to view the overall level
This commit is contained in:
Nixon 2022-08-28 17:04:02 +08:00
parent 450e448649
commit edd3f3a618
3 changed files with 44 additions and 13 deletions

View file

@ -2,8 +2,8 @@
#![allow(clippy::too_many_arguments)] #![allow(clippy::too_many_arguments)]
pub use crate::filters::*; pub use crate::filters::*;
use crate::{AppState, audio_system}; use crate::{AppState, audio_system};
use crate::levels;
use bevy::{ use bevy::{
ecs::system::EntityCommands, ecs::system::EntityCommands,
@ -28,6 +28,7 @@ impl Plugin for GamePlugin {
.init_resource::<CharacterMeshes>() .init_resource::<CharacterMeshes>()
.insert_resource(CurrentLevel(None)) .insert_resource(CurrentLevel(None))
.init_resource::<CharacterList>() .init_resource::<CharacterList>()
.init_resource::<levels::ZoomTimer>()
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup)) .add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup))
.add_system_set(SystemSet::on_enter(AppState::Win).with_system(win_setup)) .add_system_set(SystemSet::on_enter(AppState::Win).with_system(win_setup))
.add_system_set( .add_system_set(
@ -39,14 +40,14 @@ impl Plugin for GamePlugin {
.with_system(change_character_system) .with_system(change_character_system)
.with_system(player_movement_system) .with_system(player_movement_system)
.with_system(level_keyboard_system) .with_system(level_keyboard_system)
.with_system(move_camera) .with_system(camera_system)
.with_system(character_particle_effect_system), .with_system(character_particle_effect_system),
) )
.add_system_set( .add_system_set(
SystemSet::on_update(AppState::Win) SystemSet::on_update(AppState::Win)
.with_system(player_movement_system) .with_system(player_movement_system)
.with_system(level_keyboard_system) .with_system(level_keyboard_system)
.with_system(move_camera) .with_system(camera_system)
.with_system(character_particle_effect_system) .with_system(character_particle_effect_system)
.with_system(move_win_text_system), .with_system(move_win_text_system),
) )
@ -133,13 +134,14 @@ fn setup(
first_level: Res<FirstLevel>, first_level: Res<FirstLevel>,
mut current_level: ResMut<CurrentLevel>, mut current_level: ResMut<CurrentLevel>,
mut level_startup_event: EventWriter<LevelStartupEvent>, mut level_startup_event: EventWriter<LevelStartupEvent>,
mut camera_query: Query<&mut Transform, With<Camera>>, mut camera_query: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
mut zoom_timer: ResMut<levels::ZoomTimer>,
) { ) {
if current_level.0.is_none() { if current_level.0.is_none() {
current_level.0 = Some(first_level.0); current_level.0 = Some(first_level.0);
} }
crate::levels::setup_level(&mut level_startup_event, &mut camera_query); crate::levels::setup_level(&mut level_startup_event, &mut camera_query, &mut zoom_timer);
} }
pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>( pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>(
@ -551,16 +553,22 @@ fn win_setup(
.insert(WinText); .insert(WinText);
} }
fn move_camera( fn camera_system(
mut camera_query: Query<(&Camera, &mut Transform)>, mut camera_query: Query<(&Camera, &mut OrthographicProjection, &mut Transform)>,
characters: Query<&Transform, (Without<Camera>, With<Player>)>, characters: Query<&Transform, (Without<Camera>, With<Player>)>,
time: Res<Time>, time: Res<Time>,
mut zoom_timer: ResMut<levels::ZoomTimer>,
) { ) {
fn lerp(x: f32, y: f32, t: f32,) -> f32 {
return ((y - x) * t) + x;
}
const MARGIN: f32 = 300.0; const MARGIN: f32 = 300.0;
const FOLLOW_SPEED: f32 = std::f32::consts::PI; const FOLLOW_SPEED: f32 = std::f32::consts::PI;
const ZOOM_SPEED: f32 = std::f32::consts::E;
for character_transform in characters.iter() { for character_transform in characters.iter() {
let (camera, mut camera_transform) = camera_query.single_mut(); let (camera, mut projection, mut camera_transform) = camera_query.single_mut();
let size: Vec2 = camera.logical_viewport_size().unwrap(); let size: Vec2 = camera.logical_viewport_size().unwrap();
let half_height: f32 = size.y * 0.5; let half_height: f32 = size.y * 0.5;
@ -572,6 +580,10 @@ fn move_camera(
.translation .translation
.lerp(target_translation, time.delta_seconds() * FOLLOW_SPEED); .lerp(target_translation, time.delta_seconds() * FOLLOW_SPEED);
if zoom_timer.0.finished() {
projection.scale = lerp(projection.scale, 1.0, time.delta_seconds() * ZOOM_SPEED);
} else { zoom_timer.0.tick(time.delta()); }
// always make sure that camera is away from the object in order to render them // always make sure that camera is away from the object in order to render them
camera_transform.translation.z = 999.0; camera_transform.translation.z = 999.0;
} }
@ -592,11 +604,12 @@ fn level_keyboard_system(
mut commands: Commands, mut commands: Commands,
mut current_level: ResMut<CurrentLevel>, mut current_level: ResMut<CurrentLevel>,
mut level_startup_event: EventWriter<LevelStartupEvent>, mut level_startup_event: EventWriter<LevelStartupEvent>,
mut camera_query: Query<&mut Transform, With<Camera>>, mut camera_query: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut character_list: ResMut<CharacterList>, mut character_list: ResMut<CharacterList>,
level_query: Query<Entity, With<Level>>, level_query: Query<Entity, With<Level>>,
mut app_state: ResMut<State<AppState>>, mut app_state: ResMut<State<AppState>>,
mut zoom_timer: ResMut<levels::ZoomTimer>,
) { ) {
if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) { if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) {
current_level.0 = Some(LevelId( current_level.0 = Some(LevelId(
@ -611,7 +624,7 @@ fn level_keyboard_system(
commands.entity(entity).despawn_recursive(); commands.entity(entity).despawn_recursive();
} }
if app_state.replace(AppState::Game).is_err() { if app_state.replace(AppState::Game).is_err() {
crate::levels::setup_level(&mut level_startup_event, &mut camera_query); crate::levels::setup_level(&mut level_startup_event, &mut camera_query, &mut zoom_timer);
} }
} }
} }

View file

@ -1,5 +1,7 @@
#![allow(clippy::too_many_arguments)] #![allow(clippy::too_many_arguments)]
use std::time::Duration;
pub use stored::*; pub use stored::*;
use crate::game::*; use crate::game::*;
@ -7,11 +9,27 @@ use crate::game::*;
use bevy::{prelude::*, reflect::TypeUuid}; use bevy::{prelude::*, reflect::TypeUuid};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Component)]
pub struct ZoomTimer(pub Timer);
impl Default for ZoomTimer {
fn default() -> Self {
Self {
0: Timer::new(Duration::new(2, 0), false),
}
}
}
pub fn setup_level( pub fn setup_level(
level_startup_event: &mut EventWriter<LevelStartupEvent>, level_startup_event: &mut EventWriter<LevelStartupEvent>,
camera_query: &mut Query<&mut Transform, With<Camera>>, camera_query: &mut Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
zoom_timer: &mut ResMut<ZoomTimer>,
) { ) {
camera_query.single_mut().translation = Default::default(); let (mut transform, mut projection) = camera_query.single_mut();
transform.translation = Default::default();
projection.scale = 3.0;
zoom_timer.0.reset();
level_startup_event.send(LevelStartupEvent); level_startup_event.send(LevelStartupEvent);
} }

View file

@ -59,7 +59,7 @@ fn main() {
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugin(audio_system::AudioSystemPlugin) .add_plugin(audio_system::AudioSystemPlugin)
//.add_plugin(RapierDebugRenderPlugin::default()) //.add_plugin(RapierDebugRenderPlugin::default())
//.add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new()) // .add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new())
.add_plugin(JsonAssetPlugin::<levels::StoredLevels>::new(&[ .add_plugin(JsonAssetPlugin::<levels::StoredLevels>::new(&[
"levels.json", "levels.json",
])); ]));