From 4a4cdf7d148be0a2a756f323d27c0ee5b7976438 Mon Sep 17 00:00:00 2001 From: troido Date: Fri, 14 Feb 2020 14:36:32 +0100 Subject: extract the state to save --- src/systems/save.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/systems/save.rs (limited to 'src/systems/save.rs') diff --git a/src/systems/save.rs b/src/systems/save.rs new file mode 100644 index 0000000..924533a --- /dev/null +++ b/src/systems/save.rs @@ -0,0 +1,43 @@ + +use std::collections::HashMap; + +use specs::{ + Entities, + ReadStorage, + System, + Join, + Read +}; + +use crate::pos::Pos; + +use crate::components::{ + Position, + Serialise +}; + +use crate::savestate::SaveState; + +const INTERVAL: i32 = 20; + +pub struct Save(pub i32); +impl <'a> System<'a> for Save { + type SystemData = ( + Entities<'a>, + ReadStorage<'a, Position>, + ReadStorage<'a, Serialise>, + ); + + fn run(&mut self, (entities, positions, serialisers): Self::SystemData) { + self.0 -= 1; + if self.0 > 0 { + return + } + self.0 = INTERVAL; + let mut state = SaveState::new(); + for (pos, serialiser) in (&positions, &serialisers).join() { + state.changes.entry(pos.pos).or_insert(Vec::new()).push(serialiser.template.clone()); + } + println!("save {}", state.to_json().to_string()); + } +} -- cgit