use bevy::prelude::*;
use bevy_ecs_tiled::prelude::*;
use bevy_rapier2d::prelude::*;
mod helper;
const MOVE_SPEED: f32 = 200.;
const GRAVITY_SCALE: f32 = 10.0;
fn main() {
App::new()
.add_plugins(DefaultPlugins.build().set(ImagePlugin::default_nearest()))
.add_plugins(TiledPlugin::default())
.add_plugins(helper::HelperPlugin)
.add_plugins(TiledPhysicsPlugin::<TiledPhysicsRapierBackend>::default())
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
.add_plugins(RapierDebugRenderPlugin::default())
.add_systems(Startup, startup)
.add_systems(Update, move_player)
.run();
}
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2d);
commands.spawn(Text(String::from(
"Move the ball using arrow keys or try to rotate the map!",
)));
commands
.spawn((
TiledMap(asset_server.load("maps/orthogonal/multiple_layers_with_colliders.tmx")),
TilemapAnchor::Center,
))
.observe(
|_: Trigger<TiledEvent<MapCreated>>, mut commands: Commands| {
commands.spawn((
RigidBody::Dynamic,
PlayerMarker,
Name::new("PlayerControlledObject (Rapier physics)"),
Collider::ball(10.),
Velocity::zero(),
GravityScale(GRAVITY_SCALE),
Transform::from_xyz(50., -50., 0.),
));
},
)
.observe(
|trigger: Trigger<TiledEvent<ColliderCreated>>, mut commands: Commands| {
commands
.entity(trigger.event().origin)
.insert(RigidBody::Fixed);
},
);
}
#[derive(Default, Clone, Component)]
pub struct PlayerMarker;
fn move_player(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut player: Query<&mut Velocity, With<PlayerMarker>>,
) {
for mut rb_vels in player.iter_mut() {
let mut direction = Vec2::ZERO;
if keyboard_input.pressed(KeyCode::ArrowRight) {
direction += Vec2::new(1.0, 0.0);
}
if keyboard_input.pressed(KeyCode::ArrowLeft) {
direction -= Vec2::new(1.0, 0.0);
}
if keyboard_input.pressed(KeyCode::ArrowUp) {
direction += Vec2::new(0.0, 1.0);
}
if keyboard_input.pressed(KeyCode::ArrowDown) {
direction -= Vec2::new(0.0, 1.0);
}
if direction != Vec2::ZERO {
direction /= direction.length();
}
rb_vels.linvel = direction * MOVE_SPEED;
}
}