Skip to content

Commit

Permalink
Merge pull request #6 from DennisSmuda/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
DennisSmuda authored Feb 24, 2024
2 parents 019fae9 + ec82113 commit 17be9c9
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Running on Bevy v.013
This is an attempt to learn rust/bevy and recreate his experience.

![screenshot](assets/screen.png)
![screenshot](assets/screenshot.png)
Binary file removed assets/screen.png
Binary file not shown.
Binary file added assets/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/tuner_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub enum Collider {
Enemy,
}

#[derive(Component)]
struct Rotatable {
speed: f32,
direction: u8,
}

// MoveDirection Component
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum MoveDirection {
Expand Down
17 changes: 16 additions & 1 deletion src/game/enemies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ impl Plugin for EnemyPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, spawn_enemies.run_if(in_state(AppState::InGame)))
.add_systems(Update, move_enemies.run_if(in_state(AppState::InGame)))
.add_systems(Update, rotate_enemies.run_if(in_state(AppState::InGame)))
.add_systems(Update, check_collisions.run_if(in_state(AppState::InGame)))
.add_systems(OnExit(AppState::InGame), teardown);
}
Expand Down Expand Up @@ -57,32 +58,46 @@ fn spawn_enemies(mut commands: Commands, time: Res<Time>, mut timer: ResMut<Spaw
}
}

pub fn move_enemies(mut commands: Commands, mut enemies: Query<(Entity, &Enemy, &mut Transform)>) {
pub fn rotate_enemies(mut enemies: Query<(Entity, &Enemy, &mut Transform)>) {
for (_enemy_entity, _enemy, mut transform) in enemies.iter_mut() {
transform.rotate_z(0.01);
}
}

pub fn move_enemies(
mut commands: Commands,
mut enemies: Query<(Entity, &Enemy, &mut Transform)>,
mut event_writer: EventWriter<EnemyKilledEvent>,
) {
for (enemy_entity, enemy, mut transform) in enemies.iter_mut() {
let translation = &mut transform.translation;
match &enemy.direction {
&MoveDirection::Left => {
translation.x -= enemy.speed * TIME_STEP;
if translation.x < (-WINDOW_WIDTH / 2.) - 16. {
commands.entity(enemy_entity).despawn_recursive();
event_writer.send(EnemyKilledEvent(enemy_entity));
}
}
MoveDirection::Right => {
translation.x += enemy.speed * TIME_STEP;
if translation.x > (WINDOW_WIDTH / 2.) - 16. {
commands.entity(enemy_entity).despawn_recursive();
event_writer.send(EnemyKilledEvent(enemy_entity));
}
}
MoveDirection::Up => {
translation.y += enemy.speed * TIME_STEP;
if translation.y > (WINDOW_HEIGHT / 2.) - 16. {
commands.entity(enemy_entity).despawn_recursive();
event_writer.send(EnemyKilledEvent(enemy_entity));
}
}
MoveDirection::Down => {
translation.y -= enemy.speed * TIME_STEP;
if translation.y < (-WINDOW_HEIGHT / 2.) - 16. {
commands.entity(enemy_entity).despawn_recursive();
event_writer.send(EnemyKilledEvent(enemy_entity));
}
}
MoveDirection::None => {
Expand Down
36 changes: 32 additions & 4 deletions src/game/game_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ pub struct GameUiPlugin;
impl Plugin for GameUiPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::InGame), setup)
.add_systems(Update, update.run_if(in_state(AppState::InGame)))
.add_systems(
Update,
(
update.run_if(in_state(AppState::InGame)),
on_enemy_killed.run_if(on_event::<EnemyKilledEvent>()),
),
)
.add_systems(OnExit(AppState::InGame), teardown);
}
}
Expand All @@ -22,11 +28,14 @@ pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn(TextBundle {
style: Style {
position_type: PositionType::Absolute,
left: Val::Px(10.),
left: Val::Px(12.),
top: Val::Px(4.),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
text: Text::from_section(
format!("score: {} ", 0.),
format!("score: {} - enemies {}", 0., 0.),
TextStyle {
font: asset_server.load("fonts/Efforts.ttf"),
font_size: 32.0,
Expand All @@ -50,7 +59,26 @@ pub fn update(
if timer.0.tick(time.delta()).just_finished() {
game_state.score += 10;
let mut text = text_query.single_mut();
text.sections[0].value = format!("score: {}", game_state.score);
text.sections[0].value = format!(
"score: {} - enemies: {}",
game_state.score, game_state.enemies_killed
);
}
}

pub fn on_enemy_killed(
mut events: EventReader<EnemyKilledEvent>,
mut game_state: ResMut<GameState>,
mut text_query: Query<&mut Text>,
) {
for _enemy_killed in events.read() {
let mut text = text_query.single_mut();
game_state.enemies_killed += 1;
game_state.score += 5;
text.sections[0].value = format!(
"score: {} - enemies: {}",
game_state.score, game_state.enemies_killed
);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/gameover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
color: Color::WHITE,
},
),
style: Style {
margin: UiRect {
bottom: Val::Px(20.),
..default()
},
..default()
},
..default()
});
parent
Expand Down Expand Up @@ -99,5 +106,6 @@ fn update_gameover(
fn teardown(mut commands: Commands, menu_data: Res<MenuData>, mut game_state: ResMut<GameState>) {
commands.entity(menu_data.menu_node).despawn_recursive();
// Reset Score!
game_state.enemies_killed = 0;
game_state.score = 0;
}
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ pub enum AppState {
#[derive(Resource)]
pub struct GameState {
pub score: u64,
pub enemies_killed: u64,
}

#[derive(Event)]
struct EnemyKilledEvent(Entity);

fn main() {
App::new()
// Resources
.insert_resource(ClearColor(BG_COLOR))
.insert_resource(SpawnTimer(Timer::from_seconds(0.5, TimerMode::Repeating)))
.insert_resource(ScoreTimer(Timer::from_seconds(1.0, TimerMode::Repeating)))
.insert_resource(GameState { score: 0 })
.insert_resource(GameState {
score: 0,
enemies_killed: 0,
})
// State
.init_state::<AppState>()
// Plugins
Expand All @@ -56,6 +63,7 @@ fn main() {
.add_plugins(GameOverPlugin)
.add_plugins(GamePlugin)
.add_systems(Startup, setup_camera)
.add_event::<EnemyKilledEvent>()
.run();
}

Expand Down

0 comments on commit 17be9c9

Please sign in to comment.