Advertisement
443eb9

Untitled

Dec 23rd, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.54 KB | None | 0 0
  1. use bevy::{
  2.     app::{App, Startup, Update},
  3.     core_pipeline::core_2d::{Camera2d, Camera2dBundle},
  4.     ecs::system::{Commands, Res},
  5.     input::{keyboard::KeyCode, Input},
  6.     math::{UVec2, Vec2, Vec3, Vec4},
  7.     render::color::Color,
  8.     sprite::{Sprite, SpriteBundle},
  9.     transform::{components::Transform, TransformBundle},
  10.     DefaultPlugins,
  11. };
  12. use bevy_entitiles::{
  13.     math::FillArea,
  14.     tilemap::{
  15.         layer::TileLayer,
  16.         map::TilemapBuilder,
  17.         tile::{TileBuilder, TileType},
  18.     },
  19.     EntiTilesPlugin,
  20. };
  21. use bevy_xpbd_2d::{
  22.     components::{Collider, Friction, Mass, RigidBody},
  23.     resources::Gravity,
  24. };
  25. use helpers::EntiTilesDebugPlugin;
  26.  
  27. mod helpers;
  28.  
  29. fn main() {
  30.     App::new()
  31.         .add_plugins((DefaultPlugins, EntiTilesPlugin, EntiTilesDebugPlugin))
  32.         .add_systems(Startup, setup)
  33.         .add_systems(Update, tilemap)
  34.         .insert_resource(Gravity(Vec2::new(0., -98.)))
  35.         .run();
  36. }
  37.  
  38. fn setup(mut commands: Commands) {
  39.     commands.spawn(Camera2dBundle::default());
  40. }
  41.  
  42. fn tilemap(mut commands: Commands, input: Res<Input<KeyCode>>) {
  43.     if !input.just_pressed(KeyCode::Space) {
  44.         return;
  45.     }
  46.  
  47.     let mut tilemap = TilemapBuilder::new(
  48.         TileType::Square,
  49.         UVec2::new(8, 1),
  50.         Vec2::new(16., 16.),
  51.         "".to_string(),
  52.     )
  53.     .build(&mut commands);
  54.  
  55.     tilemap.fill_rect(
  56.         &mut commands,
  57.         FillArea::full(&tilemap),
  58.         TileBuilder::new().with_layer(0, TileLayer::new().with_texture_index(0)),
  59.     );
  60.  
  61.     commands.entity(tilemap.id()).insert(tilemap);
  62.     commands.spawn((
  63.         Collider::cuboid(128., 16.),
  64.         TransformBundle {
  65.             local: Transform::from_translation(Vec3::new(64., 8., 0.)),
  66.             ..Default::default()
  67.         },
  68.         RigidBody::Static,
  69.         Friction {
  70.             dynamic_coefficient: 0.5,
  71.             static_coefficient: 0.5,
  72.             ..Default::default()
  73.         },
  74.     ));
  75.  
  76.     commands.spawn((
  77.         SpriteBundle {
  78.             sprite: Sprite {
  79.                 color: Color::BLUE,
  80.                 custom_size: Some(Vec2::new(16., 16.)),
  81.                 ..Default::default()
  82.             },
  83.             transform: Transform::from_translation(Vec3::new(64., 64., 0.)),
  84.             ..Default::default()
  85.         },
  86.         RigidBody::Dynamic,
  87.         Friction {
  88.             dynamic_coefficient: 0.5,
  89.             static_coefficient: 0.5,
  90.             ..Default::default()
  91.         },
  92.         Mass(20.),
  93.         Collider::cuboid(16., 16.),
  94.     ));
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement