Skip to content

Commit

Permalink
Start working on hauling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Aug 20, 2023
1 parent dbf74e3 commit a243844
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 37 deletions.
8 changes: 5 additions & 3 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,11 @@ pub struct Nest {
pub position: Position,
}

// MARKED TASKS
#[derive(Component)]
pub struct Carryable;
#[derive(Component)]
pub struct Foragable;

#[derive(Component)]
pub struct Choppable;
#[derive(Component)]
Expand Down Expand Up @@ -645,7 +647,7 @@ pub enum ZoneType {
#[derive(Component, PartialEq, Copy, Clone, Debug)]
pub enum Task { // Sorted in order of prioritization.
Crisis, Flee, Fight, Eat, Hospital, Sleep, Sleeping, Play, Order, Work, Personality, Meander, Idle,
Doctor, Forage, Plant, Harvest, Mine, Chop, Construct, Hunt, Milk, Cook, Fish, Craft, Clean, Haul // Forms of work
Doctor, Forage, Plant, Harvest, Mine, Chop, Construct, Hunt, Milk, Cook, Fish, Craft, Clean, Pickup, Carrying // Forms of work
}
impl Task {
pub fn is_zone_task(&self) -> bool {
Expand Down Expand Up @@ -732,10 +734,10 @@ pub enum ForageType {

#[derive(Component, PartialEq, Copy, Clone, Debug)]
pub enum SelectableType {
Carryable,
Choppable,
Constructable,
Foragable,
Gatherable,
Harvestable,
Huntable,
Mineable,
Expand Down
2 changes: 2 additions & 0 deletions src/initializations/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ pub fn startup(
})
.insert(position)
.insert(position.to_transform_layer(0.5))
.insert( Object { itemtype: object_type, ..default() } )
.id()
;
object_type.add_components(&mut commands, object);
}
}
20 changes: 14 additions & 6 deletions src/interface/game_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn start_game_ui(
"CLEAR",
"CHOP",
"FORAGE",
"COLLECT",
"CARRY",
"HUNT",
"MINE",
],vec![ // farm
Expand All @@ -105,7 +105,7 @@ pub fn start_game_ui(
"BACK",
"NOTHING",
"FISHING",
"HOSPITAL",
"STORAGE",
"MEETING",
"AVOID",
],vec![ // build
Expand Down Expand Up @@ -176,7 +176,7 @@ pub fn game_ui_click(
_ => { },
}
}
MenuStates::Tasks => { // chop, forage, gather, hunt, mine
MenuStates::Tasks => { // chop, forage, carry, hunt, mine
match button_index {
0 => {
dragging.looking_for = SelectableType::Nothing;
Expand All @@ -190,11 +190,15 @@ pub fn game_ui_click(
},
3 => {
dragging.looking_for = SelectableType::Foragable;
dragging.zone_type = ZoneType::Farm;
},
4 => {
dragging.looking_for = SelectableType::Gatherable;
dragging.zone_type = ZoneType::Farm;
dragging.looking_for = SelectableType::Carryable;
},
5 => {
dragging.looking_for = SelectableType::Huntable;
},
6 => {
dragging.looking_for = SelectableType::Mineable;
},
_ => { },
}
Expand Down Expand Up @@ -249,6 +253,10 @@ pub fn game_ui_click(
}
MenuStates::Zone => {
match button_index {
3 => {
dragging.looking_for = SelectableType::Zoning;
dragging.zone_type = ZoneType::Storage;
}
5 => {
dragging.looking_for = SelectableType::Zoning;
dragging.zone_type = ZoneType::Avoid;
Expand Down
16 changes: 15 additions & 1 deletion src/objects.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, ecs::component};
use crate::prelude::*;

#[derive(Component)]
Expand Down Expand Up @@ -283,6 +283,12 @@ impl ItemType {
_ => 0.01,
}
}
pub fn carryable(&self) -> bool {
match self.group() {
ItemGroup::Statues => true,
_ => false,
}
}
pub fn passable(&self) -> bool {
match self.group() {
ItemGroup::Statues => false,
Expand All @@ -304,6 +310,14 @@ impl ItemType {
pub fn potential_replacements(&self) -> Vec<ItemType> {
self.group().items()
}
pub fn add_components(&self,
commands: &mut Commands,
entity: Entity
) {
if self.carryable() {
commands.entity(entity).insert( Carryable );
}
}

fn group(&self) -> ItemGroup {
for group in ItemGroup::all() {
Expand Down
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub use super::components::{
ActorType, Affliction, AfflictionType, AfflictionLocation, Attackable, Attacked, Attributeset,
Bed, Brain, Choppable, ClickedOn, Danger, DangerType, Dying, Food, Foragable, ForageType, GameState, GeneratedBy,
Bed, Brain, Carryable, Choppable, ClickedOn, Danger, DangerType, Dying, Food, Foragable, ForageType, GameState, GeneratedBy,
GiveMeAName, HasName, HasNameShown, HighlightBox, Highlighted, HoverNote, Huntable, InfoPanel, InGameButton, IsName,
Logs, MainMenuOverlay, MapTile, MenuStates, MonsterGenerator, Motivation, MoveRandom,
Logs, MainMenuOverlay, MapTile, MenuStates, Mineable, MonsterGenerator, Motivation, MoveRandom,
MoveTowardsNearestAttackable, MoveTowardsTarget, NearestEntity, Need, Nest,
Order, Pathing, PauseOverlay, PersonalityTrait, PhysicalBody, Plant, Position,
SelectableType, SetNest, Skillset, Skill, SizeXYZ, StrikeType,
Expand Down
83 changes: 63 additions & 20 deletions src/selection_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ impl Plugin for SelectionPlugin {
.run_if(in_state(GameState::InGame))
)
)
.add_systems(
Update,
(select_huntables, select_mineables, select_carryables)
.run_if(in_state(GameState::InGame))
)
;
}
}
Expand Down Expand Up @@ -79,16 +84,7 @@ pub fn select_choppables(
for (entity, selection_reason) in query.iter_mut() {
if selection_reason.is_some() {
commands.entity(entity).insert(WorkTarget);
let child = commands.spawn((
Text2dBundle {
text: Text::from_section("X", TextStyle { font: font.0.clone(), ..default() })
.with_alignment(TextAlignment::Center),
..default()
},
WorkMarker
))
.insert(Transform::from_xyz(10.0, 20.0, 100.0)).id();
commands.entity(entity).push_children(&[child]);
create_marker(&mut commands, &entity, font.0.clone());
}
}
unhighlight(commands, highlighteds, highlightboxes);
Expand All @@ -108,16 +104,47 @@ pub fn select_huntables(
for (entity, selection_reason) in query.iter_mut() {
if selection_reason.is_some() {
commands.entity(entity).insert(WorkTarget);
let child = commands.spawn((
Text2dBundle {
text: Text::from_section("X", TextStyle { font: font.0.clone(), ..default() })
.with_alignment(TextAlignment::Center),
..default()
},
WorkMarker
))
.insert(Transform::from_xyz(10.0, 20.0, 100.0)).id();
commands.entity(entity).push_children(&[child]);
create_marker(&mut commands, &entity, font.0.clone());
}
}
unhighlight(commands, highlighteds, highlightboxes);
}

pub fn select_mineables(
mut commands: Commands,
mut query: Query<(Entity, Option<&Mineable>), With<Highlighted>>,
highlighteds: Query<Entity, With<Highlighted>>,
highlightboxes: Query<Entity, With<HighlightBox>>,
event: EventReader<SelectionEvent>,
dragging: Res<Dragging>,
font: Res<MyFont>,
) {
if event.is_empty() { return; }
if dragging.looking_for != SelectableType::Mineable { return; }
for (entity, selection_reason) in query.iter_mut() {
if selection_reason.is_some() {
commands.entity(entity).insert(WorkTarget);
create_marker(&mut commands, &entity, font.0.clone());
}
}
unhighlight(commands, highlighteds, highlightboxes);
}
pub fn select_carryables(
mut commands: Commands,
mut query: Query<(Entity, Option<&Object>), With<Highlighted>>,
highlighteds: Query<Entity, With<Highlighted>>,
highlightboxes: Query<Entity, With<HighlightBox>>,
event: EventReader<SelectionEvent>,
dragging: Res<Dragging>,
font: Res<MyFont>,
) {
if event.is_empty() { return; }
if dragging.looking_for != SelectableType::Carryable { return; }
for (entity, selection_reason) in query.iter_mut() {
if selection_reason.is_some() {
if ! selection_reason.unwrap().itemtype.carryable() { continue; }
commands.entity(entity).insert(WorkTarget);
create_marker(&mut commands, &entity, font.0.clone());
}
}
unhighlight(commands, highlighteds, highlightboxes);
Expand Down Expand Up @@ -222,4 +249,20 @@ fn select_unzoning(
}
}
unhighlight(commands, highlighteds, highlightboxes);
}
pub fn create_marker(
commands: &mut Commands,
entity: &Entity,
font: Handle<Font>,
) {
let child = commands.spawn((
Text2dBundle {
text: Text::from_section("X", TextStyle { font: font, ..default() })
.with_alignment(TextAlignment::Center),
..default()
},
WorkMarker
))
.insert(Transform::from_xyz(10.0, 20.0, 100.0)).id();
commands.entity(*entity).push_children(&[child]);
}
15 changes: 10 additions & 5 deletions src/task_system/plant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ pub fn task_system_zone(
'targets: for (targetable_entity, targetable_position, zone) in targetables.iter() {
if zone.zone_type == ZoneType::Farm && brain.task != Some(Task::Plant) { continue; }
if zone.zone_type == ZoneType::Construction && brain.task != Some(Task::Construct) { continue; }
if zone.zone_type == ZoneType::Storage && brain.task != Some(Task::Carrying) { continue; }
for (e, obstacle) in obstacles.iter() {
if (obstacle == targetable_position) && (entity != e) { continue 'targets; }
} // Don't plant or build on top of obstacles.
// If you are already next to it, plant it, if you are targetting it.
let distance = position.distance(targetable_position);
if distance <= 1 && targeting.is_some() && targeting.unwrap().target == targetable_entity {
commands.entity(entity).remove::<Targeting>();
if zone.zone_type == ZoneType::Farm {
spawn_plant(&mut commands, targetable_position, &sprite_sheet, zone); // Did plant! Now, go ahead and try planting again....
} else {
spawn_building(&mut commands, targetable_position, &sprite_sheet, zone);
commands.entity(targetable_entity).despawn_recursive();
match zone.zone_type {
ZoneType::Farm => {
spawn_plant(&mut commands, targetable_position, &sprite_sheet, zone);
}
ZoneType::Construction => {
spawn_building(&mut commands, targetable_position, &sprite_sheet, zone);
commands.entity(targetable_entity).despawn_recursive();
}
_ => {}
}
continue 'brains;
}
Expand Down

0 comments on commit a243844

Please sign in to comment.