generated from eerii/hello-bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
239 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::{ | ||
assets::{SoundAssets, SpriteAssets}, | ||
tilemap::tile_to_pos, | ||
GameState, SCALE, | ||
}; | ||
|
||
// ······ | ||
// Plugin | ||
// ······ | ||
|
||
pub struct EnemyPlugin; | ||
|
||
impl Plugin for EnemyPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_event::<DamageEvent>() | ||
.add_systems( | ||
OnEnter(GameState::Play), | ||
init.run_if(run_once()), | ||
) | ||
.add_systems( | ||
Update, | ||
on_damage.run_if(in_state(GameState::Play)), | ||
); | ||
} | ||
} | ||
|
||
// ·········· | ||
// Components | ||
// ·········· | ||
|
||
#[derive(Component)] | ||
pub struct Enemy { | ||
pub pos: UVec2, | ||
pub health: u32, | ||
} | ||
|
||
// ······ | ||
// Events | ||
// ······ | ||
|
||
#[derive(Event)] | ||
pub struct DamageEvent(pub Entity); | ||
|
||
// ······· | ||
// Systems | ||
// ······· | ||
|
||
fn init(mut cmd: Commands, sprite_assets: Res<SpriteAssets>) { | ||
for _ in 0..3 { | ||
let (x, y) = ( | ||
rand::random::<u32>() % 11, | ||
rand::random::<u32>() % 7, | ||
); | ||
let pos = tile_to_pos(x, y); | ||
cmd.spawn(( | ||
SpriteBundle { | ||
transform: Transform::from_translation(pos.extend(5.)) | ||
.with_scale(Vec3::splat(SCALE)), | ||
texture: sprite_assets.one_bit.clone(), | ||
..default() | ||
}, | ||
TextureAtlas { | ||
layout: sprite_assets.one_bit_atlas.clone(), | ||
index: 29 + 7 * 48, | ||
}, | ||
Enemy { | ||
pos: UVec2::new(x, y), | ||
health: 2, | ||
}, | ||
)); | ||
} | ||
} | ||
|
||
fn on_damage( | ||
mut cmd: Commands, | ||
mut enemies: Query<&mut Enemy>, | ||
sound_assets: Res<SoundAssets>, | ||
mut damage_reader: EventReader<DamageEvent>, | ||
) { | ||
for DamageEvent(entity) in damage_reader.read() { | ||
if let Ok(mut enemy) = enemies.get_mut(*entity) { | ||
enemy.health -= 1; | ||
if enemy.health == 0 { | ||
cmd.entity(*entity).despawn(); | ||
} | ||
} | ||
|
||
cmd.spawn(AudioBundle { | ||
source: sound_assets.boing.clone(), | ||
settings: PlaybackSettings::DESPAWN, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::f32::consts::PI; | ||
|
||
use bevy::prelude::*; | ||
|
||
use crate::{tilemap::TILE_SEP, GameState}; | ||
|
||
// ······ | ||
// Plugin | ||
// ······ | ||
|
||
pub struct MiscPlugin; | ||
|
||
impl Plugin for MiscPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems( | ||
Update, | ||
move_to.run_if(in_state(GameState::Play)), | ||
); | ||
} | ||
} | ||
|
||
// ·········· | ||
// Components | ||
// ·········· | ||
|
||
pub enum Direction { | ||
North, | ||
South, | ||
East, | ||
West, | ||
} | ||
|
||
#[derive(Component)] | ||
pub struct MoveTo { | ||
start: Vec2, | ||
target: Vec2, | ||
bump_dir: Option<Direction>, | ||
timer: Timer, | ||
} | ||
|
||
impl MoveTo { | ||
pub fn new(start: Vec2, target: Vec2, bump_dir: Option<Direction>) -> Self { | ||
Self { | ||
start, | ||
target, | ||
bump_dir, | ||
timer: Timer::from_seconds(0.15, TimerMode::Once), | ||
} | ||
} | ||
} | ||
|
||
// ······· | ||
// Systems | ||
// ······· | ||
|
||
pub fn move_to( | ||
mut cmd: Commands, | ||
time: Res<Time>, | ||
mut movables: Query<(Entity, &mut MoveTo, &mut Transform)>, | ||
) { | ||
for (entity, mut to, mut trans) in movables.iter_mut() { | ||
let timer = to.timer.tick(time.delta()); | ||
if timer.just_finished() { | ||
cmd.entity(entity).remove::<MoveTo>(); | ||
} | ||
let t = timer.fraction(); | ||
|
||
let pos = if let Some(dir) = &to.bump_dir { | ||
let offset = (t * PI).sin() * TILE_SEP; | ||
to.start + dir_to_vec(dir, offset) | ||
} else { | ||
to.start.lerp(to.target, t) | ||
}; | ||
trans.translation = pos.extend(trans.translation.z); | ||
} | ||
} | ||
|
||
// ······· | ||
// Helpers | ||
// ······· | ||
|
||
fn dir_to_vec(dir: &Direction, val: f32) -> Vec2 { | ||
match dir { | ||
Direction::North => Vec2::new(0., val), | ||
Direction::South => Vec2::new(0., -val), | ||
Direction::East => Vec2::new(val, 0.), | ||
Direction::West => Vec2::new(-val, 0.), | ||
} | ||
} |
Oops, something went wrong.