Skip to content

Commit

Permalink
Initial work on combat.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankopf committed Aug 4, 2023
1 parent 456dc42 commit c8cdead
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/combat_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod melee;
mod ranged;

pub const HALF_SECOND: &str = "half_second";
pub const TWO_SECOND: &str = "two_second";

// Make Plugin
pub struct CombatPlugin;
Expand Down
47 changes: 36 additions & 11 deletions src/combat_system/melee.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
use crate::prelude::*;

pub fn combat_system_melee(
_commands: Commands,
mut query: Query<(&mut Brain, &mut PhysicalBody)>
mut commands: Commands,
mut query: Query<(&mut Brain, &PhysicalBody, &Position, Option<&Targeting>)>,
mut positions: Query<(Entity, &Position, &mut PhysicalBody)>,
) {
for (mut brain, mut physical_body) in query.iter_mut() {
for (mut brain, physical_body, position, targeting) in query.iter_mut() {
if brain.task != Some(Task::Fight) { continue; }

// if let Some(n) = &mut physical_body.needs_entertainment {
// n.current += 10.0;
// if n.current >= n.max {
// brain.motivation = None;
// brain.task = None;
// }
// }
if let Some(targeting) = targeting {
let mut entity_found = false;
for (entity, position2, mut physical_body2) in positions.iter_mut() {
if entity == targeting.target {
if position.distance(position2) <= 1 {
// Attack!
println!("Attack!");
entity_found = true;
do_melee_damage(&mut commands, entity, physical_body, &mut physical_body2);
//commands.entity(targeting.target);
} else {
// Try to follow/hunt the entity.
}
}
}
if !entity_found {
brain.motivation = None;
brain.task = None;
}
}
}
}

fn do_melee_damage(
commands: &mut Commands,
entity: Entity,
body1: &PhysicalBody,
body2: &mut PhysicalBody,
) {
body2.attributes.health -= 10;
if body2.attributes.health <= 0 {
commands.entity(entity).despawn_recursive();
}
}
50 changes: 48 additions & 2 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ pub struct ClickedOn;
#[derive(Component)]
pub struct InGameButton;

// #[derive(Component)]
// pub struct CombatTarget {
// pub target: Entity,
// }

#[derive(Component)]
pub struct Skill {
pub experience: i32,
Expand All @@ -199,6 +204,7 @@ pub struct Skill {

#[derive(Component)]
pub struct Attributeset {
pub health: i32,
pub strength: i32,
pub dexterity: i32,
pub constitution: i32,
Expand All @@ -209,6 +215,7 @@ pub struct Attributeset {
impl Default for Attributeset {
fn default() -> Self {
Attributeset {
health: 100,
strength: 10,
dexterity: 10,
constitution: 10,
Expand Down Expand Up @@ -333,6 +340,12 @@ impl Default for Skillset {
}
}
}
pub enum Danger {
Attacked,
Fire,
Freezing,
Overheating,
}

pub trait InfoPanel {
fn info_panel(&self) -> Vec<String>;
Expand All @@ -345,7 +358,7 @@ pub struct PhysicalBody {
pub needs_sleep: Option<NeedsSleep>,
pub index: usize,
pub crisis: Option<String>,
pub danger: Option<String>,
pub danger: Option<Danger>,
pub injured: bool,
pub afflictions: Vec<Affliction>,
pub skillset: Skillset,
Expand All @@ -367,12 +380,45 @@ impl InfoPanel for PhysicalBody {
}
}

#[derive(PartialEq)]
pub enum Order {
Eat,
Hospital,
Follow,
Stay,
Guard,
Patrol,
Wander,
Work,
Sleep,
Drink,
Play,
Party,
Socialize,
Fight,
Flee,
Doctor,
Forage,
Plant,
Harvest,
Mine,
Chop,
Construct,
Hunt,
Milk,
Cook,
Fish,
Craft,
Clean,
Haul,
None,
}

#[derive(Component, Default)]
pub struct Brain {
pub motivation: Option<Motivation>,
pub task: Option<Task>,
pub order: Option<String>,
pub order: Option<Order>,
pub personality: Vec<PersonalityTrait>,
}
impl Brain {
Expand Down
11 changes: 0 additions & 11 deletions src/melee_attacker_system.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/monstergenerator_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn monster_generator(
if !can_generate {
return;
}
let sprite = TextureAtlasSprite::new(ActorType::Rat.sprite_index()); // TO DO
let sprite = TextureAtlasSprite::new(ActorType::Rat.sprite_index());
commands
.spawn(SpriteSheetBundle {
sprite,
Expand Down
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub use super::components::{
ActorType, Attackable, Attributeset, Bed, Brain, Choppable, ClickedOn, Food, Foragable, ForageType, GameState, GeneratedBy,
ActorType, Attackable, Attributeset, Bed, Brain, Choppable, ClickedOn, Danger, 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, NeedsEntertainment, NeedsFood,
NeedsSleep, Pathing, PauseOverlay, PhysicalBody, Plant, PlantType, Position, Skillset, Skill, SelectableType, SizeXYZ,
NeedsSleep, Order, Pathing, PauseOverlay, PhysicalBody, Plant, PlantType, Position, Skillset, Skill, SelectableType, SizeXYZ,
Targeting, Task, TextName, TileType, WorkMarker, WorkTarget, Zone, ZoneMarker, ZoneType,
};
pub use crate::constants::*;
Expand Down
23 changes: 16 additions & 7 deletions src/thinking_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn thinking_system(
if let Some(n) = &physical_body.needs_food {
if n.current < 5.0 {
if let Some(order) = &brain.order {
if order == "Eat" {
if *order == Order::Eat {
brain.motivation = Some(Motivation::Order);
} else {
brain.motivation = Some(Motivation::Hunger);
Expand All @@ -77,7 +77,7 @@ pub fn thinking_system(
// HOSPITAL
if brain.motivation.is_none() && physical_body.injured {
if let Some(order) = &brain.order {
if order == "Hospital" {
if *order == Order::Hospital {
brain.motivation = Some(Motivation::Order);
} else {
brain.motivation = Some(Motivation::Injured);
Expand All @@ -91,7 +91,7 @@ pub fn thinking_system(
if let Some(n) = &physical_body.needs_sleep {
if n.current < 5.0 {
if let Some(order) = &brain.order {
if order == "Sleep" {
if *order == Order::Sleep {
brain.motivation = Some(Motivation::Order);
} else {
brain.motivation = Some(Motivation::Tired);
Expand All @@ -107,7 +107,7 @@ pub fn thinking_system(
if let Some(n) = &physical_body.needs_entertainment {
if n.current < 5.0 {
if let Some(order) = &brain.order {
if order == "Entertainment" {
if *order == Order::Play {
brain.motivation = Some(Motivation::Order);
} else {
brain.motivation = Some(Motivation::Bored);
Expand Down Expand Up @@ -146,9 +146,18 @@ pub fn thinking_system(
brain.task = Some(Task::Order);
}
} else if m == Motivation::Danger {
if let Some(_danger) = &physical_body.danger {
brain.task = Some(Task::Flee);
// TO DO: Assign FLEE or FIGHT task.
if let Some(danger) = &physical_body.danger {
match danger {
Danger::Attacked => {
brain.task = Some(Task::Fight);
},
Danger::Fire => {
brain.task = Some(Task::Flee);
},
_ => {
brain.task = Some(Task::Flee);
},
}
}
} else if m == Motivation::Hunger {
brain.task = Some(Task::Eat);
Expand Down

0 comments on commit c8cdead

Please sign in to comment.