Skip to content

Commit

Permalink
Add hint texts.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Jul 27, 2022
1 parent 134e963 commit e4d5e9f
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 13 deletions.
Binary file added assets/fonts/Alagard.ttf
Binary file not shown.
Binary file added assets/fonts/Boxy-Bold.ttf
Binary file not shown.
Binary file added assets/fonts/Invasion2000.ttf
Binary file not shown.
File renamed without changes.
7 changes: 6 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ pub const BACKGROUND_SHADER: &str = "shaders/background.wgsl";
pub const FONT_FIRA_MONO: &str = "fonts/FiraMono-Medium.ttf";
pub const FONT_FIRA_SANS: &str = "fonts/FiraSans-Bold.ttf";
pub const FONT_ARCADE: &str = "fonts/Arcade.ttf";
pub const FONT_KARMATIC: &str = "fonts/ka1.ttf";
pub const FONT_KARMATIC: &str = "fonts/Karmatic.ttf";
pub const FONT_BOXY: &str = "fonts/Boxy-Bold.ttf";
pub const FONT_ALAGARD: &str = "fonts/Alagard.ttf";
pub const FONT_INVASION: &str = "fonts/Invasion2000.ttf";

pub const PLAYER_SPRITE: &str = "sprites/player.png";
pub const ENEMY_SPRITE: &str = "sprites/enemy.png";
Expand Down Expand Up @@ -47,6 +50,8 @@ pub const AUDIO_CHANNEL_COUNT: usize = 16;
pub const MENU_MUSIC: &str = "musics/E2M2 Myrgharok - Halls of Wandering Spirits.ogg";
pub const GAME_MUSIC: &str = "musics/E3M8 Myrgharok - Mother of All Doom.ogg";

pub const MENU_MUSIC_BPM: f32 = 102.0;

pub const PREDICT_SIZE: usize = 100;
pub const PREDICT_TIME_STEP: f64 = 0.01;
pub const AI_TIME_STEP: f64 = 0.1;
Expand Down
53 changes: 45 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ pub struct AudioVolume {
pub struct MusicTrack(&'static str);

#[derive(Component)]
pub struct TextColor {
pub struct ColorText {
timer: Timer,
colors: Vec<Color>,
index: usize,
}

impl TextColor {
impl ColorText {
pub fn new(colors: Vec<Color>, duration: f32) -> Self {
Self {
timer: Timer::from_seconds(duration, true),
Expand All @@ -58,6 +58,27 @@ impl TextColor {
}
}

#[derive(Component)]
pub struct HintText {
index: usize,
timer: Timer,
}

impl HintText {
const HINT_TEXTS: [&'static str; 3] = [
"Control your ball speed!",
"Can your paddle catch the ball on its own?",
"Try to bounce; not to push!",
];

pub fn new(duration: f32) -> Self {
Self {
index: 0,
timer: Timer::from_seconds(duration, true),
}
}
}

#[wasm_bindgen]
pub fn run() {
let mut app = App::new();
Expand Down Expand Up @@ -89,7 +110,8 @@ pub fn run() {
.add_state(AppState::Loading)
.add_startup_system(setup)
.add_system(lock_release_cursor)
.add_system(text_color_system)
.add_system(color_text_system)
.add_system(hint_text_system)
.add_plugin(loading::LoadingPlugin)
.add_plugin(menu::MenuPlugin)
.add_plugin(game::GamePlugin)
Expand Down Expand Up @@ -122,11 +144,26 @@ fn lock_release_cursor(app_state: Res<State<AppState>>, mut windows: ResMut<Wind
}
}

fn text_color_system(time: Res<Time>, mut query: Query<(&mut Text, &mut TextColor)>) {
for (mut text, mut text_color) in query.iter_mut() {
text.sections[0].style.color = text_color.colors[text_color.index];
if text_color.timer.tick(time.delta()).just_finished() {
text_color.index = (text_color.index + 1) % text_color.colors.len();
fn color_text_system(time: Res<Time>, mut query: Query<(&mut Text, &mut ColorText)>) {
for (mut text, mut color_text) in query.iter_mut() {
text.sections[0].style.color = color_text.colors[color_text.index];
if color_text.timer.tick(time.delta()).just_finished() {
color_text.index = (color_text.index + 1) % color_text.colors.len();
}
}
}

pub fn hint_text_system(time: Res<Time>, mut query: Query<(&mut Text, &mut HintText)>) {
for (mut text, mut hint) in query.iter_mut() {
text.sections[0].value = HintText::HINT_TEXTS[hint.index].into();

if hint.timer.tick(time.delta()).just_finished() {
let len = HintText::HINT_TEXTS.len();
let mut next = fastrand::usize(0..len);
if next == hint.index {
next = (hint.index + 1) % len;
}
hint.index = next;
}
}
}
3 changes: 3 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ fn setup(server: Res<AssetServer>, mut loading: ResMut<AssetsLoading>) {
loading.push(server.load_untyped(FONT_FIRA_SANS));
loading.push(server.load_untyped(FONT_ARCADE));
loading.push(server.load_untyped(FONT_KARMATIC));
loading.push(server.load_untyped(FONT_BOXY));
loading.push(server.load_untyped(FONT_ALAGARD));
loading.push(server.load_untyped(FONT_INVASION));

loading.push(server.load_untyped(PLAYER_SPRITE));
loading.push(server.load_untyped(ENEMY_SPRITE));
Expand Down
34 changes: 32 additions & 2 deletions src/menu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
config::*,
utils::{cleanup_system, escape_system},
AppState, AudioVolume, MusicTrack, TextColor, TimeScale,
AppState, AudioVolume, ColorText, HintText, MusicTrack, TimeScale,
};
use bevy::prelude::*;
use bevy_kira_audio::{Audio, AudioChannel};
Expand Down Expand Up @@ -176,7 +176,37 @@ fn make_menu(
),
..Default::default()
})
.insert(TextColor::new(FLIP_TEXT_COLORS.into(), 0.3));
.insert(ColorText::new(
FLIP_TEXT_COLORS.into(),
30.0 / MENU_MUSIC_BPM,
));

parent
.spawn_bundle(TextBundle {
style: Style {
position_type: PositionType::Absolute,
position: Rect {
left: Val::Percent(10.0),
top: Val::Percent(40.0),
..Default::default()
},
..Default::default()
},
text: Text::with_section(
"",
TextStyle {
font: asset_server.load(FONT_INVASION),
font_size: 15.0,
color: HEALTH_BAR_COLOR,
},
TextAlignment {
horizontal: HorizontalAlign::Left,
..Default::default()
},
),
..Default::default()
})
.insert(HintText::new(480.0 / MENU_MUSIC_BPM));

parent
.spawn_bundle(ButtonBundle {
Expand Down
7 changes: 5 additions & 2 deletions src/score.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{config::*, AppState, TextColor, TimeScale};
use crate::{config::*, AppState, ColorText, TimeScale};
use bevy::prelude::*;

pub struct ScorePlugin;
Expand Down Expand Up @@ -79,7 +79,10 @@ fn make_ui(
),
..Default::default()
})
.insert(TextColor::new(FLIP_TEXT_COLORS.into(), 0.3));
.insert(ColorText::new(
FLIP_TEXT_COLORS.into(),
30.0 / MENU_MUSIC_BPM,
));

let term_style = Style {
size: Size::new(Val::Percent(100.0), Val::Px(30.0)),
Expand Down

0 comments on commit e4d5e9f

Please sign in to comment.