dropping objects exist now

This commit is contained in:
Schrottkatze 2024-05-10 19:47:40 +02:00
parent 1176a8ec08
commit c73b3c7066
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
5 changed files with 234 additions and 18 deletions

139
src/drops.rs Normal file
View file

@ -0,0 +1,139 @@
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
utils::HashSet,
};
use bevy_rand::prelude::*;
use bevy_rapier2d::prelude::*;
use rand::Rng;
use crate::{player::Player, scene::SceneObj, METER};
#[derive(Component)]
struct SpawnTimer(pub Timer);
#[derive(Component)]
struct DroppedCrate;
#[derive(Debug, Event)]
struct CrateDropEvent {
pos: f32,
}
pub fn spawner_plugin(app: &mut App) {
app.add_plugins(EntropyPlugin::<WyRand>::default())
.add_event::<CrateDropEvent>()
.add_event::<CrateCollision>()
.add_systems(Startup, add_timer)
.add_systems(
Update,
(drop_crates, do_drop, crate_collisions, delete_on_env_coll),
);
}
fn add_timer(mut commands: Commands) {
commands.spawn(SpawnTimer(Timer::from_seconds(2., TimerMode::Repeating)));
}
fn drop_crates(
mut ev_drop: EventWriter<CrateDropEvent>,
mut rng: ResMut<GlobalEntropy<WyRand>>,
mut spawn_timer: Query<&mut SpawnTimer>,
time: Res<Time>,
) {
let mut timer = spawn_timer.single_mut();
if timer.0.tick(time.delta()).finished() {
ev_drop.send(CrateDropEvent {
pos: rng.gen_range(-7f32..7f32),
});
}
}
fn do_drop(
mut ev_drop: EventReader<CrateDropEvent>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let rect = Mesh2dHandle(meshes.add(Rectangle::new(1. * METER, 1. * METER)));
for ev in ev_drop.read() {
println!("ev: {ev:?}");
commands
.spawn(DroppedCrate)
.insert(MaterialMesh2dBundle {
mesh: rect.clone(),
material: materials.add(Color::rgb(0.9, 0.8, 0.2)),
transform: Transform::from_xyz(ev.pos * METER, 12. * METER, 0.),
..default()
})
.insert(ActiveEvents::COLLISION_EVENTS)
.insert((
RigidBody::Dynamic,
Collider::cuboid(0.5 * METER, 0.5 * METER),
));
}
}
#[derive(Event)]
struct CrateCollision {
coll_crate: Entity,
with: CollisionType,
}
enum CollisionType {
Player(Entity),
Scene(Entity),
}
fn crate_collisions(
mut player: Query<Entity, With<Player>>,
mut ev_colls: EventWriter<CrateCollision>,
drops: Query<Entity, With<DroppedCrate>>,
scene_objs: Query<Entity, With<SceneObj>>,
mut collision_events: EventReader<CollisionEvent>,
) {
let p = player.single_mut();
let crates = drops.iter().collect::<HashSet<_>>();
let scene_objs = scene_objs.iter().collect::<HashSet<_>>();
for collision_event in collision_events.read() {
match collision_event {
CollisionEvent::Started(e1, e2, _) if crates.contains(e1) || crates.contains(e2) => {
let (coll_type, crate_) = if scene_objs.contains(e1) {
(CollisionType::Scene(*e1), e2)
} else if scene_objs.contains(e2) {
(CollisionType::Scene(*e2), e1)
} else if *e2 == p {
(CollisionType::Player(*e2), e1)
} else if *e1 == p {
(CollisionType::Player(*e1), e2)
} else if crates.contains(e1) && crates.contains(e2) {
continue;
} else {
// there can't be anything else to collide with,
// only either scene objects or the player
eprintln!("e1: {e1:?}, e2: {e2:?}");
unreachable!()
};
ev_colls.send(CrateCollision {
coll_crate: *crate_,
with: coll_type,
});
}
_ => {}
}
}
}
fn delete_on_env_coll(mut ev_colls: EventReader<CrateCollision>, mut commands: Commands) {
for CrateCollision { coll_crate, with } in ev_colls.read() {
if let CollisionType::Player(_) = with {
continue;
}
// let CollisionType::Scene(obj) = with else {
// unreachable!()
// };
commands.entity(*coll_crate).despawn();
}
}

View file

@ -3,14 +3,23 @@ use bevy::{
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_rapier2d::prelude::*;
use drops::spawner_plugin;
const METER: f32 = 48.;
fn main() {
let mut rapier_config = RapierConfiguration::new(METER * 2.5);
rapier_config.timestep_mode = TimestepMode::Variable {
max_dt: 1.0 / 60.0,
time_scale: 1.0,
substeps: 4,
};
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(RapierConfiguration::new(METER * 2.5))
// this does things with gravity
.insert_resource(rapier_config)
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(METER))
.add_plugins(RapierDebugRenderPlugin::default())
.add_plugins((spawner_plugin))
.add_systems(Startup, (scene::setup_scene, setup_cam, player::add_player))
.add_systems(
Update,
@ -26,5 +35,6 @@ fn setup_cam(mut commands: Commands) {
});
}
mod drops;
mod player;
mod scene;

View file

@ -91,7 +91,8 @@ pub fn move_player(
if kb_input.pressed(KeyCode::KeyW) && player.grounded {
moved = true;
*vel = Velocity::linear(Vec2::new(0., 9. * METER));
*vel = Velocity::linear(Vec2::new(0., 9. * METER));
// *vel = Velocity::linear(Vec2::new(0., 9.81 * METER));
}
if moved {