use init_resource everywhere

This commit is contained in:
Carter Anderson 2020-05-13 16:35:38 -07:00
parent 16b568e00e
commit b58db0749e
8 changed files with 23 additions and 32 deletions

View File

@ -16,15 +16,17 @@ pub struct AssetStorage<T> {
events: Events<AssetEvent<T>>, events: Events<AssetEvent<T>>,
} }
impl<T> AssetStorage<T> { impl<T> Default for AssetStorage<T> {
pub fn new() -> AssetStorage<T> { fn default() -> Self {
AssetStorage { AssetStorage {
assets: HashMap::new(), assets: HashMap::default(),
names: HashMap::new(), names: HashMap::default(),
events: Events::default(), events: Events::default(),
} }
} }
}
impl<T> AssetStorage<T> {
pub fn get_named(&mut self, name: &str) -> Option<Handle<T>> { pub fn get_named(&mut self, name: &str) -> Option<Handle<T>> {
self.names.get(name).map(|handle| *handle) self.names.get(name).map(|handle| *handle)
} }
@ -102,7 +104,7 @@ impl AddAsset for AppBuilder {
where where
T: Send + Sync + 'static, T: Send + Sync + 'static,
{ {
self.add_resource(AssetStorage::<T>::new()) self.init_resource::<AssetStorage<T>>()
.add_system_to_stage( .add_system_to_stage(
stage::EVENT_UPDATE, stage::EVENT_UPDATE,
AssetStorage::<T>::asset_event_system.system(), AssetStorage::<T>::asset_event_system.system(),

View File

@ -5,7 +5,7 @@ pub mod transform;
use bevy_app::{stage, AppBuilder, AppPlugin}; use bevy_app::{stage, AppBuilder, AppPlugin};
use bevy_transform::transform_system_bundle; use bevy_transform::transform_system_bundle;
use legion::prelude::IntoSystem; use legion::prelude::IntoSystem;
use time::{start_timer_system, stop_timer_system}; use time::{start_timer_system, stop_timer_system, Time};
#[derive(Default)] #[derive(Default)]
pub struct CorePlugin; pub struct CorePlugin;
@ -16,7 +16,7 @@ impl AppPlugin for CorePlugin {
app.add_system(transform_system); app.add_system(transform_system);
} }
app.add_resource(time::Time::new()) app.init_resource::<Time>()
.add_system_to_stage(stage::FIRST, start_timer_system.system()) .add_system_to_stage(stage::FIRST, start_timer_system.system())
.add_system_to_stage(stage::LAST, stop_timer_system.system()); .add_system_to_stage(stage::LAST, stop_timer_system.system());
} }

View File

@ -8,8 +8,8 @@ pub struct Time {
pub delta_seconds: f32, pub delta_seconds: f32,
} }
impl Time { impl Default for Time {
pub fn new() -> Time { fn default() -> Time {
Time { Time {
delta: Duration::from_secs(0), delta: Duration::from_secs(0),
instant: Instant::now(), instant: Instant::now(),
@ -17,7 +17,9 @@ impl Time {
delta_seconds: 0.0, delta_seconds: 0.0,
} }
} }
}
impl Time {
pub fn start(&mut self) { pub fn start(&mut self) {
self.instant = Instant::now(); self.instant = Instant::now();
} }

View File

@ -76,11 +76,11 @@ impl AppPlugin for RenderPlugin {
.add_asset::<Shader>() .add_asset::<Shader>()
.add_asset::<PipelineDescriptor>() .add_asset::<PipelineDescriptor>()
.add_resource(render_graph) .add_resource(render_graph)
.add_resource(PipelineAssignments::new()) .init_resource::<PipelineAssignments>()
.add_resource(PipelineCompiler::new()) .init_resource::<PipelineCompiler>()
.add_resource(RenderResourceAssignments::default()) .init_resource::<RenderResourceAssignments>()
.add_resource(VertexBufferDescriptors::default()) .init_resource::<VertexBufferDescriptors>()
.add_resource(EntityRenderResourceAssignments::default()) .init_resource::<EntityRenderResourceAssignments>()
// core systems // core systems
.add_system(entity_render_resource_assignments_system()) .add_system(entity_render_resource_assignments_system())
.init_system_to_stage(stage::POST_UPDATE, camera::camera_update_system) .init_system_to_stage(stage::POST_UPDATE, camera::camera_update_system)

View File

@ -21,6 +21,7 @@ pub struct ShaderSpecialization {
} }
// TODO: consider using (Typeid, fieldinfo.index) in place of string for hashes // TODO: consider using (Typeid, fieldinfo.index) in place of string for hashes
#[derive(Default)]
pub struct PipelineCompiler { pub struct PipelineCompiler {
pub shader_source_to_compiled: pub shader_source_to_compiled:
HashMap<Handle<Shader>, Vec<(ShaderSpecialization, Handle<Shader>)>>, HashMap<Handle<Shader>, Vec<(ShaderSpecialization, Handle<Shader>)>>,
@ -31,13 +32,6 @@ pub struct PipelineCompiler {
} }
impl PipelineCompiler { impl PipelineCompiler {
pub fn new() -> Self {
PipelineCompiler {
shader_source_to_compiled: HashMap::new(),
pipeline_source_to_compiled: HashMap::new(),
}
}
fn compile_shader( fn compile_shader(
&mut self, &mut self,
shader_storage: &mut AssetStorage<Shader>, shader_storage: &mut AssetStorage<Shader>,
@ -205,18 +199,11 @@ impl PipelineCompiler {
} }
} }
#[derive(Default)]
pub struct PipelineAssignments { pub struct PipelineAssignments {
pub assignments: HashMap<Handle<PipelineDescriptor>, Vec<RenderResourceAssignmentsId>>, pub assignments: HashMap<Handle<PipelineDescriptor>, Vec<RenderResourceAssignmentsId>>,
} }
impl PipelineAssignments {
pub fn new() -> Self {
PipelineAssignments {
assignments: HashMap::new(),
}
}
}
// TODO: make this a system // TODO: make this a system
pub fn update_shader_assignments(world: &mut World, resources: &Resources) { pub fn update_shader_assignments(world: &mut World, resources: &Resources) {
// PERF: this seems like a lot of work for things that don't change that often. // PERF: this seems like a lot of work for things that don't change that often.

View File

@ -31,7 +31,7 @@ impl AppPlugin for WindowPlugin {
.add_event::<WindowCreated>() .add_event::<WindowCreated>()
.add_event::<WindowCloseRequested>() .add_event::<WindowCloseRequested>()
.add_event::<CloseWindow>() .add_event::<CloseWindow>()
.add_resource(Windows::default()); .init_resource::<Windows>();
if let Some(ref primary_window_descriptor) = self.primary_window { if let Some(ref primary_window_descriptor) = self.primary_window {
let mut create_window_event = let mut create_window_event =

View File

@ -27,7 +27,7 @@ impl AppPlugin for WinitPlugin {
// TODO: It would be great to provide a raw winit WindowEvent here, but the lifetime on it is // TODO: It would be great to provide a raw winit WindowEvent here, but the lifetime on it is
// stopping us. there are plans to remove the lifetime: https://github.com/rust-windowing/winit/pull/1456 // stopping us. there are plans to remove the lifetime: https://github.com/rust-windowing/winit/pull/1456
// .add_event::<winit::event::WindowEvent>() // .add_event::<winit::event::WindowEvent>()
.add_resource(WinitWindows::default()) .init_resource::<WinitWindows>()
.set_runner(winit_runner); .set_runner(winit_runner);
} }
} }

View File

@ -4,7 +4,7 @@ fn main() {
App::build() App::build()
.add_default_plugins() .add_default_plugins()
.add_event::<MyEvent>() .add_event::<MyEvent>()
.add_resource(EventTriggerState::default()) .init_resource::<EventTriggerState>()
.init_resource::<EventListenerState>() .init_resource::<EventListenerState>()
.add_system(event_trigger_system.system()) .add_system(event_trigger_system.system())
.add_system(event_listener_system.system()) .add_system(event_listener_system.system())