-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Pausable legion systems base on GameState
Fixes #50
- Loading branch information
Showing
7 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use scion::Scion; | ||
use scion::legion::system; | ||
|
||
#[system] | ||
fn test(){ | ||
log::info!("Hello all"); | ||
} | ||
|
||
fn main() { | ||
Scion::app() | ||
.with_pausable_system(test_system(), |state| state.test()) | ||
.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// `GameState` is a convenience Resource created to keep track of | ||
/// diverse thing internally. It's also the resource used to create | ||
/// pausable systems. | ||
#[derive(Debug, Copy, Clone, Default)] | ||
pub struct GameState {} | ||
|
||
impl GameState{ | ||
pub fn test(&self)-> bool{ | ||
true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use legion::systems::{Runnable, ResourceTypeId, SystemId, UnsafeResources, ResourceSet, CommandBuffer}; | ||
use legion::storage::ComponentTypeId; | ||
use legion::{World, Read}; | ||
use legion::world::{ArchetypeAccess, WorldId}; | ||
use crate::state::GameState; | ||
|
||
pub(crate) struct PausableSystem<S> { | ||
pub(crate) system: S, | ||
pub(crate) decider: Box<fn(GameState) -> bool>, | ||
pub(crate) resource_reads: Vec<ResourceTypeId>, | ||
} | ||
|
||
impl<S> Runnable for PausableSystem<S> | ||
where | ||
S: Runnable | ||
{ | ||
fn name(&self) -> Option<&SystemId> { | ||
self.system.name() | ||
} | ||
|
||
fn reads(&self) -> (&[ResourceTypeId], &[ComponentTypeId]) { | ||
let (_, components) = self.system.reads(); | ||
(&self.resource_reads[..], components) | ||
} | ||
|
||
fn writes(&self) -> (&[ResourceTypeId], &[ComponentTypeId]) { | ||
self.system.writes() | ||
} | ||
|
||
fn prepare(&mut self, world: &World) { | ||
self.system.prepare(world) | ||
} | ||
|
||
fn accesses_archetypes(&self) -> &ArchetypeAccess { | ||
self.system.accesses_archetypes() | ||
} | ||
|
||
unsafe fn run_unsafe(&mut self, world: &World, resources: &UnsafeResources) { | ||
let resources_static = &*(resources as *const UnsafeResources); | ||
let resource_to_check = Read::<GameState>::fetch_unchecked(resources_static); | ||
|
||
if (self.decider)(*resource_to_check) { | ||
return; | ||
} else {} | ||
|
||
self.system.run_unsafe(world, resources); | ||
} | ||
|
||
fn command_buffer_mut(&mut self, world: WorldId) -> Option<&mut CommandBuffer> { | ||
self.system.command_buffer_mut(world) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod file; | |
pub mod logger; | ||
pub mod time; | ||
pub mod window; | ||
pub(crate) mod legion_ext; |