Проектная работа - WIP

This commit is contained in:
13 changed files with 149 additions and 225 deletions
+6 -25
View File
@@ -14,10 +14,7 @@ fn main() {
// Observers are systems that run when an event is "triggered". This observer runs whenever
// `ExplodeMines` is triggered.
.add_observer(
|explode_mines: On<ExplodeMines>,
mines: Query<&Mine>,
index: Res<SpatialIndex>,
mut commands: Commands| {
|explode_mines: On<ExplodeMines>, mines: Query<&Mine>, index: Res<SpatialIndex>, mut commands: Commands| {
// Access resources
for entity in index.get_nearby(explode_mines.pos) {
// Run queries
@@ -47,10 +44,7 @@ struct Mine {
impl Mine {
fn random(rand: &mut ChaCha8Rng) -> Self {
Mine {
pos: Vec2::new(
(rand.random::<f32>() - 0.5) * 1200.0,
(rand.random::<f32>() - 0.5) * 600.0,
),
pos: Vec2::new((rand.random::<f32>() - 0.5) * 1200.0, (rand.random::<f32>() - 0.5) * 600.0),
size: 4.0 + rand.random::<f32>() * 16.0,
}
}
@@ -116,20 +110,14 @@ fn setup(mut commands: Commands) {
fn on_add_mine(add: On<Add, Mine>, query: Query<&Mine>, mut index: ResMut<SpatialIndex>) {
let mine = query.get(add.entity).unwrap();
let tile = (
(mine.pos.x / CELL_SIZE).floor() as i32,
(mine.pos.y / CELL_SIZE).floor() as i32,
);
let tile = ((mine.pos.x / CELL_SIZE).floor() as i32, (mine.pos.y / CELL_SIZE).floor() as i32);
index.map.entry(tile).or_default().insert(add.entity);
}
// Remove despawned mines from our index
fn on_remove_mine(remove: On<Remove, Mine>, query: Query<&Mine>, mut index: ResMut<SpatialIndex>) {
let mine = query.get(remove.entity).unwrap();
let tile = (
(mine.pos.x / CELL_SIZE).floor() as i32,
(mine.pos.y / CELL_SIZE).floor() as i32,
);
let tile = ((mine.pos.x / CELL_SIZE).floor() as i32, (mine.pos.y / CELL_SIZE).floor() as i32);
index.map.entry(tile).and_modify(|set| {
set.remove(&remove.entity);
});
@@ -153,11 +141,7 @@ fn explode_mine(explode: On<Explode>, query: Query<&Mine>, mut commands: Command
// Draw a circle for each mine using `Gizmos`
fn draw_shapes(mut gizmos: Gizmos, mines: Query<&Mine>) {
for mine in &mines {
gizmos.circle_2d(
mine.pos,
mine.size,
Color::hsl((mine.size - 4.0) / 16.0 * 360.0, 1.0, 0.8),
);
gizmos.circle_2d(mine.pos, mine.size, Color::hsl((mine.size - 4.0) / 16.0 * 360.0, 1.0, 0.8));
}
}
@@ -194,10 +178,7 @@ const CELL_SIZE: f32 = 64.0;
impl SpatialIndex {
// Lookup all entities within adjacent cells of our spatial index
fn get_nearby(&self, pos: Vec2) -> Vec<Entity> {
let tile = (
(pos.x / CELL_SIZE).floor() as i32,
(pos.y / CELL_SIZE).floor() as i32,
);
let tile = ((pos.x / CELL_SIZE).floor() as i32, (pos.y / CELL_SIZE).floor() as i32);
let mut nearby = Vec::new();
for x in -1..2 {
for y in -1..2 {