Skip to content

Commit

Permalink
feat: final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Jul 21, 2024
1 parent 195983b commit 2d4e4b9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
59 changes: 56 additions & 3 deletions src/enemy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::f32::consts::PI;

use bevy::prelude::*;
#[cfg(feature = "persist")]
pub use bevy_persistent::prelude::Persistent;
use rand::Rng;

use crate::{
assets::{SoundAssets, ATLAS_SIZE},
assets::{CoreAssets, SoundAssets, ATLAS_SIZE},
data::{attack, max_battery, SaveData},
misc::{dir_to_vec, Direction, MoveTo, MIN_TURN_TIMER},
player::Player,
Expand Down Expand Up @@ -45,7 +47,7 @@ impl Plugin for EnemyPlugin {
Update,
(
on_damage.in_set(PlaySet::Events),
enemy_flash.in_set(PlaySet::Animation),
(damage_text, enemy_flash).in_set(PlaySet::Animation),
),
);
}
Expand Down Expand Up @@ -105,6 +107,9 @@ struct EnemyTurn(Timer);
#[derive(Component)]
struct EnemyFlash(Timer);

#[derive(Component)]
struct DamageText(Timer, Vec2);

// ······
// Events
// ······
Expand All @@ -123,6 +128,7 @@ fn on_damage(
mut damage_reader: EventReader<DamageEvent>,
mut save_data: ResMut<Persistent<SaveData>>,
mut next_play_state: ResMut<NextState<PlayState>>,
assets: Res<CoreAssets>,
) {
for DamageEvent(entity) in damage_reader.read() {
cmd.entity(*entity)
Expand All @@ -142,7 +148,7 @@ fn on_damage(
.clamp(0, max_battery(save_data.battery_level));
}

enemy.health -= match enemy.elem {
let value = match enemy.elem {
Element::Basic => match save_data.attack_selected {
Element::Basic => attack(save_data.attack_level),
Element::Fire => {
Expand Down Expand Up @@ -249,6 +255,29 @@ fn on_damage(
},
},
};

let value = value.clamp(0., enemy.health);
enemy.health -= value;

cmd.spawn((
Text2dBundle {
text: Text::from_section(
if value > 0. { format!("{:.1}", value) } else { "X".into() },
TextStyle {
font: assets.font.clone(),
font_size: 10.,
color: enemy_color(&save_data.attack_selected).lighter(0.1),
},
),
transform: Transform::from_translation(tile_to_pos(enemy.pos).extend(15.)),
..default()
},
DamageText(
Timer::from_seconds(0.3, TimerMode::Once),
tile_to_pos(enemy.pos),
),
));

if enemy.health <= 0. {
cmd.entity(*entity).despawn();
let mut rng = rand::thread_rng();
Expand Down Expand Up @@ -386,6 +415,30 @@ fn update_enemies(
}
}

fn damage_text(
mut cmd: Commands,
mut text: Query<(
Entity,
&mut Transform,
&mut Text,
&mut DamageText,
)>,
time: Res<Time>,
) {
for (entity, mut trans, mut text, mut flash) in text.iter_mut() {
let timer = flash.0.tick(time.delta());
if timer.just_finished() {
cmd.entity(entity).despawn();
}
let t = timer.fraction();
text.sections[0].style.color.set_alpha(1. - t);
let mut pos = flash.1;
pos.x += (t * PI * 2.).sin() * 5.;
pos.y += t * 50.;
trans.translation = pos.extend(15.);
}
}

// ·······
// Helpers
// ·······
Expand Down
1 change: 0 additions & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ fn init(mut cmd: Commands, sprite_assets: Res<SpriteAssets>) {
));
}

// TODO: Turn based movement
fn move_player(
mut cmd: Commands,
mut player: Query<(Entity, &mut Player)>,
Expand Down
10 changes: 3 additions & 7 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::{seq::SliceRandom, Rng};
use crate::{
assets::{SpriteAssets, ATLAS_SIZE},
data::{max_battery, max_range, Persistent, SaveData},
enemy::{enemy_color, get_enemy, Element},
enemy::{enemy_color, get_enemy},
misc::{dir_to_vec, Direction},
player::{Status, StatusEvent},
GameState, PlayState, SCALE,
Expand Down Expand Up @@ -261,13 +261,9 @@ fn generate_level(
}
let offset = offset.perp();
let left = TileData::pos(pos + offset);
if !tiles.contains_key(&left) {
tiles.insert(left, Tile::Wall);
}
tiles.entry(left).or_insert(Tile::Wall);
let right = TileData::pos(pos - offset);
if !tiles.contains_key(&right) {
tiles.insert(right, Tile::Wall);
}
tiles.entry(right).or_insert(Tile::Wall);
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/ui/menu/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
ui::{
menu::{navigation::FocusableHoverFill, MenuButton, MenuState},
widgets::{UiButtonWidget, UiOptionRowWidget, UiTextWidget},
UiRootContainer, UI_GAP,
UiRootContainer,
},
SCALE,
};
Expand Down Expand Up @@ -110,12 +110,15 @@ fn row_mapping(map: &dyn Reflect, row: &mut UiBuilder<Entity>, asset_server: &As
for prompt in prompts {
// Dynamic loading to avoid having all icons in memory
row.option_button(|button| {
button.spawn(ImageBundle {
image: UiImage::new(asset_server.load(&prompt)).with_color(Color::srgb(0.812, 0.776, 0.722)),
..default()
}).style()
.height(Val::Px(16. * SCALE))
.width(Val::Px(16. * SCALE));
button
.spawn(ImageBundle {
image: UiImage::new(asset_server.load(&prompt))
.with_color(Color::srgb(0.812, 0.776, 0.722)),
..default()
})
.style()
.height(Val::Px(16. * SCALE))
.width(Val::Px(16. * SCALE));
})
.insert(BorderRadius::all(Val::Px(16.)))
.insert(BorderColor::from(Srgba::NONE))
Expand Down

0 comments on commit 2d4e4b9

Please sign in to comment.