move the character_state test to the systems crate (where the infrastructure is complete)

This commit is contained in:
Christof Petig 2022-06-04 23:59:19 +02:00
parent b702a27b28
commit a55cbbbf11
3 changed files with 105 additions and 76 deletions

View File

@ -1268,78 +1268,3 @@ impl HandInfo {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
comp::{item::MaterialStatManifest, Ori, Pos, Vel},
resources::DeltaTime,
states::behavior::JoinStruct,
uid::Uid,
};
use specs::{
shrev::EventChannel, storage::FlaggedAccessMut, world::entity::Generation, LazyUpdate,
};
use std::marker::PhantomData;
use common_systems::character_behavior;
#[test]
fn orientation_shortcut() {
let mut ecs = specs::World::new();
ecs.register::<crate::comp::CharacterState>();
let entity = ecs
.create_entity()
.with(CharacterState::Idle(idle::Data { is_sneaking: false }))
.build();
let mut system = character_behavior::Sys::new();
system.run_now(ecs);
ecs.maintain();
// let mut char_state = CharacterState::Idle(idle::Data { is_sneaking: false });
// let mut pos = Pos(Vec3::new(11, 12, 13));
// let mut vel = Vel(Vec3::new(1.0, 0.0, 0.0));
// let mut ori = Ori::default();
// let index: specs::world::entity::Index = 42;
// let generation = Generation(43);
// let js = JoinStruct {
// entity: entity,
// //specs::world::entity::Entitiy::new(index, generation),
// uid: &Uid(44),
// char_state: FlaggedAccessMut {
// channel: &mut EventChannel::with_capacity(2),
// emit: false,
// id: 44,
// access: &mut char_state,
// phantom: PhantomData,
// },
// pos: &mut pos,
// vel: &mut vel,
// ori: &mut ori,
// mass: todo!(),
// density: todo!(),
// energy: todo!(),
// inventory: todo!(),
// controller: todo!(),
// health: todo!(),
// body: todo!(),
// physics: todo!(),
// melee_attack: todo!(),
// beam: todo!(),
// stat: todo!(),
// skill_set: todo!(),
// active_abilities: todo!(),
// combo: todo!(),
// alignment: todo!(),
// terrain: todo!(),
// mount_data: todo!(),
// };
// let lu = LazyUpdate::new();
// let msm = MaterialStatManifest::new();
// let dt = DeltaTime(0.033);
// let jd = JoinData::new(&js, &lu, &dt, &msm);
// let mut update: StateUpdate = jd.into();
// handle_orientation(&jd, &mut update, 1.0, None);
}
}

View File

@ -34,4 +34,4 @@ specs = { git = "https://github.com/amethyst/specs.git", features = ["serde", "s
[dev-dependencies]
# Setup a State
common-state = { package = "veloren-common-state", path = "../state" }
common-state = { package = "veloren-common-state", path = "../state" }

View File

@ -0,0 +1,104 @@
#[cfg(test)]
mod tests {
use common::{
comp::{
item::MaterialStatManifest, skills::GeneralSkill, CharacterState, Controller, Energy,
Ori, PhysicsState, Poise, Pos, Skill, Stats, Vel,
},
resources::{DeltaTime, GameMode, Time},
uid::Uid,
util::Dir,
SkillSetBuilder,
};
use common_ecs::dispatch;
use common_state::State;
use rand::thread_rng;
use specs::{Builder, Entity, WorldExt};
use std::time::Duration;
use vek::{approx::AbsDiffEq, Vec3};
use veloren_common_systems::character_behavior;
fn setup() -> State {
let mut state = State::new(GameMode::Server);
let msm = MaterialStatManifest::load().cloned();
state.ecs_mut().insert(msm);
state.ecs_mut().read_resource::<Time>();
state.ecs_mut().read_resource::<DeltaTime>();
state
}
fn create_entity(state: &mut State, ori: Ori) -> specs::Entity {
let body = common::comp::Body::Humanoid(common::comp::humanoid::Body::random_with(
&mut thread_rng(),
&common::comp::humanoid::Species::Human,
));
let skill_set = SkillSetBuilder::default().build();
state
.ecs_mut()
.create_entity()
.with(CharacterState::Idle(common::states::idle::Data::default()))
.with(Pos(Vec3::zero()))
.with(Vel::default())
.with(ori)
.with(body.mass())
.with(body.density())
.with(body)
.with(Energy::new(
body,
skill_set
.skill_level(Skill::General(GeneralSkill::EnergyIncrease))
.unwrap_or(0),
))
.with(Controller::default())
.with(Poise::new(body))
.with(skill_set)
.with(PhysicsState::default())
.with(Stats::empty())
.with(Uid(1))
.build()
}
fn tick(state: &mut State, dt: Duration) {
state.tick(
dt,
|dispatch_builder| {
dispatch::<character_behavior::Sys>(dispatch_builder, &[]);
},
false,
);
}
#[test]
fn orientation_shortcut() {
let mut state = setup();
const TESTCASES: usize = 5;
let testcases: [(Vec3<f32>, Vec3<f32>); TESTCASES] = [
// horizontal is unchanged
(Vec3::unit_x(), Vec3::unit_x()),
// nearly vertical takes time to adjust
(Vec3::new(0.1, 0.1, 1.0), Vec3::new(0.149, 0.149, 0.978)),
// intermediate case
(Vec3::new(0.6, 0.6, 0.1), Vec3::new(0.706, 0.706, 0.052)),
// edge case: nearly horizontal after system
(Vec3::new(0.6, 0.6, 0.0556), Vec3::new(0.707, 0.707, 0.000)),
// small enough to be horizontal in one step
(Vec3::new(0.6, 0.6, 0.04), Vec3::new(0.707, 0.707, 0.000)),
];
let mut entities: [Option<Entity>; TESTCASES] = [None; TESTCASES];
for i in 0..TESTCASES {
entities[i] = Some(create_entity(
&mut state,
Ori::from_unnormalized_vec(testcases[i].0).unwrap_or_default(),
));
}
tick(&mut state, Duration::from_secs_f32(0.033));
let results = state.ecs().read_storage::<Ori>();
for i in 0..TESTCASES {
if let Some(e) = entities[i] {
let result = Dir::from(*results.get(e).expect("Ori missing"));
assert!(result.abs_diff_eq(&testcases[i].1, 0.0005));
// println!("{:?}", result);
}
}
}
}