You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
862 lines
27 KiB
Rust
862 lines
27 KiB
Rust
// Bevy basically forces "complex types" with Querys
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
use games::physics2d::*;
|
|
use games::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((
|
|
BaseGamePlugin {
|
|
name: "flappy bird (with rewind)".into(),
|
|
game_type: GameType::Two,
|
|
},
|
|
Physics2dPlugin,
|
|
))
|
|
.insert_resource(Gravity(Vec2::NEG_Y * 9.8 * 100.0))
|
|
.init_resource::<PipeComponents>()
|
|
.init_resource::<GroundComponents>()
|
|
.init_resource::<Score>()
|
|
.init_resource::<RewindFrames>()
|
|
.init_resource::<Flaps>()
|
|
.init_resource::<Deaths>()
|
|
.init_state::<PlayerState>()
|
|
.add_systems(
|
|
Startup,
|
|
(
|
|
init_bird,
|
|
// init_obstacles,
|
|
init_obstacle_assets,
|
|
init_first_batches.after(init_obstacle_assets),
|
|
init_ui,
|
|
tweak_camera.after(create_camera_2d),
|
|
),
|
|
)
|
|
.add_systems(OnEnter(PlayerState::Alive), alive_bird)
|
|
.add_systems(OnEnter(PlayerState::Pause), pause_bird)
|
|
.add_systems(OnEnter(PlayerState::Stasis), pause_bird)
|
|
// .add_systems(OnEnter(PlayerState::Rewind), pause_bird)
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
// Systems to run when player state changes
|
|
(
|
|
// Print out when this state changes for debugging purposes
|
|
debug_state_changes::<PlayerState>,
|
|
// Toggle (UI) elements when the player dies/alives
|
|
toggle_state_visibility::<PlayerState>,
|
|
)
|
|
.run_if(state_changed::<PlayerState>),
|
|
// Detect if the bird is "dead" by checking if it is visible
|
|
// from the point of view of the camera
|
|
detect_dead.run_if(in_state(PlayerState::Alive)),
|
|
// Toggle rewinding state when "R" is pressed/released
|
|
toggle_rewind.run_if(
|
|
input_just_pressed(KeyCode::KeyR).or(input_just_released(KeyCode::KeyR)),
|
|
),
|
|
// Systems to run in the "play" state
|
|
(
|
|
// Only flap when we press the space key
|
|
flap_kb.run_if(input_just_pressed(KeyCode::Space)),
|
|
// Rewinding systems
|
|
record.run_if(any_component_changed::<Transform>),
|
|
)
|
|
.run_if(in_state(PlayerState::Alive)),
|
|
// Rewinding systems
|
|
rewind.run_if(in_state(PlayerState::Rewind)),
|
|
// Camera follows when bird moves regardless of player state
|
|
camera_follow_bird.run_if(any_component_changed::<Transform>),
|
|
// Pause when the player presses Escape
|
|
pause_game.run_if(input_just_pressed(KeyCode::Escape)),
|
|
// Transition out of the pause screen if the player presses space
|
|
un_pause_game
|
|
.run_if(input_just_pressed(KeyCode::Space))
|
|
.run_if(in_state(PlayerState::Pause)),
|
|
// 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::<Deaths>.run_if(resource_changed::<Deaths>),
|
|
sync_resource_to_ui::<RewindFrames>.run_if(resource_changed::<RewindFrames>),
|
|
),
|
|
update_batch_position.run_if(any_component_changed::<Batch>),
|
|
update_tooltip.run_if(in_state(DebuggingState::On)),
|
|
(
|
|
// Temp debugging systems
|
|
debug_collision_events.run_if(on_event::<CollisionEnded>),
|
|
),
|
|
),
|
|
)
|
|
.add_observer(debug_collision_start_observations)
|
|
.add_observer(debug_collision_end_observations)
|
|
.add_observer(flap)
|
|
.add_observer(populate_batch)
|
|
.add_observer(populate_pipe)
|
|
.add_observer(populate_ground)
|
|
.add_observer(populate_hitbox)
|
|
.add_observer(manage_batches)
|
|
.add_observer(manage_score)
|
|
.run();
|
|
}
|
|
|
|
fn tweak_camera(camera: Single<&mut Camera>) {
|
|
debug!("Tweaking camera");
|
|
let mut c = camera.into_inner();
|
|
c.clear_color = ClearColorConfig::Custom(WHITE.into());
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct Bird;
|
|
|
|
#[derive(States, Clone, Eq, PartialEq, Debug, Hash, Default, Component)]
|
|
enum PlayerState {
|
|
Alive,
|
|
Rewind,
|
|
Stasis,
|
|
// Kind of a hack, but we need a state for this
|
|
Credits,
|
|
#[default]
|
|
Pause,
|
|
}
|
|
|
|
// A tape tracking the bird's state every frame
|
|
#[derive(Component, Default)]
|
|
struct Tape {
|
|
accumulated_translations: Vec<AccumulatedTranslation>,
|
|
linear_velocities: Vec<LinearVelocity>,
|
|
angular_velocities: Vec<AngularVelocity>,
|
|
external_angular_impulses: Vec<ExternalAngularImpulse>,
|
|
external_forces: Vec<ExternalForce>,
|
|
external_impulses: Vec<ExternalImpulse>,
|
|
external_torques: Vec<ExternalTorque>,
|
|
positions: Vec<Position>,
|
|
rotations: Vec<Rotation>,
|
|
}
|
|
|
|
fn init_bird(
|
|
mut commands: Commands,
|
|
server: Res<AssetServer>,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
) {
|
|
let material = MeshMaterial2d(materials.add(ColorMaterial {
|
|
texture: Some(server.load("kenny.nl/1-bit-platformer-pack/tile_0380.png")),
|
|
color: ORANGE.into(),
|
|
alpha_mode: AlphaMode2d::Blend,
|
|
..default()
|
|
}));
|
|
|
|
let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0)));
|
|
|
|
let name = Name::new("bird");
|
|
|
|
let t = Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::splat(100.0));
|
|
|
|
let physics = (
|
|
RigidBody::Static,
|
|
Collider::circle(0.5),
|
|
Mass(10.0),
|
|
ExternalImpulse::default().with_persistence(false),
|
|
MaxLinearSpeed(500.0),
|
|
);
|
|
|
|
let tape = Tape::default();
|
|
|
|
commands.spawn((
|
|
name,
|
|
mesh,
|
|
material,
|
|
physics,
|
|
t,
|
|
Bird,
|
|
tape,
|
|
CollisionEventsEnabled,
|
|
));
|
|
}
|
|
|
|
#[derive(Component, Clone)]
|
|
struct Ground(isize);
|
|
|
|
#[derive(Component, Clone)]
|
|
enum Pipe {
|
|
Top,
|
|
Bottom,
|
|
}
|
|
|
|
#[derive(Component, Clone)]
|
|
struct Hitbox;
|
|
|
|
#[derive(Component, Clone)]
|
|
struct Batch(usize);
|
|
|
|
fn init_first_batches(mut commands: Commands) {
|
|
commands.spawn(Batch(0));
|
|
commands.spawn(Batch(1));
|
|
commands.spawn(Batch(2));
|
|
commands.spawn(Batch(3));
|
|
commands.spawn(Batch(4));
|
|
}
|
|
|
|
/// We have the concept of an environment "batch" which contains:
|
|
/// * The ground obstacle
|
|
/// * The pipe obstacle
|
|
/// * The pipe scoring sensor
|
|
///
|
|
/// These are all populated via `OnAdd` observers for the respected components.
|
|
/// This makes it much more concise to spawn each part of the environment.
|
|
fn populate_batch(
|
|
trigger: Trigger<OnAdd, Batch>,
|
|
batches: Query<&Batch>,
|
|
children: Query<&ChildOf>,
|
|
mut commands: Commands,
|
|
) {
|
|
// Only run this for top level batch entities,
|
|
// not children containing a reference to their batch, like hitboxes
|
|
if !children.contains(trigger.target()) {
|
|
let Batch(batch_id) = batches.get(trigger.target()).unwrap();
|
|
commands
|
|
.entity(trigger.target())
|
|
.insert((
|
|
Transform::from_xyz(500.0 * (*batch_id) as f32, 0.0, 0.0),
|
|
Visibility::Inherited,
|
|
))
|
|
.with_children(|parent| {
|
|
parent.spawn(Ground(-2));
|
|
parent.spawn(Ground(-1));
|
|
parent.spawn(Ground(0));
|
|
parent.spawn(Ground(1));
|
|
parent.spawn(Ground(2));
|
|
if *batch_id > 0 {
|
|
parent.spawn((Pipe::Top, Batch(*batch_id)));
|
|
parent.spawn((Pipe::Bottom, Batch(*batch_id)));
|
|
parent.spawn((Hitbox, Batch(*batch_id)));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
fn update_batch_position(
|
|
mut root_changes: Query<(&mut Transform, &Batch), (Changed<Batch>, Without<ChildOf>)>,
|
|
) {
|
|
root_changes.iter_mut().for_each(|(mut t, Batch(idx))| {
|
|
info!("Updating batch {:?}", idx);
|
|
t.translation.x = 500.0 * (*idx) as f32;
|
|
});
|
|
// todo!("Adjust pipe positions");
|
|
}
|
|
|
|
/// The ground population spawns a center peace and two pieces of ground
|
|
/// to the left and right of the center.
|
|
fn populate_ground(
|
|
trigger: Trigger<OnAdd, Ground>,
|
|
grounds: Query<&Ground>,
|
|
ground_assets: Res<GroundComponents>,
|
|
mut commands: Commands,
|
|
) {
|
|
let Ground(idx) = grounds.get(trigger.target()).unwrap();
|
|
debug!("populating ground {:?}", idx);
|
|
commands.entity(trigger.target()).insert((
|
|
ground_assets.material.clone(),
|
|
ground_assets.mesh.clone(),
|
|
Name::new("ground"),
|
|
RigidBody::Static,
|
|
Collider::rectangle(1.0, 1.0),
|
|
Transform::from_xyz(100.0 * (*idx) as f32, -300.0, -1.0).with_scale(Vec3::splat(100.0)),
|
|
));
|
|
}
|
|
|
|
/// Based on if this is a Top or Bottom pipe the placement changes
|
|
/// Otherwise this just spawns in the center of the batch.
|
|
fn populate_pipe(
|
|
trigger: Trigger<OnAdd, Pipe>,
|
|
pipes: Query<&Pipe>,
|
|
pipe_assets: Res<PipeComponents>,
|
|
mut commands: Commands,
|
|
) {
|
|
let pipe_t = match pipes.get(trigger.target()).unwrap() {
|
|
Pipe::Top => Transform::from_xyz(0.0, 200.0, -1.0).with_scale(Vec3::splat(100.0)),
|
|
Pipe::Bottom => Transform::from_xyz(0.0, -200.0, -1.0).with_scale(Vec3::splat(100.0)),
|
|
};
|
|
commands.entity(trigger.target()).insert((
|
|
pipe_t,
|
|
pipe_assets.material.clone(),
|
|
pipe_assets.mesh.clone(),
|
|
RigidBody::Static,
|
|
Collider::rectangle(1.0, 1.0),
|
|
Name::new("pipe"),
|
|
));
|
|
}
|
|
|
|
/// The hitbox should cover the entire height of the screen so if the player
|
|
/// passes between the pipes it registers a point
|
|
fn populate_hitbox(trigger: Trigger<OnAdd, Hitbox>, mut commands: Commands) {
|
|
commands.entity(trigger.target()).insert((
|
|
RigidBody::Static,
|
|
Collider::rectangle(1.0, 10.0),
|
|
Sensor,
|
|
CollisionEventsEnabled,
|
|
Name::new("hitbox"),
|
|
Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::splat(100.0)),
|
|
));
|
|
}
|
|
|
|
#[derive(Resource, Default)]
|
|
struct GroundComponents {
|
|
material: MeshMaterial2d<ColorMaterial>,
|
|
mesh: Mesh2d,
|
|
}
|
|
|
|
#[derive(Resource, Default)]
|
|
struct PipeComponents {
|
|
material: MeshMaterial2d<ColorMaterial>,
|
|
mesh: Mesh2d,
|
|
}
|
|
|
|
/// Initialize some materials and meshes used by the environment obstacles
|
|
fn init_obstacle_assets(
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
mut pipe_assets: ResMut<PipeComponents>,
|
|
mut ground_assets: ResMut<GroundComponents>,
|
|
server: Res<AssetServer>,
|
|
) {
|
|
pipe_assets.material = MeshMaterial2d(materials.add(ColorMaterial {
|
|
texture: Some(server.load("kenny.nl/1-bit-platformer-pack/tile_0247.png")),
|
|
color: GREEN.into(),
|
|
..default()
|
|
}));
|
|
pipe_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0)));
|
|
|
|
ground_assets.material = MeshMaterial2d(materials.add(ColorMaterial {
|
|
texture: Some(server.load("kenny.nl/1-bit-platformer-pack/tile_0088.png")),
|
|
color: BLACK.into(),
|
|
..default()
|
|
}));
|
|
|
|
ground_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0)));
|
|
}
|
|
|
|
fn init_ui(mut commands: Commands) {
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
align_self: AlignSelf::Center,
|
|
justify_self: JustifySelf::Center,
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
},
|
|
PlayerState::Stasis,
|
|
))
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
Text::new("Game Over...?"),
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
));
|
|
parent.spawn((
|
|
SyncResource::<Score>::default(),
|
|
Text::default(),
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
));
|
|
parent.spawn((
|
|
SyncResource::<Flaps>::default(),
|
|
Text::default(),
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
));
|
|
parent.spawn((
|
|
SyncResource::<RewindFrames>::default(),
|
|
Text::default(),
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
));
|
|
parent.spawn((
|
|
SyncResource::<Deaths>::default(),
|
|
Text::default(),
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
));
|
|
parent.spawn((
|
|
Text::new("Press R to Rewind"),
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
));
|
|
parent
|
|
.spawn((Node {
|
|
flex_direction: FlexDirection::Row,
|
|
justify_content: JustifyContent::SpaceEvenly,
|
|
..default()
|
|
},))
|
|
.with_children(|parent| {
|
|
fn quit_game(
|
|
_trigger: Trigger<Pointer<Click>>,
|
|
mut exit: EventWriter<AppExit>,
|
|
) {
|
|
warn!("Quitting game");
|
|
exit.write(AppExit::Success);
|
|
}
|
|
|
|
fn show_credits(
|
|
_trigger: Trigger<Pointer<Click>>,
|
|
mut state: ResMut<NextState<PlayerState>>,
|
|
) {
|
|
state.set(PlayerState::Credits);
|
|
}
|
|
|
|
parent
|
|
.spawn((
|
|
Button,
|
|
Node { ..default() },
|
|
children![Text::new("Credits")],
|
|
))
|
|
.observe(show_credits);
|
|
parent
|
|
.spawn((Button, Node { ..default() }, children![Text::new("Quit"),]))
|
|
.observe(quit_game);
|
|
});
|
|
});
|
|
|
|
fn hide_credits(_trigger: Trigger<Pointer<Click>>, mut state: ResMut<NextState<PlayerState>>) {
|
|
state.set(PlayerState::Stasis)
|
|
}
|
|
|
|
let credits_str = include_str!("../../../assets/flappy/CREDITS");
|
|
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
align_self: AlignSelf::Center,
|
|
justify_self: JustifySelf::Center,
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
},
|
|
PlayerState::Credits,
|
|
children![(Text::new(credits_str), TextLayout::new_with_justify(JustifyText::Center))],
|
|
))
|
|
.with_children(|parent| {
|
|
parent.spawn((Node {
|
|
align_self: AlignSelf::End,
|
|
justify_self: JustifySelf::End,
|
|
..default()
|
|
}, Button, children![Text::new("Close")]));
|
|
}).observe(hide_credits);
|
|
|
|
fn start_game(_trigger: Trigger<Pointer<Click>>, mut next: ResMut<NextState<PlayerState>>) {
|
|
next.set(PlayerState::Alive);
|
|
}
|
|
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
align_self: AlignSelf::Center,
|
|
justify_self: JustifySelf::Center,
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
},
|
|
Button,
|
|
// TODO: Add Pause (basically Stasis) state
|
|
PlayerState::Pause,
|
|
children![Text::new("Go!"),],
|
|
))
|
|
.observe(start_game);
|
|
|
|
commands
|
|
.spawn(Node {
|
|
align_self: AlignSelf::End,
|
|
justify_self: JustifySelf::Center,
|
|
flex_direction: FlexDirection::Row,
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn((
|
|
Node { ..default() },
|
|
Button,
|
|
children![Text::new("Rewind!"),],
|
|
))
|
|
.observe(start_rewind)
|
|
.observe(end_rewind);
|
|
|
|
parent
|
|
.spawn((Node { ..default() }, Button, children![Text::new("Flap!"),]))
|
|
.observe(flap_button);
|
|
});
|
|
|
|
commands.spawn((
|
|
Node {
|
|
align_self: AlignSelf::Start,
|
|
justify_self: JustifySelf::Center,
|
|
..default()
|
|
},
|
|
BackgroundColor(WHITE.into()),
|
|
SyncResource::<Score>::default(),
|
|
Text::default(),
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
|
));
|
|
}
|
|
|
|
fn start_rewind(_trigger: Trigger<Pointer<Pressed>>, mut next: ResMut<NextState<PlayerState>>) {
|
|
next.set(PlayerState::Rewind);
|
|
}
|
|
|
|
fn end_rewind(_trigger: Trigger<Pointer<Released>>, mut next: ResMut<NextState<PlayerState>>) {
|
|
next.set(PlayerState::Alive);
|
|
}
|
|
|
|
fn flap_button(
|
|
_trigger: Trigger<Pointer<Pressed>>,
|
|
mut commands: Commands,
|
|
bird: Single<Entity, With<Bird>>,
|
|
) {
|
|
let e = *bird;
|
|
debug!("Flapping {:?}", e);
|
|
commands.trigger_targets(Flap, e);
|
|
}
|
|
|
|
/// Pause the game when the player presses "Escape"
|
|
fn pause_game(mut next: ResMut<NextState<PlayerState>>) {
|
|
next.set(PlayerState::Pause);
|
|
}
|
|
|
|
fn un_pause_game(mut next: ResMut<NextState<PlayerState>>) {
|
|
next.set(PlayerState::Alive);
|
|
}
|
|
|
|
#[derive(Component, Clone, Event)]
|
|
struct Flap;
|
|
|
|
// Observer for flapping
|
|
fn flap(
|
|
trigger: Trigger<Flap>,
|
|
mut bird: Query<&mut ExternalImpulse, With<Bird>>,
|
|
mut flaps: ResMut<Flaps>,
|
|
) {
|
|
debug!("real flap for {:?}", trigger.target());
|
|
// Increment flap stat
|
|
flaps.0 += 1;
|
|
|
|
// Flap birds wings
|
|
if let Ok(mut f) = bird.get_mut(trigger.target()) {
|
|
f.apply_impulse(Vec2::Y * 5000.0 + Vec2::X * 1000.0);
|
|
}
|
|
}
|
|
|
|
fn flap_kb(
|
|
#[cfg(debug_assertions)] state: Res<State<PlayerState>>,
|
|
#[cfg(debug_assertions)] keycode: Res<ButtonInput<KeyCode>>,
|
|
birds: Query<Entity, With<Bird>>,
|
|
mut commands: Commands,
|
|
) {
|
|
debug_assert!(
|
|
matches!(state.get(), PlayerState::Alive),
|
|
"Only flap when playing"
|
|
);
|
|
debug_assert!(
|
|
keycode.just_pressed(KeyCode::Space),
|
|
"Only flap when space is just pressed"
|
|
);
|
|
|
|
birds.iter().for_each(|e| {
|
|
debug!("Flapping {:?}", e);
|
|
commands.trigger_targets(Flap, e);
|
|
});
|
|
}
|
|
|
|
fn toggle_rewind(keycode: Res<ButtonInput<KeyCode>>, mut next: ResMut<NextState<PlayerState>>) {
|
|
debug_assert!(
|
|
keycode.just_pressed(KeyCode::KeyR) || keycode.just_released(KeyCode::KeyR),
|
|
"Only toggle rewind when R is pressed"
|
|
);
|
|
|
|
if keycode.just_pressed(KeyCode::KeyR) {
|
|
debug!("Toggling rewind ON");
|
|
next.set(PlayerState::Rewind)
|
|
} else {
|
|
debug!("Toggling rewind OFF");
|
|
next.set(PlayerState::Alive)
|
|
}
|
|
}
|
|
|
|
fn record(
|
|
#[cfg(debug_assertions)] state: Res<State<PlayerState>>,
|
|
mut birds: Query<
|
|
(
|
|
&AccumulatedTranslation,
|
|
&LinearVelocity,
|
|
&AngularVelocity,
|
|
&ExternalAngularImpulse,
|
|
&ExternalForce,
|
|
&ExternalImpulse,
|
|
&ExternalTorque,
|
|
&Position,
|
|
&Rotation,
|
|
&mut Tape,
|
|
),
|
|
With<Bird>,
|
|
>,
|
|
) {
|
|
debug_assert!(
|
|
matches!(state.get(), PlayerState::Alive),
|
|
"Only record in the alive state"
|
|
);
|
|
|
|
birds
|
|
.iter_mut()
|
|
.for_each(|(at, lv, av, eai, ef, ei, et, p, r, mut tape)| {
|
|
tape.accumulated_translations.push(*at);
|
|
tape.linear_velocities.push(*lv);
|
|
tape.angular_velocities.push(*av);
|
|
tape.external_angular_impulses.push(*eai);
|
|
tape.external_forces.push(*ef);
|
|
tape.external_impulses.push(*ei);
|
|
tape.external_torques.push(*et);
|
|
tape.positions.push(*p);
|
|
tape.rotations.push(*r);
|
|
});
|
|
}
|
|
|
|
fn rewind(
|
|
#[cfg(debug_assertions)] state: Res<State<PlayerState>>,
|
|
mut birds: Query<
|
|
(
|
|
&mut AccumulatedTranslation,
|
|
&mut LinearVelocity,
|
|
&mut AngularVelocity,
|
|
&mut ExternalAngularImpulse,
|
|
&mut ExternalForce,
|
|
&mut ExternalImpulse,
|
|
&mut ExternalTorque,
|
|
&mut Position,
|
|
&mut Rotation,
|
|
&mut Tape,
|
|
),
|
|
With<Bird>,
|
|
>,
|
|
mut frames: ResMut<RewindFrames>,
|
|
) {
|
|
debug_assert!(
|
|
matches!(state.get(), PlayerState::Rewind),
|
|
"Only rewind in the rewinding state"
|
|
);
|
|
|
|
birds.iter_mut().for_each(
|
|
|(mut at, mut lv, mut av, mut eai, mut ef, mut ei, mut et, mut p, mut r, mut tape)| {
|
|
if !tape.positions.is_empty() {
|
|
at.0 = tape.accumulated_translations.pop().unwrap().0;
|
|
lv.0 = tape.linear_velocities.pop().unwrap().0;
|
|
av.0 = tape.angular_velocities.pop().unwrap().0;
|
|
eai.set_impulse(tape.external_angular_impulses.pop().unwrap().impulse());
|
|
ef.set_force(tape.external_forces.pop().unwrap().force());
|
|
ei.set_impulse(tape.external_impulses.pop().unwrap().impulse());
|
|
et.set_torque(tape.external_torques.pop().unwrap().torque());
|
|
p.0 = tape.positions.pop().unwrap().0;
|
|
*r = {
|
|
let curr = tape.rotations.pop().unwrap();
|
|
Rotation {
|
|
cos: curr.cos,
|
|
sin: curr.sin,
|
|
}
|
|
};
|
|
frames.0 += 1;
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
// PERF: May run more than necessary, should be event-driven on aabb intersection
|
|
fn detect_dead(
|
|
#[cfg(debug_assertions)] state: Res<State<PlayerState>>,
|
|
bird: Single<&ColliderAabb, With<Bird>>,
|
|
obstacles: Query<&ColliderAabb, Or<(With<Ground>, With<Pipe>)>>,
|
|
mut next: ResMut<NextState<PlayerState>>,
|
|
) {
|
|
debug_assert!(
|
|
matches!(state.get(), PlayerState::Alive),
|
|
"Only check if dead while alive"
|
|
);
|
|
|
|
if obstacles.iter().any(|obstacle| bird.intersects(obstacle)) {
|
|
next.set(PlayerState::Stasis);
|
|
}
|
|
}
|
|
|
|
fn alive_bird(
|
|
#[cfg(debug_assertions)] state: Res<State<PlayerState>>,
|
|
mut bird: Single<&mut RigidBody, With<Bird>>,
|
|
) {
|
|
debug_assert!(matches!(state.get(), PlayerState::Alive));
|
|
debug!("Setting bird to Dynamic");
|
|
**bird = RigidBody::Dynamic;
|
|
}
|
|
|
|
fn pause_bird(
|
|
state: Res<State<PlayerState>>,
|
|
mut bird: Single<&mut RigidBody, With<Bird>>,
|
|
mut deaths: ResMut<Deaths>,
|
|
) {
|
|
debug_assert!(!matches!(state.get(), PlayerState::Alive));
|
|
|
|
// Increment death count
|
|
if state.get() == &PlayerState::Stasis {
|
|
deaths.0 += 1
|
|
}
|
|
|
|
debug!("Setting bird to Static");
|
|
**bird = RigidBody::Static;
|
|
}
|
|
|
|
fn camera_follow_bird(
|
|
bird: Single<&Transform, (With<Bird>, Changed<Transform>)>,
|
|
mut camera: Single<&mut Transform, (With<Camera>, Without<Bird>)>,
|
|
) {
|
|
camera.translation.x = bird.translation.x;
|
|
}
|
|
|
|
/// How many pipes the player has passed
|
|
#[derive(Resource, Default)]
|
|
struct Score(usize);
|
|
|
|
impl Display for Score {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
writeln!(f, "Score: {}", self.0)
|
|
}
|
|
}
|
|
|
|
/// Number of frames that were rewound
|
|
#[derive(Resource, Default)]
|
|
struct RewindFrames(usize);
|
|
|
|
impl Display for RewindFrames {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
writeln!(f, "Frames Rewound: {}", self.0)
|
|
}
|
|
}
|
|
|
|
// Track number of times player flapped their wings
|
|
#[derive(Resource, Default)]
|
|
struct Flaps(usize);
|
|
|
|
impl Display for Flaps {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
writeln!(f, "Flaps: {}", self.0)
|
|
}
|
|
}
|
|
|
|
// Track number of times player died
|
|
#[derive(Resource, Default)]
|
|
struct Deaths(usize);
|
|
|
|
impl Display for Deaths {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
writeln!(f, "Deaths: {}", self.0)
|
|
}
|
|
}
|
|
|
|
// TODO: Scoring is bugged, need to make it correct.
|
|
fn manage_score(
|
|
trigger: Trigger<OnCollisionEnd>,
|
|
state: Res<State<PlayerState>>,
|
|
bird: Query<Entity, With<Bird>>,
|
|
hitboxes: Query<&Batch, With<Hitbox>>,
|
|
mut score: ResMut<Score>,
|
|
) {
|
|
let a = trigger.target();
|
|
let b = trigger.collider;
|
|
if bird.contains(a) && hitboxes.contains(b) {
|
|
debug!("Hit event while {:?}", state.get());
|
|
let Batch(id) = hitboxes.get(b).unwrap();
|
|
score.0 = match state.get() {
|
|
PlayerState::Alive => *id,
|
|
PlayerState::Rewind => *id - 1,
|
|
_ => *id, // HOW???
|
|
}
|
|
}
|
|
}
|
|
|
|
fn debug_collision_events(
|
|
mut end: EventReader<CollisionEnded>,
|
|
mut start: EventReader<CollisionStarted>,
|
|
state: Res<State<PlayerState>>,
|
|
) {
|
|
start.read().for_each(|CollisionStarted(a, b)| {
|
|
debug!("Collision started between {a} and {b} in {:?}", state.get());
|
|
});
|
|
end.read().for_each(|CollisionEnded(a, b)| {
|
|
debug!("Collision ended between {a} and {b} in {:?}", state.get());
|
|
});
|
|
}
|
|
|
|
fn debug_collision_start_observations(
|
|
trigger: Trigger<OnCollisionStart>,
|
|
state: Res<State<PlayerState>>,
|
|
) {
|
|
debug!(
|
|
"Collision started between {} and {} in {:?}",
|
|
trigger.target(),
|
|
trigger.collider,
|
|
state.get()
|
|
);
|
|
}
|
|
|
|
fn debug_collision_end_observations(
|
|
trigger: Trigger<OnCollisionEnd>,
|
|
state: Res<State<PlayerState>>,
|
|
) {
|
|
debug!(
|
|
"Collision end between {} and {} in {:?}",
|
|
trigger.target(),
|
|
trigger.collider,
|
|
state.get()
|
|
);
|
|
}
|
|
|
|
/// When the player moves forward while alive
|
|
/// spawn more batches and despawn old batches
|
|
fn manage_batches(
|
|
trigger: Trigger<OnCollisionStart>,
|
|
state: Res<State<PlayerState>>,
|
|
bird: Query<Entity, With<Bird>>,
|
|
batches: Query<(Entity, &Batch)>,
|
|
mut commands: Commands,
|
|
) {
|
|
debug_assert!(
|
|
matches!(state.get(), PlayerState::Alive) || matches!(state.get(), PlayerState::Rewind)
|
|
);
|
|
let a = trigger.target();
|
|
let b = trigger.collider;
|
|
if bird.contains(a)
|
|
&& let Ok((_e, Batch(idx))) = batches.get(b)
|
|
&& *idx > 2
|
|
{
|
|
info!("Managing batches in player state {:?}", state.get());
|
|
let (old_batch_idx, new_batch_idx) = match state.get() {
|
|
PlayerState::Alive => (idx - 2, idx + 2),
|
|
PlayerState::Rewind => (idx + 2, idx - 2),
|
|
_ => panic!("Should not happen!"),
|
|
};
|
|
// Find all entities with the old batch and adjust them
|
|
batches
|
|
.iter()
|
|
.filter_map(|(e, batch)| (batch.0 == old_batch_idx).then_some(e))
|
|
.for_each(|e| {
|
|
info!("Moving batch {} -> {}", old_batch_idx, new_batch_idx);
|
|
commands.entity(e).insert(Batch(new_batch_idx));
|
|
})
|
|
}
|
|
}
|
|
|
|
fn update_tooltip(
|
|
mut query: Query<(
|
|
&mut ToolTip,
|
|
Option<&LinearVelocity>,
|
|
Option<&Batch>,
|
|
Entity,
|
|
)>,
|
|
) {
|
|
query.iter_mut().for_each(|(mut tt, lv, b, _e)| {
|
|
// Add Linear Velocity if present on entity
|
|
lv.iter().for_each(|it| {
|
|
tt.insert("Velocity", format!("{}", it.0));
|
|
});
|
|
// Add Batch ID to if present
|
|
b.iter().for_each(|it| {
|
|
tt.insert("Batch", format!("{}", it.0));
|
|
});
|
|
});
|
|
}
|