forked from Jondolf/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_marbles.rs
124 lines (115 loc) · 3.48 KB
/
move_marbles.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#![allow(clippy::unnecessary_cast)]
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use bevy_xpbd_2d::{math::*, prelude::*};
use examples_common_2d::XpbdExamplePlugin;
fn main() {
App::new()
.add_plugins((DefaultPlugins, XpbdExamplePlugin))
.insert_resource(ClearColor(Color::rgb(0.05, 0.05, 0.1)))
.insert_resource(SubstepCount(6))
.insert_resource(Gravity(Vector::NEG_Y * 1000.0))
.add_systems(Startup, setup)
.add_systems(Update, movement)
.run();
}
#[derive(Component)]
struct Marble;
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
commands.spawn(Camera2dBundle::default());
let square_sprite = Sprite {
color: Color::rgb(0.7, 0.7, 0.8),
custom_size: Some(Vec2::splat(50.0)),
..default()
};
// Ceiling
commands.spawn((
SpriteBundle {
sprite: square_sprite.clone(),
transform: Transform::from_scale(Vec3::new(20.0, 1.0, 1.0)),
..default()
},
RigidBody::Static,
Position(Vector::Y * 50.0 * 6.0),
Collider::cuboid(50.0 * 20.0, 50.0),
));
// Floor
commands.spawn((
SpriteBundle {
sprite: square_sprite.clone(),
transform: Transform::from_scale(Vec3::new(20.0, 1.0, 1.0)),
..default()
},
RigidBody::Static,
Position(Vector::NEG_Y * 50.0 * 6.0),
Collider::cuboid(50.0 * 20.0, 50.0),
));
// Left wall
commands.spawn((
SpriteBundle {
sprite: square_sprite.clone(),
transform: Transform::from_scale(Vec3::new(1.0, 11.0, 1.0)),
..default()
},
RigidBody::Static,
Position(Vector::NEG_X * 50.0 * 9.5),
Collider::cuboid(50.0, 50.0 * 11.0),
));
// Right wall
commands.spawn((
SpriteBundle {
sprite: square_sprite,
transform: Transform::from_scale(Vec3::new(1.0, 11.0, 1.0)),
..default()
},
RigidBody::Static,
Position(Vector::X * 50.0 * 9.5),
Collider::cuboid(50.0, 50.0 * 11.0),
));
let marble_radius = 5.0;
let marble_mesh = MaterialMesh2dBundle {
mesh: meshes
.add(shape::Circle::new(marble_radius as f32).into())
.into(),
material: materials.add(ColorMaterial::from(Color::rgb(0.2, 0.7, 0.9))),
..default()
};
// Spawn stacks of marbles
for x in -20..20 {
for y in -19..19 {
let position = Vector::new(
x as Scalar * (2.5 * marble_radius),
y as Scalar * (2.5 * marble_radius),
);
commands.spawn((
marble_mesh.clone(),
RigidBody::Dynamic,
Position(position),
Collider::ball(marble_radius),
Marble,
));
}
}
}
fn movement(
keyboard_input: Res<Input<KeyCode>>,
mut marbles: Query<&mut LinearVelocity, With<Marble>>,
) {
for mut linear_velocity in &mut marbles {
if keyboard_input.pressed(KeyCode::Up) {
linear_velocity.y += 50.0;
}
if keyboard_input.pressed(KeyCode::Down) {
linear_velocity.y -= 10.0;
}
if keyboard_input.pressed(KeyCode::Left) {
linear_velocity.x -= 10.0;
}
if keyboard_input.pressed(KeyCode::Right) {
linear_velocity.x += 10.0;
}
}
}