Skip to content

Commit

Permalink
Adding territorial personality trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Aug 9, 2023
1 parent 0567aaf commit b101ba9
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 20 deletions.
11 changes: 9 additions & 2 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl ActorType {
#[derive(Component, PartialEq, Clone, Debug)]
pub enum TileType {
Grass,
Cave,
Dirt,
Gravel,
Sand,
Expand All @@ -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),
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions src/initializations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
18 changes: 8 additions & 10 deletions src/startup.rs → src/initializations/startup.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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))
;

Expand Down
1 change: 0 additions & 1 deletion src/interface/game_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/selection_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ pub fn select_zoning(
..default()
}, ZoneMarker))
.id();
println!("Zonemarker");
commands.entity(entity).push_children(&[zonemarker]);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/task_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod chop;
mod eat;
mod forage;
mod meander;
mod personality;
use personality::PersonalityPlugin;
mod plant;
mod play;
mod sleep;
Expand All @@ -14,6 +16,7 @@ pub struct TaskPlugin;
impl Plugin for TaskPlugin {
fn build(&self, app: &mut App) {
app
.add_plugins(PersonalityPlugin)
.add_systems(
Update,
(
Expand Down
17 changes: 17 additions & 0 deletions src/task_system/personality.rs
Original file line number Diff line number Diff line change
@@ -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))
);
}
}
13 changes: 13 additions & 0 deletions src/task_system/personality/nopersonality.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions src/task_system/personality/territorial.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<(Entity, Position)>>();
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;
}
}
15 changes: 14 additions & 1 deletion src/thinking_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>() % 4 == 0 {
brain.motivation = Some(Motivation::Work);
} else {
brain.motivation = Some(Motivation::Personality);
}
brain.motivation = Some(Motivation::Work);
}
}
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/unitgenerator_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit b101ba9

Please sign in to comment.