Made humanoids glide when chasing

This commit is contained in:
Joshua Barretto 2019-08-04 09:21:29 +01:00
parent ee0eb8d53c
commit c7d2fbc20e
3 changed files with 36 additions and 8 deletions

View File

@ -14,6 +14,15 @@ pub enum Body {
Object(object::Body),
}
impl Body {
pub fn is_humanoid(&self) -> bool {
match self {
Body::Humanoid(_) => true,
_ => false,
}
}
}
impl Component for Body {
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
}

View File

@ -1,4 +1,4 @@
use crate::comp::{Agent, Controller, Pos, Stats};
use crate::comp::{ActionState, Agent, Controller, Pos, Stats};
use rand::{seq::SliceRandom, thread_rng};
use specs::{Entities, Join, ReadStorage, System, WriteStorage};
use vek::*;
@ -10,11 +10,15 @@ impl<'a> System<'a> for Sys {
Entities<'a>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Stats>,
ReadStorage<'a, ActionState>,
WriteStorage<'a, Agent>,
WriteStorage<'a, Controller>,
);
fn run(&mut self, (entities, positions, stats, mut agents, mut controllers): Self::SystemData) {
fn run(
&mut self,
(entities, positions, stats, action_states, mut agents, mut controllers): Self::SystemData,
) {
for (entity, pos, agent, controller) in
(&entities, &positions, &mut agents, &mut controllers).join()
{
@ -63,8 +67,14 @@ impl<'a> System<'a> for Sys {
const SIGHT_DIST: f32 = 30.0;
let mut choose_new = false;
if let Some((Some(target_pos), Some(target_stats))) =
target.map(|target| (positions.get(target), stats.get(target)))
if let Some((Some(target_pos), Some(target_stats), Some(a))) =
target.map(|target| {
(
positions.get(target),
stats.get(target),
action_states.get(target),
)
})
{
let dist = Vec2::<f32>::from(target_pos.0 - pos.0).magnitude();
if target_stats.is_dead {
@ -86,6 +96,11 @@ impl<'a> System<'a> for Sys {
if rand::random::<f32>() < 0.02 {
controller.roll = true;
}
if a.gliding && target_pos.0.z > pos.0.z + 5.0 {
controller.glide = true;
controller.jump = true;
}
} else {
choose_new = true;
}

View File

@ -1,6 +1,6 @@
use crate::comp::{
ActionState, Attacking, Controller, Gliding, Jumping, MoveDir, Respawning, Rolling, Stats, Vel,
Wielding,
ActionState, Attacking, Body, Controller, Gliding, Jumping, MoveDir, Respawning, Rolling,
Stats, Vel, Wielding,
};
use specs::{Entities, Join, ReadStorage, System, WriteStorage};
@ -11,6 +11,7 @@ impl<'a> System<'a> for Sys {
Entities<'a>,
WriteStorage<'a, Controller>,
ReadStorage<'a, Stats>,
ReadStorage<'a, Body>,
ReadStorage<'a, Vel>,
WriteStorage<'a, ActionState>,
WriteStorage<'a, MoveDir>,
@ -28,6 +29,7 @@ impl<'a> System<'a> for Sys {
entities,
mut controllers,
stats,
bodies,
velocities,
mut action_states,
mut move_dirs,
@ -39,10 +41,11 @@ impl<'a> System<'a> for Sys {
mut glidings,
): Self::SystemData,
) {
for (entity, controller, stats, vel, mut a) in (
for (entity, controller, stats, body, vel, mut a) in (
&entities,
&mut controllers,
&stats,
&bodies,
&velocities,
// Although this is changed, it is only kept for this system
// as it will be replaced in the action state system
@ -71,7 +74,8 @@ impl<'a> System<'a> for Sys {
}
// Glide
if controller.glide && !a.on_ground && !a.attacking && !a.rolling {
if controller.glide && !a.on_ground && !a.attacking && !a.rolling && body.is_humanoid()
{
let _ = glidings.insert(entity, Gliding);
a.gliding = true;
} else {