add enemy spawners
This commit is contained in:
parent
103ff0d514
commit
cd19c1c321
6 changed files with 213 additions and 3 deletions
101
Cargo.lock
generated
101
Cargo.lock
generated
|
@ -732,12 +732,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.25.0"
|
||||
|
@ -1054,7 +1085,9 @@ name = "bin-projekt"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bevy_rand",
|
||||
"bevy_rapier2d",
|
||||
"rand",
|
||||
"some_bevy_tools",
|
||||
]
|
||||
|
||||
|
@ -2878,6 +2911,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"
|
||||
|
@ -2923,6 +2962,58 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
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_pcg"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "range-alloc"
|
||||
version = "0.1.3"
|
||||
|
@ -4143,6 +4234,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"
|
||||
|
|
|
@ -8,7 +8,9 @@ edition = "2021"
|
|||
[dependencies]
|
||||
# bevy = { version = "0.13.0", features = ["dynamic_linking"] }
|
||||
bevy = "0.13"
|
||||
bevy_rand = { version = "0.6.0", features = ["wyrand"] }
|
||||
bevy_rapier2d = { version = "0.25.0", features = [ "simd-stable", "debug-render-2d" ] }
|
||||
rand = { version = "0.8.5", features = ["small_rng"] }
|
||||
some_bevy_tools = "0.2.4"
|
||||
|
||||
# Enable a small amount of optimization in debug mode
|
||||
|
|
BIN
assets/sprites/enemy.png
Normal file
BIN
assets/sprites/enemy.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
94
src/enemy.rs
Normal file
94
src/enemy.rs
Normal file
|
@ -0,0 +1,94 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_rand::{prelude::WyRand, resource::GlobalEntropy};
|
||||
use rand::Rng;
|
||||
|
||||
use crate::METER;
|
||||
|
||||
#[derive(Debug, Event)]
|
||||
pub struct SpawnEvent {
|
||||
pos: (f32, f32),
|
||||
spawner: Entity,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Spawner {
|
||||
pos: (f32, f32),
|
||||
size: (f32, f32),
|
||||
spawn_cooldown: Timer,
|
||||
cur_enemies: u8,
|
||||
max_enemies: u8,
|
||||
amount_range: std::ops::Range<u8>,
|
||||
}
|
||||
|
||||
pub fn enemies_plugin(app: &mut App) {
|
||||
app.add_event::<SpawnEvent>()
|
||||
.add_systems(Startup, setup_spawners)
|
||||
.add_systems(Update, (spawn_enemies, do_spawn));
|
||||
}
|
||||
|
||||
pub fn setup_spawners(mut commands: Commands) {
|
||||
commands.spawn(Spawner {
|
||||
pos: (-6.5 * METER, 5.5 * METER),
|
||||
size: (14. * METER, 1.5 * METER),
|
||||
spawn_cooldown: Timer::from_seconds(5., TimerMode::Repeating),
|
||||
cur_enemies: 0,
|
||||
max_enemies: 10,
|
||||
amount_range: 2..4,
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Enemy;
|
||||
|
||||
// TODO: waves n stuff
|
||||
// and gamestate lolz
|
||||
pub fn spawn_enemies(
|
||||
mut ev_spawn: EventWriter<SpawnEvent>,
|
||||
mut rng: ResMut<GlobalEntropy<WyRand>>,
|
||||
mut query: Query<(Entity, &mut Spawner)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (entity, mut spawner) in query.iter_mut() {
|
||||
if spawner.spawn_cooldown.tick(time.delta()).finished()
|
||||
&& spawner.cur_enemies <= spawner.max_enemies
|
||||
{
|
||||
// TODO: randomness
|
||||
(0..rng.gen_range(spawner.amount_range.clone())).for_each(|_| {
|
||||
if spawner.cur_enemies < spawner.max_enemies {
|
||||
spawner.cur_enemies += 1;
|
||||
ev_spawn.send(SpawnEvent {
|
||||
pos: (
|
||||
rng.gen_range(
|
||||
(spawner.pos.0 + 0.5 * METER)
|
||||
..((spawner.pos.0) + (spawner.size.0 - 0.5 * METER)),
|
||||
),
|
||||
rng.gen_range(
|
||||
(spawner.pos.1 + 0.5 * METER)
|
||||
..((spawner.pos.1) + (spawner.size.1 - 0.5 * METER)),
|
||||
),
|
||||
),
|
||||
spawner: entity,
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_spawn(
|
||||
mut ev_spawn: EventReader<SpawnEvent>,
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
) {
|
||||
for ev in ev_spawn.read() {
|
||||
println!("spawn!: {} {}", ev.pos.0 / METER, ev.pos.1 / METER);
|
||||
commands.spawn((
|
||||
Enemy,
|
||||
SpriteBundle {
|
||||
texture: asset_server.load("sprites/enemy.png"),
|
||||
transform: Transform::from_xyz(ev.pos.0, ev.pos.1, 1.),
|
||||
..default()
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
10
src/main.rs
10
src/main.rs
|
@ -1,19 +1,27 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_rand::{plugin::EntropyPlugin, prelude::WyRand};
|
||||
use bevy_rapier2d::prelude::*;
|
||||
use enemy::enemies_plugin;
|
||||
use player::{add_player, move_player};
|
||||
use rand::SeedableRng;
|
||||
use some_bevy_tools::{camera_2d::Camera2DPlugin, controller_2d::SimpleTopDownControllerPlugin};
|
||||
const METER: f32 = 64.;
|
||||
|
||||
mod enemy;
|
||||
mod player;
|
||||
mod scene;
|
||||
|
||||
fn main() {
|
||||
let r = rand::rngs::SmallRng::from_seed(*b"Meow UwU nyaaa~ mrrrp~ meew miau");
|
||||
App::new()
|
||||
.insert_resource(RapierConfiguration {
|
||||
gravity: Vec2::new(0.0, 0.0),
|
||||
..Default::default()
|
||||
})
|
||||
.add_plugins(EntropyPlugin::<WyRand>::default())
|
||||
.add_plugins(enemies_plugin)
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(64.0))
|
||||
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(METER))
|
||||
.add_plugins(RapierDebugRenderPlugin::default())
|
||||
.add_plugins(Camera2DPlugin)
|
||||
.add_plugins(SimpleTopDownControllerPlugin)
|
||||
|
|
|
@ -3,7 +3,11 @@ use bevy::{
|
|||
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
|
||||
};
|
||||
use bevy_rapier2d::prelude::*;
|
||||
const METER: f32 = 64.;
|
||||
|
||||
use crate::METER;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Wall;
|
||||
|
||||
pub fn setup_scene(
|
||||
mut commands: Commands,
|
||||
|
@ -24,7 +28,8 @@ pub fn setup_scene(
|
|||
for (shape_, pos) in scene_objs {
|
||||
let shape = Mesh2dHandle(meshes.add(shape_));
|
||||
commands
|
||||
.spawn(MaterialMesh2dBundle {
|
||||
.spawn(Wall)
|
||||
.insert(MaterialMesh2dBundle {
|
||||
mesh: shape,
|
||||
material: materials.add(Color::rgb(1., 0., 0.)),
|
||||
transform: Transform::from_xyz(pos.0, pos.1, 1.),
|
||||
|
|
Loading…
Reference in a new issue