add plugins

cargo fmt
This commit is contained in:
rezural
2020-12-20 04:27:20 +11:00
parent 5634c51fe6
commit 315b84a85e
3 changed files with 63 additions and 53 deletions

View File

@@ -1,12 +1,9 @@
use crate::physics::{PhysicsEvents, PhysicsState}; use crate::physics::{PhysicsEvents, PhysicsState};
use crate::HarnessPlugin;
use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase};
use rapier::math::Vector; use rapier::math::Vector;
use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline};
use rapier::dynamics::{
IntegrationParameters, JointSet, RigidBodySet,
};
use rapier::geometry::{
BroadPhase, ColliderSet, NarrowPhase,
};
// #[derive(PartialEq)] // #[derive(PartialEq)]
// pub enum RunState { // pub enum RunState {
@@ -24,20 +21,18 @@ pub struct HarnessState {
// pub run_state: RunState, // pub run_state: RunState,
} }
pub struct Harness { pub struct Harness {
physics: PhysicsState, physics: PhysicsState,
max_steps: usize, max_steps: usize,
callbacks: Callbacks, callbacks: Callbacks,
// plugins: Vec<Box<dyn HarnessPlugin>>, plugins: Vec<Box<dyn HarnessPlugin>>,
time: f32, time: f32,
events: PhysicsEvents, events: PhysicsEvents,
event_handler: ChannelEventCollector, event_handler: ChannelEventCollector,
pub state: HarnessState, pub state: HarnessState,
} }
type Callbacks = type Callbacks = Vec<Box<dyn FnMut(&mut PhysicsState, &PhysicsEvents, f32)>>;
Vec<Box<dyn FnMut(&mut PhysicsState, &PhysicsEvents, f32)>>;
#[allow(dead_code)] #[allow(dead_code)]
impl Harness { impl Harness {
@@ -51,6 +46,9 @@ impl Harness {
.build() .build()
.unwrap(); .unwrap();
// #[cfg(feature = "parallel")]
// println!("Rapier Harness Parallel, num_threads: {}", num_threads);
let contact_channel = crossbeam::channel::unbounded(); let contact_channel = crossbeam::channel::unbounded();
let proximity_channel = crossbeam::channel::unbounded(); let proximity_channel = crossbeam::channel::unbounded();
let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0); let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0);
@@ -70,6 +68,7 @@ impl Harness {
physics, physics,
max_steps: 1000, max_steps: 1000,
callbacks: Vec::new(), callbacks: Vec::new(),
plugins: Vec::new(),
time: 0.0, time: 0.0,
events, events,
event_handler, event_handler,
@@ -126,7 +125,6 @@ impl Harness {
self.physics.query_pipeline = QueryPipeline::new(); self.physics.query_pipeline = QueryPipeline::new();
self.physics.pipeline = PhysicsPipeline::new(); self.physics.pipeline = PhysicsPipeline::new();
self.physics.pipeline.counters.enable(); self.physics.pipeline.counters.enable();
} }
// fn clear(&mut self) { // fn clear(&mut self) {
@@ -139,13 +137,11 @@ impl Harness {
// self.plugins.clear(); // self.plugins.clear();
// } // }
// pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) { pub fn add_plugin(&mut self, plugin: impl HarnessPlugin + 'static) {
// self.plugins.push(Box::new(plugin)); self.plugins.push(Box::new(plugin));
// } }
pub fn add_callback< pub fn add_callback<F: FnMut(&mut PhysicsState, &PhysicsEvents, f32) + 'static>(
F: FnMut(&mut PhysicsState, &PhysicsEvents, f32) + 'static,
>(
&mut self, &mut self,
callback: F, callback: F,
) { ) {
@@ -191,9 +187,14 @@ impl Harness {
.query_pipeline .query_pipeline
.update(&self.physics.bodies, &self.physics.colliders); .update(&self.physics.bodies, &self.physics.colliders);
// for plugin in &mut self.plugins { for plugin in &mut self.plugins {
// plugin.step(&mut self.physics) plugin.step(&mut self.physics)
// } }
//FIXME: not sure if this makes sense here, basically copied from Testbed
for plugin in &mut self.plugins {
plugin.run_callbacks(&mut self.physics, self.time)
}
} }
pub fn run(&mut self) { pub fn run(&mut self) {

View File

@@ -22,6 +22,8 @@ extern crate bitflags;
extern crate log; extern crate log;
pub use crate::engine::GraphicsManager; pub use crate::engine::GraphicsManager;
pub use crate::physics::PhysicsState;
pub use crate::plugin::HarnessPlugin;
pub use crate::plugin::TestbedPlugin; pub use crate::plugin::TestbedPlugin;
pub use crate::testbed::Testbed; pub use crate::testbed::Testbed;

View File

@@ -10,3 +10,10 @@ pub trait TestbedPlugin {
fn draw(&mut self); fn draw(&mut self);
fn profiling_string(&self) -> String; fn profiling_string(&self) -> String;
} }
pub trait HarnessPlugin {
//FIXME: is run_callbacks needed?
fn run_callbacks(&mut self, physics: &mut PhysicsState, t: f32);
fn step(&mut self, physics: &mut PhysicsState);
fn profiling_string(&self) -> String;
}