Skip to content

Commit

Permalink
Make cue types selectable
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmauro committed Apr 3, 2024
1 parent c1fa50e commit f9ef04d
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 115 deletions.
68 changes: 54 additions & 14 deletions src/game/core/cue.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::collections::VecDeque;

use bevy::prelude::*;
use bevy::{prelude::*, render::color};
use rand::{
distributions::{Distribution, Standard},
Rng,
};

use crate::game::tile::{color::TileColor, position::TilePosition, sound::TileSound};
use crate::game::tile::{
color::TileColor,
position::{self, TilePosition},
sound::TileSound,
};

#[derive(Component, Deref, DerefMut)]
pub struct CueTimer(pub Timer);
Expand Down Expand Up @@ -87,27 +91,63 @@ impl<T: PartialEq + Default> CueChain<T> {
#[derive(Component, Resource)]
pub struct CueEngine {
n: usize,
pub positions: CueChain<TilePosition>,
pub colors: CueChain<TileColor>,
pub sounds: CueChain<TileSound>,
pub positions: Option<CueChain<TilePosition>>,
pub colors: Option<CueChain<TileColor>>,
pub sounds: Option<CueChain<TileSound>>,
}

impl CueEngine {
pub fn with_n(n: usize) -> Self {
pub fn with(n: usize, position: bool, color: bool, sound: bool) -> Self {
let positions = if position {
Some(CueChain::with_n_back(n))
} else {
None
};

let colors = if color {
Some(CueChain::with_n_back(n))
} else {
None
};

let sounds = if sound {
Some(CueChain::with_n_back(n))
} else {
None
};

CueEngine {
n,
positions: CueChain::with_n_back(n),
colors: CueChain::with_n_back(n),
sounds: CueChain::with_n_back(n),
positions,
colors,
sounds,
}
}

pub fn n(&self) -> usize {
self.n
}

pub fn new_cue(&mut self) -> (TilePosition, TileColor, TileSound) {
(self.positions.gen(), self.colors.gen(), self.sounds.gen())
pub fn new_cue(&mut self) -> (Option<TilePosition>, Option<TileColor>, Option<TileSound>) {
let new_position = if self.positions.is_some() {
Some(self.positions.as_mut().unwrap().gen())
} else {
None
};

let new_color = if self.colors.is_some() {
Some(self.colors.as_mut().unwrap().gen())
} else {
None
};

let new_sound = if self.sounds.is_some() {
Some(self.sounds.as_mut().unwrap().gen())
} else {
None
};

(new_position, new_color, new_sound)
}
}

Expand All @@ -117,9 +157,9 @@ impl Default for CueEngine {

CueEngine {
n,
positions: CueChain::with_n_back(n),
colors: CueChain::with_n_back(n),
sounds: CueChain::with_n_back(n),
positions: Some(CueChain::with_n_back(n)),
colors: Some(CueChain::with_n_back(n)),
sounds: Some(CueChain::with_n_back(n)),
}
}
}
238 changes: 137 additions & 101 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ fn setup(
..default()
},
DualNBackBundle {
engine: CueEngine::with_n(settings.n),
engine: CueEngine::with(
settings.n,
settings.position,
settings.color,
settings.sound,
),
round: Round::with_total(settings.rounds),
timer: CueTimer::with_duration(settings.round_time),
..default()
Expand All @@ -173,95 +178,101 @@ fn setup(
OnGameScreen,
))
.with_children(|parent| {
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
left: Val::Px(-100.0),
top: Val::Px(230.0),
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
if settings.position {
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
left: Val::Px(-100.0),
top: Val::Px(230.0),
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: config::BUTTON_BORDER_COLOR.into(),
background_color: config::NORMAL_BUTTON.into(),
..default()
},
border_color: config::BUTTON_BORDER_COLOR.into(),
background_color: config::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyA),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Position (A)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
shortcut: Shortcut(KeyCode::KeyA),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Position (A)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
}

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
left: Val::Px(0.0),
top: Val::Px(230.0),
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
if settings.sound {
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
left: Val::Px(0.0),
top: Val::Px(230.0),
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: config::BUTTON_BORDER_COLOR.into(),
background_color: config::NORMAL_BUTTON.into(),
..default()
},
border_color: config::BUTTON_BORDER_COLOR.into(),
background_color: config::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyS),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Sound (S)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
shortcut: Shortcut(KeyCode::KeyS),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Sound (S)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
}

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
left: Val::Px(100.0),
top: Val::Px(230.0),
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
if settings.color {
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
left: Val::Px(100.0),
top: Val::Px(230.0),
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: config::BUTTON_BORDER_COLOR.into(),
background_color: config::NORMAL_BUTTON.into(),
..default()
},
border_color: config::BUTTON_BORDER_COLOR.into(),
background_color: config::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyD),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Color (D)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
shortcut: Shortcut(KeyCode::KeyD),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Color (D)",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
}
});
}

Expand Down Expand Up @@ -309,36 +320,61 @@ fn end_of_round_system(
query.get_single_mut()
{
if timer.just_finished() {
if round.answer.position {
if engine.positions.is_match() {
score.record_tp();
if let Some(positions) = &engine.positions {
if round.answer.position {
if positions.is_match() {
score.record_tp();
} else {
score.record_fp();
}
} else if positions.is_match() {
score.record_fn();
} else {
score.record_tn();
}
}

if let Some(colors) = &engine.colors {
if round.answer.color {
if colors.is_match() {
score.record_tp();
} else {
score.record_fp();
}
} else if colors.is_match() {
score.record_fn();
} else {
score.record_fp();
score.record_tn();
}
} else if engine.positions.is_match() {
score.record_fn();
} else {
score.record_tn();
}

if round.answer.color {
if engine.colors.is_match() {
score.record_tp();
if let Some(sounds) = &engine.sounds {
if round.answer.sound {
if sounds.is_match() {
score.record_tp();
} else {
score.record_fp();
}
} else if sounds.is_match() {
score.record_fn();
} else {
score.record_fp();
score.record_tn();
}
} else if engine.colors.is_match() {
score.record_fn();
} else {
score.record_tn();
}

round.answer.reset();

let (new_position, new_color, new_sound) = engine.new_cue();
*position = new_position;
*color = new_color;
*sound = new_sound;
if let Some(new_position) = new_position {
*position = new_position;
}
if let Some(new_color) = new_color {
*color = new_color;
}
if let Some(new_sound) = new_sound {
*sound = new_sound;
}

round.current += 1;
}
}
Expand Down
Loading

0 comments on commit f9ef04d

Please sign in to comment.