dropping objects exist now
This commit is contained in:
parent
1176a8ec08
commit
c73b3c7066
5 changed files with 234 additions and 18 deletions
95
Cargo.lock
generated
95
Cargo.lock
generated
|
@ -742,12 +742,43 @@ dependencies = [
|
|||
"thread_local",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_prng"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08497116068623556e67e59b0204f5d9d8e7a2d4fd378f87c9c798f7af93f236"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"rand_pcg",
|
||||
"rand_xoshiro",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"wyrand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_ptr"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8050e2869fe341db6874203b5a01ff12673807a2c7c80cb829f6c7bea6997268"
|
||||
|
||||
[[package]]
|
||||
name = "bevy_rand"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c98c3be37468a6008cf0bb392e3decff0fc062898570b0ea4543fa6e9355bf7a"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bevy_prng",
|
||||
"getrandom",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_rapier2d"
|
||||
version = "0.26.0"
|
||||
|
@ -1064,8 +1095,9 @@ name = "bin-projekt-evader"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bevy_rand",
|
||||
"bevy_rapier2d",
|
||||
"noise",
|
||||
"rand",
|
||||
"some_bevy_tools",
|
||||
]
|
||||
|
||||
|
@ -2520,17 +2552,6 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "noise"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6da45c8333f2e152fc665d78a380be060eb84fad8ca4c9f7ac8ca29216cff0cc"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"rand",
|
||||
"rand_xorshift",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
|
@ -2907,6 +2928,12 @@ dependencies = [
|
|||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "presser"
|
||||
version = "0.3.1"
|
||||
|
@ -2958,22 +2985,50 @@ version = "0.8.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_xorshift"
|
||||
version = "0.3.0"
|
||||
name = "rand_pcg"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f"
|
||||
checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e"
|
||||
dependencies = [
|
||||
"rand_core",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_xoshiro"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa"
|
||||
dependencies = [
|
||||
"rand_core",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -4197,6 +4252,16 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wyrand"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7dadc56f0dc130aa22c0d2fe303ccee3a2ad429c87d257642e96017920c66ed0"
|
||||
dependencies = [
|
||||
"rand_core",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "x11-dl"
|
||||
version = "2.21.0"
|
||||
|
|
|
@ -5,8 +5,9 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
bevy = {version = "0.13.2", features = ["dynamic_linking"]}
|
||||
bevy_rand = { version = "0.6.0", features = ["wyrand"] }
|
||||
bevy_rapier2d = "0.26.0"
|
||||
noise = "0.9.0"
|
||||
rand = "0.8.5"
|
||||
some_bevy_tools = "0.2.4"
|
||||
|
||||
[profile.dev]
|
||||
|
|
139
src/drops.rs
Normal file
139
src/drops.rs
Normal 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();
|
||||
}
|
||||
}
|
12
src/main.rs
12
src/main.rs
|
@ -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;
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue