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

This commit is contained in:
13 changed files with 149 additions and 225 deletions
+1 -4
View File
@@ -1,10 +1,7 @@
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(HelloPlugin)
.run();
App::new().add_plugins(DefaultPlugins).add_plugins(HelloPlugin).run();
}
#[derive(Component)]
+3 -13
View File
@@ -99,11 +99,7 @@ fn draw(
) {
if *i == 0 {
// Generate a random color on first run.
*draw_color = Color::linear_rgb(
seeded_rng.0.random(),
seeded_rng.0.random(),
seeded_rng.0.random(),
);
*draw_color = Color::linear_rgb(seeded_rng.0.random(), seeded_rng.0.random(), seeded_rng.0.random());
}
// Get the image from Bevy's asset storage.
@@ -126,17 +122,11 @@ fn draw(
// If the old color is our current color, change our drawing color.
let tolerance = 1.0 / 255.0;
if old_color.distance(&draw_color) <= tolerance {
*draw_color = Color::linear_rgb(
seeded_rng.0.random(),
seeded_rng.0.random(),
seeded_rng.0.random(),
);
*draw_color = Color::linear_rgb(seeded_rng.0.random(), seeded_rng.0.random(), seeded_rng.0.random());
}
// Set the new color, but keep old alpha value from image.
image
.set_color_at(x, y, draw_color.with_alpha(old_color.alpha()))
.unwrap();
image.set_color_at(x, y, draw_color.with_alpha(old_color.alpha())).unwrap();
*i += 1;
}
+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 {
+8 -34
View File
@@ -1,4 +1,3 @@
use bevy::math::bounding::{Aabb2d, BoundingCircle, BoundingVolume, IntersectsVolume};
use bevy::prelude::*;
@@ -7,10 +6,7 @@ fn main() {
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::srgb(0.8, 0.8, 1.0)))
.add_systems(Startup, setup)
.add_systems(
FixedUpdate,
(move_paddle, apply_velocity, check_for_collisions),
)
.add_systems(FixedUpdate, (move_paddle, apply_velocity, check_for_collisions))
.run();
}
@@ -40,11 +36,7 @@ struct Ball;
#[derive(Component)]
struct Collider;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<ColorMaterial>>) {
commands.spawn(Camera2d);
let paddle_y = BOTTOM_WALL + PADDLE_GAP_Y;
@@ -77,11 +69,7 @@ fn setup(
commands.spawn(Wall::new(WallLocation::Bottom));
}
fn move_paddle(
input: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
mut paddle_transform: Single<&mut Transform, With<Paddle>>,
) {
fn move_paddle(input: Res<ButtonInput<KeyCode>>, time: Res<Time>, mut paddle_transform: Single<&mut Transform, With<Paddle>>) {
let direction = if input.pressed(KeyCode::ArrowLeft) {
-1.0
} else if input.pressed(KeyCode::ArrowRight) {
@@ -107,19 +95,13 @@ fn apply_velocity(time: Res<Time>, mut query: Query<(&mut Transform, &Velocity)>
}
}
fn check_for_collisions(
ball_query: Single<(&mut Velocity, &Transform), With<Ball>>,
collider_query: Query<&Transform, With<Collider>>,
) {
fn check_for_collisions(ball_query: Single<(&mut Velocity, &Transform), With<Ball>>, collider_query: Query<&Transform, With<Collider>>) {
let (mut ball_velocity, ball_transform) = ball_query.into_inner();
for collider_transform in &collider_query {
let collision = ball_collision(
BoundingCircle::new(ball_transform.translation.truncate(), BALL_DIAMETER / 2.0),
Aabb2d::new(
collider_transform.translation.truncate(),
collider_transform.scale.truncate() / 2.0,
),
Aabb2d::new(collider_transform.translation.truncate(), collider_transform.scale.truncate() / 2.0),
);
if let Some(collision) = collision {
@@ -160,11 +142,7 @@ fn ball_collision(circle: BoundingCircle, rect: Aabb2d) -> Option<Collision> {
let offset = circle.center() - closest;
let side = if offset.x.abs() > offset.y.abs() {
if offset.x < 0.0 {
Collision::Left
} else {
Collision::Right
}
if offset.x < 0.0 { Collision::Left } else { Collision::Right }
} else if offset.y > 0.0 {
Collision::Top
} else {
@@ -212,12 +190,8 @@ impl WallLocation {
fn size(&self) -> Vec2 {
match self {
WallLocation::Left | WallLocation::Right => {
Vec2::new(WALL_WIDTH, TOP_WALL - BOTTOM_WALL)
}
WallLocation::Top | WallLocation::Bottom => {
Vec2::new(RIGHT_WALL - LEFT_WALL, WALL_WIDTH)
}
WallLocation::Left | WallLocation::Right => Vec2::new(WALL_WIDTH, TOP_WALL - BOTTOM_WALL),
WallLocation::Top | WallLocation::Bottom => Vec2::new(RIGHT_WALL - LEFT_WALL, WALL_WIDTH),
}
}
}
+3 -9
View File
@@ -47,21 +47,15 @@ fn runner(mut app: App) -> AppExit {
}
"f" => {
println!("FAST: setting relative speed to 2x");
app.world_mut()
.resource_mut::<Time<Virtual>>()
.set_relative_speed(2.0);
app.world_mut().resource_mut::<Time<Virtual>>().set_relative_speed(2.0);
}
"n" => {
println!("NORMAL: setting relative speed to 1x");
app.world_mut()
.resource_mut::<Time<Virtual>>()
.set_relative_speed(1.0);
app.world_mut().resource_mut::<Time<Virtual>>().set_relative_speed(1.0);
}
"s" => {
println!("SLOW: setting relative speed to 0.5x");
app.world_mut()
.resource_mut::<Time<Virtual>>()
.set_relative_speed(0.5);
app.world_mut().resource_mut::<Time<Virtual>>().set_relative_speed(0.5);
}
"p" => {
println!("PAUSE: pausing virtual clock");