Skip to content

Commit

Permalink
feat: enemy flash
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Jul 21, 2024
1 parent 5bb3fc4 commit e570d3f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 31 deletions.
55 changes: 48 additions & 7 deletions src/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const WEIGHTS: [[u32; 7]; 12] = [
[5, 40, 30, 5, 00, 20, 0],
[00, 20, 45, 25, 00, 9, 1],
[00, 10, 20, 50, 10, 9, 1],
[00, 00, 5, 55, 20, 19, 1],
[00, 00, 00, 60, 15, 19, 1],
[00, 00, 00, 40, 35, 24, 1],
[00, 00, 00, 20, 50, 29, 1],
[00, 00, 00, 10, 70, 19, 1],
[00, 00, 00, 5, 85, 9, 1],
[00, 00, 5, 55, 20, 19, 0],
[00, 00, 00, 60, 15, 19, 0],
[00, 00, 00, 40, 35, 24, 0],
[00, 00, 00, 20, 50, 29, 0],
[00, 00, 00, 10, 70, 19, 0],
[00, 00, 00, 5, 85, 9, 0],
];

// ······
Expand All @@ -43,7 +43,10 @@ impl Plugin for EnemyPlugin {
)
.add_systems(
Update,
on_damage.in_set(PlaySet::Events),
(
on_damage.in_set(PlaySet::Events),
enemy_flash.in_set(PlaySet::Animation),
),
);
}
}
Expand Down Expand Up @@ -99,6 +102,9 @@ pub struct Enemy {
#[derive(Component)]
struct EnemyTurn(Timer);

#[derive(Component)]
struct EnemyFlash(Timer);

// ······
// Events
// ······
Expand All @@ -119,6 +125,12 @@ fn on_damage(
mut next_play_state: ResMut<NextState<PlayState>>,
) {
for DamageEvent(entity) in damage_reader.read() {
cmd.entity(*entity)
.try_insert(EnemyFlash(Timer::from_seconds(
0.15,
TimerMode::Once,
)));

if let Ok(mut enemy) = enemies.get_mut(*entity) {
if let EnemyType::EndGame = enemy.typ {
next_play_state.set(PlayState::GameWon);
Expand Down Expand Up @@ -285,6 +297,26 @@ fn enemy_turn(mut cmd: Commands) {
)));
}

fn enemy_flash(
mut cmd: Commands,
mut enemies: Query<(
Entity,
&Enemy,
&mut Sprite,
&mut EnemyFlash,
)>,
time: Res<Time>,
) {
for (entity, enemy, mut sprite, mut flash) in enemies.iter_mut() {
let timer = flash.0.tick(time.delta());
if timer.just_finished() {
cmd.entity(entity).remove::<EnemyFlash>();
}
let n = (timer.fraction() * 5.) as u32;
sprite.color = if n % 2 == 0 { Color::WHITE } else { enemy_color(&enemy.elem) };
}
}

fn update_enemies(
mut cmd: Commands,
mut timer: Query<(Entity, &mut EnemyTurn)>,
Expand Down Expand Up @@ -440,3 +472,12 @@ fn enemy_elem() -> Element {
_ => Element::Basic,
}
}

pub fn enemy_color(elem: &Element) -> Color {
match elem {
Element::Basic => Color::srgb(0.812, 0.776, 0.722),
Element::Fire => Color::srgb(0.902, 0.282, 0.18),
Element::Water => Color::srgb(0.235, 0.675, 0.843),
Element::Grass => Color::srgb(0.22, 0.851, 0.451),
}
}
43 changes: 23 additions & 20 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::{get_enemy, Element},
enemy::{enemy_color, get_enemy, Element},
misc::{dir_to_vec, Direction},
player::{Status, StatusEvent},
GameState, PlayState, SCALE,
Expand Down Expand Up @@ -245,7 +245,8 @@ fn generate_level(
let center_a = a * ROOM_SEP.as_ivec2() + ROOM_SEP.as_ivec2() / 2;
let mut first_wall = false;
for pos in 0..sep {
let tile = TileData::pos(center_a + pos as i32 * offset);
let pos = center_a + pos as i32 * offset;
let tile = TileData::pos(pos);
// Find the first wall and start laying paths
if !first_wall {
if let Some(Tile::Wall) = tiles.get(&tile) {
Expand All @@ -258,30 +259,37 @@ fn generate_level(
else if let Some(Tile::Wall) = tiles.insert(tile, Tile::Path) {
break;
}
let offset = offset.perp();
let left = TileData::pos(pos + offset);
if !tiles.contains_key(&left) {
tiles.insert(left, Tile::Wall);
}
let right = TileData::pos(pos - offset);
if !tiles.contains_key(&right) {
tiles.insert(right, Tile::Wall);
}
}
}
}
aux.remove(&a);
}

// Insert ladder down or final key
// This iterator is supposed to be random
if level < 9 {
for (_, tile) in tiles.iter_mut() {
if !matches!(tile, Tile::Ground) {
continue;
}
*tile = Tile::LadderDown;
break;
}
}

// Insert ladder up
tiles.insert(
TileData::pos(ROOM_SEP.as_ivec2() / 2),
Tile::LadderUp,
);

// Insert ladder down or final key
// This iterator is supposed to be random
for (_, tile) in tiles.iter_mut() {
if !matches!(tile, Tile::Ground) {
continue;
}
*tile = if level < 9 { Tile::LadderDown } else { Tile::Enemy };
break;
}

// Create actual tiles
let mut unique = false;
tiles
Expand Down Expand Up @@ -353,12 +361,7 @@ fn create_tile(
.with_scale(Vec3::splat(SCALE)),
texture: sprite_assets.one_bit.clone(),
sprite: Sprite {
color: match enemy.elem {
Element::Basic => Color::srgb(0.812, 0.776, 0.722),
Element::Fire => Color::srgb(0.902, 0.282, 0.18),
Element::Water => Color::srgb(0.235, 0.675, 0.843),
Element::Grass => Color::srgb(0.22, 0.851, 0.451),
},
color: enemy_color(&enemy.elem),
..default()
},
..default()
Expand Down
5 changes: 2 additions & 3 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, UiImageWidget, UiOptionRowWidget, UiTextWidget},
UiRootContainer, UI_GAP,
UiRootContainer,
},
};

Expand All @@ -41,8 +41,7 @@ pub(super) fn open(
.style()
.width(Val::Percent(100.))
.align_items(AlignItems::Center)
.justify_content(JustifyContent::Center)
.row_gap(UI_GAP);
.justify_content(JustifyContent::Center);

column.title("Mappings".into(), assets.font.clone());

Expand Down
2 changes: 1 addition & 1 deletion src/ui/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl UiOptionRowWidget for UiBuilder<'_, Entity> {
..default()
},
background_color: BUTTON_COLOR.into(),
border_radius: BorderRadius::MAX,
// border_radius: BorderRadius::MAX,
..default()
},
spawn_children,
Expand Down

0 comments on commit e570d3f

Please sign in to comment.