Skip to content

Commit

Permalink
✨ sword cooldown & duration
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Nov 3, 2023
1 parent 9190478 commit 4f85192
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 44 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ windows from linux: https://bevy-cheatbook.github.io/setup/cross/linux-windows.h

⚠️ Does not work from linux to M1 because of a error regarding the compilation of `objc_exception` ([github issue](https://github.com/SSheldon/rust-objc-exception/issues/13))

- Need to read and try this (packaging xcode sdk), using cross: https://github.com/cross-rs/cross-toolchains#apple-targets

```sh
rustup target add aarch64-apple-darwin
rustup target add x86_64-pc-windows-msvc
Expand Down
125 changes: 81 additions & 44 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ use bevy_rapier2d::prelude::*;
const DEFAULT_HAND_COLOR: Color = Color::rgb(0.8, 0.25, 0.24);
const JOYSTICK_SCALE: f32 = 200.;

pub struct Cooldowns {
pub sword: Timer,
}

#[derive(Component)]
pub struct Player {
pub gold: f32,
pub cooldowns: Cooldowns,
}

#[derive(Component)]
Expand All @@ -27,11 +32,38 @@ struct Hand;
struct GoldUI;

#[derive(Component)]
struct Sword(Entity);
struct Sword {
entity: Entity,
duration: Timer,
}

#[derive(Bundle)]
struct SwordBundle {
pub sprite: SpriteBundle,
pub sword: Sword,
pub sensor: Sensor,
pub collider: Collider,
}

impl Sword {
pub fn collider() -> Collider {
Collider::cuboid(50.0, 25.)
impl SwordBundle {
pub fn new(parent_entity: Entity) -> Self {
Self {
sprite: SpriteBundle {
sprite: Sprite {
color: Color::rgb(0.25, 0.6, 0.25),
custom_size: Some(Vec2::new(100.0, 50.0)),
..default()
},
transform: Transform::from_xyz(0.0, 35.0, 10.0),
..default()
},
collider: Collider::cuboid(50.0, 25.),
sensor: Sensor,
sword: Sword {
entity: parent_entity,
duration: Timer::from_seconds(0.2, TimerMode::Once),
},
}
}
}

Expand All @@ -46,12 +78,17 @@ impl Plugin for LocalPlayerPlugin {
update_button_values,
check_collisions_sword,
update_ui,
update_sword,
update_cooldowns,
),
);
}
}

fn setup(mut commands: Commands) {
let mut sword_cooldown = Timer::from_seconds(0.3, TimerMode::Once);
sword_cooldown.set_elapsed(sword_cooldown.duration());

let entity = commands
.spawn((
SpriteBundle {
Expand All @@ -68,7 +105,12 @@ fn setup(mut commands: Commands) {
Collider::cuboid(25.0, 25.),
ActiveEvents::COLLISION_EVENTS,
LocalPlayer {},
Player { gold: 20. },
Player {
gold: 20.,
cooldowns: Cooldowns {
sword: sword_cooldown,
},
},
Health::new(100.),
Name("local_player".to_string()),
Team {
Expand All @@ -89,21 +131,6 @@ fn setup(mut commands: Commands) {
},
Hand {},
));

parent.spawn((
SpriteBundle {
sprite: Sprite {
color: Color::rgb(0.25, 0.6, 0.25),
custom_size: Some(Vec2::new(100.0, 50.0)),
..default()
},
transform: Transform::from_xyz(0.0, 35.0, 10.0),
visibility: Visibility::Hidden,
..default()
},
Sensor,
Sword(parent.parent_entity()),
));
})
.id();

Expand Down Expand Up @@ -179,40 +206,32 @@ fn update_axes(
fn update_button_values(
mut commands: Commands,
mut events: EventReader<GamepadButtonChangedEvent>,
mut query_local_player: Query<(&mut Player, &Transform, &Team, &Children), With<LocalPlayer>>,
mut query_local_player: Query<
(&mut Player, &Transform, &Team, Entity, &Children),
With<LocalPlayer>,
>,
mut query: Query<&mut Sprite, With<Hand>>,
query_sword: Query<Entity, With<Sword>>,
) {
for button_event in events.iter() {
let (mut player, transform, team, entity, children) = query_local_player.single_mut();
if button_event.button_type == GamepadButtonType::South {
for (_, _, _, children) in &query_local_player {
for child in children {
if let Ok(mut sprite) = query.get_mut(*child) {
if button_event.value != 0. {
sprite.color = Color::rgb(0.25, 0.75, 0.25)
} else {
sprite.color = DEFAULT_HAND_COLOR
}
}

if let Ok(entity) = query_sword.get(*child) {
if button_event.value != 0. {
commands
.entity(entity)
.insert((Sword::collider(), Visibility::Visible));
} else {
commands
.entity(entity)
.remove::<Collider>()
.insert(Visibility::Hidden);
for child in children {
if let Ok(mut sprite) = query.get_mut(*child) {
if button_event.value != 0. {
if player.cooldowns.sword.finished() {
sprite.color = Color::rgb(0.25, 0.75, 0.25);
let sword_entity = commands.spawn(SwordBundle::new(entity)).id();
commands.entity(entity).add_child(sword_entity);
player.cooldowns.sword.reset();
}
} else {
sprite.color = DEFAULT_HAND_COLOR;
}
}
}
}

if button_event.button_type == GamepadButtonType::East && button_event.value != 0. {
let (mut player, transform, team, _) = query_local_player.single_mut();
if player.gold >= RACK_GOLD_VALUE {
crate::racks::spawn_rack(&mut commands, *transform, team.clone());
player.gold -= RACK_GOLD_VALUE;
Expand Down Expand Up @@ -266,7 +285,7 @@ fn check_collisions_sword(
health.hit(20.);

// player attached to this sword receive gold
if let Ok((mut player, team)) = query_player.get_mut(sword.0) {
if let Ok((mut player, team)) = query_player.get_mut(sword.entity) {
if team.id != hit_team.id {
player.gold += rewards.gold;
}
Expand All @@ -276,3 +295,21 @@ fn check_collisions_sword(
}
}
}

fn update_sword(
mut commands: Commands,
time: Res<Time>,
mut query_swords: Query<(Entity, &mut Sword)>,
) {
for (entity, mut sword) in query_swords.iter_mut() {
if sword.duration.tick(time.delta()).just_finished() {
commands.entity(entity).despawn_recursive();
}
}
}

fn update_cooldowns(time: Res<Time>, mut query_players: Query<&mut Player>) {
for mut player in query_players.iter_mut() {
player.cooldowns.sword.tick(time.delta());
}
}

0 comments on commit 4f85192

Please sign in to comment.