I am trying to build a scene manager that lets you push scenes onto a stack. When each scene is popped off the stack, it is run until stopped and then we repeat.
An example is a menu in a game; which is one scene. When you close it, the game map behind it is another scene.
pub trait Scene {
fn start(&mut self) {}
fn update(&mut self) {}
fn stop(&mut self) {}
fn is_active(&self) -> bool {
return false;
}
}
pub struct SceneManager {
scenes: Vec<Box<Scene>>,
}
impl SceneManager {
fn new<T>(scene: T) -> SceneManager
where
T: Scene + 'static,
{
SceneManager { scenes: vec![Box::new(scene)] }
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Sample {
running: bool,
}
impl Scene for Sample {
fn start(&mut self) {
self.running = true;
}
fn update(&mut self) {
if self.running {
self.stop()
}
}
fn stop(&mut self) {
self.running = false;
}
fn is_active(&self) -> bool {
self.running
}
}
#[test]
fn test_is_running() {
let scene_manager = SceneManager::new(Scene);
}
}
The Scene
trait is implemented for some structure that contains some way to tell if that scene is running or not. In this case, a structure called Sample
.
You implement the Scene
for Sample
and then push that scene to the scene manager).
error[E0423]: expected value, found trait `Scene`
--> src/engine/scene.rs:48:47
|
48 | let scene_manager = SceneManager::new(Scene);
| ^^^^^ not a value
Not exactly sure what to do at this point. How do I get my scene on to the "stack" of scenes? I implemented the new
function of SceneManager
to take a type where the type matches a Scene
definition (if I understood that correctly). This alleviates me of having to specify a specific size and thus allowing me to push it to the heap instead of the stack.
What am I doing wrong and how do I alleviate the problem at hand and what does this even mean?