forked from amethyst/specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveload.rs
112 lines (87 loc) · 2.32 KB
/
saveload.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
extern crate ron;
#[macro_use]
extern crate serde;
extern crate specs;
use specs::{Component, RunNow, System, VecStorage, World};
use specs::error::NoError;
use specs::saveload::{U64Marker, U64MarkerAllocator, WorldDeserialize, WorldSerialize};
const ENTITIES: &str = "
[
(
marker: (0),
components: (
Some((
x: 10,
y: 20,
)),
Some((30.5)),
),
),
(
marker: (1),
components: (
Some(Pos(
x: 5,
y: 2,
)),
None,
),
),
]
";
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct Pos {
x: f32,
y: f32,
}
impl Component for Pos {
type Storage = VecStorage<Self>;
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
struct Mass(f32);
impl Component for Mass {
type Storage = VecStorage<Self>;
}
fn main() {
use specs::Join;
let mut world = World::new();
world.register::<Pos>();
world.register::<Mass>();
world.register::<U64Marker>();
world.add_resource(U64MarkerAllocator::new());
world
.create_entity()
.with(Pos { x: 1.0, y: 2.0 })
.with(Mass(0.5))
.marked::<U64Marker>()
.build();
world
.create_entity()
.with(Pos { x: 7.0, y: 2.0 })
.with(Mass(4.5))
.marked::<U64Marker>()
.build();
struct Serialize;
impl<'a> System<'a> for Serialize {
type SystemData = WorldSerialize<'a, U64Marker, NoError, (Pos, Mass)>;
fn run(&mut self, mut world: Self::SystemData) {
let s = ron::ser::pretty::to_string(&world).unwrap();
println!("{}", s);
world.remove_serialized();
}
}
Serialize.run_now(&world.res);
// -----------------
struct Deserialize;
impl<'a> System<'a> for Deserialize {
type SystemData = WorldDeserialize<'a, U64Marker, NoError, (Pos, Mass)>;
fn run(&mut self, world: Self::SystemData) {
use ron::de::Deserializer;
use serde::de::DeserializeSeed;
let mut de = Deserializer::from_str(ENTITIES);
world.deserialize(&mut de).unwrap();
}
}
Deserialize.run_now(&world.res);
println!("{:#?}", (&world.read::<Pos>()).join().collect::<Vec<_>>());
}