Skip to content

Commit

Permalink
some bots now can use weapons
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Apr 18, 2021
1 parent 2de4590 commit 9de4b0d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 35 deletions.
8 changes: 4 additions & 4 deletions data/configs/bots.ron
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
spine: "", // Empty because cannot use weapons.
walk_speed: 0.7,
scale: 1.0,
weapon_scale: 1.0,
weapon_scale: 130.0,
health: 1000.0,
v_aim_angle_hack: 0.0,
can_use_weapons: false,
Expand Down Expand Up @@ -72,7 +72,7 @@
spine: "", // Empty because cannot use weapons.
walk_speed: 2.8,
scale: 1.0,
weapon_scale: 1.0,
weapon_scale: 130.0,
health: 300.0,
v_aim_angle_hack: 0.0,
can_use_weapons: false,
Expand Down Expand Up @@ -120,10 +120,10 @@
spine: "Spine",
walk_speed: 1.2,
scale: 1.0,
weapon_scale: 1.0,
weapon_scale: 130.0,
health: 100.0,
v_aim_angle_hack: 12.0,
can_use_weapons: false,
can_use_weapons: true,
close_combat_distance: 0.5,
pain_sounds: [
"data/sounds/zombie_pain_1.wav",
Expand Down
2 changes: 1 addition & 1 deletion data/configs/weapons.ron
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"data/sounds/glock_shot_3.wav"
],
projectile: Ray(damage: Point(10.0)),
shoot_interval: 0.3,
shoot_interval: 0.15,
yaw_correction: -10.0,
pitch_correction: -4.0,
ammo_indicator_offset: (-0.15, -0.0, 0.0),
Expand Down
Binary file modified data/levels/lab.rgs
Binary file not shown.
36 changes: 17 additions & 19 deletions src/bot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
use crate::actor::TargetKind;
use crate::inventory::{Inventory, ItemEntry};
use crate::{
actor::{Actor, TargetDescriptor},
actor::{Actor, TargetDescriptor, TargetKind},
bot::{
lower_body::{LowerBodyMachine, LowerBodyMachineInput},
upper_body::{UpperBodyMachine, UpperBodyMachineInput},
},
character::{find_hit_boxes, Character},
inventory::{Inventory, ItemEntry},
item::ItemKind,
level::{footstep_ray_check, UpdateContext},
message::Message,
utils::BodyImpactHandler,
weapon::projectile::Damage,
CollisionGroups, GameTime,
};
use rg3d::core::rand::seq::IteratorRandom;
use rg3d::{
animation::machine::{Machine, PoseNode},
core::{
algebra::{Isometry3, Matrix4, Point3, Translation3, UnitQuaternion, Vector3},
color::Color,
math::{frustum::Frustum, ray::Ray, SmoothAngle},
pool::Handle,
rand::Rng,
rand::{seq::IteratorRandom, Rng},
visitor::{Visit, VisitResult, Visitor},
},
engine::resource_manager::ResourceManager,
Expand Down Expand Up @@ -647,13 +645,18 @@ impl Bot {

self.update_frustum(position, &context.scene.graph);

let was_damaged = self.character.health < self.last_health;
if was_damaged {
let was_injured = (self.character.health - self.last_health).abs() > 30.0;
if was_injured {
self.restoration_time = 0.8;
self.last_health = self.character.health;
}

can_aim = self.restoration_time <= 0.0;
self.last_health = self.character.health;
can_aim = self.restoration_time <= 0.0
&& self
.inventory
.items()
.iter()
.any(|i| i.kind.associated_weapon().is_some());

if !self.is_dead() && !in_close_combat && self.target.is_some() {
let mut vel = (self.move_target - position).scale(1.0 / context.time.delta);
Expand All @@ -675,15 +678,7 @@ impl Bot {
{
let weapon = *weapon;

let ammo_per_shot =
context.weapons[weapon].definition.ammo_consumption_per_shot;

if context.weapons[weapon].can_shoot(context.time)
&& self
.inventory
.try_extract_exact_items(ItemKind::Ammo, ammo_per_shot)
== ammo_per_shot
{
if context.weapons[weapon].can_shoot(context.time) {
sender
.send(Message::ShootWeapon {
weapon,
Expand All @@ -705,7 +700,10 @@ impl Bot {
.get_mut(current_attack_animation)
.pop_event()
{
if event.signal_id == UpperBodyMachine::HIT_SIGNAL && in_close_combat {
if event.signal_id == UpperBodyMachine::HIT_SIGNAL
&& in_close_combat
&& !self.can_shoot()
{
sender
.send(Message::DamageActor {
actor: target.handle,
Expand Down
41 changes: 39 additions & 2 deletions src/level/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,14 @@ pub async fn analyze(
rotation: **node.local_transform().rotation(),
bot_kind: BotKind::Zombie,
spawned: false,
with_gun: false,
})
} else if name.starts_with("Mutant") {
spawn_points.push(SpawnPoint {
position: node.global_position(),
rotation: **node.local_transform().rotation(),
bot_kind: BotKind::Mutant,
with_gun: false,
spawned: false,
})
} else if name.starts_with("Parasite") {
Expand All @@ -412,6 +414,7 @@ pub async fn analyze(
rotation: **node.local_transform().rotation(),
bot_kind: BotKind::Parasite,
spawned: false,
with_gun: false,
})
} else if name.starts_with("PlayerSpawnPoint") {
player_spawn_position = node.global_position();
Expand Down Expand Up @@ -473,6 +476,13 @@ pub async fn analyze(
.add(Turret::new(handle, scene, ShootMode::Consecutive, Hostility::All).await);
}
"NextLevelTrigger" => triggers.add(Trigger::new(handle, TriggerKind::NextLevel)),
"ZombieWithGun" => spawn_points.push(SpawnPoint {
position: node.global_position(),
rotation: **node.local_transform().rotation(),
bot_kind: BotKind::Zombie,
spawned: false,
with_gun: true,
}),
_ => (),
}
}
Expand Down Expand Up @@ -585,6 +595,8 @@ async fn spawn_bot(
resource_manager: ResourceManager,
sender: Sender<Message>,
scene: &mut Scene,
weapon: Option<WeaponKind>,
weapons: &mut WeaponContainer,
) -> Handle<Actor> {
spawn_point.spawned = true;

Expand All @@ -593,12 +605,26 @@ async fn spawn_bot(
spawn_point.position,
spawn_point.rotation,
actors,
resource_manager,
sender,
resource_manager.clone(),
sender.clone(),
scene,
)
.await;

if let Some(weapon) = weapon {
give_new_weapon(
weapon,
bot,
sender.clone(),
resource_manager.clone(),
true,
weapons,
actors,
scene,
)
.await
}

bot
}

Expand Down Expand Up @@ -721,6 +747,12 @@ impl BaseLevel {
resource_manager.clone(),
sender.clone(),
&mut scene,
if pt.with_gun {
Some(WeaponKind::Ak47)
} else {
None
},
&mut weapons,
)
.await;
}
Expand Down Expand Up @@ -1387,6 +1419,8 @@ impl BaseLevel {
engine.resource_manager.clone(),
self.sender.clone().unwrap(),
&mut engine.scenes[self.scene],
None,
&mut self.weapons,
)
.await;
}
Expand Down Expand Up @@ -1537,6 +1571,7 @@ pub struct SpawnPoint {
rotation: UnitQuaternion<f32>,
bot_kind: BotKind,
spawned: bool,
with_gun: bool,
}

impl Default for SpawnPoint {
Expand All @@ -1546,6 +1581,7 @@ impl Default for SpawnPoint {
rotation: Default::default(),
bot_kind: BotKind::Zombie,
spawned: false,
with_gun: false,
}
}
}
Expand All @@ -1557,6 +1593,7 @@ impl Visit for SpawnPoint {
self.position.visit("Position", visitor)?;
self.rotation.visit("Rotation", visitor)?;
self.spawned.visit("Spawned", visitor)?;
self.with_gun.visit("WithGun", visitor)?;

let mut kind_id = self.bot_kind.id();
kind_id.visit("BotKind", visitor)?;
Expand Down
20 changes: 11 additions & 9 deletions src/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,17 @@ impl SoundManager {
if let Some(material) = material {
if let Some(map) = self.sound_base.material_to_sound.get(&material) {
if let Some(sound_list) = map.get(&sound_kind) {
self.play_sound(
sound_list.choose(&mut rand::thread_rng()).unwrap().as_ref(),
position,
gain,
rolloff_factor,
radius,
resource_manager,
)
.await;
if let Some(sound) = sound_list.choose(&mut rand::thread_rng()) {
self.play_sound(
sound.as_ref(),
position,
gain,
rolloff_factor,
radius,
resource_manager,
)
.await;
}
}
}
}
Expand Down

0 comments on commit 9de4b0d

Please sign in to comment.