diff --git a/assets/config.json b/assets/config.json index 4798184..0e6ba58 100644 --- a/assets/config.json +++ b/assets/config.json @@ -27,5 +27,6 @@ "growth_stages": 6 } ], - "wonder_event_url": "wss://pomomon.farm/ws" + "wonder_event_url": "wss://pomomon.farm/ws", + "berries_per_focus_minute": 1 } diff --git a/src/features/config/components.rs b/src/features/config/components.rs index d9b3b14..1f3ccb8 100644 --- a/src/features/config/components.rs +++ b/src/features/config/components.rs @@ -11,6 +11,7 @@ pub struct GameConfig { pub shovel_rate: f32, pub berry_seeds: Vec, pub wonder_event_url: String, + pub berries_per_focus_minute: u32, } #[derive(Deserialize, Debug, Clone)] @@ -54,6 +55,7 @@ impl Default for GameConfig { }, ], wonder_event_url: "wss://pomomon.farm/ws".into(), + berries_per_focus_minute: 1, } } } diff --git a/src/features/mod.rs b/src/features/mod.rs index 609a10c..6b6c2b8 100644 --- a/src/features/mod.rs +++ b/src/features/mod.rs @@ -5,6 +5,7 @@ pub mod grid; pub mod hud; pub mod input; pub mod inventory; +pub mod notification; pub mod phase; pub mod pom; pub mod savegame; @@ -19,6 +20,7 @@ pub use grid::GridPlugin; pub use hud::HudPlugin; pub use input::InputPlugin; pub use inventory::InventoryPlugin; +pub use notification::NotificationPlugin; pub use phase::PhasePlugin; pub use pom::PomPlugin; pub use savegame::SavegamePlugin; diff --git a/src/features/notification/components.rs b/src/features/notification/components.rs new file mode 100644 index 0000000..3f1f2b0 --- /dev/null +++ b/src/features/notification/components.rs @@ -0,0 +1,74 @@ +use crate::prelude::*; + +#[derive(Resource)] +pub struct Notifications { + items: Vec<(Notification, NotificationLevel)>, +} + +impl Default for Notifications { + fn default() -> Self { + Self { items: Vec::new() } + } +} + +impl Notifications { + fn new( + &mut self, + level: NotificationLevel, + title: Option>, + message: impl Into, + ) { + self.items.push(( + Notification { + title: title.map(|s| s.into()), + message: message.into(), + }, + level, + )); + } + + pub fn drain(&mut self) -> std::vec::Drain<'_, (Notification, NotificationLevel)> { + self.items.drain(..) + } + + pub fn info(&mut self, title: Option>, message: impl Into) { + self.new(NotificationLevel::Info, title, message); + } + + pub fn warn(&mut self, title: Option>, message: impl Into) { + self.new(NotificationLevel::Warning, title, message); + } + + pub fn error(&mut self, title: Option>, message: impl Into) { + self.new(NotificationLevel::Error, title, message); + } +} + +#[derive(Component)] +pub struct NotificationContainer; + +#[derive(Component)] +pub struct NotificationLifetime(pub Timer); + +#[derive(Component)] +pub struct Notification { + pub title: Option, + pub message: String, +} + +#[derive(Component)] +pub enum NotificationLevel { + Info, + Warning, + Error, +} + +impl NotificationLevel { + pub fn bg_color(&self) -> Color { + match self { + NotificationLevel::Info => Color::srgba(0.0, 0.0, 0.0, 0.7), + NotificationLevel::Warning => Color::srgba(1.0, 1.0, 0.0, 0.7), + NotificationLevel::Error => Color::srgba(1.0, 0.0, 0.0, 0.7), + } + } +} diff --git a/src/features/notification/mod.rs b/src/features/notification/mod.rs new file mode 100644 index 0000000..a639706 --- /dev/null +++ b/src/features/notification/mod.rs @@ -0,0 +1,58 @@ +use crate::{ + features::notification::ui::{notification_container, spawn_notification}, + prelude::*, +}; +use components::{NotificationContainer, NotificationLifetime, Notifications}; + +pub mod components; +pub mod ui; + +pub struct NotificationPlugin; + +impl Plugin for NotificationPlugin { + fn build(&self, app: &mut App) { + app.init_resource::() + .add_systems(Startup, setup) + .add_systems(Update, handle_notification); + } +} + +// Spawns the notification container up +fn setup(mut commands: Commands) { + commands.spawn(notification_container()); +} + +// Spawns/Despawns UI elements for each item in the `Notifications` Resource +fn handle_notification( + mut commands: Commands, + mut notifications: ResMut, + container_q: Query>, + mut notification_entities: Query<(Entity, &mut NotificationLifetime)>, + time: Res