From b101ba9c0cf9eea5f4036deface6eb68bfa65538 Mon Sep 17 00:00:00 2001 From: ryankopf Date: Tue, 8 Aug 2023 21:08:15 -0500 Subject: [PATCH] Adding territorial personality trait. --- src/components.rs | 11 +++++++-- src/initializations.rs | 2 ++ src/{ => initializations}/startup.rs | 18 +++++++-------- src/interface/game_ui.rs | 1 - src/main.rs | 2 -- src/prelude.rs | 2 +- src/selection_systems.rs | 1 - src/task_system.rs | 3 +++ src/task_system/personality.rs | 17 ++++++++++++++ src/task_system/personality/nopersonality.rs | 13 +++++++++++ src/task_system/personality/territorial.rs | 24 ++++++++++++++++++++ src/thinking_system.rs | 15 +++++++++++- src/unitgenerator_system.rs | 4 ++-- 13 files changed, 93 insertions(+), 20 deletions(-) rename src/{ => initializations}/startup.rs (88%) create mode 100644 src/task_system/personality.rs create mode 100644 src/task_system/personality/nopersonality.rs create mode 100644 src/task_system/personality/territorial.rs diff --git a/src/components.rs b/src/components.rs index 73bd8a0..4b1e98f 100644 --- a/src/components.rs +++ b/src/components.rs @@ -129,6 +129,7 @@ impl ActorType { #[derive(Component, PartialEq, Clone, Debug)] pub enum TileType { Grass, + Cave, Dirt, Gravel, Sand, @@ -145,6 +146,7 @@ impl TileType { pub fn sprite_row_and_col(&self) -> (usize, usize) { match self { TileType::Grass => (9, 11), + TileType::Cave => (13, 7), TileType::Dirt => (4, 1), TileType::Gravel => (7, 42), TileType::Sand => (7, 42), @@ -547,6 +549,11 @@ pub enum PersonalityTrait { Creature, Social, Vicious, Territorial, Docile, } +#[derive(Component)] +pub struct Nest { + pub position: Position, +} + #[derive(Component)] pub struct Foragable; @@ -605,12 +612,12 @@ 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, Meander, Idle, + 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 } #[derive(Component, PartialEq, Copy, Clone, Debug)] pub enum Motivation { // Sorted in order of prioritization. - Crisis, Rage, Order, Danger, Hunger, Thirst, Tired, Injured, Sick, Bored, Happy, Sad, Angry, Lonely, Love, Fear, Hate, Work, Meander, Idle + Crisis, Rage, Order, Danger, Hunger, Thirst, Tired, Injured, Sick, Bored, Happy, Sad, Angry, Lonely, Love, Fear, Hate, Work, Personality, Meander, Idle } #[derive(Component, PartialEq, Copy, Clone, Debug)] diff --git a/src/initializations.rs b/src/initializations.rs index e3d8b4a..897e9e6 100644 --- a/src/initializations.rs +++ b/src/initializations.rs @@ -4,5 +4,7 @@ pub mod load; pub use load::*; pub mod map; pub use map::*; +pub mod startup; +pub use startup::*; pub mod window_system; pub use window_system::*; \ No newline at end of file diff --git a/src/startup.rs b/src/initializations/startup.rs similarity index 88% rename from src/startup.rs rename to src/initializations/startup.rs index 7aba697..dcf56d8 100644 --- a/src/startup.rs +++ b/src/initializations/startup.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use super::components::{Position, SizeXYZ}; -use super::prelude::*; +use crate::components::{Position, SizeXYZ}; +use crate::prelude::*; use crate::spawn_unit_from_template; use crate::UnitTemplate; @@ -42,19 +42,17 @@ pub fn startup( spawn_unit_from_template(&mut commands, position, &sprite_sheet, &UnitTemplate::crab()); } - let position = Position { x: 10, y: 10, z: 0 }; + let position = Position { x: 10, y: 6, z: 0 }; + let sprite = TextureAtlasSprite::new(TileType::Cave.sprite_index()); commands - .spawn(SpriteBundle { - sprite: Sprite { - color: Color::BLACK, - custom_size: Some(Vec2::new(TILE_SIZE, TILE_SIZE)), - ..default() - }, + .spawn(SpriteSheetBundle { + sprite, + texture_atlas: sprite_sheet.0.clone(), ..default() }) .insert(position) .insert(SizeXYZ::cube(1.1)) - .insert(super::components::MonsterGenerator) + .insert(MonsterGenerator) .insert(position.to_transform_layer(1.0)) ; diff --git a/src/interface/game_ui.rs b/src/interface/game_ui.rs index 7eb4d92..0075942 100644 --- a/src/interface/game_ui.rs +++ b/src/interface/game_ui.rs @@ -167,7 +167,6 @@ pub fn game_ui_click( let y = window.height() - wc.y; if y > 30.0 && y < 92.0 { let button_index = (wc.x as i32 - 100) / 100; - println!("BUTTON: {}", button_index); match menu_state.state { MenuStates::Home => { match button_index { diff --git a/src/main.rs b/src/main.rs index bfaf4b0..5ae63ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,8 +32,6 @@ mod selection_systems; use selection_systems::*; mod spoilage_system; use spoilage_system::*; -mod startup; -use startup::*; mod statusdisplay_system; use statusdisplay_system::*; mod task_system; diff --git a/src/prelude.rs b/src/prelude.rs index bc44077..403e141 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,7 +3,7 @@ pub use super::components::{ Bed, Brain, Choppable, ClickedOn, Danger, DangerType, Dying, Food, Foragable, ForageType, GameState, GeneratedBy, GiveMeAName, HasName, HasNameShown, HighlightBox, Highlighted, HoverNote, InfoPanel, InGameButton, IsName, ItemType, Logs, MainMenuOverlay, MapTile, MenuStates, MonsterGenerator, Motivation, MoveRandom, - MoveTowardsNearestAttackable, MoveTowardsTarget, NearestEntity, Need, + MoveTowardsNearestAttackable, MoveTowardsTarget, NearestEntity, Need, Nest, Order, Pathing, PauseOverlay, PersonalityTrait, PhysicalBody, Plant, PlantType, Position, SelectableType, Skillset, Skill, SizeXYZ, StrikeType, Targeting, Task, TemporaryVisualElement, TextName, TileType, WorkMarker, WorkTarget, Zone, ZoneMarker, ZoneType, diff --git a/src/selection_systems.rs b/src/selection_systems.rs index f79cd74..afa5f0c 100644 --- a/src/selection_systems.rs +++ b/src/selection_systems.rs @@ -108,7 +108,6 @@ pub fn select_zoning( ..default() }, ZoneMarker)) .id(); - println!("Zonemarker"); commands.entity(entity).push_children(&[zonemarker]); } } diff --git a/src/task_system.rs b/src/task_system.rs index 9f14609..2cf21e7 100644 --- a/src/task_system.rs +++ b/src/task_system.rs @@ -3,6 +3,8 @@ mod chop; mod eat; mod forage; mod meander; +mod personality; +use personality::PersonalityPlugin; mod plant; mod play; mod sleep; @@ -14,6 +16,7 @@ pub struct TaskPlugin; impl Plugin for TaskPlugin { fn build(&self, app: &mut App) { app + .add_plugins(PersonalityPlugin) .add_systems( Update, ( diff --git a/src/task_system/personality.rs b/src/task_system/personality.rs new file mode 100644 index 0000000..1b88fb2 --- /dev/null +++ b/src/task_system/personality.rs @@ -0,0 +1,17 @@ +use crate::prelude::*; +pub mod territorial; +pub mod nopersonality; + +pub struct PersonalityPlugin; + +impl Plugin for PersonalityPlugin { + fn build(&self, app: &mut App) { + app + .add_systems( + Update, + (territorial::personality_territorial, nopersonality::personality_nopersonality) + .run_if(bevy::time::common_conditions::on_timer(bevy::utils::Duration::from_secs_f32(0.5))) + .run_if(in_state(GameState::InGame)) + ); + } +} diff --git a/src/task_system/personality/nopersonality.rs b/src/task_system/personality/nopersonality.rs new file mode 100644 index 0000000..863b736 --- /dev/null +++ b/src/task_system/personality/nopersonality.rs @@ -0,0 +1,13 @@ +use crate::prelude::*; + +pub fn personality_nopersonality( + mut entities: Query<(Entity, &mut Brain, &mut PhysicalBody, &Position)> +) { + for (entity, mut brain, mut physical_body, position) in entities.iter_mut() { + if brain.task != Some(Task::Personality) { continue; } + if !brain.personality.is_empty() { continue; } + + brain.motivation = Some(Motivation::Meander); + brain.task = None; + } +} \ No newline at end of file diff --git a/src/task_system/personality/territorial.rs b/src/task_system/personality/territorial.rs new file mode 100644 index 0000000..6b28c49 --- /dev/null +++ b/src/task_system/personality/territorial.rs @@ -0,0 +1,24 @@ +use crate::prelude::*; + +pub fn personality_territorial( + mut entities: Query<(Entity, &mut Brain, &mut PhysicalBody, &Position, Option<&Nest>)> +) { + let potential_targets = entities.iter() + .map(|(entity, _, _, position, _)| (entity, *position)) // Clone the Position data + .collect::>(); + for (entity, mut brain, mut physical_body, position, nest) in entities.iter_mut() { + if brain.task != Some(Task::Personality) { continue; } + if !brain.personality.contains(&PersonalityTrait::Territorial) { continue; } + // Anything to defend against? + for (target_entity, target_position) in potential_targets.iter() { + if entity == *target_entity { continue; } + if target_position.distance(position) < 5 { + physical_body.danger = Some(Danger { danger_type: DangerType::Attacked, danger_source: Some(*target_entity) }); + brain.remotivate(); + break; + } + } + brain.motivation = Some(Motivation::Meander); + brain.task = None; + } +} \ No newline at end of file diff --git a/src/thinking_system.rs b/src/thinking_system.rs index 6d5523b..f5d12d9 100644 --- a/src/thinking_system.rs +++ b/src/thinking_system.rs @@ -147,7 +147,18 @@ pub fn thinking_system( if brain.personality.contains(&PersonalityTrait::Vicious) { brain.motivation = Some(Motivation::Rage); } - if !brain.personality.contains(&PersonalityTrait::Creature) { + if brain.personality.contains(&PersonalityTrait::Territorial) { + // brain.motivation = Some(Motivation::DefendTerritory); + } + if brain.personality.contains(&PersonalityTrait::Creature) { + brain.motivation = Some(Motivation::Personality); + } else { + // Work (75% chance) or Personality (25% chance) + if random::() % 4 == 0 { + brain.motivation = Some(Motivation::Work); + } else { + brain.motivation = Some(Motivation::Personality); + } brain.motivation = Some(Motivation::Work); } } @@ -196,6 +207,8 @@ pub fn thinking_system( brain.task = Some(Task::Play); } else if m == Motivation::Work { brain.task = Some(Task::Work); + } else if m == Motivation::Personality { + brain.task = Some(Task::Personality); } else if m == Motivation::Meander { brain.task = Some(Task::Meander); } diff --git a/src/unitgenerator_system.rs b/src/unitgenerator_system.rs index e27bcea..a290aba 100644 --- a/src/unitgenerator_system.rs +++ b/src/unitgenerator_system.rs @@ -155,7 +155,7 @@ impl UnitTemplate { food_need: None, entertainment_need: None, sleep_need: None, - personality: vec![PersonalityTrait::Creature, PersonalityTrait::Vicious], + personality: vec![PersonalityTrait::Creature, PersonalityTrait::Territorial], afflictions: random_afflictions.to_vec(), skillset: Skillset::default(), attributes: Attributeset::default(), @@ -172,7 +172,7 @@ impl UnitTemplate { food_need: None, entertainment_need: None, sleep_need: None, - personality: vec![PersonalityTrait::Creature, PersonalityTrait::Vicious], + personality: vec![PersonalityTrait::Creature, PersonalityTrait::Territorial], afflictions: random_afflictions.to_vec(), skillset: Skillset::default(), attributes: Attributeset::default(),