Skip to content

Commit

Permalink
Better messages when hovering over items.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Aug 1, 2023
1 parent 1592434 commit ce39d14
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/click.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::components::HoverNote;

use super::prelude::*;
use super::selection_systems::SelectionEvent;

Expand Down Expand Up @@ -137,7 +139,7 @@ pub fn mouse_move_system(
windows: Res<Windows>,
q_camera: Query<(&Camera, &GlobalTransform)>,
// dragging: Res<Dragging>, Use to only highlight a specific type in the future??
positions: Query<(Entity, &Position, Option<&Brain>)>,
positions: Query<(Entity, &Position, Option<&Brain>, Option<&Food>, Option<&Plant>)>,
mut object_info: ResMut<SelectedObjectInformation>,
) {
let (camera, camera_transform) = q_camera.single();
Expand All @@ -150,12 +152,21 @@ pub fn mouse_move_system(
let pos = pos.unwrap();
// Append info for each object to the SelectedObjectInfo.
object_info.info = vec![];
for (_e, p, b) in positions.iter() {
for (_e, p, b, f, plant) in positions.iter() {
if (p.x == pos.x) && (p.y == pos.y) {
object_info.info.push("Object ".to_string());
if let Some(f) = f {
object_info.info.push(f.hover_note());
}
if let Some(plant) = plant {
object_info.info.push(plant.hover_note());
}
if let Some(brain) = b {
object_info.info.push(format!("Task: {:?}", brain.task));
object_info.info.push(format!("Motivation: {:?}", brain.motivation));
if let Some(task) = brain.task {
object_info.info.push(format!("Task: {:?}", task));
}
if let Some(motivation) = brain.motivation {
object_info.info.push(format!("Motivation: {:?}", motivation));
}
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,33 @@ pub struct PauseOverlay;
#[derive(Component)]
pub struct MainMenuOverlay;

pub trait HoverNote {
fn hover_note(&self) -> String;
}

#[derive(Component)]
pub struct Food {
pub nutrition: f32,
pub spoilage: f32,
pub spoilage_rate: f32,
pub name: String,
}
impl Default for Food {
fn default() -> Self {
Food {
nutrition: 10.0,
spoilage: 1.0,
spoilage_rate: 0.1,
spoilage_rate: 0.03,
name: "Food".to_string(),
}
}
}
impl HoverNote for Food {
fn hover_note(&self) -> String {
let spoilage_percent = self.spoilage * 100.0;
format!("Spoilage: {:.2}%", spoilage_percent)
}
}

#[derive(Component)]
pub struct HasName {
Expand Down Expand Up @@ -177,6 +189,11 @@ pub struct Plant {
pub growth: f32,
pub plant_type: PlantType,
}
impl HoverNote for Plant {
fn hover_note(&self) -> String {
format!("{:?} Growth: {:.2}%", self.plant_type, self.growth * 100.0)
}
}

#[derive(Component)]
pub struct WorkMarker;
Expand Down Expand Up @@ -342,6 +359,7 @@ pub enum SelectableType {
Gatherable,
Harvestable,
Mineable,
Nothing,
Unselecting,
Unzoning,
Zoning,
Expand Down
12 changes: 9 additions & 3 deletions src/game_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn start_game_ui(
],vec![ // farm
"BACK",
"NOTHING",
"BERRIES",
"CABBAGE",
"PINE",
"OAK",
"CEDAR",
Expand Down Expand Up @@ -173,7 +173,10 @@ pub fn game_ui_click(
}
MenuStates::Tasks => { // chop, forage, gather, hunt, mine
match button_index {
0 => { menu_state.state = MenuStates::Home; },
0 => {
dragging.looking_for = SelectableType::Nothing;
menu_state.state = MenuStates::Home;
},
1 => {
dragging.looking_for = SelectableType::Unselecting;
},
Expand All @@ -193,7 +196,10 @@ pub fn game_ui_click(
}
MenuStates::Farm => {
match button_index {
0 => { menu_state.state = MenuStates::Home; },
0 => {
dragging.looking_for = SelectableType::Nothing;
menu_state.state = MenuStates::Home;
},
1 => {
dragging.looking_for = SelectableType::Unzoning;
},
Expand Down
16 changes: 16 additions & 0 deletions src/selection_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ impl Plugin for SelectionPlugin {
SystemSet::on_update(GameState::InGame)
.with_system(select_unzoning),
)
.add_system_set(
SystemSet::on_update(GameState::InGame)
.with_system(select_nothing),
)
;
}
}
Expand Down Expand Up @@ -144,6 +148,18 @@ fn select_unselecting(
unhighlight(commands, highlighteds, highlightboxes);
}

fn select_nothing(
mut commands: Commands,
highlighteds: Query<Entity, With<Highlighted>>,
highlightboxes: Query<Entity, With<HighlightBox>>,
event: EventReader<SelectionEvent>,
dragging: Res<Dragging>,
) {
if event.is_empty() { return; }
if dragging.looking_for != SelectableType::Nothing { return; }
unhighlight(commands, highlighteds, highlightboxes);
}

fn select_unzoning(
mut commands: Commands,
highlighteds: Query<Entity, With<Highlighted>>,
Expand Down

0 comments on commit ce39d14

Please sign in to comment.