Skip to content

Commit

Permalink
Simple physics: we fall, we hit object, we stop
Browse files Browse the repository at this point in the history
  • Loading branch information
bas-ie committed Dec 29, 2024
1 parent 1c28707 commit 3988aee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/dev_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub(super) fn plugin(app: &mut App) {

// Avian
app.add_plugins((FrameTimeDiagnosticsPlugin, PhysicsDebugPlugin::default()));
app.insert_gizmo_config(PhysicsGizmos::default(), GizmoConfig {
// Off by default, enables with toggle key
enabled: false,
..default()
});
}

const TOGGLE_KEY: KeyCode = KeyCode::Backquote;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Plugin for GamePlugin {
}),
);

app.add_plugins(PhysicsPlugins::default());
app.add_plugins(PhysicsPlugins::default().with_length_unit(20.0));

app.add_plugins((assets::plugin, screens::plugin, game::plugin));

Expand Down
52 changes: 48 additions & 4 deletions src/screens/ingame/playing.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
use avian2d::prelude::*;
use avian2d::{
math::{Scalar, Vector},
prelude::*,
};
use bevy::prelude::*;

use crate::game::Game;
use crate::screens::Screen;

pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(Game::Playing), init);
app.add_systems(OnEnter(Screen::InGame), init);
app.insert_resource(Gravity(Vector::NEG_Y * 1000.0));
}

fn init() {}
#[derive(Component, Debug)]
struct Player;

#[derive(Component)]
struct MovementSpeed(Scalar);

fn init(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut time: ResMut<Time<Physics>>,
) {
let platform = Sprite {
color: Color::srgba(0.7, 0.7, 0.8, 0.25),
custom_size: Some(Vec2::splat(50.0)),
..default()
};
commands.spawn((
Name::new("Platform"),
platform.clone(),
Collider::rectangle(50., 50.),
RigidBody::Static,
StateScoped(Screen::InGame),
Transform::from_xyz(0., 16. * 6., 0.).with_scale(Vec3::new(10., 0.5, 10.)),
));

commands.spawn((
Name::new("Player"),
Player,
Collider::circle(20.),
LockedAxes::ROTATION_LOCKED,
Mesh2d(meshes.add(Circle::new(20.))),
MeshMaterial2d(materials.add(Color::srgb(0.2, 0.7, 0.9))),
MovementSpeed(250.),
Restitution::ZERO.with_combine_rule(CoefficientCombine::Min),
RigidBody::Dynamic,
StateScoped(Screen::InGame),
Transform::from_xyz(0., 200., 0.),
));
time.unpause();
}

0 comments on commit 3988aee

Please sign in to comment.