|
|
|
|
@ -6,6 +6,7 @@ use bevy::render::view::ColorGrading;
|
|
|
|
|
use games::physics2d::*;
|
|
|
|
|
use games::*;
|
|
|
|
|
use std::hash::BuildHasher;
|
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
App::new()
|
|
|
|
|
@ -26,6 +27,7 @@ fn main() {
|
|
|
|
|
.init_resource::<RewindFrames>()
|
|
|
|
|
.init_resource::<Flaps>()
|
|
|
|
|
.init_resource::<Deaths>()
|
|
|
|
|
.init_resource::<LongestRun>()
|
|
|
|
|
.init_state::<PlayerState>()
|
|
|
|
|
.add_systems(
|
|
|
|
|
Startup,
|
|
|
|
|
@ -39,8 +41,11 @@ fn main() {
|
|
|
|
|
tweak_camera.after(create_camera_2d),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(OnEnter(PlayerState::Alive), alive_bird)
|
|
|
|
|
.add_systems(OnEnter(PlayerState::Alive), reset_button::<FlapButton>)
|
|
|
|
|
.add_systems(
|
|
|
|
|
OnEnter(PlayerState::Alive),
|
|
|
|
|
(alive_bird, start_run, reset_button::<FlapButton>),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(OnExit(PlayerState::Alive), end_run)
|
|
|
|
|
.add_systems(OnEnter(PlayerState::Rewind), alive_bird)
|
|
|
|
|
.add_systems(OnEnter(PlayerState::Pause), pause_bird)
|
|
|
|
|
.add_systems(OnEnter(PlayerState::Stasis), pause_bird)
|
|
|
|
|
@ -84,8 +89,9 @@ fn main() {
|
|
|
|
|
// Stats that get synced to the UI
|
|
|
|
|
(
|
|
|
|
|
sync_resource_to_ui::<Score>.run_if(resource_changed::<Score>),
|
|
|
|
|
sync_resource_to_ui::<Flaps>.run_if(resource_changed::<Flaps>),
|
|
|
|
|
sync_resource_to_ui::<LongestRun>.run_if(resource_changed::<LongestRun>),
|
|
|
|
|
sync_resource_to_ui::<Deaths>.run_if(resource_changed::<Deaths>),
|
|
|
|
|
sync_resource_to_ui::<Flaps>.run_if(resource_changed::<Flaps>),
|
|
|
|
|
sync_resource_to_ui::<RewindFrames>.run_if(resource_changed::<RewindFrames>),
|
|
|
|
|
),
|
|
|
|
|
(update_tooltip, debug_trail).run_if(in_state(DebuggingState::On)),
|
|
|
|
|
@ -346,7 +352,8 @@ fn move_pipe(
|
|
|
|
|
rand: Res<Rand>,
|
|
|
|
|
) {
|
|
|
|
|
if let Ok((Batch(id), pipe, mut pipe_t)) = pipes.get_mut(trigger.target()) {
|
|
|
|
|
*pipe_t = {
|
|
|
|
|
*pipe_t =
|
|
|
|
|
{
|
|
|
|
|
let offset = {
|
|
|
|
|
let val = rand.0.hash_one(id);
|
|
|
|
|
|
|
|
|
|
@ -361,12 +368,10 @@ fn move_pipe(
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match pipe {
|
|
|
|
|
Pipe::Top => {
|
|
|
|
|
Transform::from_xyz(0.0, 300.0 + offset, -2.0).with_scale(Vec3::splat(100.0))
|
|
|
|
|
}
|
|
|
|
|
Pipe::Bottom => {
|
|
|
|
|
Transform::from_xyz(0.0, -300.0 + offset, -2.0).with_scale(Vec3::splat(100.0))
|
|
|
|
|
}
|
|
|
|
|
Pipe::Top => Transform::from_xyz(0.0, 300.0 + offset, -2.0)
|
|
|
|
|
.with_scale(Vec3::splat(100.0)),
|
|
|
|
|
Pipe::Bottom => Transform::from_xyz(0.0, -300.0 + offset, -2.0)
|
|
|
|
|
.with_scale(Vec3::splat(100.0)),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
@ -508,35 +513,50 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BackgroundColor(Color::WHITE),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
PlayerState::Stasis,
|
|
|
|
|
))
|
|
|
|
|
.with_children(|parent| {
|
|
|
|
|
parent.spawn((
|
|
|
|
|
Text::new("Game Over...?"),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
));
|
|
|
|
|
parent.spawn((
|
|
|
|
|
SyncResource::<Score>::default(),
|
|
|
|
|
Text::default(),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
));
|
|
|
|
|
parent.spawn((
|
|
|
|
|
SyncResource::<Flaps>::default(),
|
|
|
|
|
SyncResource::<LongestRun>::default(),
|
|
|
|
|
Text::default(),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
));
|
|
|
|
|
parent.spawn((
|
|
|
|
|
SyncResource::<RewindFrames>::default(),
|
|
|
|
|
SyncResource::<Deaths>::default(),
|
|
|
|
|
Text::default(),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
));
|
|
|
|
|
parent.spawn((
|
|
|
|
|
SyncResource::<Deaths>::default(),
|
|
|
|
|
SyncResource::<Flaps>::default(),
|
|
|
|
|
Text::default(),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
));
|
|
|
|
|
parent.spawn((
|
|
|
|
|
SyncResource::<RewindFrames>::default(),
|
|
|
|
|
Text::default(),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
));
|
|
|
|
|
parent.spawn((
|
|
|
|
|
Text::new("Press R to Rewind"),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
));
|
|
|
|
|
parent
|
|
|
|
|
@ -564,12 +584,20 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
parent
|
|
|
|
|
.spawn((
|
|
|
|
|
Button,
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
Node { ..default() },
|
|
|
|
|
children![Text::new("Credits")],
|
|
|
|
|
children![(TextColor(BLACK.into()), Text::new("Credits")),],
|
|
|
|
|
))
|
|
|
|
|
.observe(show_credits);
|
|
|
|
|
parent
|
|
|
|
|
.spawn((Button, Node { ..default() }, children![Text::new("Quit"),]))
|
|
|
|
|
.spawn((
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
Button,
|
|
|
|
|
Node { ..default() },
|
|
|
|
|
children![(Text::new("Quit"), TextColor(BLACK.into()))],
|
|
|
|
|
))
|
|
|
|
|
.observe(quit_game);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
@ -588,9 +616,13 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BackgroundColor(WHITE.into()),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
PlayerState::Credits,
|
|
|
|
|
children![(
|
|
|
|
|
Text::new(credits_str),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center)
|
|
|
|
|
)],
|
|
|
|
|
))
|
|
|
|
|
@ -600,25 +632,57 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
align_self: AlignSelf::Center,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
Button,
|
|
|
|
|
children![Text::new("Close")],
|
|
|
|
|
children![(Text::new("Close"), TextColor(BLACK.into()))],
|
|
|
|
|
));
|
|
|
|
|
})
|
|
|
|
|
.observe(hide_credits);
|
|
|
|
|
|
|
|
|
|
let logo =
|
|
|
|
|
server.load_with_settings("flappy/logo.png", |settings: &mut ImageLoaderSettings| {
|
|
|
|
|
// Need to use nearest filtering to avoid bleeding between the slices with tiling
|
|
|
|
|
settings.sampler = ImageSampler::nearest();
|
|
|
|
|
});
|
|
|
|
|
commands.spawn((
|
|
|
|
|
Node {
|
|
|
|
|
align_self: AlignSelf::Center,
|
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
|
align_items: AlignItems::Center,
|
|
|
|
|
justify_items: JustifyItems::Center,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BackgroundColor(Color::NONE),
|
|
|
|
|
PlayerState::Pause,
|
|
|
|
|
Text::new("PAUSED"),
|
|
|
|
|
children![
|
|
|
|
|
(
|
|
|
|
|
ImageNode {
|
|
|
|
|
image: logo,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
Node {
|
|
|
|
|
width: Val::Px(256.0),
|
|
|
|
|
..default()
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
Node {
|
|
|
|
|
margin: UiRect::all(Val::Px(5.0)),
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
Text::new("(paused)"),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
commands
|
|
|
|
|
.spawn(Node {
|
|
|
|
|
.spawn((
|
|
|
|
|
Node {
|
|
|
|
|
align_self: AlignSelf::End,
|
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
|
flex_direction: FlexDirection::Row,
|
|
|
|
|
@ -626,7 +690,8 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
width: Val::Percent(100.0),
|
|
|
|
|
min_height: Val::Percent(10.0),
|
|
|
|
|
..default()
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
.with_children(|parent| {
|
|
|
|
|
let rewind_image = server.load_with_settings(
|
|
|
|
|
"flappy/rewind.png",
|
|
|
|
|
@ -644,8 +709,10 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
BackgroundColor(Color::WHITE.with_alpha(0.9)),
|
|
|
|
|
Button,
|
|
|
|
|
BackgroundColor::default(),
|
|
|
|
|
RewindButton,
|
|
|
|
|
children![
|
|
|
|
|
(
|
|
|
|
|
@ -660,7 +727,8 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
Text::new("Rewind! (R)"),
|
|
|
|
|
Text::new("Rewind!\n(Hold R)"),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextFont::from_font_size(30.0),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center)
|
|
|
|
|
),
|
|
|
|
|
@ -684,11 +752,15 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
BackgroundColor(Color::WHITE.with_alpha(0.9)),
|
|
|
|
|
Button,
|
|
|
|
|
FlapButton,
|
|
|
|
|
children![
|
|
|
|
|
(
|
|
|
|
|
Text::new("Flap! (Spacebar)"),
|
|
|
|
|
Text::new("Flap!\n(Spacebar)"),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextFont::from_font_size(30.0),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center)
|
|
|
|
|
),
|
|
|
|
|
@ -714,10 +786,13 @@ fn init_ui(mut commands: Commands, server: Res<AssetServer>) {
|
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
BackgroundColor(WHITE.into()),
|
|
|
|
|
BorderRadius::all(Val::Px(5.0)),
|
|
|
|
|
BackgroundColor(Color::WHITE),
|
|
|
|
|
BorderColor(BLACK.into()),
|
|
|
|
|
children![(
|
|
|
|
|
SyncResource::<Score>::default(),
|
|
|
|
|
Text::default(),
|
|
|
|
|
TextColor(BLACK.into()),
|
|
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
|
|
|
)],
|
|
|
|
|
));
|
|
|
|
|
@ -993,6 +1068,61 @@ impl Display for Flaps {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Track the longest run a player has done
|
|
|
|
|
/// Store the latest run and upsert latest run to the longest run if it is longer
|
|
|
|
|
#[derive(Resource, Default)]
|
|
|
|
|
struct LongestRun {
|
|
|
|
|
/// Longest run start
|
|
|
|
|
start: Option<Instant>,
|
|
|
|
|
/// Longest run end
|
|
|
|
|
end: Option<Instant>,
|
|
|
|
|
/// Latest run start
|
|
|
|
|
latest_start: Option<Instant>,
|
|
|
|
|
/// Latest run start
|
|
|
|
|
latest_end: Option<Instant>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for LongestRun {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
|
match (self.start, self.end) {
|
|
|
|
|
(Some(a), Some(b)) => {
|
|
|
|
|
if b > a {
|
|
|
|
|
writeln!(f, "Longest Run: {:.2?}", b - a)
|
|
|
|
|
} else {
|
|
|
|
|
writeln!(f, "Longest Run: ???")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => writeln!(f, "Longest Run: ???"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_run(mut longest_run: ResMut<LongestRun>) {
|
|
|
|
|
longest_run.latest_start = Some(Instant::now());
|
|
|
|
|
longest_run.latest_end = None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn end_run(mut longest_run: ResMut<LongestRun>) {
|
|
|
|
|
longest_run.latest_end = Some(Instant::now());
|
|
|
|
|
|
|
|
|
|
match (longest_run.start, longest_run.end) {
|
|
|
|
|
// Longest run hasn't been set yet, so set it to the latest run
|
|
|
|
|
(None, None) => {
|
|
|
|
|
longest_run.start = longest_run.latest_start;
|
|
|
|
|
longest_run.end = longest_run.latest_end;
|
|
|
|
|
}
|
|
|
|
|
// Longest run was previously set
|
|
|
|
|
(Some(start), Some(end)) => {
|
|
|
|
|
// Check if latest run is longer than current longest run
|
|
|
|
|
if longest_run.latest_end.unwrap() - longest_run.latest_start.unwrap() > end - start {
|
|
|
|
|
longest_run.start = longest_run.latest_start;
|
|
|
|
|
longest_run.end = longest_run.latest_end;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => panic!("What?"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Track number of times player died
|
|
|
|
|
#[derive(Resource, Default)]
|
|
|
|
|
struct Deaths(usize);
|
|
|
|
|
@ -1142,10 +1272,10 @@ fn debug_trail(
|
|
|
|
|
fn shimmer_button<T: Component>(mut bg: Single<&mut BackgroundColor, With<T>>, time: Res<Time>) {
|
|
|
|
|
let t = time.elapsed_secs();
|
|
|
|
|
let period = 3.0;
|
|
|
|
|
let r = (((t / period) % 1.0) * std::f32::consts::PI).cos();
|
|
|
|
|
let g = ((((t / period) + 0.3) % 1.0) * std::f32::consts::PI).cos();
|
|
|
|
|
let b = ((((t / period) + 0.6) % 1.0) * std::f32::consts::PI).cos();
|
|
|
|
|
bg.0 = Srgba::rgb(r, g, b).into();
|
|
|
|
|
let red = (((t / period) % 1.0) * std::f32::consts::PI).cos();
|
|
|
|
|
let green = ((((t / period) + 0.3) % 1.0) * std::f32::consts::PI).cos();
|
|
|
|
|
let blue = ((((t / period) + 0.6) % 1.0) * std::f32::consts::PI).cos();
|
|
|
|
|
bg.0 = Srgba { red, green, blue, alpha: bg.0.alpha() }.into();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset_button<T: Component>(mut bg: Single<&mut BackgroundColor, With<T>>) {
|
|
|
|
|
|