Skip to content

Commit

Permalink
Some work on ecs scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 8, 2024
1 parent 667b97e commit e5e7d88
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
6 changes: 6 additions & 0 deletions core/scenes/src/canvas/sprites.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
use crate::{Component, Event};

/// A component that renders a sprite.
pub struct SpriteComponent {}

impl Component for SpriteComponent {
fn on_event(&self, event: &dyn Event) {}
}
33 changes: 27 additions & 6 deletions core/scenes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@ pub use spatial::*;
mod canvas;
mod spatial;

use common::Camera;
use graphics::{RenderFrame, RenderPass, RenderQueue, RenderScene};

pub struct AwakeEvent;
pub struct StartEvent;
pub struct UpdateEvent;
pub struct DrawEvent;
pub struct DestroyEvent;

pub struct DrawEvent<'a> {
queue: &'a mut RenderQueue,
}

pub trait Event {}

impl Event for AwakeEvent {}
impl Event for StartEvent {}
impl Event for UpdateEvent {}
impl Event for DrawEvent {}
impl Event for DestroyEvent {}

impl<'a> Event for DrawEvent<'a> {}

pub trait Component {
fn on_event(&mut self, event: &dyn Event);
fn on_event(&self, event: &dyn Event);
}

pub trait System {
fn on_event(&mut self, event: &dyn Event);
fn on_event(&self, event: &dyn Event);
}

pub trait IntoSystem {
Expand All @@ -46,10 +53,24 @@ struct Entity {

impl Scene {
/// Emits an event into the scene graph.
pub fn emit(&mut self, event: &dyn Event) {
pub fn emit(&self, event: &mut dyn Event) {
// propagate the event to all systems
for system in &mut self.systems {
for system in &self.systems {
system.on_event(event);
}
}
}

pub struct SceneRenderPass {}

impl RenderScene for Scene {
fn cameras(&self) -> Vec<&Self::Camera> {
vec![]
}
}

impl RenderPass<Scene> for SceneRenderPass {
fn render_camera(&mut self, scene: &Scene, _camera: &dyn Camera, frame: &mut RenderFrame<'_>) {
scene.emit(&mut DrawEvent { queue: frame.queue });
}
}

0 comments on commit e5e7d88

Please sign in to comment.