-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminimal.rs
More file actions
170 lines (153 loc) · 5.19 KB
/
minimal.rs
File metadata and controls
170 lines (153 loc) · 5.19 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! - WASD for movement
//! - Space bar for jumping
use avian3d::prelude::*;
use bevy::{
color::palettes::css::*,
image::{ImageAddressMode, ImageSamplerDescriptor},
input::InputSystems,
math::Affine2,
prelude::*,
};
use happy_feet::prelude::*;
// Configure movement for ground and air states
const GROUND_MOVEMENT: CharacterMovement = CharacterMovement {
target_speed: 8.0, // Maximum movement speed
acceleration: 100.0,
};
const AIR_MOVEMENT: CharacterMovement = CharacterMovement {
target_speed: 8.0,
acceleration: 20.0,
};
fn main() -> AppExit {
App::new()
.add_plugins((
// This is used for tiling the ground texture
DefaultPlugins.set(ImagePlugin {
default_sampler: ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
..Default::default()
},
}),
// Make sure the physics plugins and character plugins are configured to run in the same schedule (default is FixedPostUpdate for both)
PhysicsPlugins::default(),
CharacterPlugins::default(),
// This is used for debugging and can be removed
PhysicsDebugPlugin::default(),
))
.add_systems(Startup, (setup_character, setup_level))
.add_systems(PreUpdate, character_input.after(InputSystems))
.add_observer(init_ground_movement)
.add_observer(init_air_movement)
.run()
}
fn setup_character(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let shape = Capsule3d::new(0.2, 1.0);
commands.spawn((
Character,
AIR_MOVEMENT,
// Enable stepping
SteppingConfig {
max_vertical: 0.3,
..Default::default()
},
// Enable gravity
CharacterGravity::new(Vec3::Y * -20.0),
// Enable ground friction
GroundFriction(60.0),
Collider::from(shape),
Mesh3d(meshes.add(shape)),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: MIDNIGHT_BLUE.into(),
perceptual_roughness: 0.8,
..Default::default()
})),
));
}
fn character_input(
key: Res<ButtonInput<KeyCode>>,
mut query: Query<(&mut MoveInput, &mut KinematicVelocity, &mut Grounding)>,
) {
let x = match (key.pressed(KeyCode::KeyA), key.pressed(KeyCode::KeyD)) {
(true, false) => -1.0,
(false, true) => 1.0,
_ => 0.0,
};
let z = match (key.pressed(KeyCode::KeyW), key.pressed(KeyCode::KeyS)) {
(true, false) => -1.0,
(false, true) => 1.0,
_ => 0.0,
};
let move_direction = Vec3::new(x, 0.0, z).normalize_or_zero();
for (mut move_input, mut velocity, mut grounding) in &mut query {
move_input.value = move_direction;
if grounding.is_grounded() && key.just_pressed(KeyCode::Space) {
velocity.y = 6.0;
grounding.detach(); // Detach from the ground to avoid snapping back to it during character update
}
}
}
fn init_ground_movement(trigger: On<OnGroundEnter>, mut commands: Commands) {
commands
.entity(trigger.event().entity)
.insert(GROUND_MOVEMENT);
}
fn init_air_movement(trigger: On<OnGroundLeave>, mut commands: Commands) {
commands.entity(trigger.event().entity).insert(AIR_MOVEMENT);
}
fn setup_level(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
// floor
let floor_size = 100.0;
commands.spawn((
RigidBody::Static,
Collider::half_space(Vec3::Y),
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(floor_size)))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color_texture: Some(asset_server.load("texture_08.png")),
uv_transform: Affine2::from_scale(Vec2::splat(floor_size)),
..Default::default()
})),
));
// camera
commands.spawn((
Camera3d::default(),
Projection::Perspective(PerspectiveProjection {
fov: 75.0_f32.to_radians(),
..Default::default()
}),
Transform::from_xyz(0.0, 4.0, 10.0).looking_at(Vec3::ZERO, Dir3::Y),
));
// light
commands.spawn((
DirectionalLight {
shadows_enabled: true,
..Default::default()
},
Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Dir3::Z),
));
// obstacles
let mut spawn_cube = |pos, size| {
let cube = Cuboid::from_size(size);
commands.spawn((
RigidBody::Static,
Collider::from(cube),
Transform::from_translation(pos),
Mesh3d(meshes.add(cube)),
MeshMaterial3d(materials.add(StandardMaterial {
base_color_texture: Some(asset_server.load("texture_08.png")),
..Default::default()
})),
));
};
spawn_cube(Vec3::new(8.0, 0.2, 0.0), Vec3::new(4.0, 0.4, 4.0));
spawn_cube(Vec3::new(-8.0, 2.0, 0.0), Vec3::new(4.0, 4.0, 4.0));
}