feat: basic attacks

This commit is contained in:
Muhammad Nauman Raza 2024-03-16 00:14:10 +00:00
parent 5d8eb93b32
commit b8f1849d7a
Signed by: devraza
GPG key ID: 91EAD6081011574B
4 changed files with 49 additions and 24 deletions

2
Cargo.lock generated
View file

@ -129,7 +129,7 @@ dependencies = [
[[package]] [[package]]
name = "ambition" name = "ambition"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy_egui", "bevy_egui",

View file

@ -8,11 +8,11 @@
"rust-analyzer-src": "rust-analyzer-src" "rust-analyzer-src": "rust-analyzer-src"
}, },
"locked": { "locked": {
"lastModified": 1710397340, "lastModified": 1710483719,
"narHash": "sha256-cetHTP4c7eILb1v5nD0HO4Nev1egEDuqo8OOk90khYg=", "narHash": "sha256-Ev/hJ59IAA3dWfTB3CWxMv/V/owO1yKyq0nwsek/d9o=",
"owner": "nix-community", "owner": "nix-community",
"repo": "fenix", "repo": "fenix",
"rev": "80409698f025fcbf931d9bd7a6a07a3a06356271", "rev": "d0439c495e5cd13ff252ade520ca620f52abb40b",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -97,11 +97,11 @@
"rust-analyzer-src": { "rust-analyzer-src": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1710319630, "lastModified": 1710430493,
"narHash": "sha256-2PO3d37euWrUTFofT/Edji+zorOgfiIrMKqhuq3ffHU=", "narHash": "sha256-KfmUsf/d62ANcFhSTR3BDIpk2ww0AcxXdi9lpZJ5UtQ=",
"owner": "rust-lang", "owner": "rust-lang",
"repo": "rust-analyzer", "repo": "rust-analyzer",
"rev": "e8182a5bb32ca5763fd817661447f75138fb3883", "rev": "14558af15ee3d471bf8f4212f7609ae1f9647bc5",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -55,7 +55,7 @@ fn main() {
.init_resource::<UiState>() .init_resource::<UiState>()
.init_resource::<OpenWindows>() .init_resource::<OpenWindows>()
.add_systems(Startup, (setup, setup_ui)) .add_systems(Startup, (setup, setup_ui))
.add_systems(Update, (render_ui, movement, camera_follow, player_regen)) .add_systems(Update, (render_ui, movement, camera_follow, player_regen, attack))
.run(); .run();
} }

View file

@ -12,6 +12,13 @@ pub struct Player {
pub stamina_max: f32, pub stamina_max: f32,
} }
// Define the attacking component
#[derive(Component)]
pub struct Attack {
pub velocity: f32,
pub damage: f32,
}
// Define the player movement system // Define the player movement system
pub fn movement( pub fn movement(
time: Res<Time>, time: Res<Time>,
@ -46,22 +53,6 @@ pub fn movement(
movement_distance = 256.; movement_distance = 256.;
} }
if keys.pressed(KeyCode::ArrowLeft) {
transform.rotation = Quat::from_rotation_z((90_f32).to_radians());
movement_factor = 1.;
} else if keys.pressed(KeyCode::ArrowRight) {
transform.rotation = Quat::from_rotation_z((270_f32).to_radians());
movement_factor = 1.;
}
if keys.pressed(KeyCode::ArrowUp) {
transform.rotation = Quat::from_rotation_z((0_f32).to_radians());
movement_factor = 1.;
} else if keys.pressed(KeyCode::ArrowDown) {
transform.rotation = Quat::from_rotation_z((180_f32).to_radians());
movement_factor = 1.;
}
// Get the player's *forward* vector // Get the player's *forward* vector
let movement_direction = transform.rotation * Vec3::Y; let movement_direction = transform.rotation * Vec3::Y;
@ -75,6 +66,40 @@ pub fn movement(
transform.translation += movement_direction * movement_distance; transform.translation += movement_direction * movement_distance;
} }
pub fn attack(
keys: Res<ButtonInput<KeyCode>>,
mut set: ParamSet<(
Query<&mut Transform, With<Attack>>,
Query<&Transform, With<Player>>
)>,
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
for player_transform in set.p1().iter_mut() {
if keys.just_pressed(KeyCode::Enter) {
commands
.spawn(SpriteBundle {
texture: asset_server.load("player/player-4x.png"),
transform: Transform {
scale: Vec3::splat(0.1),
translation: player_transform.translation,
rotation: player_transform.rotation,
},
..default()
})
.insert(Attack {
velocity: 10.,
damage: 20.,
});
}
}
for mut attack_transform in set.p0().iter_mut() {
let direction = attack_transform.rotation * Vec3::Y;
attack_transform.translation += direction * 10.;
}
}
// Function to make the camera follow the plaeyr // Function to make the camera follow the plaeyr
pub fn camera_follow( pub fn camera_follow(
mut player: Query<(&Player, &mut Transform)>, mut player: Query<(&Player, &mut Transform)>,