-
Notifications
You must be signed in to change notification settings - Fork 3
/
controls.rs
161 lines (147 loc) · 4.59 KB
/
controls.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
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
use crate::{
assets::ImageAssets,
kenney_assets::KenneySpriteSheetAsset,
ship::PlayerShipType, ui::pause::Pausable, GameState,
Player,
};
use bevy::prelude::*;
use bevy_xpbd_2d::prelude::*;
use std::time::Duration;
pub struct ControlsPlugin;
impl Plugin for ControlsPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<MovementFactor>().add_systems(
Update,
(
player_movement_system
.run_if(in_state(GameState::Playing)),
laser_movement,
)
.run_if(resource_equals(
Pausable::NotPaused,
)),
);
}
}
#[derive(Component)]
pub struct Laser(Vec2);
#[derive(Resource, Default, Deref, DerefMut)]
pub struct MovementFactor(pub Vec2);
fn laser_movement(
mut lasers: Query<(&mut Transform, &Laser)>,
time: Res<Time>,
) {
for (
mut transform,
Laser(preexisting_movement_factor),
) in &mut lasers
{
let laser_facing_direction =
transform.rotation * Vec3::Y;
let translation_delta = *preexisting_movement_factor
+ laser_facing_direction.xy()
* 1000.
* time.delta_seconds();
transform.translation.x += translation_delta.x;
transform.translation.y += translation_delta.y;
}
}
fn player_movement_system(
mut commands: Commands,
time: Res<Time>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<
(&mut Transform, &PlayerShipType),
With<Player>,
>,
mut movement_factor: ResMut<MovementFactor>,
images: Res<ImageAssets>,
sheets: Res<Assets<KenneySpriteSheetAsset>>,
mut last_shot: Local<Option<Duration>>,
) {
let Ok((mut transform, ship)) = query.get_single_mut()
else {
error!(
"Only expected one Player component. got {}",
query.iter().count()
);
return;
};
let space_sheet =
sheets.get(&images.space_sheet).unwrap();
let mut rotation_factor = 0.0;
if keyboard_input.pressed(KeyCode::ArrowLeft) {
rotation_factor += 1.0;
}
if keyboard_input.pressed(KeyCode::ArrowRight) {
rotation_factor -= 1.0;
}
// update the ship rotation around the Z axis
// (perpendicular to the 2D plane of the screen)
transform.rotate_z(
rotation_factor
* ship.base_ship_speed().rotation_speed
* time.delta_seconds(),
);
// get the ship's forward vector by applying the
// current rotation to the ships initial facing
// vector
let user_facing_direction =
transform.rotation * Vec3::Y;
if keyboard_input.pressed(KeyCode::Space) {
let can_shoot = last_shot.is_none() || {
if let Some(shot) = *last_shot {
time.elapsed() - shot
> Duration::from_millis(200)
} else {
false
}
};
if can_shoot {
*last_shot = Some(time.elapsed());
commands.spawn((
SpriteBundle {
transform: *transform,
texture: space_sheet.sheet.clone(),
..default()
},
TextureAtlas {
layout: space_sheet
.texture_atlas_layout
.clone(),
index: 105,
},
Laser(**movement_factor),
Collider::triangle(
Vec2::new(0., -27.),
Vec2::new(4.5, 27.),
Vec2::new(-4.5, 27.),
),
));
}
}
if keyboard_input.pressed(KeyCode::ArrowUp) {
movement_factor.0 = (movement_factor.0
+ 0.01 * user_facing_direction.xy())
.clamp(Vec2::splat(-1.0), Vec2::splat(1.0));
} else {
// decay movement factor?
// movement_factor.0 = (movement_factor.0
// * 0.95)
// .clamp(Vec2::splat(-1.0),
// Vec2::splat(1.0));
}
// get the distance the ship will move based on
// direction, the ship's movement speed and delta
// time
let movement_distance = movement_factor.0
* ship.base_ship_speed().movement_speed
* time.delta_seconds();
// create the change in translation using the new
// movement direction and distance
let translation_delta = movement_distance;
// update the ship translation with our new
// translation delta
transform.translation.x += translation_delta.x;
transform.translation.y += translation_delta.y;
}