Skip to content

Commit

Permalink
Fixing vicious personality
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Aug 10, 2023
1 parent 5b0c5b8 commit e87a6c8
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 21 deletions.
27 changes: 25 additions & 2 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bevy::prelude::*;
use crate::unitgenerator_system::UnitTemplate;

use super::prelude::*;

#[derive(Component, Copy, Clone, Debug, PartialEq, Hash, Eq)]
Expand Down Expand Up @@ -91,7 +93,8 @@ pub enum ActorType { // Entity? Character? Creature? Actor? Avatar? Unit? Agent?
IceFox,
BrownRat,
Spider,
Crab
Crab,
Cyclops,
}
impl ActorType {
pub fn sprite_row_and_col(&self) -> (usize, usize) {
Expand All @@ -118,6 +121,7 @@ impl ActorType {
ActorType::BrownRat => (64, 28),
ActorType::Spider => (64, 29),
ActorType::Crab => (63, 29),
ActorType::Cyclops => (59, 8),
}
}
pub fn sprite_index(&self) -> usize {
Expand Down Expand Up @@ -917,7 +921,26 @@ pub struct MapTile;
pub struct MoveRandom;

#[derive(Component)]
pub struct MonsterGenerator;
pub struct MonsterGenerator {
pub monsters: Vec<(UnitTemplate, u8)>,
}
impl MonsterGenerator {
pub fn pick(&self) -> &UnitTemplate {
let mut rng = rand::thread_rng();
let mut total = 0;
for (_, chance) in self.monsters.iter() {
total += chance;
}
let mut roll = rng.gen_range(0..total);
for (monster, chance) in self.monsters.iter() {
if roll < *chance {
return monster;
}
roll -= chance;
}
panic!("MonsterGenerator::pick failed");
}
}

#[derive(Component)]
pub struct GeneratedBy {
Expand Down
2 changes: 1 addition & 1 deletion src/initializations/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn startup(
})
.insert(position)
.insert(SizeXYZ::cube(1.1))
.insert(MonsterGenerator)
.insert(MonsterGenerator { monsters: vec![(UnitTemplate::rat(),1),(UnitTemplate::spider(),5),(UnitTemplate::cyclops(),1)] })
.insert(position.to_transform_layer(1.0))
;

Expand Down
16 changes: 4 additions & 12 deletions src/monstergenerator_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ impl Plugin for MonsterGeneratorPlugin {

pub fn monster_generator(
mut commands: Commands,
entities: Query<(Entity, &Position), With<MonsterGenerator>>,
entities: Query<(Entity, &Position, &MonsterGenerator)>,
tile_types: Query<(&Position, &TileType)>,
generated_monsters: Query<(Entity, &GeneratedBy)>,
sprite_sheet: Res<SpriteSheet>,
) {
for (entity, position) in entities.iter() {
for (entity, position, monster_generator) in entities.iter() {
let mut new_position = *position;
let dir = random::<i32>() % 4;
match dir {
Expand All @@ -44,16 +44,8 @@ pub fn monster_generator(
if !can_generate {
return;
}
match rand::thread_rng().gen_range(0..6) {
0 => {
let monster = spawn_unit_from_template(&mut commands, new_position, &sprite_sheet, &UnitTemplate::rat());
commands.entity(monster).insert(GeneratedBy { entity });
}
_ => {
let monster = spawn_unit_from_template(&mut commands, new_position, &sprite_sheet, &UnitTemplate::spider());
commands.entity(monster).insert(GeneratedBy { entity });
}
}
let monster = spawn_unit_from_template(&mut commands, new_position, &sprite_sheet, monster_generator.pick());
commands.entity(monster).insert(GeneratedBy { entity });

//*position = new_position;
}
Expand Down
4 changes: 4 additions & 0 deletions src/task_system/personality.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
pub mod territorial;
pub mod nopersonality;
pub mod vicious;

pub struct PersonalityPlugin;

Expand All @@ -26,6 +27,9 @@ pub fn personalities(
if brain.task != Some(Task::Personality) { continue; }
let next_trait = brain.get_next_personality_trait();
match next_trait {
Some(PersonalityTrait::Vicious) => {
vicious::vicious(entity, brain, physical_body, position, nest, &potential_targets);
}
Some(PersonalityTrait::Territorial) => {
territorial::territorial(entity, brain, physical_body, position, nest, &potential_targets);
},
Expand Down
15 changes: 15 additions & 0 deletions src/task_system/personality/vicious.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::prelude::*;

pub fn vicious(
entity: Entity,
mut brain: Mut<Brain>,
mut physical_body: Mut<PhysicalBody>,
_position: &Position,
nest: Option<&Nest>,
potential_targets: &Vec<(Entity, Position)>
) {
if brain.task != Some(Task::Personality) { return; }
if !brain.personality.contains(&PersonalityTrait::Vicious) { return; }
brain.motivation = Some(Motivation::Rage);
brain.task = Some(Task::Fight);
}
6 changes: 0 additions & 6 deletions src/thinking_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,6 @@ pub fn thinking_system(
}
// Now decide what the unit should be doing based on its personality.
if brain.motivation.is_none() {
if brain.personality.contains(&PersonalityTrait::Vicious) {
brain.motivation = Some(Motivation::Rage);
}
if brain.personality.contains(&PersonalityTrait::Territorial) {
// brain.motivation = Some(Motivation::DefendTerritory);
}
if brain.personality.contains(&PersonalityTrait::Creature) {
brain.motivation = Some(Motivation::Personality);
} else {
Expand Down
19 changes: 19 additions & 0 deletions src/unitgenerator_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ impl UnitTemplate {
],
}
}
pub fn cyclops() -> Self {
let actor_type = ActorType::Cyclops;
let random_afflictions = vec![];//Self::random_afflictions_animal();
Self {
actor_type,
food_need: None,
entertainment_need: None,
sleep_need: None,
personality: vec![PersonalityTrait::Creature, PersonalityTrait::Vicious],
afflictions: random_afflictions.to_vec(),
skillset: Skillset::default(),
attributes: Attributeset::default(),
component_builders: vec![
|commands: &mut Commands, entity: Entity| {
commands.entity(entity).insert(HasName { name: "Cyclops".to_string() });
},
],
}
}
pub fn random_afflictions_humanoid() -> Vec<Affliction> {
////////////////////////////
// Select some Afflictions
Expand Down

0 comments on commit e87a6c8

Please sign in to comment.